Skip to content

Commit 435fa11

Browse files
authored
Merge pull request #115 from chris-rudmin/preload-worker
Preload worker
2 parents 2cdfffe + 888fe0e commit 435fa11

File tree

4 files changed

+49
-24
lines changed

4 files changed

+49
-24
lines changed

dist/recorder.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "opus-recorder",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"description": "A library for recording opus encoded audio",
55
"homepage": "https://github.com/chris-rudmin/opus-recorder",
66
"author": "Chris Rudmin",

src/recorder.js

+25-18
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ var Recorder = function( config ){
1818
this.monitorNode = this.audioContext.createGain();
1919
this.config = Object.assign({
2020
bufferLength: 4096,
21-
command: "init",
2221
encoderApplication: 2049,
2322
encoderFrameSize: 20,
2423
encoderPath: 'encoderWorker.min.js',
@@ -35,6 +34,7 @@ var Recorder = function( config ){
3534
wavSampleRate: this.audioContext.sampleRate
3635
}, config );
3736

37+
this.initWorker();
3838
this.setMonitorGain( this.config.monitorGain );
3939
this.scriptProcessorNode = this.audioContext.createScriptProcessor( this.config.bufferLength, this.config.numberOfChannels, this.config.numberOfChannels );
4040
this.scriptProcessorNode.onaudioprocess = function( e ){
@@ -108,6 +108,25 @@ Recorder.prototype.initStream = function(){
108108
return getUserMedia(constraints).then( onStreamInit, onStreamError );
109109
};
110110

111+
Recorder.prototype.initWorker = function(){
112+
var that = this;
113+
this.encoder = new global.Worker( this.config.encoderPath );
114+
115+
if (this.config.streamPages){
116+
this.encoder.addEventListener( "message", function( e ) {
117+
that.streamPage( e.data );
118+
});
119+
}
120+
121+
else {
122+
this.recordedPages = [];
123+
this.totalLength = 0;
124+
this.encoder.addEventListener( "message", function( e ) {
125+
that.storePage( e.data );
126+
});
127+
}
128+
};
129+
111130
Recorder.prototype.pause = function(){
112131
if ( this.state === "recording" ){
113132
this.state = "paused";
@@ -132,22 +151,10 @@ Recorder.prototype.setMonitorGain = function( gain ){
132151

133152
Recorder.prototype.start = function(){
134153
if ( this.state === "inactive" && this.stream ) {
135-
var that = this;
136-
this.encoder = new global.Worker( this.config.encoderPath );
137154

138-
if (this.config.streamPages){
139-
this.encoder.addEventListener( "message", function( e ) {
140-
that.streamPage( e.data );
141-
});
142-
}
143-
144-
else {
145-
this.recordedPages = [];
146-
this.totalLength = 0;
147-
this.encoder.addEventListener( "message", function( e ) {
148-
that.storePage( e.data );
149-
});
150-
}
155+
this.encoder.postMessage(Object.assign({
156+
command: 'init'
157+
}, this.config));
151158

152159
// First buffer can contain old data. Don't encode it.
153160
this.encodeBuffers = function(){
@@ -158,7 +165,6 @@ Recorder.prototype.start = function(){
158165
this.monitorNode.connect( this.audioContext.destination );
159166
this.scriptProcessorNode.connect( this.audioContext.destination );
160167
this.eventTarget.dispatchEvent( new global.Event( 'start' ) );
161-
this.encoder.postMessage( this.config );
162168
}
163169
};
164170

@@ -190,7 +196,7 @@ Recorder.prototype.storePage = function( page ) {
190196
detail: outputData
191197
}));
192198

193-
this.recordedPages = [];
199+
this.initWorker();
194200
this.eventTarget.dispatchEvent( new global.Event( 'stop' ) );
195201
}
196202

@@ -202,6 +208,7 @@ Recorder.prototype.storePage = function( page ) {
202208

203209
Recorder.prototype.streamPage = function( page ) {
204210
if ( page === null ) {
211+
this.initWorker();
205212
this.eventTarget.dispatchEvent( new global.Event( 'stop' ) );
206213
}
207214

test/recorder.js

+22-4
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,10 @@ describe('Recorder', function(){
272272
var rec = new Recorder();
273273
return rec.initStream().then(function(){
274274
rec.start();
275-
expect(global.Worker).to.have.been.calledWithNew;
276-
expect(rec.encoder.addEventListener).to.have.been.calledOnce;
277-
expect(rec.encoder.addEventListener).to.have.been.calledWith('message');
278275
expect(rec.state).to.equal('recording');
279276
expect(rec.scriptProcessorNode.connect).to.have.been.calledWith( rec.audioContext.destination );
280277
expect(global.Event).to.have.been.calledWith('start');
281-
expect(rec.encoder.postMessage).to.have.been.calledWith( rec.config );
278+
expect(rec.encoder.postMessage).to.have.been.calledWithMatch({ command: 'init' });
282279
});
283280
});
284281

@@ -299,4 +296,25 @@ describe('Recorder', function(){
299296
expect(rec.monitorNode.gain.setTargetAtTime).to.have.been.calledOnce;
300297
});
301298
});
299+
300+
it('should init worker', function () {
301+
var rec = new Recorder();
302+
expect(global.Worker).to.have.been.calledWithNew;
303+
expect(rec.encoder.addEventListener).to.have.been.calledOnce;
304+
expect(rec.encoder.addEventListener).to.have.been.calledWith('message');
305+
});
306+
307+
it('should re-init worker when storePage completes', function () {
308+
var rec = new Recorder();
309+
expect(global.Worker).to.have.been.calledOnce;
310+
rec.storePage(null);
311+
expect(global.Worker).to.have.been.calledTwice;
312+
});
313+
314+
it('should re-init worker when streamPage completes', function () {
315+
var rec = new Recorder();
316+
expect(global.Worker).to.have.been.calledOnce;
317+
rec.streamPage(null);
318+
expect(global.Worker).to.have.been.calledTwice;
319+
});
302320
});

0 commit comments

Comments
 (0)