├── .nvmrc ├── .travis.yml ├── .gitignore ├── index.d.ts ├── index.js ├── package.json ├── LICENSE ├── README.md └── test.js /.nvmrc: -------------------------------------------------------------------------------- 1 | 6 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | node_modules 3 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | interface Arg { 2 | audioContext?: AudioContext; 3 | output?: AudioDestinationNode; 4 | } 5 | declare function _exports(arg?: Arg): (nodeParams$: any) => any; 6 | export = _exports; 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var createVirtualAudioGraph = require("virtual-audio-graph").default; 4 | 5 | module.exports = function makeAudioGraphDriver(arg) { 6 | var config = arg || {}; 7 | var audioContext = config.audioContext || new AudioContext(); 8 | var output = config.output || audioContext.destination; 9 | 10 | var virtualAudioGraph = createVirtualAudioGraph({ 11 | audioContext: audioContext, 12 | output: output, 13 | }); 14 | 15 | return function (nodeParams$) { 16 | return nodeParams$.addListener({ 17 | next: function next(nodeParams) { 18 | return virtualAudioGraph.update(nodeParams); 19 | }, 20 | }); 21 | }; 22 | }; 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cycle-audio-graph", 3 | "version": "1.1.0", 4 | "description": "Audio graph driver for Cycle.js based on virtual-audio-graph", 5 | "keywords": [ 6 | "api", 7 | "audio", 8 | "audiograph", 9 | "cycle", 10 | "cycle-audio-graph", 11 | "declarative", 12 | "driver", 13 | "functional", 14 | "graph", 15 | "virtual-audio-graph", 16 | "virtual", 17 | "web" 18 | ], 19 | "main": "index.js", 20 | "scripts": { 21 | "test": "node test" 22 | }, 23 | "author": "Ben Hall", 24 | "license": "MIT", 25 | "devDependencies": { 26 | "tape": "^4.13.3", 27 | "virtual-audio-graph": "^1.1.6", 28 | "web-audio-test-api": "^0.5.2", 29 | "xstream": "^11.13.0" 30 | }, 31 | "peerDependencies": { 32 | "virtual-audio-graph": "^1.0.0" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Ben Hall 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # cycle-audio-graph 2 | 3 | [![npm version](https://badge.fury.io/js/cycle-audio-graph.svg)](https://badge.fury.io/js/cycle-audio-graph) 4 | [![Build Status](https://travis-ci.org/benji6/cycle-audio-graph.svg)](https://travis-ci.org/benji6/cycle-audio-graph) 5 | 6 | Audio graph driver for [Cycle.js](https://github.com/cyclejs) based on [virtual-audio-graph](https://github.com/benji6/virtual-audio-graph). 7 | 8 | Check out [Awesome Cycle.js](https://github.com/vic/awesome-cyclejs) for more Cycle.js resources. 9 | 10 | ## Installation 11 | 12 | ```bash 13 | npm i -S cycle-audio-graph 14 | ``` 15 | 16 | ## Usage 17 | 18 | ### makeAudioGraphDriver 19 | 20 | The default export of `cycle-audio-graph` is `makeAudioGraphDriver` which takes an optional configuration object with two optional key-value pairs: 21 | 22 | ```javascript 23 | import makeAudioGraphDriver from 'cycle-audio-graph' 24 | 25 | const audioContext = new AudioContext() 26 | 27 | // if no configuration object is provided the defaults 28 | // detailed below will be used 29 | makeAudioGraphDriver({ 30 | // if audioContext is not provided then cycle-audio-graph 31 | // will attempt to construct its own instance 32 | audioContext, 33 | // output could be any valid AudioNode destination. 34 | // If not provided then cycle-audio-graph will use 35 | // the destination of its audioContext instance 36 | output: audioContext.destination, 37 | }) 38 | 39 | ``` 40 | 41 | ### Very Basic Example 42 | 43 | ```javascript 44 | import {run} from '@cycle/core' 45 | import makeAudioGraphDriver from 'cycle-audio-graph' 46 | 47 | const audioContext = new AudioContext() 48 | 49 | const main = responses => { 50 | // ... example$ could be some sort of user input 51 | graph$ = example$.map(_ => { 52 | // ... 53 | const {currentTime} = audioContext 54 | return { 55 | 0: ['gain', 'output', {gain: 0.2}], 56 | 1: ['oscillator', 0, { 57 | type: 'square', 58 | frequency: 440, 59 | startTime: currentTime + 1, 60 | stopTime: currentTime + 2, 61 | }], 62 | } 63 | }) 64 | return { 65 | audioGraph: graph$, 66 | // ... etc. 67 | } 68 | } 69 | 70 | const drivers = { 71 | audioGraph: makeAudioGraphDriver({ 72 | audioContext, 73 | output: audioContext.destination, 74 | }), 75 | // ... etc. 76 | } 77 | 78 | run(main, drivers) 79 | ``` 80 | 81 | ### virtual-audio-graph 82 | 83 | For more info on the `graph` objects in the `graph$` check out the documentation for [virtual-audio-graph](https://github.com/benji6/virtual-audio-graph). 84 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | require('web-audio-test-api') 2 | 3 | const {gain, oscillator} = require('virtual-audio-graph') 4 | 5 | const of = require('xstream').default.of 6 | const test = require('tape') 7 | const makeAudioGraphDriver = require('./') 8 | 9 | WebAudioTestAPI.setState({ 10 | 'AudioContext#createStereoPanner': 'enabled', 11 | 'AnalyserNode#getFloatTimeDomainData': 'enabled' 12 | }) 13 | 14 | test('no destination in config', function (t) { 15 | const audioContext = new AudioContext 16 | 17 | const audioGraphDriver = makeAudioGraphDriver({ 18 | audioContext: audioContext, 19 | }) 20 | 21 | audioGraphDriver(of({ 22 | 0: gain('output', {gain: 0.2}), 23 | 1: oscillator(0, {type: 'square', frequency: 440}) 24 | })) 25 | t.deepEqual(audioContext.toJSON(), { 26 | name: 'AudioDestinationNode', 27 | inputs: [ 28 | { 29 | gain: {inputs: [], value: 0.2}, 30 | inputs: [ 31 | { 32 | detune: {value: 0, inputs: []}, 33 | frequency: {value: 440, inputs: []}, 34 | inputs: [], 35 | name: 'OscillatorNode', 36 | type: 'square' 37 | } 38 | ], 39 | name: 'GainNode' 40 | } 41 | ] 42 | }) 43 | t.end() 44 | }) 45 | 46 | test('a few graphs', function(t) { 47 | const audioContext = new AudioContext 48 | 49 | const audioGraphDriver = makeAudioGraphDriver({ 50 | audioContext: audioContext, 51 | output: audioContext.destination 52 | }) 53 | 54 | audioGraphDriver(of({ 55 | 0: gain('output', {gain: 0.2}), 56 | 1: oscillator(0, {type: 'square', frequency: 440}) 57 | })) 58 | t.deepEqual(audioContext.toJSON(), { 59 | name: 'AudioDestinationNode', 60 | inputs: [ 61 | { 62 | gain: {inputs: [], value: 0.2}, 63 | inputs: [ 64 | { 65 | detune: {value: 0, inputs: []}, 66 | frequency: {value: 440, inputs: []}, 67 | inputs: [], 68 | name: 'OscillatorNode', 69 | type: 'square' 70 | } 71 | ], 72 | name: 'GainNode' 73 | } 74 | ] 75 | }) 76 | 77 | audioGraphDriver(of({ 78 | 0: gain('output', {gain: 0.1}), 79 | 1: oscillator(0, {type: 'sine', frequency: 220}) 80 | })) 81 | t.deepEqual(audioContext.toJSON(), { 82 | name: 'AudioDestinationNode', 83 | inputs: [ 84 | { 85 | gain: {inputs: [], value: 0.1}, 86 | inputs: [ 87 | { 88 | detune: {value: 0, inputs: []}, 89 | frequency: {value: 220, inputs: []}, 90 | inputs: [], 91 | name: 'OscillatorNode', 92 | type: 'sine' 93 | } 94 | ], 95 | name: 'GainNode' 96 | } 97 | ] 98 | }) 99 | 100 | audioGraphDriver(of({})) 101 | t.deepEqual(audioContext.toJSON(), { 102 | name: 'AudioDestinationNode', 103 | inputs: [] 104 | }) 105 | 106 | audioGraphDriver(of({ 107 | 0: gain('output', {gain: 0.2}), 108 | 1: oscillator(0, {type: 'square', frequency: 440}) 109 | })) 110 | t.deepEqual(audioContext.toJSON(), { 111 | name: 'AudioDestinationNode', 112 | inputs: [ 113 | { 114 | gain: {inputs: [], value: 0.2}, 115 | inputs: [ 116 | { 117 | detune: {value: 0, inputs: []}, 118 | frequency: {value: 440, inputs: []}, 119 | inputs: [], 120 | name: 'OscillatorNode', 121 | type: 'square' 122 | } 123 | ], 124 | name: 'GainNode' 125 | } 126 | ] 127 | }) 128 | t.end() 129 | }) 130 | --------------------------------------------------------------------------------