Skip to content

Commit

Permalink
use built-in cache in readable (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-ayf authored May 26, 2024
1 parent 81728ba commit 781792f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
36 changes: 9 additions & 27 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ exports.Application = Application;
const { version: OM_SYRINX_VERSION } = require("../package.json");
exports.OM_SYRINX_VERSION = OM_SYRINX_VERSION;

const { setTimeout } = require("node:timers/promises");

class SyrinxStream extends Readable {
/**
* @param {import("./native").Syrinx} syrinx
Expand All @@ -43,21 +45,6 @@ class SyrinxStream extends Readable {
* @private
*/
this._option = option;
/**
* @type {Buffer[]}
* @private
*/
this._cache = [];
/**
* @type {number}
* @private
*/
this._waiting = 0;
/**
* @type {boolean}
* @private
*/
this._ended = false;
}

/**
Expand All @@ -74,22 +61,17 @@ class SyrinxStream extends Readable {
callback(error);

await synthesizer?.synthesize((err, result) => {
if (err) return this.emit("error", err);
if (this._waiting > 0) {
this._waiting--;
this.push(result);
} else this._cache.push(result);
if (err) this.emit("error", err);
else this.push(result);
});

this._ended = true;
// The last push sometimes occurs after the Promise resolution,
// so we need to make sure that the last push occurs before the stream ends.
await setTimeout();
this.push(null);
}

_read() {
const cache = this._cache.shift();
if (cache) this.push(cache);
else if (this._ended) this.push(null);
else this._waiting++;
}
_read() {}
}

class Syrinx {
Expand Down
9 changes: 5 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,16 @@ async function fetchAndExtract(url, path) {
* @param {Syrinx} syrinx
* @param {string} inputText
* @param {import("../lib").SynthesisOption} option
* @param {boolean} [wait]
* @returns {Promise<Buffer[]>}
*/
async function synthesize(syrinx, inputText, option) {
async function synthesize(syrinx, inputText, option, wait = false) {
const stream = syrinx.synthesize(inputText, option);
/** @type {Buffer[]} */
const result = [];
for await (const item of stream) {
result.push(item);
await setTimeout(20);
if (wait) await setTimeout(20);
}
return result;
}
Expand Down Expand Up @@ -130,7 +131,7 @@ describe("synthesis", () => {
encoder: { type: EncoderType.Raw },
});

const bonsai = await synthesize(pcm, "盆栽", {});
const bonsai = await synthesize(pcm, "盆栽", {}, true);
await checksum(
Buffer.concat(bonsai),
"36050aef60896f56bbfe59868a3f57b6bbc5b147",
Expand Down Expand Up @@ -160,7 +161,7 @@ describe("synthesis", () => {
encoder: { type: EncoderType.Opus },
});

const bonsai = await synthesize(opus, "盆栽", {});
const bonsai = await synthesize(opus, "盆栽", {}, true);
assert.strictEqual(bonsai.length, 78);

const bonsaiDecoded = decoder.decodeFrames(bonsai);
Expand Down

0 comments on commit 781792f

Please sign in to comment.