mirror of
https://github.com/samirkumardas/pcm-player.git
synced 2025-04-16 15:21:24 +02:00
pcm samples by channel
This commit is contained in:
parent
14254cc819
commit
556104a3e8
3 changed files with 7 additions and 6 deletions
|
@ -12,10 +12,10 @@
|
||||||
window.onload = function() {
|
window.onload = function() {
|
||||||
var socketURL = 'ws://localhost:8080';
|
var socketURL = 'ws://localhost:8080';
|
||||||
var player = new PCMPlayer({
|
var player = new PCMPlayer({
|
||||||
encoding: '16bitInt',
|
encoding: '32bitFloat',
|
||||||
channels: 2,
|
channels: 2,
|
||||||
sampleRate: 8000,
|
sampleRate: 8000,
|
||||||
flushingTime: 1000
|
flushingTime: 2000
|
||||||
});
|
});
|
||||||
|
|
||||||
var ws = new WebSocket(socketURL);
|
var ws = new WebSocket(socketURL);
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const WebSocket = require('ws');
|
const WebSocket = require('ws');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
const pcm_file = './16bit-8000.raw';
|
const pcm_file = './32bit (float)-8000.raw';
|
||||||
let interval = 0,
|
let interval = 0,
|
||||||
sampleRate = 8000,
|
sampleRate = 8000,
|
||||||
channels = 2,
|
channels = 2,
|
||||||
|
|
|
@ -86,17 +86,18 @@ PCMPlayer.prototype.destroy = function() {
|
||||||
PCMPlayer.prototype.flush = function() {
|
PCMPlayer.prototype.flush = function() {
|
||||||
if (!this.samples.length) return;
|
if (!this.samples.length) return;
|
||||||
var bufferSource = this.audioCtx.createBufferSource(),
|
var bufferSource = this.audioCtx.createBufferSource(),
|
||||||
length = this.samples.length,
|
length = this.samples.length / this.option.channels,
|
||||||
audioBuffer = this.audioCtx.createBuffer(this.option.channels, length, this.option.sampleRate),
|
audioBuffer = this.audioCtx.createBuffer(this.option.channels, length, this.option.sampleRate),
|
||||||
audioData,
|
audioData,
|
||||||
channel,
|
channel,
|
||||||
offset,
|
offset,
|
||||||
i,
|
i,
|
||||||
decrement = 50;
|
decrement;
|
||||||
|
|
||||||
for (channel = 0; channel < this.option.channels; channel++) {
|
for (channel = 0; channel < this.option.channels; channel++) {
|
||||||
audioData = audioBuffer.getChannelData(channel);
|
audioData = audioBuffer.getChannelData(channel);
|
||||||
offset = channel;
|
offset = channel;
|
||||||
|
decrement = 50;
|
||||||
for (i = 0; i < length; i++) {
|
for (i = 0; i < length; i++) {
|
||||||
audioData[i] = this.samples[offset];
|
audioData[i] = this.samples[offset];
|
||||||
/* fadein */
|
/* fadein */
|
||||||
|
@ -114,7 +115,7 @@ PCMPlayer.prototype.flush = function() {
|
||||||
if (this.startTime < this.audioCtx.currentTime) {
|
if (this.startTime < this.audioCtx.currentTime) {
|
||||||
this.startTime = this.audioCtx.currentTime;
|
this.startTime = this.audioCtx.currentTime;
|
||||||
}
|
}
|
||||||
console.log('start vs current '+this.startTime+' vs '+this.audioCtx.currentTime);
|
console.log('start vs current '+this.startTime+' vs '+this.audioCtx.currentTime+' duration: '+audioBuffer.duration);
|
||||||
bufferSource.buffer = audioBuffer;
|
bufferSource.buffer = audioBuffer;
|
||||||
bufferSource.connect(this.gainNode);
|
bufferSource.connect(this.gainNode);
|
||||||
bufferSource.start(this.startTime);
|
bufferSource.start(this.startTime);
|
||||||
|
|
Loading…
Add table
Reference in a new issue