Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
simple-audio-recorder
Advanced tools
Web audio recording library with encoding to mp3 and chunked output
A simple web audio recording library with encoding to MP3 (using lamejs) and optional streaming/chunked output. Made by Vocaroo, the quick and easy online voice recorder!
Now including both a vanilla-js version and an easy to use react hook and component!
import AudioRecorder from "simple-audio-recorder";
AudioRecorder.preload("mp3worker.js");
let recorder = new AudioRecorder();
recorder.start().then(() => {
console.log("Recording started...");
}).catch(error => {
console.log(error);
});
recorder.stop().then(mp3Blob => {
console.log("Recording stopped...");
const newAudio = document.createElement("audio");
newAudio.src = URL.createObjectURL(mp3Blob);
newAudio.controls = true;
document.body.append(newAudio);
}).catch(error => {
console.log(error);
});
import {SimpleAudioRecorder, useSimpleAudioRecorder} from "simple-audio-recorder/react";
export default function App() {
const recorder = useSimpleAudioRecorder({workerUrl : "mp3worker.js"});
const viewInitial = <button onClick={recorder.start}>start recording</button>;
const viewRecording = <button onClick={recorder.stop}>stop recording</button>;
const viewError = (<>{viewInitial} <div>Error occurred! {recorder.errorStr}</div></>);
return (
<div>
<SimpleAudioRecorder
{...recorder.getProps()}
viewInitial={viewInitial}
viewRecording={viewRecording}
viewError={viewError}/>
{recorder.mp3Urls.map(url =>
<audio key={url} src={url} controls/>
)}
</div>
);
}
To run the built in examples in the ./examples/ directory, start a dev server from the project root and then navigate to them.
Or start developing with:
yarn install
yarn start
...or whatever the npm equivalant is.
yarn add simple-audio-recorder
import AudioRecorder from "simple-audio-recorder";
Alternatively, just use a script tag:
<script type="text/javascript" src="audiorecorder.js"></script>
Also, you must make sure that you distribute the web worker file "mp3worker.js" along with your application.
// This is a static method.
// You should preload the worker immediately on page load to enable recording to start quickly
AudioRecorder.preload("./mp3worker.js");
let recorder = new AudioRecorder({
recordingGain : 1, // Initial recording volume
encoderBitRate : 96, // MP3 encoding bit rate
streaming : false, // Data will be returned in chunks (ondataavailable callback) as it is encoded,
// rather than at the end as one large blob
streamBufferSize : 50000, // Size of encoded mp3 data chunks returned by ondataavailable, if streaming is enabled
constraints : { // Optional audio constraints, see https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
channelCount : 1, // Set to 2 to hint for stereo if it's available, or leave as 1 to force mono at all times
autoGainControl : true,
echoCancellation : true,
noiseSuppression : true
},
// Used for debugging only. Force using the older script processor instead of AudioWorklet.
// forceScriptProcessor : true
});
recorder.start().then(() => {
console.log("Recording started...");
}).catch(error => {
console.log(error);
});
recorder.stop().then(mp3Blob => {
// Do something with the mp3 Blob!
}).catch(error => {
console.log(error);
});
recorder.onstart = () => {
console.log("Recording started...");
};
recorder.onstop = (mp3Blob) => {
// Do something with the mp3 Blob!
// When using onstop, mp3Blob could in rare cases be null if nothing was recorded
// (with the Promise API, that would be a stop() promise rejection)
};
recorder.onerror = (error) => {
console.log(error);
};
// if onerror is set, start and stop won't return a promise
recorder.start();
// later...
recorder.stop();
Want to receive encoded data chunks as they are produced? Useful for streaming uploads to a remote server.
let recorder = new AudioRecorder({
streaming : true,
streamBufferSize : 50000
});
let audioChunks = [];
recorder.ondataavailable = (data) => {
// 50 KB of MP3 data received!
audioChunks.push(data);
};
recorder.start();
// No mp3Blob will be received either with the promise API or via recorder.onstop if streaming is enabled.
recorder.stop().then(() => {
// ...do something with all the chunks that were received by ondataavailable
let mp3Blob = new Blob(audioChunks, {type : "audio/mpeg"});
});
recorder.start(paused = false); // Supports starting in paused mode
recorder.pause();
recorder.resume();
recorder.setRecordingGain(gain); // Change the volume while recording is in progress (0.0 to 1.0)
recorder.time; // Access the current recorded duration in milliseconds. Time pauses when recording is paused.
// Get the amount of data remaining to be encoded
// Will only be much above zero on very slow systems as mp3 encoding is quite fast.
// A large value indicates there might be a delay between calling stop() and getting the mp3Blob
recorder.getEncodingQueueSize();
AudioRecorder.isRecordingSupported(); // Static method. Does this browser support getUserMedia?
Error handling can be done either via promises and catching errors, or via the onerror event handler if it is set.
These are named via the error.name property
Please see the react hook and component example for a working example of usage.
import {
useSimpleAudioRecorder,
SimpleAudioRecorder,
preloadWorker,
RecorderStates
} from "simple-audio-recorder/react"
const {
error, // Any current error object, or null
errorStr, // Error object as string, or null
time, // Current recorded time in milliseconds
countdownTimeLeft, // Time left of the countdown before recording will start, if one was set
mp3Blobs, // List of all recordings as a blob
mp3Urls, // List of all recordings as URLs (created with URL.createObjectURL)
mp3Blob, // Single most recent recording blob
mp3Url, // Single most recent recording URL
start, stop, pause, resume, // Recording functions
recorderState, // Current state of recorder (see RecorderStates)
getProps // Function to get the props that can be passed to the SimpleAudioRecorder react component
} = useSimpleAudioRecorder({
workerUrl, onDataAvailable, onComplete, onError, options, cleanup = false, timeUpdateStep = 111, countdown = 0
})
{mp3Blob, mp3Url}
when recording and encoding is finished.This is a very simple state machine component that shows a different view component depending on the current recorder state.
SimpleAudioRecorder({
// As returned by useSimpleAudioRecorder
recorderState,
// The components to display in each of the states.
// Only viewInitial and viewRecording are absolutely required.
viewInitial, viewStarting, viewCountdown, viewRecording, viewPaused, viewEncoding, viewComplete, viewError
})
start
function from useSimpleAudioRecorder.stop
and pause
functions.Instead of passing a workerUrl to useSimpleAudioRecorder
, it's better to call this function somewhere at the start of your app to preload the worker as soon as possible.
An enumeration of possible recorder states. Used by the SimpleAudioRecorder component.
RecorderStates = {
INITIAL,
STARTING,
RECORDING,
PAUSED,
ENCODING,
COMPLETE,
ERROR,
COUNTDOWN
}
Simple Audio Recorder uses an AudioWorkletNode to extract the audio data, where supported, and falls back to using the deprecated ScriptProcessorNode on older browsers. However, there seem to be some occasional issues using AudioWorkletNode on iOS/Safari. After about 45 seconds, audio packets from the microphone start to get dropped, creating a recording that is shorter than expected with stuttering and glitches. So currently, the deprecated ScriptProcessorNode will always be used on iOS/Safari.
AFAIK this is an unsolved issue, perhaps related to Safari's implementation of AudioWorklets and them not being given enough CPU priority. These issues only appear on some devices. Curiously, similar glitches have also been experienced when using the old ScriptProcessorNode on Chrome on other platforms.
Chrome isn't any better on iOS either as they are forced to use Safari under the hood (somehow, this feels rather familiar).
SimpleAudioRecorder is mostly MIT licenced, but the worker is probably LGPL as it uses lamejs.
FAQs
Web audio recording library with encoding to mp3 and chunked output
The npm package simple-audio-recorder receives a total of 553 weekly downloads. As such, simple-audio-recorder popularity was classified as not popular.
We found that simple-audio-recorder demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.