|
|
@ -1,11 +1,46 @@
|
|
|
|
function PCMPlayer(channels, sampleRate) {
|
|
|
|
function PCMPlayer(option) {
|
|
|
|
|
|
|
|
this.init(option);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PCMPlayer.prototype.init = function(option) {
|
|
|
|
|
|
|
|
var default = {
|
|
|
|
|
|
|
|
encoding: '16bitInt',
|
|
|
|
|
|
|
|
channels: 1,
|
|
|
|
|
|
|
|
sampleRate: 8000,
|
|
|
|
|
|
|
|
flushingTime: 200
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
this.option = Object.assign({}, default, option);
|
|
|
|
this.samples = new Float32Array();
|
|
|
|
this.samples = new Float32Array();
|
|
|
|
this.flushingTime = 200;
|
|
|
|
this.flush = this.flush.bind(this);
|
|
|
|
this.channels = channels;
|
|
|
|
this.interval = setInterval(this.flush, this.flushingTime);
|
|
|
|
this.sampleRate = sampleRate;
|
|
|
|
this.maxValue = this.getMaxValue();
|
|
|
|
|
|
|
|
this.typedArray = this.getTypedArray();
|
|
|
|
|
|
|
|
this.createContext();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PCMPlayer.prototype.getMaxValue = function () {
|
|
|
|
|
|
|
|
var encodings = {
|
|
|
|
|
|
|
|
'8bitInt': 128,
|
|
|
|
|
|
|
|
'16bitInt': 32768,
|
|
|
|
|
|
|
|
'32bitInt': 2147483648,
|
|
|
|
|
|
|
|
'32bitFloat': 1
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.createContext = function() {
|
|
|
|
return encodings[this.option.encoding] ? encodings[this.option.encoding] : encodings['16bitInt'];
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PCMPlayer.prototype.getTypedArray = function () {
|
|
|
|
|
|
|
|
var typedArrays = {
|
|
|
|
|
|
|
|
'8bitInt': 'Int8Array',
|
|
|
|
|
|
|
|
'16bitInt': 'Int16Array',
|
|
|
|
|
|
|
|
'32bitInt': 'Int32Array',
|
|
|
|
|
|
|
|
'32bitFloat': 'Float32Array'
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return typedArrays[this.option.encoding] ? typedArrays[this.option.encoding] : typedArrays['16bitInt'];
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PCMPlayer.prototype.createContext = function() {
|
|
|
|
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
|
|
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
|
|
this.gainNode = this.audioCtx.createGain();
|
|
|
|
this.gainNode = this.audioCtx.createGain();
|
|
|
|
this.gainNode.gain.value = 1;
|
|
|
|
this.gainNode.gain.value = 1;
|
|
|
@ -13,31 +48,53 @@ function PCMPlayer(channels, sampleRate) {
|
|
|
|
this.startTime = this.audioCtx.currentTime;
|
|
|
|
this.startTime = this.audioCtx.currentTime;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.stopFlushing = function() {
|
|
|
|
PCMPlayer.prototype.isTypedArray = function(data) {
|
|
|
|
if (this.interval) {
|
|
|
|
return (data.byteLength && data.buffer && data.buffer.constructor == ArrayBuffer);
|
|
|
|
clearInterval(this.interval);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.feed = function(data) {
|
|
|
|
PCMPlayer.prototype.feed = function(data) {
|
|
|
|
let tmp = new Float32Array(this.samples.length + data.length);
|
|
|
|
if (!this.isTypedArray(isTypedArray)) return;
|
|
|
|
|
|
|
|
data = this.getFormatedValue(data);
|
|
|
|
|
|
|
|
var tmp = new Float32Array(this.samples.length + data.length);
|
|
|
|
tmp.set(this.samples, 0);
|
|
|
|
tmp.set(this.samples, 0);
|
|
|
|
tmp.set(data, this.samples.length);
|
|
|
|
tmp.set(data, this.samples.length);
|
|
|
|
this.samples = tmp;
|
|
|
|
this.samples = tmp;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.flush = function() {
|
|
|
|
PCMPlayer.prototype.getFormatedValue = function(data) {
|
|
|
|
if (!this.channels || !this.sampleRate || !this.samples.length) return;
|
|
|
|
var data = new this.typedArray(data.buffer),
|
|
|
|
let bufferSource = this.audioCtx.createBufferSource(),
|
|
|
|
float32 = new Float32Array(data.length),
|
|
|
|
|
|
|
|
i;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < data.length; i++) {
|
|
|
|
|
|
|
|
float32[i] = data[i] / this.maxValue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return float32;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PCMPlayer.prototype.volume = function(volume) {
|
|
|
|
|
|
|
|
this.gainNode.gain.value = volume;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PCMPlayer.prototype.destroy = function() {
|
|
|
|
|
|
|
|
if (this.interval) {
|
|
|
|
|
|
|
|
clearInterval(this.interval);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.samples = null;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PCMPlayer.prototype.flush = function() {
|
|
|
|
|
|
|
|
if (!this.samples.length) return;
|
|
|
|
|
|
|
|
var bufferSource = this.audioCtx.createBufferSource(),
|
|
|
|
length = this.samples.length,
|
|
|
|
length = this.samples.length,
|
|
|
|
audioBuffer = this.audioCtx.createBuffer(this.channels, length, this.sampleRate),
|
|
|
|
audioBuffer = this.audioCtx.createBuffer(this.option.channels, length, this.option.sampleRate),
|
|
|
|
audioData,
|
|
|
|
audioData,
|
|
|
|
channel,
|
|
|
|
channel,
|
|
|
|
offset,
|
|
|
|
offset,
|
|
|
|
i,
|
|
|
|
i,
|
|
|
|
decrement = 50;
|
|
|
|
decrement = 50;
|
|
|
|
|
|
|
|
|
|
|
|
for (channel = 0; channel < this.channels; channel++) {
|
|
|
|
for (channel = 0; channel < this.option.channels; channel++) {
|
|
|
|
audioData = audioBuffer.getChannelData(channel);
|
|
|
|
audioData = audioBuffer.getChannelData(channel);
|
|
|
|
offset = channel;
|
|
|
|
offset = channel;
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
@ -50,7 +107,7 @@ function PCMPlayer(channels, sampleRate) {
|
|
|
|
if (i >= (length - 51)) {
|
|
|
|
if (i >= (length - 51)) {
|
|
|
|
audioData[i] = (audioData[i] * decrement--) / 50;
|
|
|
|
audioData[i] = (audioData[i] * decrement--) / 50;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
offset += this.channels;
|
|
|
|
offset += this.option.channels;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -63,9 +120,3 @@ function PCMPlayer(channels, sampleRate) {
|
|
|
|
this.startTime += audioBuffer.duration;
|
|
|
|
this.startTime += audioBuffer.duration;
|
|
|
|
this.samples = new Float32Array();
|
|
|
|
this.samples = new Float32Array();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* initiate start flushing */
|
|
|
|
|
|
|
|
this.flush = this.flush.bind(this);
|
|
|
|
|
|
|
|
this.createContext();
|
|
|
|
|
|
|
|
this.interval = setInterval(this.flush, this.flushingTime);
|
|
|
|
|
|
|
|
}
|
|
|
|
|