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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
| 'use strict';
WaveSurfer.WebAudio = {
scriptBufferSize: 256,
fftSize: 128,
getAudioContext: function () {
if (!(window.AudioContext || window.webkitAudioContext)) {
throw new Error("Your browser doesn't support Web Audio");
}
if (!WaveSurfer.WebAudio.audioContext) {
WaveSurfer.WebAudio.audioContext = new (
window.AudioContext || window.webkitAudioContext
);
}
return WaveSurfer.WebAudio.audioContext;
},
init: function (params) {
this.params = params;
this.ac = params.audioContext || this.getAudioContext();
this.firedFinish = false;
this.lastStartPosition = 0;
this.lastPlay = this.lastPause = this.nextPause = this.ac.currentTime;
this.createVolumeNode();
this.createScriptNode();
this.createAnalyserNode();
this.setPlaybackRate(this.params.audioRate);
},
disconnectFilters: function () {
if (this.filters) {
this.filters.forEach(function (filter) {
filter && filter.disconnect();
});
this.filters = null;
}
},
// Unpacked filters
setFilter: function () {
this.setFilters([].slice.call(arguments));
},
/**
* @param {Array} filters Packed ilters array
*/
setFilters: function (filters) {
this.disconnectFilters();
if (filters && filters.length) {
this.filters = filters;
// Connect each filter in turn
filters.reduce(function (prev, curr) {
prev.connect(curr);
return curr;
}, this.analyser).connect(this.gainNode);
} else {
this.analyser.connect(this.gainNode);
}
},
createScriptNode: function () {
var my = this;
var bufferSize = this.scriptBufferSize;
if (this.ac.createScriptProcessor) {
this.scriptNode = this.ac.createScriptProcessor(bufferSize);
} else {
this.scriptNode = this.ac.createJavaScriptNode(bufferSize);
}
this.scriptNode.connect(this.ac.destination);
this.scriptNode.onaudioprocess = function () {
var time = my.getCurrentTime();
if (!my.firedFinish && my.buffer && time >= my.getDuration()) {
my.firedFinish = true;
my.fireEvent('finish');
}
if (!my.isPaused()) {
my.fireEvent('audioprocess', time);
}
};
},
createAnalyserNode: function () {
this.analyser = this.ac.createAnalyser();
this.analyser.fftSize = this.fftSize;
this.analyserData = new Uint8Array(this.analyser.frequencyBinCount);
this.analyser.connect(this.gainNode);
},
/**
* Create the gain node needed to control the playback volume.
*/
createVolumeNode: function () {
// Create gain node using the AudioContext
if (this.ac.createGain) {
this.gainNode = this.ac.createGain();
} else {
this.gainNode = this.ac.createGainNode();
}
// Add the gain node to the graph
this.gainNode.connect(this.ac.destination);
},
/**
* Set the gain to a new value.
*
* @param {Number} newGain The new gain, a floating point value
* between 0 and 1. 0 being no gain and 1 being maximum gain.
*/
setVolume: function (newGain) {
this.gainNode.gain.value = newGain;
},
/**
* Get the current gain.
*
* @returns {Number} The current gain, a floating point value
* between 0 and 1. 0 being no gain and 1 being maximum gain.
*/
getVolume: function () {
return this.gainNode.gain.value;
},
decodeArrayBuffer: function (arraybuffer, callback, errback) {
var my = this;
this.ac.decodeAudioData(arraybuffer, function (data) {
my.buffer = data;
callback(data);
}, errback);
},
/**
* @returns {Float32Array} Array of peaks.
*/
getPeaks: function (length) {
var buffer = this.buffer;
var sampleSize = buffer.length / length;
var sampleStep = ~~(sampleSize / 10) || 1;
var channels = buffer.numberOfChannels;
var peaks = new Float32Array(length);
for (var c = 0; c < channels; c++) {
var chan = buffer.getChannelData(c);
for (var i = 0; i < length; i++) {
var start = ~~(i * sampleSize);
var end = ~~(start + sampleSize);
var max = 0;
for (var j = start; j < end; j += sampleStep) {
var value = chan[j];
if (value > max) {
max = value;
// faster than Math.abs
} else if (-value > max) {
max = -value;
}
}
if (c == 0 || max > peaks[i]) {
peaks[i] = max;
}
}
}
return peaks;
},
getPlayedPercents: function () {
var duration = this.getDuration();
return (this.getCurrentTime() / duration) || 0;
},
disconnectSource: function () {
this.firedFinish = false;
if (this.source) {
this.source.disconnect();
}
},
/**
* Returns the real-time waveform data.
*
* @return {Uint8Array} The frequency data.
* Values range from 0 to 255.
*/
waveform: function () {
this.analyser.getByteTimeDomainData(this.analyserData);
return this.analyserData;
},
destroy: function () {
this.pause();
this.unAll();
this.buffer = null;
this.disconnectFilters();
this.disconnectSource();
this.gainNode.disconnect();
this.scriptNode.disconnect();
this.analyser.disconnect();
},
load: function (buffer) {
this.lastStartPosition = 0;
this.lastPlay = this.lastPause = this.nextPause = this.ac.currentTime;
this.buffer = buffer;
this.createSource();
},
createSource: function () {
this.disconnectSource();
this.source = this.ac.createBufferSource();
this.source.playbackRate.value = this.playbackRate;
this.source.buffer = this.buffer;
this.source.connect(this.analyser);
},
isPaused: function () {
return this.nextPause <= this.ac.currentTime;
},
getDuration: function () {
return this.buffer.duration;
},
/**
* Plays the loaded audio region.
*
* @param {Number} start Start offset in seconds,
* relative to the beginning of a clip.
* @param {Number} end When to stop
* relative to the beginning of a clip.
*/
play: function (start, end) {
// need to re-create source on each playback
this.createSource();
if (start == null) {
start = this.getCurrentTime();
if (start >= this.getDuration()) {
start = 0;
}
}
if (end == null) {
end = this.getDuration();
}
this.lastPlay = this.ac.currentTime;
this.lastStartPosition = start;
this.lastPause = this.nextPause = this.ac.currentTime + (end - start);
if (this.source.start) {
this.source.start(0, start, end - start);
} else {
this.source.noteGrainOn(0, start, end - start);
}
this.fireEvent('play');
},
/**
* Pauses the loaded audio.
*/
pause: function () {
this.scheduledPause = null;
this.lastPause = this.nextPause = this.ac.currentTime;
if (this.source) {
alert(this.source);
if (this.source.stop) {
this.source.stop(0);
} else {
this.source.noteOff(0);
}
}
this.fireEvent('pause');
},
getCurrentTime: function () {
if (this.isPaused()) {
return this.lastStartPosition + (this.lastPause - this.lastPlay) * this.playbackRate;
} else {
return this.lastStartPosition + (this.ac.currentTime - this.lastPlay) * this.playbackRate;
}
},
/**
* Set the audio source playback rate.
*/
setPlaybackRate: function (value) {
this.playbackRate = value || 1;
if (this.source) {
this.source.playbackRate.value = this.playbackRate;
}
}
};
WaveSurfer.util.extend(WaveSurfer.WebAudio, WaveSurfer.Observer); |
Partager