Skip to content

Commit

Permalink
Merge pull request #32 from benji6/node-fns
Browse files Browse the repository at this point in the history
Node Functions
  • Loading branch information
benji6 authored Mar 28, 2018
2 parents 8cdcf0c + 7ee89ed commit f2aaf78
Show file tree
Hide file tree
Showing 30 changed files with 1,095 additions and 756 deletions.
210 changes: 117 additions & 93 deletions README.md

Large diffs are not rendered by default.

90 changes: 54 additions & 36 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,37 @@ <h1>virtual-audio-graph Demos</h1>
<section>
<h2>Example 1</h2>
<pre>
const {currentTime} = virtualAudioGraph
import createVirtualAudioGraph, {
createNode,
delay,
gain,
oscillator,
stereoPanner,
} from '../src/index'

const audioContext = new AudioContext()

const virtualAudioGraph = createVirtualAudioGraph({
audioContext,
output: audioContext.destination,
})

const { currentTime } = virtualAudioGraph

virtualAudioGraph.update({
0: ['gain', 'output', {gain: 0.2}],
1: ['oscillator', 0, {
0: gain('output', { gain: 0.2 }),
1: oscillator(0, {
frequency: 440,
stopTime: currentTime + 1,
type: 'square',
}],
2: ['oscillator', 0, {
}),
2: oscillator(0, {
detune: 4,
frequency: 660,
startTime: currentTime + 0.5,
stopTime: currentTime + 1.5,
type: 'sawtooth',
}],
}),
})
</pre>
<button id="example1">Play</button>
Expand All @@ -36,13 +51,13 @@ <h2>Example 1</h2>
<section>
<h2>Example 2</h2>
<pre>
const {currentTime} = virtualAudioGraph
const { currentTime } = virtualAudioGraph

virtualAudioGraph.update({
0: ['gain', 'output', {gain: 0.2}],
1: ['oscillator', 0, {stopTime: currentTime + 3}],
2: ['gain', {destination: 'frequency', key: 1}, {gain: 10}],
3: ['oscillator', [2, 'output'], {frequency: 1, type: 'triangle'}],
0: gain('output', { gain: 0.2 }),
1: oscillator(0, { stopTime: currentTime + 3 }),
2: gain({ destination: 'frequency', key: '1' }, { gain: 10 }),
3: oscillator([2, 'output'], { frequency: 1, type: 'triangle' }),
})
</pre>
<button id="example2">Play</button>
Expand All @@ -53,48 +68,51 @@ <h2>Example 3</h2>
<pre>
const chromaticScale = n => 440 * Math.pow(2, n / 12)

const pingPongDelay = ({
const pingPongDelay = createNode(({
decay,
delayTime,
}) => ({
0: ['stereoPanner', 'output', {pan: -1}],
1: ['stereoPanner', 'output', {pan: 1}],
2: ['delay', [1, 'five'], {delayTime, maxDelayTime: delayTime}],
3: ['gain', 2, {gain: decay}],
4: ['delay', [0, 3], {delayTime, maxDelayTime: delayTime}],
five: ['gain', 4, {gain: decay}, 'input'],
})
0: stereoPanner('output', { pan: -1 }),
1: stereoPanner('output', { pan: 1 }),
2: delay([1, 'five'], { delayTime, maxDelayTime: delayTime }),
3: gain(2, { gain: decay }),
4: delay([0, 3], { delayTime, maxDelayTime: delayTime }),
five: gain(4, { gain: decay }, 'input'),
}))

const oscillators = ({
const oscillators = createNode(({
currentTime = virtualAudioGraph.currentTime,
notes,
noteLength,
}) => notes.reduce((acc, frequency, i) => {
const startTime = currentTime + noteLength * 2 * i
acc[i] = ['oscillator', 'output', {
frequency,
startTime,
stopTime: startTime + noteLength,
}]
return acc
}, {})
}) => notes.reduce(
(acc, frequency, i) => {
const startTime = currentTime + noteLength * 2 * i
acc[i] = oscillator('output', {
frequency,
startTime,
stopTime: startTime + noteLength,
})
return acc
},
{}),
)

const noteLength = 0.075

const up = Array.from({length: 16}, (_, i) => chromaticScale(i))
const up = Array.apply(null, { length: 16 }).map((_, i) => chromaticScale(i))
const down = [...up].reverse()

virtualAudioGraph.update({
0: ['gain', 'output', {gain: 0.2}],
1: [pingPongDelay, 0, {
0: gain('output', { gain: 0.2 }),
1: pingPongDelay(0, {
decay: 1 / 2,
delayTime: noteLength * 3 / 2,
}],
2: ['gain', [0, 1], {gain: 1 / 4}],
3: [oscillators, [0, 1], {
}),
2: gain([0, 1], { gain: 1 / 4 }),
3: oscillators([0, 1], {
noteLength,
notes: [...up, ...down],
}],
}),
})
</pre>
<button id="example3">Play</button>
Expand Down
Loading

0 comments on commit f2aaf78

Please sign in to comment.