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

Commit d3b6064

Browse files
committed
VoiceRecording: test audio settings
Signed-off-by: László Várady <[email protected]>
1 parent 38fb60c commit d3b6064

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

src/audio/VoiceRecording.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import * as Recorder from 'opus-recorder';
17+
// @ts-ignore
18+
import Recorder from 'opus-recorder/dist/recorder.min.js';
1819
import encoderPath from 'opus-recorder/dist/encoderWorker.min.js';
1920
import { SimpleObservable } from "matrix-widget-api";
2021
import EventEmitter from "events";

test/audio/VoiceRecording-test.ts

+69-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,24 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
import { VoiceRecording } from "../../src/audio/VoiceRecording";
17+
import { mocked } from 'jest-mock';
18+
// @ts-ignore
19+
import Recorder from 'opus-recorder/dist/recorder.min.js';
20+
21+
import { VoiceRecording, voiceRecorderOptions, higQualityRecorderOptions } from "../../src/audio/VoiceRecording";
22+
import { createAudioContext } from '../..//src/audio/compat';
23+
import MediaDeviceHandler from "../../src/MediaDeviceHandler";
24+
25+
jest.mock('opus-recorder/dist/recorder.min.js');
26+
const RecorderMock = mocked(Recorder);
27+
28+
jest.mock('../../src/audio/compat', () => ({
29+
createAudioContext: jest.fn(),
30+
}));
31+
const createAudioContextMock = mocked(createAudioContext);
32+
33+
jest.mock("../../src/MediaDeviceHandler");
34+
const MediaDeviceHandlerMock = mocked(MediaDeviceHandler);
1835

1936
/**
2037
* The tests here are heavily using access to private props.
@@ -43,6 +60,7 @@ describe("VoiceRecording", () => {
4360
// @ts-ignore
4461
recording.observable = {
4562
update: jest.fn(),
63+
close: jest.fn(),
4664
};
4765
jest.spyOn(recording, "stop").mockImplementation();
4866
recorderSecondsSpy = jest.spyOn(recording, "recorderSeconds", "get");
@@ -52,6 +70,56 @@ describe("VoiceRecording", () => {
5270
jest.resetAllMocks();
5371
});
5472

73+
describe("when starting a recording", () => {
74+
beforeEach(() => {
75+
const mockAudioContext = {
76+
createMediaStreamSource: jest.fn().mockReturnValue({
77+
connect: jest.fn(),
78+
disconnect: jest.fn(),
79+
}),
80+
createScriptProcessor: jest.fn().mockReturnValue({
81+
connect: jest.fn(),
82+
disconnect: jest.fn(),
83+
addEventListener: jest.fn(),
84+
removeEventListener: jest.fn(),
85+
}),
86+
destination: {},
87+
close: jest.fn(),
88+
};
89+
createAudioContextMock.mockReturnValue(mockAudioContext as unknown as AudioContext);
90+
});
91+
92+
afterEach(async () => {
93+
await recording.stop();
94+
});
95+
96+
it("should record high-quality audio if voice processing is disabled", async () => {
97+
MediaDeviceHandlerMock.getAudioNoiseSuppression.mockReturnValue(false);
98+
await recording.start();
99+
100+
expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledWith(expect.objectContaining({
101+
audio: expect.objectContaining({ noiseSuppression: { ideal: false } }),
102+
}));
103+
expect(RecorderMock).toHaveBeenCalledWith(expect.objectContaining({
104+
encoderBitRate: higQualityRecorderOptions.bitrate,
105+
encoderApplication: higQualityRecorderOptions.encoderApplication,
106+
}));
107+
});
108+
109+
it("should record normal-quality voice if voice processing is enabled", async () => {
110+
MediaDeviceHandlerMock.getAudioNoiseSuppression.mockReturnValue(true);
111+
await recording.start();
112+
113+
expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledWith(expect.objectContaining({
114+
audio: expect.objectContaining({ noiseSuppression: { ideal: true } }),
115+
}));
116+
expect(RecorderMock).toHaveBeenCalledWith(expect.objectContaining({
117+
encoderBitRate: voiceRecorderOptions.bitrate,
118+
encoderApplication: voiceRecorderOptions.encoderApplication,
119+
}));
120+
});
121+
});
122+
55123
describe("when recording", () => {
56124
beforeEach(() => {
57125
// @ts-ignore

0 commit comments

Comments
 (0)