@makefully/dutifully 1.10.1 → 1.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.js CHANGED
@@ -1,58 +1,58 @@
1
- /* eslint-disable no-sync */
2
- /* global console, process, require */
3
- const
4
- LipSync = require('./classes/LipSync'),
5
- parsers = {
6
- allosaurus: LipSync,
7
- elevenlabs: require('./classes/ElevenLabs'),
8
- ffmpeg: require('./classes/FFMPEG'),
9
- polly: require('./classes/Polly'),
10
- rasterize: require('./classes/Rasterize'),
11
- rhubarb: LipSync,
12
- transcription: require('./classes/Transcription')
13
- },
14
- getJSON = require('./helpers/getJSON'),
15
- send = function (contents) {
16
- const
17
- {id, service} = contents,
18
- configs = Array.isArray(contents[service]) ? contents[service] : [contents[service]];
19
-
20
- configs.forEach(async (config, index, {length}) => {
21
- if (!config) {
22
- console.warn(`Empty configuration for "${service}" service.`);
23
- } else {
24
- const
25
- serviceHandler = new parsers[service]({config, contents});
26
-
27
- try {
28
- await serviceHandler.prepare({...contents, ...config});
29
- } catch (e) {
30
- console.warn(e.message);
31
- return;
32
- }
33
-
34
- await serviceHandler.send({
35
- instanceId: `${id}-${service}${length > 1 ? `-${index}` : ''}`
36
- });
37
- }
38
- });
39
- };
40
-
41
- const cheer = async (cmdArgs) => {
42
- const
43
- package = await getJSON('./package.json') ?? {},
44
- config = await getJSON('./cheerfully.json') ?? {},
45
- env = await getJSON('./env-cheerfully.json') ?? {};
46
-
47
- send({
48
- id: `${package.name}-${package.version}`,
49
- package,
50
- ...config,
51
- ...env,
52
- ...cmdArgs
53
- });
54
- };
55
-
56
- cheer.say = require('./say');
57
-
58
- module.exports = cheer;
1
+ /* eslint-disable no-sync */
2
+ /* global console, process, require */
3
+ const
4
+ LipSync = require('./classes/LipSync'),
5
+ parsers = {
6
+ allosaurus: LipSync,
7
+ elevenlabs: require('./classes/ElevenLabs'),
8
+ ffmpeg: require('./classes/FFMPEG'),
9
+ polly: require('./classes/Polly'),
10
+ rasterize: require('./classes/Rasterize'),
11
+ rhubarb: LipSync,
12
+ transcription: require('./classes/Transcription')
13
+ },
14
+ getJSON = require('./helpers/getJSON'),
15
+ send = function (contents) {
16
+ const
17
+ {id, service} = contents,
18
+ configs = Array.isArray(contents[service]) ? contents[service] : [contents[service]];
19
+
20
+ configs.forEach(async (config, index, {length}) => {
21
+ if (!config) {
22
+ console.warn(`Empty configuration for "${service}" service.`);
23
+ } else {
24
+ const
25
+ serviceHandler = new parsers[service]({config, contents});
26
+
27
+ try {
28
+ await serviceHandler.prepare({...contents, ...config});
29
+ } catch (e) {
30
+ console.warn(e.message);
31
+ return;
32
+ }
33
+
34
+ await serviceHandler.send({
35
+ instanceId: `${id}-${service}${length > 1 ? `-${index}` : ''}`
36
+ });
37
+ }
38
+ });
39
+ };
40
+
41
+ const cheer = async (cmdArgs) => {
42
+ const
43
+ package = await getJSON('./package.json') ?? {},
44
+ config = await getJSON('./cheerfully.json') ?? {},
45
+ env = await getJSON('./env-cheerfully.json') ?? {};
46
+
47
+ send({
48
+ id: `${package.name}-${package.version}`,
49
+ package,
50
+ ...config,
51
+ ...env,
52
+ ...cmdArgs
53
+ });
54
+ };
55
+
56
+ cheer.say = require('./say');
57
+
58
+ module.exports = cheer;
package/src/say.js CHANGED
@@ -1,141 +1,141 @@
1
- /* eslint-disable no-sync */
2
- /* global console, process, require */
3
- const
4
- fs = require('fs'),
5
- fsp = require('fs').promises,
6
- path = require('path'),
7
- {createHash} = require('crypto'),
8
- {AudioContext} = require('node-web-audio-api'),
9
- ElevenLabs = require('./classes/ElevenLabs'),
10
- getJSON = require('./helpers/getJSON'),
11
- CACHE_DIR = '.cache/tts',
12
- resolveVoiceId = (voice) => {
13
- if (!voice) {
14
- return null;
15
- }
16
-
17
- if (typeof voice === 'string') {
18
- return voice;
19
- }
20
-
21
- return voice.default ?? Object.values(voice)[0] ?? null;
22
- },
23
- cacheKey = ({voice, text}) => createHash('sha256')
24
- .update(JSON.stringify({
25
- version: 1,
26
- voice,
27
- text
28
- }))
29
- .digest('hex'),
30
- playMp3 = async (file) => {
31
- const
32
- audioContext = new AudioContext(),
33
- data = await fsp.readFile(file),
34
- arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength),
35
- buffer = await audioContext.decodeAudioData(arrayBuffer),
36
- source = audioContext.createBufferSource();
37
-
38
- source.buffer = buffer;
39
- source.connect(audioContext.destination);
40
-
41
- await new Promise((resolve, reject) => {
42
- source.onended = resolve;
43
- try {
44
- source.start();
45
- } catch (e) {
46
- reject(e);
47
- }
48
- });
49
-
50
- await audioContext.close();
51
- },
52
- generate = async ({text, voice, key, contents, elevenlabsConfig}) => {
53
- const
54
- output = `${CACHE_DIR}/`,
55
- config = {
56
- ...elevenlabsConfig,
57
- files: {
58
- [key]: text
59
- },
60
- path: '',
61
- voice,
62
- output
63
- },
64
- handler = new ElevenLabs({
65
- config,
66
- contents: {
67
- ...contents,
68
- service: 'elevenlabs'
69
- }
70
- });
71
-
72
- delete config.script;
73
-
74
- await handler.prepare({
75
- difference: false,
76
- extract: true
77
- });
78
- await handler.send({
79
- instanceId: `${contents.id}-say`
80
- });
81
-
82
- return path.join(CACHE_DIR, `${key}.mp3`);
83
- };
84
-
85
- module.exports = async (textOrArgs = {}) => {
86
- const
87
- isString = typeof textOrArgs === 'string',
88
- cmdArgs = isString ? {} : (textOrArgs ?? {}),
89
- text = (isString ? textOrArgs : (cmdArgs.text ?? '')).trim();
90
-
91
- if (!text) {
92
- throw new Error('Usage: say "Hello world"');
93
- }
94
-
95
- const
96
- packageJson = await getJSON('./package.json') ?? {},
97
- cheerfully = await getJSON('./cheerfully.json') ?? {},
98
- env = await getJSON('./env-cheerfully.json') ?? {},
99
- contents = {
100
- id: `${packageJson.name ?? 'cheerfully'}-${packageJson.version ?? '0'}`,
101
- package: packageJson,
102
- ...cheerfully,
103
- ...env,
104
- ...cmdArgs
105
- },
106
- elevenlabsConfig = Array.isArray(contents.elevenlabs)
107
- ? (contents.elevenlabs[0] ?? {})
108
- : (contents.elevenlabs ?? {}),
109
- voice = elevenlabsConfig.voice,
110
- voiceId = resolveVoiceId(voice);
111
-
112
- if (!voiceId) {
113
- throw new Error('No ElevenLabs voice configured. Set elevenlabs[0].voice in cheerfully.json.');
114
- }
115
-
116
- if (!contents.server || !contents.accessToken) {
117
- throw new Error('Missing server/accessToken. Set them in env-cheerfully.json.');
118
- }
119
-
120
- await fsp.mkdir(CACHE_DIR, {recursive: true});
121
-
122
- const
123
- key = cacheKey({
124
- voice: voiceId,
125
- text
126
- }),
127
- mp3File = path.join(CACHE_DIR, `${key}.mp3`);
128
-
129
- if (!fs.existsSync(mp3File)) {
130
- console.log('Generating speech...');
131
- await generate({
132
- text,
133
- voice,
134
- key,
135
- contents,
136
- elevenlabsConfig
137
- });
138
- }
139
-
140
- await playMp3(mp3File);
141
- };
1
+ /* eslint-disable no-sync */
2
+ /* global console, process, require */
3
+ const
4
+ fs = require('fs'),
5
+ fsp = require('fs').promises,
6
+ path = require('path'),
7
+ {createHash} = require('crypto'),
8
+ {AudioContext} = require('node-web-audio-api'),
9
+ ElevenLabs = require('./classes/ElevenLabs'),
10
+ getJSON = require('./helpers/getJSON'),
11
+ CACHE_DIR = '.cache/tts',
12
+ resolveVoiceId = (voice) => {
13
+ if (!voice) {
14
+ return null;
15
+ }
16
+
17
+ if (typeof voice === 'string') {
18
+ return voice;
19
+ }
20
+
21
+ return voice.default ?? Object.values(voice)[0] ?? null;
22
+ },
23
+ cacheKey = ({voice, text}) => createHash('sha256')
24
+ .update(JSON.stringify({
25
+ version: 1,
26
+ voice,
27
+ text
28
+ }))
29
+ .digest('hex'),
30
+ playMp3 = async (file) => {
31
+ const
32
+ audioContext = new AudioContext(),
33
+ data = await fsp.readFile(file),
34
+ arrayBuffer = data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength),
35
+ buffer = await audioContext.decodeAudioData(arrayBuffer),
36
+ source = audioContext.createBufferSource();
37
+
38
+ source.buffer = buffer;
39
+ source.connect(audioContext.destination);
40
+
41
+ await new Promise((resolve, reject) => {
42
+ source.onended = resolve;
43
+ try {
44
+ source.start();
45
+ } catch (e) {
46
+ reject(e);
47
+ }
48
+ });
49
+
50
+ await audioContext.close();
51
+ },
52
+ generate = async ({text, voice, key, contents, elevenlabsConfig}) => {
53
+ const
54
+ output = `${CACHE_DIR}/`,
55
+ config = {
56
+ ...elevenlabsConfig,
57
+ files: {
58
+ [key]: text
59
+ },
60
+ path: '',
61
+ voice,
62
+ output
63
+ },
64
+ handler = new ElevenLabs({
65
+ config,
66
+ contents: {
67
+ ...contents,
68
+ service: 'elevenlabs'
69
+ }
70
+ });
71
+
72
+ delete config.script;
73
+
74
+ await handler.prepare({
75
+ difference: false,
76
+ extract: true
77
+ });
78
+ await handler.send({
79
+ instanceId: `${contents.id}-say`
80
+ });
81
+
82
+ return path.join(CACHE_DIR, `${key}.mp3`);
83
+ };
84
+
85
+ module.exports = async (textOrArgs = {}) => {
86
+ const
87
+ isString = typeof textOrArgs === 'string',
88
+ cmdArgs = isString ? {} : (textOrArgs ?? {}),
89
+ text = (isString ? textOrArgs : (cmdArgs.text ?? '')).trim();
90
+
91
+ if (!text) {
92
+ throw new Error('Usage: say "Hello world"');
93
+ }
94
+
95
+ const
96
+ packageJson = await getJSON('./package.json') ?? {},
97
+ cheerfully = await getJSON('./cheerfully.json') ?? {},
98
+ env = await getJSON('./env-cheerfully.json') ?? {},
99
+ contents = {
100
+ id: `${packageJson.name ?? 'cheerfully'}-${packageJson.version ?? '0'}`,
101
+ package: packageJson,
102
+ ...cheerfully,
103
+ ...env,
104
+ ...cmdArgs
105
+ },
106
+ elevenlabsConfig = Array.isArray(contents.elevenlabs)
107
+ ? (contents.elevenlabs[0] ?? {})
108
+ : (contents.elevenlabs ?? {}),
109
+ voice = elevenlabsConfig.voice,
110
+ voiceId = resolveVoiceId(voice);
111
+
112
+ if (!voiceId) {
113
+ throw new Error('No ElevenLabs voice configured. Set elevenlabs[0].voice in cheerfully.json.');
114
+ }
115
+
116
+ if (!contents.server || !contents.accessToken) {
117
+ throw new Error('Missing server/accessToken. Set them in env-cheerfully.json.');
118
+ }
119
+
120
+ await fsp.mkdir(CACHE_DIR, {recursive: true});
121
+
122
+ const
123
+ key = cacheKey({
124
+ voice: voiceId,
125
+ text
126
+ }),
127
+ mp3File = path.join(CACHE_DIR, `${key}.mp3`);
128
+
129
+ if (!fs.existsSync(mp3File)) {
130
+ console.log('Generating speech...');
131
+ await generate({
132
+ text,
133
+ voice,
134
+ key,
135
+ contents,
136
+ elevenlabsConfig
137
+ });
138
+ }
139
+
140
+ await playMp3(mp3File);
141
+ };