├── .travis.yml ├── LICENSE ├── README.md ├── example.js ├── index.js ├── package.json └── watch.js /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 0.6 4 | - 0.8 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 'Dominic Tarr' 2 | 3 | Permission is hereby granted, free of charge, 4 | to any person obtaining a copy of this software and 5 | associated documentation files (the "Software"), to 6 | deal in the Software without restriction, including 7 | without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom 10 | the Software is furnished to do so, 11 | subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 20 | ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hyperaudio 2 | 3 | composable web audio, inspired by hyperscript. 4 | 5 | The basic idea is to take web audio componests and make them composable. 6 | The main thing is a function to `connect` components, while also treating 7 | them as a single object. 8 | 9 | For example, we might make simple bass synth like this: 10 | ``` js 11 | var ctx =new AudioContext() 12 | var w = require('hyperaudio')(ctx) 13 | 14 | //set the osc to the correct frequency 15 | function noteOn (freq, time) { 16 | this.frequency.setValueAtTime(freq, time) 17 | } 18 | function decay(para, time, start, end, length) { 19 | para.setValueAtTime(start, time) 20 | para.exponentialRampToValueAtTime(end, time+length) 21 | } 22 | 23 | var c = w( 24 | //use two saws, slightly detuned to give a cool phasing sound 25 | w.osc({type: 'sawtooth', noteOn: noteOn}), 26 | w.osc({type: 'sawtooth', detune: -7, noteOn: noteOn}), 27 | //lowpass filter with some resonance and exponential envelope 28 | w.filter({ 29 | type:'lowpass', Q: 10, 30 | frequency: 1000, 31 | noteOn: function (freq, time) { 32 | decay(this.frequency, time, freq*10, freq, 1) 33 | } 34 | }), 35 | //also exponential decay amplitude, for punchyness 36 | w.gain({ 37 | noteOn: function (_, time) { 38 | decay(this.gain, time, 1, 0.001, 0.2) 39 | } 40 | }) 41 | ) 42 | ``` 43 | 44 | The `hyperaudio` "w" function combines the webaudio nodes, 45 | and returns a proxy which can control all the nodes. 46 | 47 | so we can now trigger notes using the combined object. 48 | ``` js 49 | //play one note 50 | c.noteOn(440, ctx.currentTime) 51 | c.start() 52 | ``` 53 | the proxy also calls the `noteOn` function which was added to all the nodes, 54 | allowing the entire sound making assemblage to be easily triggered by a sequencer! 55 | 56 | 57 | ## License 58 | 59 | MIT 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | module.exports = function run (w, step) { 2 | var toFreq = require('notes-to-frequencies') 3 | 4 | function Sequencer(notes, s, osc) { 5 | var stepTime = step 6 | notes.forEach(function (v) { 7 | osc.noteOn(toFreq(v.note), s) 8 | s+=v.length*step 9 | }) 10 | } 11 | 12 | function noteOn (freq, time) { 13 | this.frequency.setValueAtTime(freq, time) 14 | } 15 | 16 | ///////////////////////////////////////////////////// 17 | 18 | function decay(para, time, start, end, length) { 19 | para.setValueAtTime(start, time) 20 | para.exponentialRampToValueAtTime(end, time+length) 21 | } 22 | 23 | var c = w( 24 | w.osc({type: 'sawtooth', noteOn: noteOn}), 25 | w.osc({type: 'sawtooth', detune: -7, noteOn: noteOn}), 26 | w.filter({ 27 | type:'lowpass', Q: 15, 28 | // frequency: 500, 29 | noteOn: function (freq, time) { 30 | decay(this.frequency, time, freq*5, freq*2, 1.8) 31 | } 32 | }), 33 | w.gain({ 34 | noteOn: function (_, time) { 35 | decay(this.gain, time, 5, 0.001, 1) 36 | } 37 | }), 38 | w.reverb({dry: 1, wet: 0.8, time: 0.5}) 39 | ) 40 | 41 | // c.connect(w.context.destination) 42 | 43 | var notes = [ 44 | {note: 'c2', length: 3}, 45 | {note: 'd#2', length: 3}, 46 | {note: 'f2', length: 3}, 47 | {note: 'c2', length: 3}, 48 | {note: 'd#2', length: 4} 49 | 50 | // {note: 'c2', length: 3}, 51 | // {note: 'd#2', length: 2}, 52 | // {note: 'f3', length: 3}, 53 | // {note: 'c2', length: 2}, 54 | // {note: 'd#3', length: 3}, 55 | // {note: 'g#3', length: 3}, 56 | ] 57 | 58 | var beats = [ 59 | {note: 'c3', length: 4}, 60 | {note: 'c3', length: 4}, 61 | {note: 'c3', length: 4}, 62 | {note: 'c3', length: 4} 63 | ] 64 | 65 | var d = w.osc({noteOn: function (_, time) { 66 | this.frequency.setValueAtTime(400, time) 67 | this.frequency.exponentialRampToValueAtTime(1, time+0.3) 68 | }}) 69 | 70 | var e = w( 71 | w.osc({noteOn: function (freq, time) { 72 | this.frequency.setValueAtTime(freq, time) 73 | this.frequency.exponentialRampToValueAtTime(freq/2, time+0.01) 74 | }}), 75 | w.gain({noteOn: function (_, time) { 76 | this.gain.setValueAtTime(0.1, time) 77 | this.gain.linearRampToValueAtTime(0, time+0.01) 78 | }}) 79 | ) 80 | 81 | // d.connect(w.context.destination) 82 | // e.connect(w.context.destination) 83 | 84 | var start = w.context.currentTime 85 | 86 | var g = w.gain({gain: 1, 87 | noteOn: function (_, time) { 88 | Sequencer(notes, time, c) 89 | Sequencer(beats, time, d) 90 | var N = 16 91 | for(var i = 0; i < N; i++) { 92 | if(i%2) e.noteOn(1600, time+bar*2 + (i/N)*2 + 1/N/8) 93 | else e.noteOn(1600, time+bar*2 + (i/N)*2) 94 | } 95 | } 96 | }) 97 | 98 | w(c, g) 99 | w(d, g) 100 | w(e, g) 101 | 102 | c.start() 103 | d.start() 104 | e.start() 105 | 106 | // g.connect(w.context.destination) 107 | 108 | var bar = 0 109 | e.noteOn(1600, start) 110 | var N = 16 111 | for(var i = 0; i < N; i++) { 112 | if(i%2) e.noteOn(1600, start+bar*2 + (i/N)*2 + 1/N/8) 113 | else e.noteOn(1600, start+bar*2 + (i/N)*2) 114 | } 115 | 116 | return g 117 | 118 | return function (start) { 119 | Sequencer(notes, start, c) 120 | Sequencer(beats, start, d) 121 | 122 | // bar++ 123 | // Sequencer(notes, start+(bar)*2, c) 124 | // Sequencer(beats, start+(bar)*2, d) 125 | } 126 | 127 | } 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var Reverb = require('soundbank-reverb') 2 | /* 3 | oscilator 4 | filter 5 | gain 6 | 7 | buffer 8 | panner 9 | periodic 10 | script 11 | wave 12 | */ 13 | 14 | function isObject (o) { 15 | return o && 'object' === typeof o 16 | } 17 | 18 | function setup (node, opts) { 19 | for(var k in opts) { 20 | //AudioParam 21 | if(isObject(node[k])) 22 | node[k].value = opts[k] 23 | //normal value, i.e. type 24 | else 25 | node[k] = opts[k] 26 | } 27 | return node 28 | } 29 | 30 | function _all (ary, method) { 31 | return function (value, time) { ary.forEach(function (v) { v[method] && v[method](value, time) })} 32 | } 33 | 34 | function combine () { 35 | var args = [].slice.call(arguments) 36 | var last 37 | 38 | function connect (dest) { 39 | if(last) last.forEach(function (source) { 40 | source.connect(dest) 41 | }) 42 | } 43 | 44 | args.forEach(function (node) { 45 | if(!last) last = [node] 46 | else if(node.numberOfInputs) { 47 | connect(node) 48 | last = [node] 49 | } 50 | else 51 | last.push(node) 52 | }) 53 | 54 | var obj = { 55 | connect: connect 56 | } 57 | return new Proxy(obj, {get: function (_, property) { 58 | return obj[property] ? obj[property] : obj[property] = _all(args, property) 59 | }}) 60 | } 61 | 62 | var A = module.exports = function (ctx) { 63 | 64 | function _setup(fn) { 65 | if(!fn) throw new Error('expected function') 66 | return function (opts) { return setup(fn.call(ctx), opts) } 67 | } 68 | 69 | w = combine 70 | 71 | w.osc = _setup(ctx.createOscillator) 72 | w.gain = _setup(ctx.createGain) 73 | w.filter = _setup(ctx.createBiquadFilter) 74 | 75 | w.reverb = function (opts) { 76 | return setup(Reverb(ctx), opts) 77 | } 78 | 79 | w.context = ctx 80 | return w 81 | } 82 | 83 | 84 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hyperaudio", 3 | "description": "", 4 | "version": "1.0.0", 5 | "homepage": "https://github.com/dominictarr/hyperaudio", 6 | "repository": { 7 | "type": "git", 8 | "url": "git://github.com/dominictarr/hyperaudio.git" 9 | }, 10 | "dependencies": { 11 | "notes-to-frequencies": "^1.0.0", 12 | "soundbank-reverb": "^1.1.2" 13 | }, 14 | "devDependencies": {}, 15 | "scripts": { 16 | "test": "set -e; for t in test/*.js; do node $t; done" 17 | }, 18 | "author": "'Dominic Tarr' (dominictarr.com)", 19 | "license": "MIT" 20 | } 21 | -------------------------------------------------------------------------------- /watch.js: -------------------------------------------------------------------------------- 1 | var path = require('path') 2 | 3 | var loop = require('./example'), _loop 4 | var id = require.resolve('./example') 5 | var _c 6 | require('fs').watch(path.join(__dirname, 'example.js'), {recursive: true}, function (e, filename) { 7 | require('module')._cache[id] = null 8 | _loop = require('./example') 9 | }) 10 | 11 | function Context () { 12 | return require('./')(new (window.AudioContext || window.webkitAudioContext)()) 13 | } 14 | 15 | var c = Context() 16 | var play = loop(c, 0.125) 17 | 18 | play.connect(c.context.destination) 19 | //play(c.context.currentTime) 20 | play.noteOn(null, c.context.currentTime) 21 | 22 | setInterval(function () { 23 | if(_loop) { 24 | play = _loop(c, 0.125) 25 | loop = _loop 26 | _loop = null 27 | } 28 | play.noteOn(null, c.context.currentTime) 29 | }, 1000*(0.125*16)) 30 | 31 | --------------------------------------------------------------------------------