Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added raw packets for realtime encode/decode #221

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
996 changes: 816 additions & 180 deletions dist-unminified/decoderWorker.js

Large diffs are not rendered by default.

Binary file modified dist-unminified/decoderWorker.wasm
Binary file not shown.
191,532 changes: 191,532 additions & 0 deletions dist-unminified/decoderWorker.wat

Large diffs are not rendered by default.

1,018 changes: 846 additions & 172 deletions dist-unminified/encoderWorker.js

Large diffs are not rendered by default.

294,008 changes: 294,008 additions & 0 deletions dist-unminified/encoderWorker.wat

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/decoderWorker.min.js

Large diffs are not rendered by default.

Binary file modified dist/decoderWorker.min.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/encoderWorker.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions example/encoder.html
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ <h2>Log</h2>
recordingGain: parseInt(recordingGain.value, 10),
numberOfChannels: parseInt(numberOfChannels.value, 10),
encoderSampleRate: parseInt(encoderSampleRate.value, 10),
rawPacket:true,
encoderPath: "../dist/encoderWorker.min.js"
// sourceNode: sourceNode
};
Expand Down
26 changes: 26 additions & 0 deletions src/decoderWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ global['onmessage'] = function( e ){
}
break;

case 'decodeRawPacket':
if (decoder){
decoder.decodeRawPacket( e['data']['rawPacket'] );
}
break;

case 'done':
if (decoder) {
decoder.sendLastBuffer();
Expand All @@ -26,6 +32,7 @@ global['onmessage'] = function( e ){
break;

default:
console.warn("Unsupported Message");
// Ignore any unknown commands and continue recieving commands
}
});
Expand Down Expand Up @@ -60,6 +67,25 @@ var OggOpusDecoder = function( config, Module ){
this.outputBuffers = [];
};

OggOpusDecoder.prototype.decodeRawPacket = function(typedArray) {
if (!this.inited) {
this.numberOfChannels = typedArray[0] & 0x04 ? 2 : 1;
this.init();
this.inited = true;
}
this.decoderBuffer.set( typedArray );

// Decode raw opus packet
var outputSampleLength = this._opus_decode_float( this.decoder, this.decoderBufferPointer, typedArray.length, this.decoderOutputPointer, this.decoderOutputMaxLength, 0);
var resampledLength = Math.ceil( outputSampleLength * this.config.outputBufferSampleRate / this.config.decoderSampleRate );
this.HEAP32[ this.decoderOutputLengthPointer >> 2 ] = outputSampleLength;
this.HEAP32[ this.resampleOutputLengthPointer >> 2 ] = resampledLength;
this._speex_resampler_process_interleaved_float( this.resampler, this.decoderOutputPointer, this.decoderOutputLengthPointer, this.resampleOutputBufferPointer, this.resampleOutputLengthPointer );
this.sendToOutputBuffers( this.HEAPF32.subarray( this.resampleOutputBufferPointer >> 2, (this.resampleOutputBufferPointer >> 2) + resampledLength * this.numberOfChannels ) );
this.decoderBufferIndex = 0;

return;
}

OggOpusDecoder.prototype.decode = function( typedArray ) {
var dataView = new DataView( typedArray.buffer );
Expand Down
14 changes: 14 additions & 0 deletions src/encoderWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ OggOpusEncoder.prototype.getChecksum = function( data ){
};

OggOpusEncoder.prototype.generateCommentPage = function(){
if ( this.config.rawPacket ) { return; }
var segmentDataView = new DataView( this.segmentData.buffer );
segmentDataView.setUint32( 0, 1937076303, true ) // Magic Signature 'Opus'
segmentDataView.setUint32( 4, 1936154964, true ) // Magic Signature 'Tags'
Expand All @@ -159,6 +160,7 @@ OggOpusEncoder.prototype.generateCommentPage = function(){
};

OggOpusEncoder.prototype.generateIdPage = function(){
if ( this.config.rawPacket ) { return; }
var segmentDataView = new DataView( this.segmentData.buffer );
segmentDataView.setUint32( 0, 1937076303, true ) // Magic Signature 'Opus'
segmentDataView.setUint32( 4, 1684104520, true ) // Magic Signature 'Head'
Expand All @@ -175,6 +177,7 @@ OggOpusEncoder.prototype.generateIdPage = function(){
};

OggOpusEncoder.prototype.generatePage = function(){
if ( this.config.rawPacket ) { return; }
var granulePosition = ( this.lastPositiveGranulePosition === this.granulePosition) ? -1 : this.granulePosition;
var pageBuffer = new ArrayBuffer( 27 + this.segmentTableIndex + this.segmentDataIndex );
var pageBufferView = new DataView( pageBuffer );
Expand Down Expand Up @@ -282,6 +285,17 @@ OggOpusEncoder.prototype.interleave = function( buffers ) {
};

OggOpusEncoder.prototype.segmentPacket = function( packetLength ) {
if (this.config.rawPacket) {
if (packetLength > 0) {
var page = new Uint8Array( HEAPU8.subarray(this.encoderOutputPointer, this.encoderOutputPointer + packetLength) );
if (postMessage) {
postMessage(page, [page.buffer]);
}
return;
}
}


var packetIndex = 0;
var exportPages = [];

Expand Down