@@ -18,7 +18,6 @@ var Recorder = function( config ){
18
18
this . monitorNode = this . audioContext . createGain ( ) ;
19
19
this . config = Object . assign ( {
20
20
bufferLength : 4096 ,
21
- command : "init" ,
22
21
encoderApplication : 2049 ,
23
22
encoderFrameSize : 20 ,
24
23
encoderPath : 'encoderWorker.min.js' ,
@@ -35,6 +34,7 @@ var Recorder = function( config ){
35
34
wavSampleRate : this . audioContext . sampleRate
36
35
} , config ) ;
37
36
37
+ this . initWorker ( ) ;
38
38
this . setMonitorGain ( this . config . monitorGain ) ;
39
39
this . scriptProcessorNode = this . audioContext . createScriptProcessor ( this . config . bufferLength , this . config . numberOfChannels , this . config . numberOfChannels ) ;
40
40
this . scriptProcessorNode . onaudioprocess = function ( e ) {
@@ -108,6 +108,25 @@ Recorder.prototype.initStream = function(){
108
108
return getUserMedia ( constraints ) . then ( onStreamInit , onStreamError ) ;
109
109
} ;
110
110
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
+
111
130
Recorder . prototype . pause = function ( ) {
112
131
if ( this . state === "recording" ) {
113
132
this . state = "paused" ;
@@ -132,22 +151,10 @@ Recorder.prototype.setMonitorGain = function( gain ){
132
151
133
152
Recorder . prototype . start = function ( ) {
134
153
if ( this . state === "inactive" && this . stream ) {
135
- var that = this ;
136
- this . encoder = new global . Worker ( this . config . encoderPath ) ;
137
154
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 ) ) ;
151
158
152
159
// First buffer can contain old data. Don't encode it.
153
160
this . encodeBuffers = function ( ) {
@@ -158,7 +165,6 @@ Recorder.prototype.start = function(){
158
165
this . monitorNode . connect ( this . audioContext . destination ) ;
159
166
this . scriptProcessorNode . connect ( this . audioContext . destination ) ;
160
167
this . eventTarget . dispatchEvent ( new global . Event ( 'start' ) ) ;
161
- this . encoder . postMessage ( this . config ) ;
162
168
}
163
169
} ;
164
170
@@ -190,7 +196,7 @@ Recorder.prototype.storePage = function( page ) {
190
196
detail : outputData
191
197
} ) ) ;
192
198
193
- this . recordedPages = [ ] ;
199
+ this . initWorker ( ) ;
194
200
this . eventTarget . dispatchEvent ( new global . Event ( 'stop' ) ) ;
195
201
}
196
202
@@ -202,6 +208,7 @@ Recorder.prototype.storePage = function( page ) {
202
208
203
209
Recorder . prototype . streamPage = function ( page ) {
204
210
if ( page === null ) {
211
+ this . initWorker ( ) ;
205
212
this . eventTarget . dispatchEvent ( new global . Event ( 'stop' ) ) ;
206
213
}
207
214
0 commit comments