Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

closes #6

Added support for 1 channel, 22050 sample rate (assuming browser
samples at 44100) and 8192 buffer size (185 ms approx)
Alternate samples are dropped and upto 8 posts are allowed
simultaneously
worker does sample conversion to 16 bit signed integer
for better working encoding to be done in worker and encoded packets
are sent (TBD)
  • Loading branch information...
commit 274ddc9961427fdba30202b220b28b3c70efa674 1 parent b676db0
@ariemtech authored
View
60 client/WebAudioWorker.js
@@ -4,6 +4,10 @@ var recLength = 0,
url,
chunk= 1;
+var postsInProgress = 0,
+ sentPackets = 0,
+ statsInterval = 5000;
+
this.onmessage = function(e){
switch(e.data.command){
case 'init':
@@ -12,6 +16,8 @@ this.onmessage = function(e){
break;
case 'config':
url = e.data.config.url;
+ postsInProgress = 0;
+ setInterval(stats, statsInterval);
break;
case 'record':
record(e.data.buffer);
@@ -24,13 +30,15 @@ function init(config){
}
function record(inputBuffer){
- var bufferL = inputBuffer[0];
- var bufferR = inputBuffer[1];
- recBuffers = interleave(bufferL, bufferR);
+ var bufferL = inputBuffer[0]; //only bufferL to be used for channel 1 and no interleave needed
+ //var bufferR = inputBuffer[1];
+ //recBuffers = interleave(bufferL, bufferR);
//recBuffers.push(interleaved);
- recLength = recBuffers.length;
- var dataview = encodeWAV(recBuffers);
- sendPCMData(dataview);
+ //recLength = recBuffers.length;
+ recLength = bufferL.length;
+ //var dataview = encodeWAV(recBuffers);
+ var dataview = encodeWAV(bufferL);
+ queuePCMData(dataview);
}
function interleave(inputL, inputR){
@@ -49,7 +57,7 @@ function interleave(inputL, inputR){
}
function floatTo16BitPCM(output, offset, input){
- for (var i = 0; i < input.length; i++, offset+=2){
+ for (var i = 0; i < input.length; i+=2, offset+=2){ //note i+=2, skipping alternate sample
var s = Math.max(-1, Math.min(1, input[i]));
output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true);
}
@@ -62,7 +70,7 @@ function writeString(view, offset, string){
}
function encodeWAV(samples){
- var buffer = new ArrayBuffer(samples.length * 2);
+ var buffer = new ArrayBuffer(samples.length ); //since alternate samples are removed size is half
var view = new DataView(buffer);
///* RIFF identifier */
@@ -100,9 +108,45 @@ function encodeWAV(samples){
function sendPCMData (ab) {
// Get an XMLHttpRequest instance
var xhr = new XMLHttpRequest();
+ postsInProgress++;
//console.log("data recieved: "+ab.byteLength+"sending to url: "+url);
// Set up request & send
+ xhr.onerror = function (e) {
+ postsInProgress--;
+ sendNext();
+ };
+ xhr.onreadystatechange = function(){
+ if ( xhr.readyState == 4 ) {
+ postsInProgress--;
+ sendNext();
+ }
+ };
xhr.open('POST', url+"&chunk="+chunk, true);
chunk++;
xhr.send(ab);
}
+
+function queuePCMData (ab) {
+ if (recBuffers.length > 50) {
+ this.postMessage("post queue length exceeded 50, emptying the old data");
+ recBuffers = [];
+ }
+ recBuffers.push(ab);
+ if (postsInProgress < 8 ) {
+ sendNext();
+ }
+}
+
+function sendNext () {
+ if (recBuffers.length > 0) {
+ sendPCMData (recBuffers[0]);
+ recBuffers.shift();
+ sentPackets +=1 ;
+ }
+}
+
+function stats (){
+ this.postMessage("current queue depth: "+recBuffers.length+", postsInProgress: "+postsInProgress);
+ this.postMessage("transmission rate in packets: "+ sentPackets * 1000 /statsInterval);
+ sentPackets = 0;
+}
View
13 client/js-cast.js
@@ -87,7 +87,7 @@
var WebAudioClass = function() {
this.audioSessionInProgress = false;
- this.numChannels = 2; //hardcoded to 2
+ this.numChannels = 1; //hardcoded to 1
this.Float32ArrayBuffer = [];
this.context = null;
@@ -107,14 +107,14 @@
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
- var default_config= {bufferLen: 4096}
+ var default_config= {bufferLen: 8192}
var config = a_config || {};
for (var key in default_config){
this[key] = typeof (config[key]) !== "undefined"? config[key] : default_config[key];
}
this.Float32ArrayBuffer[0] = new Float32Array(this.bufferLen);
- this.Float32ArrayBuffer[1] = new Float32Array(this.bufferLen);
+ //this.Float32ArrayBuffer[1] = new Float32Array(this.bufferLen);
var createAudioContext = function(localMediaStream) {
@@ -137,7 +137,8 @@
self.worker.onmessage = function(e){
var ab = e.data;
//self.emit('data',ab);
- sendPCMData(self._recording_url, ab);
+ //sendPCMData(self._recording_url, ab);
+ console.log(e.data); //post is done in worker itself, only console messages are sent
}
self.worker.postMessage({
@@ -157,8 +158,8 @@
self.worker.postMessage({
command: 'record',
buffer: [
- e.inputBuffer.getChannelData(0),
- e.inputBuffer.getChannelData(1)
+ e.inputBuffer.getChannelData(0)
+ //e.inputBuffer.getChannelData(1)
]
});
}
View
4 lib/ReqQueue.js
@@ -13,7 +13,7 @@ var ReqQueue= function(writestream, min, max){
this.MAX_THRESHOLD= max;
this._writestream= writestream;
this._stopped = true;
- this.writeTimer = setInterval(this.flush.bind(this),92);
+ this.writeTimer = setInterval(this.flush.bind(this),184); //coresponding to 8192 buffer size on client
};
util.inherits(ReqQueue, EventEmitter);
@@ -62,7 +62,7 @@ ReqQueue.prototype.flush= function(){
//send a single packet
var pkt= this._q[0];
if (pkt.complete){
- console.log("Writing packet -- "+pkt.index);
+ //console.log("Writing packet -- "+pkt.index);
for (var i=0, count= pkt.data.length; i< count; i++){
if (!this._writestream.write(pkt.data[i])) {
this.emit("data", "encoder is asking to stop the write");
View
2  lib/js-cast.js
@@ -15,7 +15,7 @@ var DEFAULT_OPTIONS = {
encoder: "oggenc"
};
var ENCODER_ARGS = {
- oggenc: "-r -Q --ignorelength -k -q 10 -B 16 -C 2 -R 44100 -",
+ oggenc: "-r -Q -q 10 -B 16 -C 1 -R 22050 -",
//oggenc: "-r -Q --ignorelength -k -q 10 -B 16 -C 1 -R 22050 --raw-endianness 1 -",
ffmpeg: "-i - -f ogg -oggpagesize 250 -acodec libvorbis -ab 22050 -aq 10 -ac 1 -",
avconv: "-i - -f ogg -oggpagesize 250 -acodec libvorbis -ab 22050 -aq 10 -ac 1 -",
Please sign in to comment.
Something went wrong with that request. Please try again.