-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsound.js
More file actions
59 lines (50 loc) · 1.52 KB
/
sound.js
File metadata and controls
59 lines (50 loc) · 1.52 KB
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
/*
This demo uses the Mozilla Audio Data API (https://wiki.mozilla.org/Audio_Data_API)
Firefox OS does not yet support the Web Audio API or that would have been used as the Audio Data API is deprecated
*/
var soundData = [];
var audioContext = new Audio();
// Given an array of morse code information, play the sound through the speaker
function playSounds(durations) {
soundData = [];
var sound = true;
for (var i = 0; i < durations.length; i++) {
if (sound) {
playSoundForMillis(durations[i]);
}
else {
playSilenceForMillis(durations[i]);
}
sound = !sound;
}
audioContext.mozSetup(1, 15000);
writeSoundBuffer();
}
/*
Write the actual sound data to the API. Only some of the data will fit, thus
we write more every 100ms until it is all written
*/
function writeSoundBuffer() {
var written = audioContext.mozWriteAudio(soundData);
console.log(written + ' of ' + soundData.length + ' written');
if (written > 0) {
soundData = soundData.splice(written);
setTimeout(writeSoundBuffer, 100);
}
}
// Create the sound data (of a tone) for a given length of time
function playSoundForMillis(length) {
var iterations = length / 2;
for (var i = 0; i < iterations; i++) {
for (var j = 0; j < 30; j++) {
soundData.push(3 * Math.sin(Math.PI * (j/15)));
}
}
}
// Create the sound data (of silence) for a given length of time
function playSilenceForMillis(length) {
var iterations = length / 2;
for (var i = 0; i < iterations * 30; i++) {
soundData.push(0);
}
}