forked from bnchdrff/happysoundplaytime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs.js
153 lines (132 loc) · 3.45 KB
/
js.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
$(document).ready(function() {
// create start sounds
var data = [];
$('<audio />').attr({id: 'el1', src: encodeAudio8bit(data)}).appendTo('#sounds1');
$('<audio />').attr({id: 'el2', src: encodeAudio8bit(data)}).appendTo('#sounds2');
var currentlyPlayingAudioThing = "el1";
var notCurrentlyPlayingAudioThing = "el2";
// loop em
$('#el1').on('ended', function() {
currentlyPlayingAudioThing = 'el1';
notCurrentlyPlayingAudioThing = 'el2';
this.pause();
this.currentTime = 0;
$('#el2')[0].play();
});
$('#el2').on('ended', function() {
currentlyPlayingAudioThing = 'el2';
notCurrentlyPlayingAudioThing = 'el1';
this.pause();
this.currentTime = 0;
$('#el1')[0].play();
});
function playSound(freq) {
var data = [];
carrierWave(data, 10000, freq);
// applySinDistort(data, 2);
// applyEnvelope(data, 1, 2, 3, 4);
var encodedAudioUrl = encodeAudio8bit(data);
$('#el1')[0].pause();
$('#el2')[0].pause();
$('#el1').attr({src: encodedAudioUrl});
$('#el2').attr({src: encodedAudioUrl});
$('#el1')[0].play();
}
var freqTable = {
A: 261.626,
W: 277.183,
S: 293.665,
E: 311.127,
D: 329.628,
F: 349.228,
T: 369.994,
G: 391.995,
Y: 415.305,
H: 440.000,
U: 466.164,
J: 493.883,
K: 523.251
};
$(document).keydown(function(ev) {
var freq = freqTable[String.fromCharCode(ev.keyCode)];
if (freq) {
playSound(freq);
}
});
$(document).keyup(function(ev) {
$('#el1')[0].pause();
$('#el2')[0].pause();
});
});
var sampleRate = 1378.125;
function applyEnvelope(data, attack, decay, sustain, release) {
var dataLength = data.length;
attack *= dataLength;
decay *= dataLength;
release *= dataLength;
var is = 1 - sustain;
for (var i = 0; i < attack; ++i) {
data[i] *= i / attack;
}
for (; i < (attack + decay); ++i) {
data[i] *= (1 - (i - attack) / decay) * is + sustain;
}
for (; i < (dataLength - release); ++i) {
data[i] *= sustain;
}
for (var j = i; i < dataLength; ++i) {
data[i] *= sustain * (1 - (i - j) / release);
}
}
function getFreqSweep(data, length, freq1, freq2) {
var d1 = Math.PI * 2 * freq1 / sampleRate,
d2 = Math.PI * 2 * freq2 / sampleRate,
dd = (d2 - d1) / length,
dt = d1,
t = 0;
for (var i = 0; i < length; ++i) {
data[i] = Math.sin(t);
t += dt;
dt += dd;
}
}
function carrierWave(data, length, freq) {
var d1 = Math.PI * 2 * freq / sampleRate;
for (var i = 0; i < length; ++i) {
data[i] = Math.sin(i * d1);
}
}
function encodeAudio8bit(data) {
var n = data.length, i;
// 8-bit mono WAVE header template
var header = "RIFF<##>WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00<##><##>\x01\x00\x08\x00data<##>";
// Helper to insert a 32-bit little endian int.
function insertLong(value) {
var bytes = "";
for (i = 0; i < 4; ++i) {
bytes += String.fromCharCode(value % 256);
value = Math.floor(value / 256);
}
header = header.replace('<##>', bytes);
}
// ChunkSize
insertLong(36 + n);
// SampleRate
insertLong(sampleRate);
// ByteRate
insertLong(sampleRate);
// Subchunk2Size
insertLong(n);
// Output sound data
for (var i = 0; i < n; ++i) {
header += String.fromCharCode(Math.round(Math.min(1, Math.max(-1, data[i])) * 127 + 127));
}
return 'data:audio/wav;base64,' + btoa(header);
}
function applySinDistort(data, drive) {
var n = data.length;
var s = Math.PI * drive / 2;
for (var i = 0; i < n; ++i) {
data[i] = Math.sin(data[i] * s);
}
}