added encodec type

pull/14/head
Samir 7 years ago
parent 6228cf0d71
commit 42d56c4a1a

@ -1,33 +0,0 @@
{
"env": {
"browser": true,
"es6": true
},
"globals": {
"ENV": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
},
"rules": {
"indent": [
"error",
4,
{ "SwitchCase": 1 }
],
"no-unused-vars": "off",
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}

Binary file not shown.

Binary file not shown.

@ -1,71 +1,122 @@
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();
};
this.createContext = function() { PCMPlayer.prototype.getMaxValue = function () {
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)(); var encodings = {
this.gainNode = this.audioCtx.createGain(); '8bitInt': 128,
this.gainNode.gain.value = 1; '16bitInt': 32768,
this.gainNode.connect(this.audioCtx.destination); '32bitInt': 2147483648,
this.startTime = this.audioCtx.currentTime; '32bitFloat': 1
}; }
this.stopFlushing = function() { return encodings[this.option.encoding] ? encodings[this.option.encoding] : encodings['16bitInt'];
if (this.interval) { };
clearInterval(this.interval);
}
};
this.feed = function(data) { PCMPlayer.prototype.getTypedArray = function () {
let tmp = new Float32Array(this.samples.length + data.length); var typedArrays = {
tmp.set(this.samples, 0); '8bitInt': 'Int8Array',
tmp.set(data, this.samples.length); '16bitInt': 'Int16Array',
this.samples = tmp; '32bitInt': 'Int32Array',
}; '32bitFloat': 'Float32Array'
}
this.flush = function() { return typedArrays[this.option.encoding] ? typedArrays[this.option.encoding] : typedArrays['16bitInt'];
if (!this.channels || !this.sampleRate || !this.samples.length) return; };
let bufferSource = this.audioCtx.createBufferSource(),
length = this.samples.length,
audioBuffer = this.audioCtx.createBuffer(this.channels, length, this.sampleRate),
audioData,
channel,
offset,
i,
decrement = 50;
for (channel = 0; channel < this.channels; channel++) { PCMPlayer.prototype.createContext = function() {
audioData = audioBuffer.getChannelData(channel); this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
offset = channel; this.gainNode = this.audioCtx.createGain();
for (i = 0; i < length; i++) { this.gainNode.gain.value = 1;
audioData[i] = this.samples[offset]; this.gainNode.connect(this.audioCtx.destination);
/* fadein */ this.startTime = this.audioCtx.currentTime;
if (i < 50) { };
audioData[i] = (audioData[i] * i) / 50;
} PCMPlayer.prototype.isTypedArray = function(data) {
/* fadeout*/ return (data.byteLength && data.buffer && data.buffer.constructor == ArrayBuffer);
if (i >= (length - 51)) { };
audioData[i] = (audioData[i] * decrement--) / 50;
} PCMPlayer.prototype.feed = function(data) {
offset += this.channels; 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(data, this.samples.length);
this.samples = tmp;
};
if (this.startTime < this.audioCtx.currentTime) { PCMPlayer.prototype.getFormatedValue = function(data) {
this.startTime = this.audioCtx.currentTime; var data = new this.typedArray(data.buffer),
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,
audioBuffer = this.audioCtx.createBuffer(this.option.channels, length, this.option.sampleRate),
audioData,
channel,
offset,
i,
decrement = 50;
for (channel = 0; channel < this.option.channels; channel++) {
audioData = audioBuffer.getChannelData(channel);
offset = channel;
for (i = 0; i < length; i++) {
audioData[i] = this.samples[offset];
/* fadein */
if (i < 50) {
audioData[i] = (audioData[i] * i) / 50;
}
/* fadeout*/
if (i >= (length - 51)) {
audioData[i] = (audioData[i] * decrement--) / 50;
}
offset += this.option.channels;
} }
bufferSource.buffer = audioBuffer; }
bufferSource.connect(this.gainNode);
bufferSource.start(this.startTime);
this.startTime += audioBuffer.duration;
this.samples = new Float32Array();
};
/* initiate start flushing */ if (this.startTime < this.audioCtx.currentTime) {
this.flush = this.flush.bind(this); this.startTime = this.audioCtx.currentTime;
this.createContext(); }
this.interval = setInterval(this.flush, this.flushingTime); bufferSource.buffer = audioBuffer;
} bufferSource.connect(this.gainNode);
bufferSource.start(this.startTime);
this.startTime += audioBuffer.duration;
this.samples = new Float32Array();
};
Loading…
Cancel
Save