-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaudio.js
158 lines (147 loc) · 3.9 KB
/
audio.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*global audio: true*/
/*global AudioContext*/
audio =
(function () {
"use strict";
var TICK = 1 / 3, //up to 1 / 6
audioContext,
metronomeOscillator, metronomeGain,
notesOscillator = [], notesGain = [], noteIndex = 0,
needsInit = true,
isMuted = false,
startTime = -1, lastTime,
notes = [
//Auf den Erfinder des Metronoms, (probably) by Anton Schindler,
//based on the Symphony No. 8 by Ludwig van Beethoven
['d', 2], null,
['d', 2], null,
['d', 2], null,
['d', 2], null,
['d', 2], null,
['d', 2], null,
['d', 2], null,
['c#', 1], ['e', 1],
['d', 2], null,
['c#', 1], ['e', 1],
['d', 2], null,
['c#', 1], ['e', 1], //c?
['d', 3], null,
null, ['B', 1],
['B', 1], ['A', 1],
['A', 1], ['B', 1],
['c', 2], null,
['A', 1], ['B', 1],
['G', 2], null,
['G', 2], null,
null, null,
null, null
], freqs = {
G: 220 * Math.pow(2, -1 / 6),
A: 220,
B: 220 * Math.pow(2, 1 / 6),
c: 220 * Math.pow(2, 1 / 4),
'c#': 220 * Math.pow(2, 1 / 3),
d: 220 * Math.pow(2, 5 / 12),
e: 220 * Math.pow(2, 7 / 12)
}, factor = 1, tempo = 3,
checkbox;
function init () {
needsInit = false;
if (!window.AudioContext) {
isMuted = true;
return;
}
checkbox = document.getElementById('audio');
checkbox.addEventListener('change', onChange);
onChange();
audioContext = new AudioContext();
metronomeOscillator = audioContext.createOscillator();
notesOscillator[0] = audioContext.createOscillator();
notesOscillator[1] = audioContext.createOscillator();
metronomeOscillator.frequency.value = 194;
notesOscillator[0].type = 'triangle';
notesOscillator[1].type = 'triangle';
metronomeGain = audioContext.createGain();
notesGain[0] = audioContext.createGain();
notesGain[1] = audioContext.createGain();
metronomeGain.gain.value = 0;
notesGain[0].gain.value = 0;
notesGain[1].gain.value = 0;
metronomeOscillator.connect(metronomeGain);
notesOscillator[0].connect(notesGain[0]);
notesOscillator[1].connect(notesGain[1]);
metronomeGain.connect(audioContext.destination);
notesGain[0].connect(audioContext.destination);
notesGain[1].connect(audioContext.destination);
metronomeOscillator.start();
notesOscillator[0].start();
notesOscillator[1].start();
}
function onChange () {
isMuted = !checkbox.checked;
startTime = -1;
}
function start () {
startTime = audioContext.currentTime;
lastTime = 0;
factor = 1;
TICK = 1 / tempo;
}
function queueMetronome (n) {
var t = startTime + n * TICK;
metronomeGain.gain.setValueAtTime(0.01, t);
metronomeGain.gain.exponentialRampToValueAtTime(0.5, t + 0.1);
metronomeGain.gain.exponentialRampToValueAtTime(0.01, t + 0.2);
metronomeGain.gain.setValueAtTime(0, t + 0.3);
}
function queueNote (n, freq, duration) {
var t = startTime + n * TICK, end = t + duration * TICK;
notesOscillator[noteIndex].frequency.setValueAtTime(freq, t);
notesGain[noteIndex].gain.setValueAtTime(0.001, t);
notesGain[noteIndex].gain.exponentialRampToValueAtTime(0.05, t + 0.05);
notesGain[noteIndex].gain.linearRampToValueAtTime(0.05, end - 0.1);
notesGain[noteIndex].gain.exponentialRampToValueAtTime(0.001, end - 0.05);
notesGain[noteIndex].gain.setValueAtTime(0, end);
noteIndex = 1 - noteIndex;
}
function queueAudio () {
var note;
if (startTime + lastTime * TICK - audioContext.currentTime > 0) {
return;
}
lastTime++;
if (lastTime % 2 === 1) {
queueMetronome(lastTime);
}
note = notes[(lastTime - 1) % notes.length];
if (note) {
queueNote(lastTime, freqs[note[0]] * factor, note[1]);
}
if ((lastTime - 1) % notes.length === notes.length - 1) {
TICK = 1 / tempo;
factor = Math.pow(2, (Math.floor(3 * Math.random()) - 1) / 12);
}
}
function handleAudio (newTempo) {
if (newTempo) {
tempo = newTempo;
}
if (needsInit) {
init();
}
if (isMuted) {
return;
}
if (startTime < 0) {
start();
}
queueAudio();
}
return function (tempo) {
try {
handleAudio(tempo);
} catch (e) {
//some not really reproducible error when switching on and off fast
}
};
})();