Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
VoiceRecording: test audio settings
Browse files Browse the repository at this point in the history
Signed-off-by: László Várady <laszlo.varady@protonmail.com>
  • Loading branch information
MrAnno committed Nov 27, 2022
1 parent 38fb60c commit 2c8be3c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/audio/VoiceRecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import * as Recorder from 'opus-recorder';
import Recorder from 'opus-recorder/dist/recorder.min.js';
import encoderPath from 'opus-recorder/dist/encoderWorker.min.js';
import { SimpleObservable } from "matrix-widget-api";
import EventEmitter from "events";
Expand Down
69 changes: 68 additions & 1 deletion test/audio/VoiceRecording-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,23 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { VoiceRecording } from "../../src/audio/VoiceRecording";
import { mocked } from 'jest-mock';
import Recorder from 'opus-recorder/dist/recorder.min.js';

import { VoiceRecording, voiceRecorderOptions, higQualityRecorderOptions } from "../../src/audio/VoiceRecording";
import { createAudioContext } from '../..//src/audio/compat';
import MediaDeviceHandler from "../../src/MediaDeviceHandler";

jest.mock('opus-recorder/dist/recorder.min.js');
const RecorderMock = mocked(Recorder);

jest.mock('../../src/audio/compat', () => ({
createAudioContext: jest.fn(),
}));
const createAudioContextMock = mocked(createAudioContext);

jest.mock("../../src/MediaDeviceHandler");
const MediaDeviceHandlerMock = mocked(MediaDeviceHandler);

/**
* The tests here are heavily using access to private props.
Expand Down Expand Up @@ -43,6 +59,7 @@ describe("VoiceRecording", () => {
// @ts-ignore
recording.observable = {
update: jest.fn(),
close: jest.fn(),
};
jest.spyOn(recording, "stop").mockImplementation();
recorderSecondsSpy = jest.spyOn(recording, "recorderSeconds", "get");
Expand All @@ -52,6 +69,56 @@ describe("VoiceRecording", () => {
jest.resetAllMocks();
});

describe("when starting a recording", () => {
beforeEach(() => {
const mockAudioContext = {
createMediaStreamSource: jest.fn().mockReturnValue({
connect: jest.fn(),
disconnect: jest.fn(),
}),
createScriptProcessor: jest.fn().mockReturnValue({
connect: jest.fn(),
disconnect: jest.fn(),
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
}),
destination: {},
close: jest.fn(),
};
createAudioContextMock.mockReturnValue(mockAudioContext as unknown as AudioContext);
});

afterEach(async () => {
await recording.stop();
});

it("should record high-quality audio if voice processing is disabled", async () => {
MediaDeviceHandlerMock.getAudioNoiseSuppression.mockReturnValue(false);
await recording.start();

expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledWith(expect.objectContaining({
audio: expect.objectContaining({ noiseSuppression: { ideal: false } }),
}));
expect(RecorderMock).toHaveBeenCalledWith(expect.objectContaining({
encoderBitRate: higQualityRecorderOptions.bitrate,
encoderApplication: higQualityRecorderOptions.encoderApplication,
}));
});

it("should record normal-quality voice if voice processing is enabled", async () => {
MediaDeviceHandlerMock.getAudioNoiseSuppression.mockReturnValue(true);
await recording.start();

expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledWith(expect.objectContaining({
audio: expect.objectContaining({ noiseSuppression: { ideal: true } }),
}));
expect(RecorderMock).toHaveBeenCalledWith(expect.objectContaining({
encoderBitRate: voiceRecorderOptions.bitrate,
encoderApplication: voiceRecorderOptions.encoderApplication,
}));
});
});

describe("when recording", () => {
beforeEach(() => {
// @ts-ignore
Expand Down

0 comments on commit 2c8be3c

Please sign in to comment.