Skip to content

Commit

Permalink
Merge pull request #11 from lepinsk/target-bitrate
Browse files Browse the repository at this point in the history
Adding configurable target bitrate
  • Loading branch information
chris-rudmin committed Apr 29, 2015
2 parents 832c48e + c3b3199 commit 66eaf31
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
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

0 comments on commit 66eaf31

Please sign in to comment.