-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnd.js
136 lines (102 loc) · 3.23 KB
/
snd.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
var context,
bufferLoader,
panner,
finalGain,
compressor,
audioFile,
audioFileArray = [],
nChan,
channels = [];
function initAudio() {
if ('webkitAudioContext' in window || 'AudioContext' in window) {
console.log('Your browser DOES support Web Audio API');
context = new webkitAudioContext() || AudioContext() || mozAudioContext || oAudioContext || msAudioContext;
var p = navigator.platform;
console.log(p);
if ( p === 'iPad' || p === 'iPhone' || p === 'iPod' ) {
bufferLoader = new BufferLoader(
context, ["data/bach_canon_harp.aac",
], finishedLoading
);
} else {
bufferLoader = new BufferLoader(
context, ["data/bach_canon_harp.mp3",
], finishedLoading
);
}
bufferLoader.load();
} else {
alert('Your browser does not support Web Audio API');
}
}
function initAudioChain(bufferList) {
finalGain = context.createGain();
compressor = context.createDynamicsCompressor();
compressor.treshold = 0;
compressor.ratio = 20;
audioFile = context.createBufferSource();
audioFile.buffer = bufferList[0];
nChan = audioFile.buffer.numberOfChannels;
channels = [];
for (var i = 0; i < nChan; i++) {
channels[i] = new Float32Array(audioFile.buffer.getChannelData(i));
}
compressor.connect(context.destination);
}
function finishedLoading(bufferList) {
console.log("finished loading!");
initAudioChain(bufferList);
createBuffers(1);
playAtIndex(0);
}
function createBuffers(indexMax) {
for (var index = 0; index < indexMax; index++) {
var len;
if (indexMax === 1) {
len = audioFile.buffer.length;
} else {
len = Math.floor(audioFile.buffer.length / ( Math.pow(2, ( getBaseLog(2, Math.sqrt(indexMax)) + (getBaseLog(2, Math.sqrt(indexMax)) - 1) ) )));
}
var tempIndx = len * Math.floor(index / 2);
var newBuffer = context.createBuffer(nChan, len, audioFile.buffer.sampleRate);
for (var i = 0; i < nChan; i++) {
var tempBuffer = new Array(len);
for (var k = 0; k < tempBuffer.length; k++) {
//if index is even the buffer is written forward
if ((index % 2) === 0) {
tempBuffer[k] = channels[i][tempIndx + k];
//if index is odd the buffer is written backwards
} else {
tempBuffer[k] = channels[i][tempIndx + len - k];
}
}
//tempBuffer is saved in newBuffer
newBuffer.getChannelData(i).set(tempBuffer);
}
audioFileArray[index] = context.createBufferSource();
audioFileArray[index].buffer = newBuffer;
audioFileArray[index].loop = true;
var panner = context.createPanner();
panner.panningModel = "equalpower";
var xPos;
if (indexMax === 1) xPos = 0;
else xPos = mapRange(index, 0, indexMax, -1, 1);
panner.setPosition(xPos, 0, 0);
audioFileArray[index].connect(panner);
panner.connect(compressor);
}
}
function playAtIndex(index) {
audioFileArray[index].start(0);
}
function stopSound(indexMax) {
for (var i = 0; i < indexMax; i++) {
audioFileArray[i].stop(0);
}
}
function getBaseLog(x, y) {
return Math.log(y) / Math.log(x);
}
function mapRange(value, low1, high1, low2, high2) {
return low2+(high2 - low2) * (value - low1) / (high1 - low1);
}