-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(time): Made setValueAtTime() work by tweaking .currentTime, so t…
…hat it is more in sync with the
- Loading branch information
1 parent
611be7f
commit bd5e6df
Showing
12 changed files
with
201 additions
and
25 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Delayed monosynth</title> | ||
</head> | ||
|
||
<body> | ||
Press the following keys to play the monosynth: | ||
<pre> | ||
W E T Y U | ||
A S D F G H J K | ||
</pre> | ||
<b>Notice that the notes are delayed half a second!</b> | ||
<script src="../common/virtual-webaudio.js"></script> | ||
<script src="./script.js"></script> | ||
<script src="/reload/reload.js"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* global virtualWebaudio, AudioContext */ | ||
|
||
const { VirtualAudioContext, patch, render, diff } = virtualWebaudio | ||
|
||
const sound = (frequency, volume, delayInSeconds) => { | ||
const ctx = new VirtualAudioContext() | ||
|
||
const osc = ctx.createOscillator() | ||
const gain = ctx.createGain() | ||
|
||
osc.frequency.setValueAtTime(frequency, ctx.currentTime + delayInSeconds) | ||
osc.type = 'triangle' | ||
|
||
gain.gain.setValueAtTime(volume, ctx.currentTime + delayInSeconds) | ||
|
||
osc.connect(gain) | ||
gain.connect(ctx.destination) | ||
|
||
osc.start() | ||
|
||
return ctx | ||
} | ||
|
||
const frequencies = [ | ||
261.6, | ||
277.1, | ||
293.6, | ||
311.1, | ||
329.6, | ||
349.2, | ||
369.9, | ||
391.9, | ||
415.3, | ||
440, | ||
466.1, | ||
493.8, | ||
523.2 | ||
] | ||
const keyMap = { | ||
A: 0, | ||
W: 1, | ||
S: 2, | ||
E: 3, | ||
D: 4, | ||
F: 5, | ||
T: 6, | ||
G: 7, | ||
Z: 8, | ||
Y: 8, | ||
H: 9, | ||
U: 10, | ||
J: 11, | ||
K: 12 | ||
} | ||
|
||
const ctx = new AudioContext() | ||
|
||
let old = null | ||
|
||
const change = (virtualCtx, ctx) => { | ||
if (old === null) { | ||
render(virtualCtx, ctx) | ||
} else { | ||
patch(diff(old, virtualCtx), ctx) | ||
} | ||
|
||
old = virtualCtx | ||
} | ||
|
||
let lastPressedNoteIndex = null | ||
let isNotePlaying = false | ||
const keyPressed = { | ||
A: false, | ||
W: false, | ||
S: false, | ||
E: false, | ||
D: false, | ||
F: false, | ||
T: false, | ||
G: false, | ||
Z: false, | ||
Y: false, | ||
H: false, | ||
U: false, | ||
J: false, | ||
K: false | ||
} | ||
|
||
document.body.addEventListener('keydown', e => { | ||
const pressedChar = String.fromCharCode(e.keyCode) | ||
if (!keyPressed[pressedChar]) { | ||
keyPressed[pressedChar] = true | ||
|
||
if (keyMap.hasOwnProperty(pressedChar)) { | ||
if (lastPressedNoteIndex !== keyMap[pressedChar] || !isNotePlaying) { | ||
lastPressedNoteIndex = keyMap[pressedChar] | ||
isNotePlaying = true | ||
|
||
change(sound(frequencies[lastPressedNoteIndex], 1, 0.5), ctx) | ||
} | ||
} | ||
} | ||
}) | ||
|
||
document.body.addEventListener('keyup', e => { | ||
const pressedChar = String.fromCharCode(e.keyCode) | ||
if (keyPressed[pressedChar]) { | ||
keyPressed[pressedChar] = false | ||
|
||
if (keyMap.hasOwnProperty(pressedChar)) { | ||
if (lastPressedNoteIndex === keyMap[pressedChar] && isNotePlaying) { | ||
lastPressedNoteIndex = keyMap[pressedChar] | ||
isNotePlaying = false | ||
change(sound(frequencies[lastPressedNoteIndex], 0, 0.5), ctx) | ||
} | ||
} | ||
} | ||
}) | ||
|
||
// setting initial volume to 0 | ||
change(sound(0, 0, 0), ctx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Envelope</title> | ||
</head> | ||
|
||
<body> | ||
<script src="//cdn.jsdelivr.net/npm/ramda@latest/dist/ramda.min.js"></script> | ||
<script src="../common/virtual-webaudio.js"></script> | ||
<script src="./script.js"></script> | ||
<script src="/reload/reload.js"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Manual LFO</title> | ||
</head> | ||
|
||
<body> | ||
<script src="//cdn.jsdelivr.net/npm/ramda@latest/dist/ramda.min.js"></script> | ||
<script src="../common/virtual-webaudio.js"></script> | ||
<script src="./script.js"></script> | ||
<script src="/reload/reload.js"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Monosynth</title> | ||
</head> | ||
|
||
<body> | ||
Press the following keys to play the monosynth: | ||
<pre> | ||
W E T Y U I | ||
W E T Y U | ||
A S D F G H J K | ||
</pre> | ||
<script src="//cdn.jsdelivr.net/npm/ramda@latest/dist/ramda.min.js"></script> | ||
<script src="../common/virtual-webaudio.js"></script> | ||
<script src="./script.js"></script> | ||
<script src="/reload/reload.js"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters