added encodec type
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);
|
||||||
|
}
|
||||||
|
|
||||||
this.samples = new Float32Array();
|
PCMPlayer.prototype.init = function(option) {
|
||||||
this.flushingTime = 200;
|
var default = {
|
||||||
this.channels = channels;
|
encoding: '16bitInt',
|
||||||
this.sampleRate = sampleRate;
|
channels: 1,
|
||||||
|
sampleRate: 8000,
|
||||||
this.createContext = function() {
|
flushingTime: 200
|
||||||
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
||||||
this.gainNode = this.audioCtx.createGain();
|
|
||||||
this.gainNode.gain.value = 1;
|
|
||||||
this.gainNode.connect(this.audioCtx.destination);
|
|
||||||
this.startTime = this.audioCtx.currentTime;
|
|
||||||
};
|
};
|
||||||
|
this.option = Object.assign({}, default, option);
|
||||||
|
this.samples = new Float32Array();
|
||||||
|
this.flush = this.flush.bind(this);
|
||||||
|
this.interval = setInterval(this.flush, this.flushingTime);
|
||||||
|
this.maxValue = this.getMaxValue();
|
||||||
|
this.typedArray = this.getTypedArray();
|
||||||
|
this.createContext();
|
||||||
|
};
|
||||||
|
|
||||||
this.stopFlushing = function() {
|
PCMPlayer.prototype.getMaxValue = function () {
|
||||||
if (this.interval) {
|
var encodings = {
|
||||||
clearInterval(this.interval);
|
'8bitInt': 128,
|
||||||
}
|
'16bitInt': 32768,
|
||||||
};
|
'32bitInt': 2147483648,
|
||||||
|
'32bitFloat': 1
|
||||||
|
}
|
||||||
|
|
||||||
this.feed = function(data) {
|
return encodings[this.option.encoding] ? encodings[this.option.encoding] : encodings['16bitInt'];
|
||||||
let tmp = new Float32Array(this.samples.length + data.length);
|
};
|
||||||
tmp.set(this.samples, 0);
|
|
||||||
tmp.set(data, this.samples.length);
|
PCMPlayer.prototype.getTypedArray = function () {
|
||||||
this.samples = tmp;
|
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.gainNode = this.audioCtx.createGain();
|
||||||
|
this.gainNode.gain.value = 1;
|
||||||
|
this.gainNode.connect(this.audioCtx.destination);
|
||||||
|
this.startTime = this.audioCtx.currentTime;
|
||||||
|
};
|
||||||
|
|
||||||
|
PCMPlayer.prototype.isTypedArray = function(data) {
|
||||||
|
return (data.byteLength && data.buffer && data.buffer.constructor == ArrayBuffer);
|
||||||
|
};
|
||||||
|
|
||||||
|
PCMPlayer.prototype.feed = function(data) {
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
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),
|
||||||
length = this.samples.length,
|
i;
|
||||||
audioBuffer = this.audioCtx.createBuffer(this.channels, length, this.sampleRate),
|
|
||||||
audioData,
|
|
||||||
channel,
|
|
||||||
offset,
|
|
||||||
i,
|
|
||||||
decrement = 50;
|
|
||||||
|
|
||||||
for (channel = 0; channel < this.channels; channel++) {
|
for (i = 0; i < data.length; i++) {
|
||||||
audioData = audioBuffer.getChannelData(channel);
|
float32[i] = data[i] / this.maxValue;
|
||||||
offset = channel;
|
}
|
||||||
for (i = 0; i < length; i++) {
|
return float32;
|
||||||
audioData[i] = this.samples[offset];
|
};
|
||||||
/* fadein */
|
|
||||||
if (i < 50) {
|
PCMPlayer.prototype.volume = function(volume) {
|
||||||
audioData[i] = (audioData[i] * i) / 50;
|
this.gainNode.gain.value = volume;
|
||||||
}
|
};
|
||||||
/* fadeout*/
|
|
||||||
if (i >= (length - 51)) {
|
PCMPlayer.prototype.destroy = function() {
|
||||||
audioData[i] = (audioData[i] * decrement--) / 50;
|
if (this.interval) {
|
||||||
}
|
clearInterval(this.interval);
|
||||||
offset += this.channels;
|
}
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (this.startTime < this.audioCtx.currentTime) {
|
|
||||||
this.startTime = this.audioCtx.currentTime;
|
if (this.startTime < this.audioCtx.currentTime) {
|
||||||
}
|
this.startTime = this.audioCtx.currentTime;
|
||||||
bufferSource.buffer = audioBuffer;
|
}
|
||||||
bufferSource.connect(this.gainNode);
|
bufferSource.buffer = audioBuffer;
|
||||||
bufferSource.start(this.startTime);
|
bufferSource.connect(this.gainNode);
|
||||||
this.startTime += audioBuffer.duration;
|
bufferSource.start(this.startTime);
|
||||||
this.samples = new Float32Array();
|
this.startTime += audioBuffer.duration;
|
||||||
};
|
this.samples = new Float32Array();
|
||||||
|
};
|
||||||
/* initiate start flushing */
|
|
||||||
this.flush = this.flush.bind(this);
|
|
||||||
this.createContext();
|
|
||||||
this.interval = setInterval(this.flush, this.flushingTime);
|
|
||||||
}
|
|
Loading…
Reference in New Issue