Skip to content

Commit e53baf0

Browse files
authored
Add MediaTrackConstraints options for the audio track (#45)
1 parent edc8208 commit e53baf0

File tree

7 files changed

+59
-9
lines changed

7 files changed

+59
-9
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ const addAudioElement = (blob) => {
2929
ReactDOM.createRoot(document.getElementById("root")).render(
3030
<React.StrictMode>
3131
<AudioRecorder
32-
onRecordingComplete={addAudioElement}
32+
onRecordingComplete={addAudioElement}
33+
audioTrackConstraints={{
34+
noiseSuppression: true,
35+
echoCancellation: true,
36+
}}
3337
downloadOnSavePress={true}
3438
downloadFileExtension="mp3"
3539
/>
@@ -40,6 +44,7 @@ ReactDOM.createRoot(document.getElementById("root")).render(
4044
| Props | Description | Default | Optional |
4145
| :------------ |:--------------- |:--------------- | :--------------- |
4246
| **`onRecordingComplete`** | A method that gets called when "Save recording" option is pressed | N/A | Yes |
47+
| **`audioTrackConstraints`** | Takes a [subset](https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackSettings#instance_properties_of_audio_tracks) of `MediaTrackConstraints` that apply to the audio track | N/A | Yes
4348
| **`downloadOnSavePress`** | A `boolean` value that determines if the recording should be downloaded when "Save recording" option is pressed | `false` | Yes |
4449
| **`downloadFileExtension`** | The file extension to be used for the downloaded file. Allowed values are `mp3`, `wav` and `webm` | `mp3` | Yes |
4550
| **`classes`** | This allows class names to be passed to modify the styles for the entire component or specific portions of it | N/A | Yes |
@@ -48,6 +53,10 @@ ReactDOM.createRoot(document.getElementById("root")).render(
4853

4954
If you prefer to build up your own UI but take advantage of the implementation provided by this package, you can use this hook instead of the component
5055

56+
| Params | Description |
57+
| :------------ |:---------------|
58+
| **`audioTrackConstraints`** | Takes a [subset](https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackSettings#instance_properties_of_audio_tracks) of `MediaTrackConstraints` that apply to the audio track |
59+
5160
The hook returns the following:
5261

5362
| Identifiers | Description |

package-lock.json

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

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-audio-voice-recorder",
33
"private": false,
4-
"version": "1.1.3",
4+
"version": "1.1.4",
55
"license": "MIT",
66
"author": "",
77
"repository": {

src/components/AudioRecordingComponent.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ import "../styles/audio-recorder.css";
1616
* @prop `onRecordingComplete` Method that gets called when save recording option is clicked
1717
* @prop `recorderControls` Externally initilize hook and pass the returned object to this param, this gives your control over the component from outside the component.
1818
* https://github.com/samhirtarif/react-audio-recorder#combine-the-useaudiorecorder-hook-and-the-audiorecorder-component
19+
* @prop `audioTrackConstraints`: Takes a {@link https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackSettings#instance_properties_of_audio_tracks subset} of `MediaTrackConstraints` that apply to the audio track
1920
* @prop `downloadOnSavePress` If set to `true` the file gets downloaded when save recording is pressed. Defaults to `false`
2021
* @prop `downloadFileExtension` File extension for the audio filed that gets downloaded. Defaults to `mp3`. Allowed values are `mp3`, `wav` and `webm`
2122
* @prop `classes` Is an object with attributes representing classes for different parts of the component
2223
*/
2324
const AudioRecorder: (props: Props) => ReactElement = ({
2425
onRecordingComplete,
2526
recorderControls,
27+
audioTrackConstraints,
2628
downloadOnSavePress = false,
2729
downloadFileExtension = "mp3",
2830
classes,
@@ -36,7 +38,7 @@ const AudioRecorder: (props: Props) => ReactElement = ({
3638
isPaused,
3739
recordingTime,
3840
// eslint-disable-next-line react-hooks/rules-of-hooks
39-
} = recorderControls ?? useAudioRecorder();
41+
} = recorderControls ?? useAudioRecorder(audioTrackConstraints);
4042

4143
const [shouldSave, setShouldSave] = useState(false);
4244

src/components/interfaces.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { recorderControls } from "../hooks/useAudioRecorder";
1+
import {
2+
MediaAudioTrackConstraints,
3+
recorderControls,
4+
} from "../hooks/useAudioRecorder";
25

36
interface StyleProps {
47
/**
@@ -39,6 +42,20 @@ export interface Props {
3942
* @sample_usage https://github.com/samhirtarif/react-audio-recorder#combine-the-useaudiorecorder-hook-and-the-audiorecorder-component
4043
**/
4144
recorderControls?: recorderControls;
45+
/**
46+
* Takes a {@link https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackSettings#instance_properties_of_audio_tracks subset} of
47+
* `MediaTrackConstraints` that apply to the audio track
48+
*
49+
* @Property `deviceId`
50+
* @Property `groupId`
51+
* @Property `autoGainControl`
52+
* @Property `channelCount`
53+
* @Property `echoCancellation`
54+
* @Property `noiseSuppression`
55+
* @Property `sampleRate`
56+
* @Property `sampleSize`
57+
*/
58+
audioTrackConstraints?: MediaAudioTrackConstraints;
4259
/**
4360
* If set to `true` the file gets downloaded when save recording is pressed
4461
**/

src/hooks/useAudioRecorder.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,23 @@ export interface recorderControls {
1111
mediaRecorder?: MediaRecorder;
1212
}
1313

14+
export type MediaAudioTrackConstraints = Pick<
15+
MediaTrackConstraints,
16+
| "deviceId"
17+
| "groupId"
18+
| "autoGainControl"
19+
| "channelCount"
20+
| "echoCancellation"
21+
| "noiseSuppression"
22+
| "sampleRate"
23+
| "sampleSize"
24+
>;
25+
1426
/**
1527
* @returns Controls for the recording. Details of returned controls are given below
1628
*
29+
* @param `audioTrackConstraints`: Takes a {@link https://developer.mozilla.org/en-US/docs/Web/API/MediaTrackSettings#instance_properties_of_audio_tracks subset} of `MediaTrackConstraints` that apply to the audio track
30+
*
1731
* @details `startRecording`: Calling this method would result in the recording to start. Sets `isRecording` to true
1832
* @details `stopRecording`: This results in a recording in progress being stopped and the resulting audio being present in `recordingBlob`. Sets `isRecording` to false
1933
* @details `togglePauseResume`: Calling this method would pause the recording if it is currently running or resume if it is paused. Toggles the value `isPaused`
@@ -23,7 +37,9 @@ export interface recorderControls {
2337
* @details `recordingTime`: Number of seconds that the recording has gone on. This is updated every second
2438
* @details `mediaRecorder`: The current mediaRecorder in use
2539
*/
26-
const useAudioRecorder: () => recorderControls = () => {
40+
const useAudioRecorder: (
41+
audioTrackConstraints?: MediaAudioTrackConstraints
42+
) => recorderControls = (audioTrackConstraints) => {
2743
const [isRecording, setIsRecording] = useState(false);
2844
const [isPaused, setIsPaused] = useState(false);
2945
const [recordingTime, setRecordingTime] = useState(0);
@@ -50,7 +66,7 @@ const useAudioRecorder: () => recorderControls = () => {
5066
if (timerInterval != null) return;
5167

5268
navigator.mediaDevices
53-
.getUserMedia({ audio: true })
69+
.getUserMedia({ audio: audioTrackConstraints ?? true })
5470
.then((stream) => {
5571
setIsRecording(true);
5672
const recorder: MediaRecorder = new MediaRecorder(stream);

src/main.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ const addAudioElement = (blob: Blob) => {
1212

1313
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
1414
<React.StrictMode>
15-
<AudioRecorder onRecordingComplete={(blob) => addAudioElement(blob)} />
15+
<AudioRecorder
16+
onRecordingComplete={(blob) => addAudioElement(blob)}
17+
audioTrackConstraints={{
18+
noiseSuppression: true,
19+
echoCancellation: true,
20+
}}
21+
/>
1622
</React.StrictMode>
1723
);

0 commit comments

Comments
 (0)