-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38a1845
commit 2643164
Showing
4 changed files
with
55 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { AltJTalkConfig, SynthesisOption } from "node-altjtalk-binding"; | ||
|
||
export type Task = { | ||
inputText: string; | ||
option: SynthesisOption; | ||
}; | ||
|
||
export type Result = { | ||
type: "task"; | ||
data: Int16Array; | ||
}; | ||
|
||
export function isAltJTalkConfigValid(arg: unknown): arg is AltJTalkConfig { | ||
return ( | ||
typeof arg === "object" && | ||
arg !== null && | ||
"dictionary" in arg && | ||
"model" in arg && | ||
typeof arg.dictionary === "string" && | ||
typeof arg.model === "string" | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,19 @@ | ||
import { parentPort } from "node:worker_threads"; | ||
import { | ||
AltJTalk, | ||
AltJTalkConfig, | ||
SynthesisOption, | ||
} from "node-altjtalk-binding"; | ||
import { parentPort, workerData } from "node:worker_threads"; | ||
import { AltJTalk } from "node-altjtalk-binding"; | ||
import { Result, Task, isAltJTalkConfigValid } from "./common"; | ||
|
||
export type Task = | ||
| { | ||
type: "setup"; | ||
config: AltJTalkConfig; | ||
} | ||
| { | ||
type: "task"; | ||
inputText: string; | ||
option: SynthesisOption; | ||
}; | ||
if (!isAltJTalkConfigValid(workerData)) | ||
throw new Error("AltJTalk config is invalid."); | ||
|
||
export type Result = { | ||
type: "task"; | ||
data: Int16Array; | ||
}; | ||
|
||
let synthesizer: AltJTalk | undefined = undefined; | ||
const synthesizer: AltJTalk = AltJTalk.fromConfig(workerData); | ||
|
||
if (parentPort) { | ||
parentPort.on("message", (task: Task) => { | ||
if (!parentPort) return; | ||
|
||
switch (task.type) { | ||
case "setup": | ||
synthesizer = AltJTalk.fromConfig(task.config); | ||
break; | ||
case "task": { | ||
if (!synthesizer) throw new Error("Synthesizer is not initialized!"); | ||
const data = synthesizer.synthesize(task.inputText, task.option); | ||
parentPort.postMessage({ | ||
type: "task", | ||
data, | ||
} satisfies Result); | ||
break; | ||
} | ||
} | ||
const data = synthesizer.synthesize(task.inputText, task.option); | ||
parentPort.postMessage({ | ||
type: "task", | ||
data, | ||
} satisfies Result); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters