-
Notifications
You must be signed in to change notification settings - Fork 91
/
AudioOutputStreamTests.ts
243 lines (198 loc) · 7.93 KB
/
AudioOutputStreamTests.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { AudioOutputFormatImpl } from "../src/sdk/Audio/AudioOutputFormat";
import { PullAudioOutputStream, PullAudioOutputStreamImpl } from "../src/sdk/Audio/AudioOutputStream";
import { Settings } from "./Settings";
import { closeAsyncObjects } from "./Utilities";
let objsToClose: any[];
jest.mock("../src/common.browser/AudioWorkerUrl", () => ({
getAudioWorkerUrl: (): string => "speech-processor.js"
}));
beforeAll(() => {
// Override inputs, if necessary
Settings.LoadSettings();
});
beforeEach(() => {
// eslint-disable-next-line no-console
console.info("------------------Starting test case: " + expect.getState().currentTestName + "-------------------------");
objsToClose = [];
const used = process.memoryUsage().heapUsed / 1024 / 1024;
// eslint-disable-next-line no-console
console.log(`Heap memory usage before test: ${Math.round(used * 100) / 100} MB`);
});
jest.retryTimes(Settings.RetryCount);
afterEach(async (): Promise<void> => {
// eslint-disable-next-line no-console
console.info("End Time: " + new Date(Date.now()).toLocaleString());
await closeAsyncObjects(objsToClose);
const used = process.memoryUsage().heapUsed / 1024 / 1024;
// eslint-disable-next-line no-console
console.log(`Heap memory usage after test: ${Math.round(used * 100) / 100} MB`);
});
const ReadPullAudioOutputStream: (stream: PullAudioOutputStream, length?: number) => Promise<void> =
async (stream: PullAudioOutputStream, length?: number): Promise<void> => {
const audioBuffer = new ArrayBuffer(1024);
const bytesRead: number = await stream.read(audioBuffer);
if (bytesRead > 0) {
await ReadPullAudioOutputStream(stream, length === undefined ? undefined : length - bytesRead);
} else {
if (length !== undefined) {
expect(length).toEqual(0);
}
return Promise.resolve();
}
};
test("PullAudioOutputStreamImpl basic test", (done: jest.DoneCallback) => {
const size: number = 256;
const ps: PullAudioOutputStreamImpl = new PullAudioOutputStreamImpl();
objsToClose.push(ps);
ps.format = AudioOutputFormatImpl.getDefaultOutputFormat();
const ab: ArrayBuffer = new ArrayBuffer(size);
const abView: Uint8Array = new Uint8Array(ab);
for (let i: number = 0; i < size; i++) {
abView[i] = i % 256;
}
ps.write(abView);
let bytesRead: number = 0;
const audioBuffer = new ArrayBuffer(size);
ps.read(audioBuffer).then((readSize: number) => {
try {
expect(readSize).toEqual(size);
const readView: Uint8Array = new Uint8Array(audioBuffer);
for (let i: number = 0; i < readSize; i++) {
expect(readView[i]).toEqual(bytesRead++ % 256);
}
} catch (error) {
done(error);
}
done();
}, (error: string) => {
done(error);
});
});
test("PullAudioOutputStreamImpl multiple writes read after close", (done: jest.DoneCallback) => {
const ps: PullAudioOutputStreamImpl = new PullAudioOutputStreamImpl();
const format = AudioOutputFormatImpl.getDefaultOutputFormat();
ps.format = format;
const bufferSize = Math.floor(format.avgBytesPerSec / 10);
const ab: ArrayBuffer = new ArrayBuffer(bufferSize * 4);
const abView: Uint8Array = new Uint8Array(ab);
for (let i: number = 0; i < bufferSize * 4; i++) {
abView[i] = i % 256;
}
let j: number = 0;
for (j = 0; j < bufferSize * 4; j += 100) {
ps.write(ab.slice(j, j + 100));
}
ps.close();
let bytesReadTotal: number = 0;
const audioBuffer = new ArrayBuffer(bufferSize);
const readLoop = () => {
ps.read(audioBuffer).then((bytesRead: number) => {
try {
if (bytesRead === 0) {
expect(bytesReadTotal).toEqual(bufferSize * 4);
} else {
const readView: Uint8Array = new Uint8Array(audioBuffer);
for (let i: number = 0; i < bytesRead; i++) {
expect(readView[i]).toEqual(bytesReadTotal++ % 256);
}
}
} catch (error) {
done(error);
}
if (bytesRead > 0) {
readLoop();
} else {
done();
}
}, (error: string) => {
done(error);
});
};
readLoop();
});
test("PullAudioOutputStreamImpl multiple writes and reads", (done: jest.DoneCallback) => {
const ps: PullAudioOutputStreamImpl = new PullAudioOutputStreamImpl();
objsToClose.push(ps);
const format = AudioOutputFormatImpl.getDefaultOutputFormat();
ps.format = format;
const bufferSize = Math.floor(format.avgBytesPerSec / 10);
const ab: ArrayBuffer = new ArrayBuffer(bufferSize * 4);
const abView: Uint8Array = new Uint8Array(ab);
for (let i: number = 0; i < bufferSize * 4; i++) {
abView[i] = i % 256;
}
let j: number = 0;
for (j = 0; j < bufferSize * 4; j += 100) {
ps.write(ab.slice(j, j + 100));
}
ps.write(ab.slice(j));
let bytesReadTotal: number = 0;
const audioBuffer = new ArrayBuffer(bufferSize);
const readLoop = () => {
ps.read(audioBuffer).then((bytesRead: number) => {
try {
expect(bytesRead).toBeLessThanOrEqual(bufferSize);
const readView: Uint8Array = new Uint8Array(audioBuffer);
for (let i: number = 0; i < bytesRead; i++) {
expect(readView[i]).toEqual(bytesReadTotal++ % 256);
}
} catch (error) {
done(error);
}
if (bytesReadTotal < bufferSize * 4) {
readLoop();
} else {
done();
}
}, (error: string) => {
done(error);
});
};
readLoop();
});
test("PullAudioOutputStreamImpl reads before writing", async (): Promise<void> => {
const ps: PullAudioOutputStreamImpl = new PullAudioOutputStreamImpl();
objsToClose.push(ps);
const format = AudioOutputFormatImpl.getDefaultOutputFormat();
ps.format = format;
const bufferSize = Math.floor(format.avgBytesPerSec / 10);
const readPromise: Promise<void> = ReadPullAudioOutputStream(ps, bufferSize * 4);
process.nextTick((): void => {
const ab: ArrayBuffer = new ArrayBuffer(bufferSize * 4);
const abView: Uint8Array = new Uint8Array(ab);
for (let i: number = 0; i < bufferSize * 4; i++) {
abView[i] = i % 256;
}
let j: number = 0;
for (j = 0; j < bufferSize * 4; j += 100) {
ps.write(ab.slice(j, j + 100));
}
ps.write(ab.slice(j));
ps.close();
});
await readPromise;
});
test("PullAudioOutputStreamImpl read all audio data in single read", async (): Promise<void> => {
const ps: PullAudioOutputStreamImpl = new PullAudioOutputStreamImpl();
const format = AudioOutputFormatImpl.fromSpeechSynthesisOutputFormatString("raw-24khz-16bit-mono-pcm");
ps.format = format;
const bufferSize = Math.floor(format.avgBytesPerSec / 10);
const ab: ArrayBuffer = new ArrayBuffer(bufferSize * 4);
const abView: Uint8Array = new Uint8Array(ab);
for (let k: number = 0; k < 1500; k++) { // 10 minutes data
for (let i: number = 0; i < bufferSize * 4; i++) {
abView[i] = (i + k * bufferSize * 4) % 256;
}
ps.write(ab);
}
ps.close();
const audioBuffer = new ArrayBuffer(bufferSize * 6000);
const bytesRead: number = await ps.read(audioBuffer);
expect(bytesRead).toEqual(bufferSize * 6000);
const readView: Uint8Array = new Uint8Array(audioBuffer);
for (let i: number = 0; i < bytesRead - 1000; i += 997) { // not check all to avoid long running.
expect(readView[i]).toEqual(i % 256);
}
});