-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractor.js
38 lines (34 loc) · 1.28 KB
/
extractor.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
// Create an audio context and source node
var audioContext = new AudioContext();
var audioBufferSourceNode = audioContext.createBufferSource();
var startTime = 0;
// Create a file reader and extract audio samples
var fileReader = new FileReader();
var decodeSuccessCallback = function(audioBuffer) {
// Send audio samples to processor
process(audioBuffer.getChannelData(0), audioBuffer.sampleRate);
// Play audio
audioBufferSourceNode.disconnect();
audioBufferSourceNode = audioContext.createBufferSource();
audioBufferSourceNode.buffer = audioBuffer;
audioBufferSourceNode.connect(audioContext.destination);
audioBufferSourceNode.start();
// Record current time as audio start time
startTime = audioContext.currentTime;
};
var decodeErrorCallback = function() {
console.log('Error decoding audio data');
};
fileReader.addEventListener('load', function() {
// Extract audio samples
var arrayBuffer = fileReader.result;
audioContext.decodeAudioData(arrayBuffer, decodeSuccessCallback, decodeErrorCallback);
});
fileReader.addEventListener('error', function() {
console.log('Error reading file');
});
// Set action for input element on input change
var input = document.getElementById('input');
input.addEventListener('change', function() {
fileReader.readAsArrayBuffer(input.files[0]);
});