Skip to content

Commit 3f9ac8c

Browse files
authored
Merge pull request #212 from chris-rudmin/audioWorklets
Add Audio Worklet Support
2 parents 497402e + 0a1af3d commit 3f9ac8c

20 files changed

+1671
-1222
lines changed

Makefile

+9-8
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ WAVE_WORKER_SRC=$(INPUT_DIR)/waveWorker.js
3030

3131
default: $(LIBOPUS_ENCODER) $(LIBOPUS_ENCODER_MIN) $(LIBOPUS_DECODER) $(LIBOPUS_DECODER_MIN) $(RECORDER) $(RECORDER_MIN) $(WAVE_WORKER) $(WAVE_WORKER_MIN) test
3232

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

38+
cleanAll: cleanDist
39+
rm -rf $(LIBOPUS_DIR) $(LIBSPEEXDSP_DIR)
40+
3841
test:
3942
# Tests need to run relative to `dist` folder for wasm file import
4043
cd $(OUTPUT_DIR); node --expose-wasm ../test.js
@@ -55,12 +58,10 @@ $(LIBSPEEXDSP_OBJ): $(LIBSPEEXDSP_DIR)/autogen.sh
5558
cd $(LIBSPEEXDSP_DIR); emmake make
5659

5760
$(LIBOPUS_ENCODER): $(LIBOPUS_ENCODER_SRC) $(LIBOPUS_OBJ) $(LIBSPEEXDSP_OBJ)
58-
npm run webpack -- --config webpack.config.js -d --output-library EncoderWorker $(LIBOPUS_ENCODER_SRC) -o $@
59-
emcc -o $@ $(EMCC_OPTS) -g3 -s EXPORTED_FUNCTIONS="[$(DEFAULT_EXPORTS),$(LIBOPUS_ENCODER_EXPORTS),$(LIBSPEEXDSP_EXPORTS)]" --pre-js $@ $(LIBOPUS_OBJ) $(LIBSPEEXDSP_OBJ)
61+
emcc -o $@ $(EMCC_OPTS) -s BINARYEN_ASYNC_COMPILATION=0 -s SINGLE_FILE=1 -g3 -s EXPORTED_FUNCTIONS="[$(DEFAULT_EXPORTS),$(LIBOPUS_ENCODER_EXPORTS),$(LIBSPEEXDSP_EXPORTS)]" --post-js $(LIBOPUS_ENCODER_SRC) $(LIBOPUS_OBJ) $(LIBSPEEXDSP_OBJ)
6062

6163
$(LIBOPUS_ENCODER_MIN): $(LIBOPUS_ENCODER_SRC) $(LIBOPUS_OBJ) $(LIBSPEEXDSP_OBJ)
62-
npm run webpack -- --config webpack.config.js -p --output-library EncoderWorker $(LIBOPUS_ENCODER_SRC) -o $@
63-
emcc -o $@ $(EMCC_OPTS) -s EXPORTED_FUNCTIONS="[$(DEFAULT_EXPORTS),$(LIBOPUS_ENCODER_EXPORTS),$(LIBSPEEXDSP_EXPORTS)]" --pre-js $@ $(LIBOPUS_OBJ) $(LIBSPEEXDSP_OBJ)
64+
emcc -o $@ $(EMCC_OPTS) -s BINARYEN_ASYNC_COMPILATION=0 -s SINGLE_FILE=1 -s EXPORTED_FUNCTIONS="[$(DEFAULT_EXPORTS),$(LIBOPUS_ENCODER_EXPORTS),$(LIBSPEEXDSP_EXPORTS)]" --post-js $(LIBOPUS_ENCODER_SRC) $(LIBOPUS_OBJ) $(LIBSPEEXDSP_OBJ)
6465

6566
$(LIBOPUS_DECODER): $(LIBOPUS_DECODER_SRC) $(LIBOPUS_OBJ) $(LIBSPEEXDSP_OBJ)
6667
npm run webpack -- --config webpack.config.js -d --output-library DecoderWorker $(LIBOPUS_DECODER_SRC) -o $@
@@ -77,7 +78,7 @@ $(RECORDER_MIN): $(RECORDER_SRC)
7778
npm run webpack -- --config webpack.config.js -p --output-library Recorder $(RECORDER_SRC) -o $@
7879

7980
$(WAVE_WORKER): $(WAVE_WORKER_SRC)
80-
npm run webpack -- --config webpack.config.js -d --output-library WaveWorker $(WAVE_WORKER_SRC) -o $@
81+
npm run webpack -- -d $(WAVE_WORKER_SRC) -o $@
8182

8283
$(WAVE_WORKER_MIN): $(WAVE_WORKER_SRC)
83-
npm run webpack -- --config webpack.config.js -p --output-library WaveWorker $(WAVE_WORKER_SRC) -o $@
84+
npm run webpack -- -p $(WAVE_WORKER_SRC) -o $@

README.md

+39-19
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ Creates a recorder instance.
3333
---------
3434
#### General Config options
3535

36-
- **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`.
37-
- **encoderPath** - (*optional*) Path to `encoderWorker.min.js` or `waveWorker.min.js` worker script. Defaults to `encoderWorker.min.js`
36+
- **bufferLength** - (*optional*) The length of the buffer that the scriptProcessorNode uses to capture the audio. Defaults to `4096`.
37+
- **encoderPath** - (*optional*) Path to desired worker script. Defaults to `encoderWorker.min.js`
3838
- **mediaTrackConstraints** - (*optional*) Object to specify [media track constraints](https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackConstraints). Defaults to `true`.
3939
- **monitorGain** - (*optional*) Sets the gain of the monitoring output. Gain is an a-weighted value between `0` and `1`. Defaults to `0`
4040
- **numberOfChannels** - (*optional*) The number of channels to record. `1` = mono, `2` = stereo. Defaults to `1`. Maximum `2` channels are supported.
@@ -52,7 +52,6 @@ Creates a recorder instance.
5252
- **originalSampleRateOverride** - (*optional*) Override the ogg opus 'input sample rate' field. Google Speech API requires this field to be `16000`.
5353
- **resampleQuality** - (*optional*) Value between 0 and 10 which determines latency and processing for resampling. `0` is fastest with lowest quality. `10` is slowest with highest quality. Defaults to `3`.
5454
- **streamPages** - (*optional*) `dataAvailable` event will fire after each encoded page. Defaults to `false`.
55-
- **reuseWorker** - (*optional*) If true, the worker is not automatically destroyed when `stop` is called. Instead, it is reused for subsequent `start` calls and must be explicitly destroyed after stopping by calling `destroyWorker`. Defaults to `false`.
5655

5756

5857
#### Config options for WAV recorder
@@ -100,18 +99,6 @@ rec.stop()
10099

101100
**stop** will cease capturing audio and disable the monitoring and mic input stream. Will request the recorded data and then terminate the worker once the final data has been published. Will call the `onstop` callback when stopped.
102101

103-
```js
104-
rec.destroyWorker()
105-
```
106-
107-
**destroyWorker** will destroy the worker freeing up the browser resources. If the recorder is re-started, a new worker will be created. Note that `destroyWorker` is automatically called when stopping unless `reuseWorker` is true.
108-
109-
```js
110-
rec.loadWorker()
111-
```
112-
113-
**loadWorker** triggers pre-loading of the worker. This can reduce the startup latency when calling `start`. Call `destroyWorker` to clean the worker when the recorder is stopped/not started, or it will be automatically cleaned up after stopping unless `reuseWorker` is true.
114-
115102
---------
116103
#### Instance Fields
117104

@@ -131,6 +118,15 @@ Recorder.isRecordingSupported()
131118
Returns a truthy value indicating if the browser supports recording.
132119

133120

121+
#### Static Properties
122+
123+
```js
124+
Recorder.version
125+
```
126+
127+
The version of the library.
128+
129+
134130
---------
135131
#### Callback Handlers
136132

@@ -164,14 +160,38 @@ rec.onstop()
164160

165161
A callback which occurs when media recording ends.
166162

163+
---------
164+
### Getting started with webpack
165+
- To use in a web-app built with webpack, be sure to load the worker files as a chunk. This can be done with file-loader plugin.
166+
167+
Add to your webpack.config.js before all other loaders.
168+
```js
169+
module.exports = {
170+
module: {
171+
rules: [
172+
{
173+
test: /encoderWorker\.min\.js$/,
174+
use: [{ loader: 'file-loader' }]
175+
}
176+
]
177+
}
178+
};
179+
```
180+
181+
Then get the encoderPath using an import
182+
```js
183+
import Recorder from 'opus-recorder';
184+
import encoderPath from 'opus-recorder/dist/encoderWorker.min.js';
185+
186+
const rec = new Recorder({ encoderPath });
187+
```
188+
167189

168190
---------
169191
### Gotchas
170192
- To be able to read the mic stream, the page must be served over https
171-
- iOS Safari requires `rec.start()` to be called from a user initiated event
172-
- macOS Safari v11 native opus playback is not yet supported
173-
- iOS Safari v11 native opus playback is not yet supported
174-
- Microsoft Edge native opus playback is not yet supported
193+
- macOS and iOS Safari requires `rec.start()` to be called from a user initiated event. Otherwise the mic stream will be empty with no logged errors
194+
- macOS and iOS Safari native opus playback is not yet supported
175195

176196

177197
---------

dist-unminified/encoderWorker.js

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

dist-unminified/encoderWorker.wasm

-286 KB
Binary file not shown.

dist-unminified/recorder.js

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

0 commit comments

Comments
 (0)