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

adding configurable target bitrate #11

Merged
merged 5 commits into from
Apr 29, 2015
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The Opus encoder will not work if the value is not 8000, 12000, 16000, 24000 or
- **maxBuffersPerPage** - (*optional*) Specifies the maximum number of buffers to use before generating an Ogg page. This can be used to lower the streaming latency. The lower the value the more overhead the ogg stream will incur. Defaults to 40.
- **encoderApplication** - (*optional*) Specifies the encoder application. Supported values are 2048 - Voice, 2049 - Full Band Audio, 2051 - Restricted Low Delay. Defaults to 2049.
- **encoderFrameSize** (*optional*) Specifies the frame size in ms used for encoding. Defaults to 20.
- **bitRate** (*optional*) Specifies the target bitrate in bits/sec. The encoder selects an application-specific default when this is not specified.


---------
Expand Down
7 changes: 6 additions & 1 deletion example.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ <h2>Options</h2>
<input id="sampleRate" type="number" value="48000" />
</div>

<div>
<label>bitRate</label>
<input id="bitRate" type="number" value="64000" />
</div>

<h2>Commands</h2>
<button id="init">init</button>
<button id="start" disabled>start</button>
Expand Down Expand Up @@ -76,7 +81,7 @@ <h2>Log</h2>
monitorGain: monitorGain.value,
numberOfChannels: numberOfChannels.value,
bitDepth: bitDepth.options[ bitDepth.selectedIndex ].value,
recordOpus: recordOpus.checked,
recordOpus: recordOpus.checked ? {bitRate: bitRate.value} : false,
sampleRate: sampleRate.value
});

Expand Down
9 changes: 9 additions & 0 deletions oggopus.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var OggOpus = function( config ){
this.maxBuffersPerPage = config.recordOpus.maxBuffersPerPage || 40; // Limit latency for streaming
this.encoderApplication = config.recordOpus.encoderApplication || 2049; // 2048 = Voice, 2049 = Full Band Audio, 2051 = Restricted Low Delay
this.encoderFrameSize = config.recordOpus.encoderFrameSize || 20; // 20ms frame
this.bitRate = config.recordOpus.bitRate;
this.wavepcm = new WavePCM( config );

this.pageIndex = 0;
Expand Down Expand Up @@ -138,6 +139,14 @@ OggOpus.prototype.initChecksumTable = function(){

OggOpus.prototype.initCodec = function() {
this.encoder = _opus_encoder_create( this.outputSampleRate, this.numberOfChannels, this.encoderApplication, allocate(4, 'i32', ALLOC_STACK) );

if ( this.bitRate ) {
var bitRateLocation = _malloc( 4 );
HEAP32[bitRateLocation >>> 2] = this.bitRate;
_opus_encoder_ctl( this.encoder, 4002, bitRateLocation );
_free( bitRateLocation );
}

this.encoderBufferIndex = 0;
this.encoderSamplesPerChannelPerPacket = this.outputSampleRate * this.encoderFrameSize / 1000;
this.encoderBufferLength = this.encoderSamplesPerChannelPerPacket * this.numberOfChannels;
Expand Down