Skip to content

Commit

Permalink
feat(poc): Added a dummy demo for voice-pool, but it needs debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Feb 8, 2019
1 parent ce03b4c commit 90632de
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
17 changes: 17 additions & 0 deletions poc/voice-pool/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Voice Pool</title>
</head>
<body>
<nav>
<a href="/">demo index</a> » <b>voice pool</b>
</nav>
<button id="play">Play demo</button>
<script src="../common/virtual-webaudio.js"></script>
<script src="../common/helpers.js"></script>
<script src="./script.js"></script>
<script src="/reload/reload.js"></script>
</body>
</html>
86 changes: 86 additions & 0 deletions poc/voice-pool/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* global virtualWebaudio, AudioContext, once */

const { VirtualAudioContext, patch, render, diff } = virtualWebaudio

const chords = [
[430.05, 538.71],
[430.05, 538.71, 616.67, 772.46],
[411.11, 492.29, 563.53, 616.67],
[393, 492.29, 563.53, 616.67],
[299.92, 359.14, 470.6, 538.71],
[299.92, 359.14, 492.29, 563.53],
[313.74, 375.69, 492.29, 616.67]
]

const addNote = (osc, gain) => (frequency, t) => {
osc.frequency.setValueAtTime(frequency, t)
/*
gain.gain.cancelAndHoldAtTime(t)
gain.gain.linearRampToValueAtTime(0.5, t + 0.05)
gain.gain.exponentialRampToValueAtTime(0.1, t + 1)
gain.gain.linearRampToValueAtTime(0, t + 1.5)
*/
}

const createVoice = (ctx) => {
const osc = ctx.createOscillator()
const gain = ctx.createGain()

osc.type = 'triangle'
gain.gain.value = 0
osc.connect(gain)
gain.connect(ctx.destination)

osc.start()

return addNote(osc, gain)
}

const sound = frequencies => {
const ctx = new VirtualAudioContext()

const voices = [
// createVoice(ctx),
// createVoice(ctx),
// createVoice(ctx),
createVoice(ctx)
]

frequencies[0] && voices[0](frequencies[0], ctx.currentTime)
// frequencies[1] && voices[1](frequencies[1], ctx.currentTime)
// frequencies[2] && voices[2](frequencies[2], ctx.currentTime)
// frequencies[3] && voices[3](frequencies[3], ctx.currentTime)

return ctx
}

let old = null

const change = (virtualCtx, ctx) => {
if (old === null) {
render(virtualCtx, ctx)
} else {
patch(diff(old, virtualCtx), ctx)
}

old = virtualCtx
}

const vCtx1 = sound(chords[0])
const vCtx2 = sound(chords[1])

console.log(vCtx1._.events.data)
console.log(vCtx2._.events.data)
console.log(diff(vCtx1, vCtx2))

/*
const playDemo = () => {
const ctx = new AudioContext()
chords.map((chord, index) => {
setTimeout(() => change(sound(chord), ctx), index * 1000)
})
}
document.getElementById('play').addEventListener('click', once(playDemo))
*/

0 comments on commit 90632de

Please sign in to comment.