Skip to content

Commit dac6f43

Browse files
authored
Merge pull request #130 from chris-rudmin/mediaRecorder
Recorder refactoring
2 parents 36bcbca + d56b7a2 commit dac6f43

23 files changed

+682
-765
lines changed

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
INPUT_DIR=./src
22
OUTPUT_DIR=./dist
3-
OUTPUT_DIR_UNMINIFIED=./dist/unminified
4-
EMCC_OPTS=-O3 --llvm-lto 1 --memory-init-file 0 -s NO_DYNAMIC_EXECUTION=1 -s NO_FILESYSTEM=1 -s WASM=1
3+
OUTPUT_DIR_UNMINIFIED=./dist-unminified
4+
EMCC_OPTS=-O3 --llvm-lto 1 -s NO_DYNAMIC_EXECUTION=1 -s NO_FILESYSTEM=1 -s WASM=1
55
DEFAULT_EXPORTS:='_malloc','_free'
66

77
LIBOPUS_ENCODER_SRC=$(INPUT_DIR)/encoderWorker.js
@@ -31,7 +31,7 @@ WAVE_WORKER_SRC=$(INPUT_DIR)/waveWorker.js
3131
default: $(LIBOPUS_ENCODER) $(LIBOPUS_ENCODER_MIN) $(LIBOPUS_DECODER) $(LIBOPUS_DECODER_MIN) $(RECORDER) $(RECORDER_MIN) $(WAVE_WORKER) $(WAVE_WORKER_MIN) test
3232

3333
clean:
34-
rm -rf $(OUTPUT_DIR) $(LIBOPUS_DIR) $(LIBSPEEXDSP_DIR)
34+
rm -rf $(OUTPUT_DIR) $(OUTPUT_DIR_UNMINIFIED) $(LIBOPUS_DIR) $(LIBSPEEXDSP_DIR)
3535
mkdir $(OUTPUT_DIR)
3636
mkdir $(OUTPUT_DIR_UNMINIFIED)
3737

README.md

+59-33
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
# Opus & Wave Recorder
22

3-
A javascript library to encode the output of Web Audio API nodes in Ogg Opus or WAV format using WebAssembly. Audio encoded and decoded using libopus v1.2.1. Audio resampling is performed by speexDSP 1.2RC3.
4-
Encoded and muxed audio will be returned as typedArray in `dataAvailable` event.
3+
A javascript library to encode the output of Web Audio API nodes in Ogg Opus or WAV format using WebAssembly.
54

6-
For legacy asm.js, please use version 1.2.0
75

8-
### Usage
6+
#### Libraries Used
7+
8+
- Libopus: v1.2.1 compiled with emscripten 1.37.28
9+
- speexDSP: 1.2RC3 compiled with emscripten 1.37.28
910

11+
#### Required Files
12+
13+
The required files are in the `dist` folder. Unminified sources are in `dist-unminified`.
14+
Examples for recording, encoding, and decoding are in `examples` folder.
1015

1116
---------
17+
### Usage
18+
19+
1220
#### Constructor
1321

1422
The `Recorder` object is available in the global namespace and supports CommonJS and AMD imports.
1523

1624
```js
1725
var rec = new Recorder([config]);
1826
```
27+
1928
Creates a recorder instance.
2029

21-
- **config** - An optional configuration object (see **config** section below)
30+
- **config** - An optional configuration object.
2231

2332

2433
---------
@@ -41,7 +50,6 @@ Creates a recorder instance.
4150
- **streamPages** - (*optional*) `dataAvailable` event will fire after each encoded page. Defaults to `false`.
4251

4352

44-
---------
4553
#### Config Options for WAV recorder
4654

4755
- **bufferLength** - (*optional*) The length of the buffer that the internal JavaScriptNode uses to capture the audio. Can be tweaked if experiencing performance issues. Defaults to `4096`.
@@ -56,31 +64,12 @@ Creates a recorder instance.
5664
---------
5765
#### Instance Methods
5866

59-
```js
60-
rec.addEventListener( type, listener[, useCapture] )
61-
```
62-
63-
**addEventListener** will add an event listener to the event target. Available events are `streamError`, `streamReady`, `dataAvailable`, `start`, `pause`, `resume` and `stop`.
64-
65-
```js
66-
rec.initStream()
67-
```
68-
69-
**initStream** will request the user for permission to access the the audio stream and raise `streamReady` or `streamError`.
70-
Returns a Promise which resolves the audio stream when it is ready.
71-
7267
```js
7368
rec.pause()
7469
```
7570

7671
**pause** will keep the stream and monitoring alive, but will not be recording the buffers. Will raise the pause event. Subsequent calls to **resume** will add to the current recording.
7772

78-
```js
79-
rec.removeEventListener( type, listener[, useCapture] )
80-
```
81-
82-
**removeEventListener** will remove an event listener from the event target.
83-
8473
```js
8574
rec.resume()
8675
```
@@ -94,10 +83,10 @@ rec.setMonitorGain( gain )
9483
**setMonitorGain** will set the volume on what will be passed to the monitor. Monitor level does not affect the recording volume. Gain is an a-weighted value between `0` and `1`.
9584

9685
```js
97-
rec.start()
86+
rec.start( [sourceNode] )
9887
```
9988

100-
**start** will initalize the worker and begin capturing audio if the audio stream is ready. Will raise the `start` event when started.
89+
**start** Initalizes the worker, audio context, and an audio stream and begin capturing audio. Returns a promise which resolves when recording is started. Will callback `onstart` when started. Optionally accepts a source node which can be used in place of initializing the microphone stream. For iOS support, `start` needs to be initiated from a user action.
10190

10291
```js
10392
rec.stop()
@@ -109,7 +98,7 @@ rec.stop()
10998
rec.clearStream()
11099
```
111100

112-
**clearStream** will stop and delete the stream got from `initStream`, you will only ever call this manually if you have `config.leaveStreamOpen` set to `true`.
101+
**clearStream** will stop and delete the stream as well as close the audio context. You will only ever call this manually if you have `config.leaveStreamOpen` set to `true`.
113102

114103

115104
---------
@@ -122,6 +111,47 @@ Recorder.isRecordingSupported()
122111
Returns a truthy value indicating if the browser supports recording.
123112

124113

114+
---------
115+
#### Callback Handlers
116+
117+
```js
118+
rec.ondataavailable( arrayBuffer )
119+
```
120+
A callback which returns an array buffer of audio data. If `streamPages` is `true`, this will be called with each page of encoded audio. If `streamPages` is `false`, this will be called when the recording is finished with the complete data.
121+
122+
123+
```js
124+
rec.onpause()
125+
```
126+
127+
A callback which occurs when media recording is paused.
128+
129+
```js
130+
rec.onresume()
131+
```
132+
133+
A callback which occurs when media recording resumes after being paused.
134+
135+
136+
```js
137+
rec.onstart()
138+
```
139+
140+
A callback which occurs when media recording starts.
141+
142+
```js
143+
rec.onstop()
144+
```
145+
146+
A callback which occurs when media recording ends.
147+
148+
```js
149+
rec.onstreamerror( error )
150+
```
151+
152+
A callback which occurs when a stream error occurs.
153+
154+
125155
---------
126156
### Browser Support
127157

@@ -146,6 +176,7 @@ Unsupported:
146176
- macOS Safari v11 native opus playback not yet supported
147177
- iOS Safari v11 native opus playback not yet supported
148178
- Microsoft Edge native opus playback not yet supported
179+
- iOS Safari requires `rec.start()` to be called from a user initiated event
149180

150181

151182
---------
@@ -187,8 +218,3 @@ Clean the dist folder and git submodules:
187218
make clean
188219
```
189220

190-
191-
---------
192-
### Required Files
193-
194-
The required files to record audio to ogg/opus are `dist/recorder.min.js` and `dist/encoderWorker.min.js`. Optionally `dist/decoderWorker.min.js` will help decode ogg/opus files and `dist/waveWorker.min.js` is a helper to transform floating point PCM data into wave/pcm. The source files `src/encoderWorker.js` and `src/decoderWorker.js` do not work without building process; it will produce an error `ReferenceError: _malloc is not defined`. You need to either use compiled file in `dist/` folder or build by yourself.
File renamed without changes.

dist/unminified/encoderWorker.js renamed to dist-unminified/encoderWorker.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -302,10 +302,10 @@ OggOpusEncoder.prototype.initChecksumTable = function(){
302302
};
303303

304304
OggOpusEncoder.prototype.setOpusControl = function( control, value ){
305-
var location = this._malloc( 4 );
306-
this.HEAP32[ location >> 2 ] = value;
307-
this._opus_encoder_ctl( this.encoder, control, location );
308-
this._free( location );
305+
var location = this._malloc( 4 );
306+
this.HEAP32[ location >> 2 ] = value;
307+
this._opus_encoder_ctl( this.encoder, control, location );
308+
this._free( location );
309309
};
310310

311311
OggOpusEncoder.prototype.initCodec = function() {

0 commit comments

Comments
 (0)