LinguaRecorder is a fast cross-browser voice recording JS library.
- Easily record your users directly from their browser (no software/plugin/whatsoever needed)
- Both desktop and mobile friendly
- Fully configurable
- Intelligent cutting to avoid blanks at the start/end of a record
- Saturation control to cancel/discard bad records
- Whole bunch of events allowing you to asynchronously manage your user's actions
- Wide possibilities for exporting your records, including:
- Play in browser
- Direct download
- WAV-encoded Blob (to send to an API for example)
- URL object
- ...
Tested in the following browsers/versions:
| master branch | legacy branch | |
|---|---|---|
| Firefox | 76+ | 25+ |
| Chrome | 66+ | 22+ |
| Firefox for android | 79+ | 57+ * |
| Chrome for android | 66+ | 63+ * |
| Microsoft Edge | 79+ | 12+ |
| Safari | 14.1+ | 11+ |
| Opera | 53+ | 18+ |
It may work on older versions of the browsers marked with *, but it has not been tested.
The master branch uses internally the new AudioWorklet API, whereas the legacy branch uses the old and now deprecated BaseAudioContext:createScriptProcessor method.
The LinguaRecorder sandbox allows you to get familiar with (hardly) all features of the library, and to play with it's configuration possibilities.
The demo folder contain several other implementation examples.
- Clone github repository:
git clone https://github.com/lingua-libre/LinguaRecorder.git - Install with npm:
npm install lingua-recorder - Use a CDN: todo
Then include the three files stored in the src folder in your HTML page:
<script src="/path/to/RecordingProcessor.js"></script>
<script src="/path/to/AudioRecord.js"></script>
<script src="/path/to/LinguaRecorder.js"></script>
<script>
var recorder = new LinguaRecorder();
...
</script>Imported this way, the LinguaRecorder and AudioRecord classes will be present in the document's script namespace.
There will also be a window.LinguaRecorder object created, with properties .AudioRecord, .LinguaRecorder, .recordingProcessorEncapsulation.
If you use a bundler like webpack, none of this will be present in the namespace because of the encapsulation, so you need to handle the imported functions/classes yourself.
{ LinguaRecorder } = require('lingua-recorder')
var recorder = new LinguaRecorder();
...to do
Set to true to wait for voice detection before effectively starting the record when calling the start() method.
Set to true to stop the record when there is a silence.
Maximum time (in seconds) after which it is necessary to stop recording. Set to 0 (default) for no time limit.
Tell what to do when a record is saturated. Accepted values are 'none' (default), 'cancel' and 'discard'.
Amplitude value between 0 and 1 included. Only used if onSaturate is different from 'none'. Threshold above which a record should be flagged as saturated.
Amplitude value between 0 and 1 included. Only used if autoStart is set to true. Amplitude to reach to auto-start the recording.
Amplitude value between 0 and 1 included. Only used if autoStop is set to true. Amplitude not to exceed in a stopDuration interval to auto-stop recording.
Duration value in seconds. Only used if autoStop is set to true. Duration during which not to exceed the stopThreshold in order to auto-stop recording.
Duration value in seconds. Only used if autoStart is set to true.
Duration value in seconds. Only used if autoStop is set to true.
Duration value in seconds. Discard the record if it last less than minDuration. Default value to 0.15, use 0 to disable.
Creates a new LinguaRecorder instance.
- config:
ObjectoptionalConfiguration options as described above. - ⇒
this
Start to record.
If autoStart is set to true, enter in listening state and postpone the start of the recording when a voice will be detected.
- ⇒
this
Switch the record to the pause state.
While in pause, all the inputs coming from the microphone will be ignored. To resume to the recording state, just call the start() method again. It is also still possible to stop() or cancel() a record, and you have to do so upstream if you wish to start a new one.
- ⇒
this
Stop the recording process and fire the record to the user.
Depending of the configuration, this method could discard the record if it fails some quality controls (duration and saturation).
To start a new record afterwards, just call the start() method again.
- cancelRecord:
booleanoptionalIf set to true, cancel and discard the record in any cases. - ⇒
this
Stop a recording, but without saving the record. This is an alias for stop( true ).
- ⇒
this
Toggle between the recording and stopped state.
- ⇒
this
Attach a handler function to a given event.
- event:
stringName of an event. See the dedicated section for a list of all the events available. - handler:
functionA function to execute when the event is triggered. - ⇒
this
Remove all the handler function from an event.
- event:
stringName of an event. See the dedicated section for a list of all the events available. - ⇒
this
Add an extra AudioNode
This can be used to draw a live visualization of the sound, or to perform some live editing tasks on the stream before it is recorded. See https://developer.mozilla.org/fr/docs/Web/API/AudioNode
Note that it can produce a little interrupt in the record if you are in listening or recording state.
- node:
AudioNodeNode to connect inside the recording context. - ⇒
this
Remove an extra AudioNode previously added with connectAudioNode.
Note that it can produce a little interrupt in the record if you are in listening or recording state.
- node:
AudioNodeNode to disconnect from the recording context. - ⇒
this
Return the current duration of the record.
- ⇒
numberThe duration in seconds
Return the current state of the recorder.
- ⇒
stringOne of the following: 'stop', 'listening', 'recording', 'paused'
Return the audioContext initialized and used by the recorder.
see https://developer.mozilla.org/fr/docs/Web/API/AudioContext
- ⇒
AudioContextThe AudioContext object used by the recorder.
Cleanly stop the threaded execution of the audio recorder in preparation for the destruction of the current LinguaRecorder instance. This method has to be called, otherwise memory leak will happened.
- ⇒
this
- ready:
MediaStreamThe user has allowed your script to use the microphone, the recorder is ready to start a record. - readyFail:
DOMExceptionSomething got wrong during the initialization; maybe the user has no microphone, or he has not allowed you to use it. For the full exceptions list, see https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia#Exceptions. - started: A record has just started; or to say it differently the recorder switched to the record state.
- recording:
Float32ArrayFired when in the record state, each time it has N new samples available (N =bufferSize); the given parameter is an array containing those new samples. - listening:
Float32ArraySame as the recording event, but when the recorder is in the listen state. - saturated: Fired each time the record get saturated.
- paused: The record has just being paused; or to say it differently the recorder switched to the pause state.
- stopped:
AudioRecordThe record has just being stopped; or to say it differently the recorder switched to the stop state. It includes a reference to an AudioRecord, containing the stopped record. - canceled:
stringThe record has just being canceled, thestringcontains the reason of the cancellation, one of the following: 'asked', 'saturated' (whenonSaturateis set to 'cancel'), 'tooShort' (whenminDurationis not reached).
- stop:
defaultNot recording yet, what are you waiting? - listen: You've hit start, but you've not spoken yet, it's time to do so! (only when
autoStartis true) - record: Currently recording. That's amazing, isn't it?
- pause: It was recording, but a dog just walked in so you paused the record the time to kick it away, but you wish to finish it later.
Creates a new AudioRecord instance.
- samples:
Float32ArrayThe raw samples that will make up the record. - sampleRate:
NumberRate at which the samples added to this object should be played. - ⇒
this
Change the declared sample rate.
- value:
Numbernew sample rate to set.
Get the sample rate in use.
- ⇒
NumberSample rate of the record.
Get the total number of samples in the record.
- ⇒
NumberNumber of samples.
Get the duration of the record. This is based on the number of samples and the declared sample rate.
- ⇒
NumberDuration (in seconds) of the record.
Get all the raw samples that make up the record.
- ⇒
Float32ArrayList of all samples.
Trim the record, starting with the beginning of the record (the left side).
- duration:
Numberduration (in seconds) to trim.
Trim the record, starting with the end of the record (the right side).
- duration:
Numberduration (in seconds) to trim.
Clear the record.
Play the record to the audio output (aka the user's loudspeaker).
Get a WAV-encoded Blob version of the record.
- ⇒
BlobWAV-encoded audio record.
Alias of getBlob()
Generate an object URL representing the WAV-encoded record. For performance reasons, you should unload the objectURL once you're done with it, see https://developer.mozilla.org/en-US/docs/Web/API/URL/revokeObjectURL
- ⇒
DOMStringURL representing the record.
Start the download process of the record as if it where a normal file.
- fileName:
Stringoptionalname of the file that will be downloaded, default to 'record.wav'.
Generate an HTML5 <audio> element containing the WAV-encoded record.
- ⇒
HTMLElementaudio element containing the record.
As the BaseAudioContext:createScriptProcessor is now deprecated, it became important to migrate to the new AudioWorklet API. This change needed a deep rewrite of the library, but we wanted to keep the API as unchanged as possible. However, there are a few minor breaking changes to note:
- A new file has to be included: src/RecordingProcessor.js
- on LinguaRecorder:
- The methods
start,pause,stop,cancelandtogglenow return the current LinguaRecorder instance (to made it chainable) instead of a boolean. - The
bufferSizeconfiguration option has been deleted.
- The methods
- The AudioRecord class now only accepts data when it is created. For this reason:
- The
constructorprototype has changed fromAudioRecord(sampleRate)toAudioRecord(samples, sampleRate). - The
pushmethod was removed.
- The
Note also that using AudioWorklet breaks the compatibility with old browsers.
The type declarations for this library are provided in index.d.ts. You can copy this into your codebase to provide your project with the necessary interfaces.
The LinguaRecorder was originally a part of LinguaLibre, developed by Nicolas Vion (@zmoostik), but has then been split out and completely rewritten by Antoine Lamielle (@0x010C).
Released under the MIT License.