-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathletters.js
78 lines (67 loc) · 2.04 KB
/
letters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Synth } from "./audiosynth.js";
const instruments = {
piano: Synth.createInstrument('piano'),
edm: Synth.createInstrument('edm'),
acoustic: Synth.createInstrument('acoustic'),
organ: Synth.createInstrument('organ'),
}
let letters = {
"c": 16.35,
"c#": 17.32,
"d": 18.35,
"d#": 19.45,
"e": 20.6,
"f": 21.83,
"f#": 23.12,
"g": 24.5,
"g#": 25.96,
"a": 27.5,
"a#": 29.14,
"b": 30.87,
}
for (let i = 1; i < 10; i++) {
for (const k in letters) {
letters[`${k}${i}`] = letters[k]*2**i;
}
}
async function playNote(frequency, duration, ctx, ops = {}) {
var o = ctx.createOscillator()
var g = ctx.createGain()
o.frequency.value = frequency;
o.type = ops.type || 'sine';
o.connect(g)
g.connect(ctx.destination)
o.start();
const endTime = ctx.currentTime + duration*2/1000;
o.stop(endTime)
g.gain.setValueAtTime(0, ctx.currentTime);
g.gain.linearRampToValueAtTime(.2, ctx.currentTime + duration/5/1000);
g.gain.exponentialRampToValueAtTime(0.00001, ctx.currentTime + duration/1000)
g.gain.linearRampToValueAtTime(0, ctx.currentTime + duration*2/1000) // does this ramp from the last ramp
o.onended = () => {
g.disconnect();
};
}
function getLetters(ops) {
const type = ops.type ?? "sine";
const volume = ops.volume ?? 100;
const newLetters = {};
if ( ["sine", "triangle", "square", "sawtooth"].includes(type) ) {
for (const k in letters) {
let hz = letters[k];
newLetters[k] = (duration, ctx) => playNote(hz, duration, ctx, ops);
}
} else if ( ["piano", "edm", "organ", "acoustic"].includes(type) ) {
const instrument = instruments[type]
Synth.setVolume(volume/100);
const twelveNotes = ["a", "a#", "b", "c", "c#", "d", "d#", "e", "f", "f#", "g", "g#"];
for (let i = 1; i < 9; i++) {
twelveNotes.forEach(n => {
const note = n.toUpperCase();
newLetters[`${n}${i}`] = (duration) => instrument.play(note, i, duration/1000);
})
}
} else console.error("Unexpected synth type:", type)
return newLetters;
}
export { getLetters };