-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
60 lines (50 loc) · 1.41 KB
/
worker.js
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
const { parentPort } = require('worker_threads');
const { say, Speaker } = require('dectalk');
const speakerModule = require('speaker');
const { Readable } = require('stream');
parentPort.on('message', async (textObj) => {
const { text, username } = textObj;
const voice = generateVoice(username);
const sampleRate = generateSampleRate(username);
const options = {
voice: voice,
sampleRate: sampleRate,
EnableCommands: false,
};
const audioData = await say(text, options);
const audioStream = new Readable({
read() {
this.push(audioData);
this.push(null);
},
});
const newSpeaker = new speakerModule({
channels: 1,
bitDepth: 16,
sampleRate: sampleRate,
signed: true,
});
newSpeaker.on('finish', () => parentPort.postMessage('finished'));
audioStream.pipe(newSpeaker);
});
function generateVoice(username) {
let hash = 0;
for (let i = 0; i < username.length; i++) {
const charCode = username.charCodeAt(i);
hash = (hash << 5) - hash + charCode;
hash |= 0;
}
const voice = hash % Object.keys(Speaker).length;
return voice;
}
function generateSampleRate(username) {
let hash = 0;
for (let i = 0; i < username.length; i++) {
const charCode = username.charCodeAt(i);
hash = (hash << 5) - hash + charCode;
hash |= 0;
}
const range = 12000 - 8000;
const sampleRate = 8000 + Math.abs(hash % (range + 1));
return sampleRate;
}