Use read().done to verify all bytes are written

Substitute using existing defined done of ReadableStreamDefaultReader.read() for value.length < this.channelDataLength &&
(await this.inputReader.read()).done to verify all input stream bytes are written
This commit is contained in:
guest271314
2021-07-24 20:03:03 -07:00
committed by GitHub
parent 0a06191497
commit 6874f6e913
+14 -19
View File
@@ -1,4 +1,3 @@
class AudioStream {
constructor({ stdin, recorder = false }) {
if (!/^espeak-ng/.test(stdin)) {
@@ -197,7 +196,21 @@ class AudioStream {
write: async ({ timestamp }) => {
const { value, done } = await this.inputReader.read();
if (done) {
await this.inputReader.closed;
console.log({ done });
try {
await this.disconnect();
} catch (err) {
console.warn(err.message);
}
console.log(
`readOffset:${this.readOffset}, duration:${this.duration}, ac.currentTime:${this.ac.currentTime}`,
`generator.readyState:${this.generator.readyState}, audioWriter.desiredSize:${this.audioWriter.desiredSize}`
);
return await Promise.all([
new Promise((resolve) => (this.stream.oninactive = resolve)),
new Promise((resolve) => (this.ac.onstatechange = resolve)),
]);
}
const uint16 = new Uint16Array(value.buffer);
// https://stackoverflow.com/a/35248852
@@ -228,24 +241,6 @@ class AudioStream {
this.recorder.start();
}
await this.audioWriter.write(frame);
if (
value.length < this.channelDataLength &&
(await this.inputReader.read()).done
) {
try {
await this.disconnect();
} catch (err) {
console.warn(err.message);
}
console.log(
`readOffset:${this.readOffset}, duration:${this.duration}, ac.currentTime:${this.ac.currentTime}`,
`generator.readyState:${this.generator.readyState}, audioWriter.desiredSize:${this.audioWriter.desiredSize}`
);
return await Promise.all([
new Promise((resolve) => (this.stream.oninactive = resolve)),
new Promise((resolve) => (this.ac.onstatechange = resolve)),
]);
}
},
abort(e) {
console.error(e.message);