From 556104a3e8f6ca5b8a385b30f86dc20d9f02bcbd Mon Sep 17 00:00:00 2001 From: Samir Date: Sat, 11 Nov 2017 00:19:08 +0600 Subject: [PATCH] pcm samples by channel --- example/index.html | 4 ++-- example/server/server.js | 2 +- pcm-player.js | 7 ++++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/example/index.html b/example/index.html index ece6493..1be1213 100644 --- a/example/index.html +++ b/example/index.html @@ -12,10 +12,10 @@ window.onload = function() { var socketURL = 'ws://localhost:8080'; var player = new PCMPlayer({ - encoding: '16bitInt', + encoding: '32bitFloat', channels: 2, sampleRate: 8000, - flushingTime: 1000 + flushingTime: 2000 }); var ws = new WebSocket(socketURL); diff --git a/example/server/server.js b/example/server/server.js index 2feed77..14ab641 100644 --- a/example/server/server.js +++ b/example/server/server.js @@ -1,7 +1,7 @@ const WebSocket = require('ws'); const fs = require('fs'); -const pcm_file = './16bit-8000.raw'; +const pcm_file = './32bit (float)-8000.raw'; let interval = 0, sampleRate = 8000, channels = 2, diff --git a/pcm-player.js b/pcm-player.js index fd7322d..15872d6 100644 --- a/pcm-player.js +++ b/pcm-player.js @@ -86,17 +86,18 @@ PCMPlayer.prototype.destroy = function() { PCMPlayer.prototype.flush = function() { if (!this.samples.length) return; 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), audioData, channel, offset, i, - decrement = 50; + decrement; for (channel = 0; channel < this.option.channels; channel++) { audioData = audioBuffer.getChannelData(channel); offset = channel; + decrement = 50; for (i = 0; i < length; i++) { audioData[i] = this.samples[offset]; /* fadein */ @@ -114,7 +115,7 @@ PCMPlayer.prototype.flush = function() { if (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.connect(this.gainNode); bufferSource.start(this.startTime);