-
Notifications
You must be signed in to change notification settings - Fork 7
/
makenoise.js
67 lines (59 loc) · 1.78 KB
/
makenoise.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
//#EEEEEE - 0 (min)
//#D6E685 - 1
//#8CC665 - 2
//#44A340 - 3
//#1E6823 - 4 (max)
((audioContext, freq, delay, waveform) => {
'use strict';
const levels = { '#eeeeee': freq, '#d6e685': freq * (3 / 2), '#8cc665': freq * (3 / 2) * 2, '#44a340': freq * (3 / 2) * 3, '#1e6823': freq * (3 / 2) * 4 };
const createOscillator = (fillNodeValue) => {
const oscillator = audioContext.createOscillator();
const gainNode = audioContext.createGain();
gainNode.gain.value = 0.1;
oscillator.type = waveform;
oscillator.frequency.value = levels[fillNodeValue];
oscillator.connect(gainNode);
gainNode.connect(audioContext.destination);
return {
start: () => oscillator.start(0),
stop: () => oscillator.stop()
};
};
const createNotes = (week) => {
return week.map((day) => {
const fillNodeValue = day.attributes.fill.nodeValue;
const oscillator = createOscillator(fillNodeValue);
return {
play: () => {
day.attributes.fill.nodeValue = 'red';
oscillator.start();
},
stop: () => {
day.attributes.fill.nodeValue = fillNodeValue;
oscillator.stop();
}
};
});
};
const delayPlay = (noteBar) => {
return (iter) => {
setTimeout(() => {
for (let note of noteBar) {
note.play();
setTimeout(() => {
note.stop();
}, delay / 2);
}
}, delay * iter);
};
};
const noteBars = ([].slice.call(document.getElementsByTagName('g'))).slice(1).map((week) => {
return createNotes([].slice.call(week.getElementsByClassName('day')));
}).map((day) => {
return delayPlay(day);
});
let i = 0;
for (let noteBar of noteBars) {
noteBar(i += 1);
}
})(new AudioContext, 440, 110, 'sawtooth');