@vdegenne/voicevox 0.1.16 → 0.1.18
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/README.md +123 -31
- package/lib/client.d.ts +30 -0
- package/lib/client.d.ts.map +1 -0
- package/lib/client.js +146 -0
- package/lib/client.js.map +1 -0
- package/lib/core.d.ts +22 -0
- package/lib/core.d.ts.map +1 -0
- package/lib/core.js +22 -0
- package/lib/core.js.map +1 -0
- package/package.json +10 -9
- package/lib/index.d.ts +0 -44
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -208
- package/lib/index.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,60 +1,152 @@
|
|
|
1
1
|
# @vdegenne/voicevox
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Lightweight TypeScript client for interacting with a local VOICEVOX TTS engine.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm
|
|
8
|
+
npm install @vdegenne/voicevox
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Requirements
|
|
12
|
+
|
|
13
|
+
Install and run [VOICEVOX](https://voicevox.hiroshiba.jp/) on your machine.
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
The client connects to `http://127.0.0.1:50021` by default.
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
## Usage
|
|
16
18
|
|
|
17
19
|
```ts
|
|
18
|
-
import {
|
|
20
|
+
import {VoicevoxProxyClient} from '@vdegenne/voicevox';
|
|
21
|
+
|
|
22
|
+
const voicevox = new VoicevoxProxyClient();
|
|
19
23
|
|
|
20
|
-
const
|
|
24
|
+
const options = {
|
|
25
|
+
input: 'こんにちは',
|
|
26
|
+
speaker: 0,
|
|
27
|
+
speed: 1,
|
|
28
|
+
};
|
|
21
29
|
|
|
22
30
|
try {
|
|
23
|
-
await voicevox.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
31
|
+
const audio = await voicevox.getAudio(options);
|
|
32
|
+
|
|
33
|
+
// Use the returned Blob with your audio player.
|
|
34
|
+
} catch (error) {
|
|
35
|
+
console.error('VOICEVOX request failed:', error);
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Configuration
|
|
40
|
+
|
|
41
|
+
You can specify a custom VOICEVOX server endpoint:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
const voicevox = new VoicevoxProxyClient('http://127.0.0.1:50021');
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Speech options
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
{
|
|
51
|
+
input: 'こんにちは',
|
|
52
|
+
speaker: 0,
|
|
53
|
+
speed: 1,
|
|
27
54
|
}
|
|
28
55
|
```
|
|
29
56
|
|
|
30
|
-
|
|
57
|
+
| Property | Type | Default | Description |
|
|
58
|
+
| --------- | -------- | -------- | ----------------------- |
|
|
59
|
+
| `input` | `string` | Required | Text to synthesize |
|
|
60
|
+
| `speaker` | `number` | Required | VOICEVOX style ID |
|
|
61
|
+
| `speed` | `number` | `1` | Speech speed multiplier |
|
|
62
|
+
|
|
63
|
+
## Audio requests
|
|
64
|
+
|
|
65
|
+
`createAudioRequest()` returns an audio request descriptor containing a cache key and a function that resolves to the audio Blob.
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
const request = await voicevox.createAudioRequest({
|
|
69
|
+
input: 'こんにちは',
|
|
70
|
+
speaker: 0,
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
console.log(request.key);
|
|
74
|
+
|
|
75
|
+
const audio = await request.audio();
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Both `getAudio()` and `createAudioRequest()` use an in-memory cache to avoid generating the same audio multiple times.
|
|
79
|
+
|
|
80
|
+
## Voices
|
|
81
|
+
|
|
82
|
+
The client provides methods to retrieve and search available VOICEVOX speakers and styles.
|
|
83
|
+
|
|
84
|
+
### Get speakers
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const speakers = await voicevox.getSpeakers();
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Speakers are fetched lazily on the first request and cached in memory for subsequent calls. Concurrent requests share the same pending fetch.
|
|
91
|
+
|
|
92
|
+
### Get available voices
|
|
93
|
+
|
|
94
|
+
`getVoices()` returns a flattened list of available styles.
|
|
31
95
|
|
|
32
96
|
```ts
|
|
33
|
-
voicevox.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
97
|
+
const voices = await voicevox.getVoices();
|
|
98
|
+
|
|
99
|
+
// Example result:
|
|
100
|
+
// {
|
|
101
|
+
// id: 2,
|
|
102
|
+
// name: '四国めたん (ノーマル)',
|
|
103
|
+
// speakerName: '四国めたん',
|
|
104
|
+
// styleName: 'ノーマル'
|
|
105
|
+
// }
|
|
42
106
|
```
|
|
43
107
|
|
|
44
|
-
###
|
|
108
|
+
### Find a voice by ID
|
|
45
109
|
|
|
46
|
-
|
|
110
|
+
```ts
|
|
111
|
+
const voice = await voicevox.getVoiceById(2);
|
|
112
|
+
|
|
113
|
+
console.log(voice?.name);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
### Find a voice ID by name
|
|
47
117
|
|
|
48
118
|
```ts
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
119
|
+
const voiceId = await voicevox.getVoiceIdByName('四国めたん (ノーマル)');
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Both lookup methods return `undefined` if no matching voice is found.
|
|
123
|
+
|
|
124
|
+
## Error handling
|
|
52
125
|
|
|
53
|
-
|
|
126
|
+
The client throws standard JavaScript `Error` instances when a request fails.
|
|
54
127
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
128
|
+
```ts
|
|
129
|
+
try {
|
|
130
|
+
const speakers = await voicevox.getSpeakers();
|
|
131
|
+
} catch (error) {
|
|
132
|
+
console.error(error);
|
|
59
133
|
}
|
|
60
134
|
```
|
|
135
|
+
|
|
136
|
+
HTTP error responses are inspected for a descriptive error message. The client also supports the VOICEVOX engine's native `detail` error field.
|
|
137
|
+
|
|
138
|
+
Failed speaker requests are not cached, allowing subsequent calls to retry.
|
|
139
|
+
|
|
140
|
+
## Exports
|
|
141
|
+
|
|
142
|
+
The package exposes the client from its root entry point:
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import {VoicevoxProxyClient} from '@vdegenne/voicevox';
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
Core types and utilities are available through the `/core` subpath:
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
import type {VoicevoxSpeechOptions} from '@vdegenne/voicevox/core';
|
|
152
|
+
```
|
package/lib/client.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { normalizeSpeechOptions, type VoicevoxSpeaker, type VoicevoxSpeechOptions } from './core.js';
|
|
2
|
+
export interface VoicevoxAudioRequest {
|
|
3
|
+
key: string;
|
|
4
|
+
audio: () => Promise<Blob>;
|
|
5
|
+
}
|
|
6
|
+
export interface VoicevoxVoice {
|
|
7
|
+
id: number;
|
|
8
|
+
name: string;
|
|
9
|
+
speakerName: string;
|
|
10
|
+
styleName: string;
|
|
11
|
+
}
|
|
12
|
+
export declare class VoicevoxProxyClient {
|
|
13
|
+
readonly endpoint: string;
|
|
14
|
+
private cache;
|
|
15
|
+
private speakers?;
|
|
16
|
+
private speakersPromise?;
|
|
17
|
+
constructor(endpoint?: string);
|
|
18
|
+
getSpeakers(): Promise<VoicevoxSpeaker[]>;
|
|
19
|
+
private fetchSpeakers;
|
|
20
|
+
getVoices(): Promise<VoicevoxVoice[]>;
|
|
21
|
+
getVoiceById(id: number): Promise<VoicevoxVoice | undefined>;
|
|
22
|
+
getVoiceIdByName(name: string): Promise<number | undefined>;
|
|
23
|
+
getAudio(options: VoicevoxSpeechOptions): Promise<Blob>;
|
|
24
|
+
createAudioRequest(options: VoicevoxSpeechOptions): Promise<VoicevoxAudioRequest>;
|
|
25
|
+
private getCachedAudio;
|
|
26
|
+
protected fetchAudio(options: ReturnType<typeof normalizeSpeechOptions>): Promise<Blob>;
|
|
27
|
+
private fetch;
|
|
28
|
+
private createResponseError;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,sBAAsB,EACtB,KAAK,eAAe,EACpB,KAAK,qBAAqB,EAC1B,MAAM,WAAW,CAAC;AAEnB,MAAM,WAAW,oBAAoB;IACpC,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3B;AAED,MAAM,WAAW,aAAa;IAC7B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;CAClB;AAED,qBAAa,mBAAmB;IAC/B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAE1B,OAAO,CAAC,KAAK,CAA2C;IAExD,OAAO,CAAC,QAAQ,CAAC,CAAoB;IACrC,OAAO,CAAC,eAAe,CAAC,CAA6B;IAErD,YAAY,QAAQ,SAA2B,EAE9C;IAIK,WAAW,IAAI,OAAO,CAAC,eAAe,EAAE,CAAC,CAiB9C;YAEa,aAAa;IAmBrB,SAAS,IAAI,OAAO,CAAC,aAAa,EAAE,CAAC,CAW1C;IAEK,YAAY,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC,CAGjE;IAEK,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAGhE;IAIK,QAAQ,CAAC,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC,CAK5D;IAEK,kBAAkB,CACvB,OAAO,EAAE,qBAAqB,GAC5B,OAAO,CAAC,oBAAoB,CAAC,CAQ/B;YAEa,cAAc;IA0B5B,UAAgB,UAAU,CACzB,OAAO,EAAE,UAAU,CAAC,OAAO,sBAAsB,CAAC,GAChD,OAAO,CAAC,IAAI,CAAC,CAwCf;YAIa,KAAK;YAgBL,mBAAmB;CAkCjC"}
|
package/lib/client.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { getCacheKey, normalizeSpeechOptions, } from './core.js';
|
|
2
|
+
export class VoicevoxProxyClient {
|
|
3
|
+
constructor(endpoint = 'http://127.0.0.1:50021') {
|
|
4
|
+
this.cache = new Map();
|
|
5
|
+
this.endpoint = endpoint.replace(/\/+$/, '');
|
|
6
|
+
}
|
|
7
|
+
// Speakers
|
|
8
|
+
async getSpeakers() {
|
|
9
|
+
if (this.speakers) {
|
|
10
|
+
return this.speakers;
|
|
11
|
+
}
|
|
12
|
+
if (this.speakersPromise) {
|
|
13
|
+
return this.speakersPromise;
|
|
14
|
+
}
|
|
15
|
+
this.speakersPromise = this.fetchSpeakers();
|
|
16
|
+
try {
|
|
17
|
+
this.speakers = await this.speakersPromise;
|
|
18
|
+
return this.speakers;
|
|
19
|
+
}
|
|
20
|
+
finally {
|
|
21
|
+
this.speakersPromise = undefined;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
async fetchSpeakers() {
|
|
25
|
+
const response = await this.fetch(`${this.endpoint}/speakers`);
|
|
26
|
+
if (!response.ok) {
|
|
27
|
+
throw await this.createResponseError(response, 'VOICEVOX speakers request failed');
|
|
28
|
+
}
|
|
29
|
+
const speakers = await response.json();
|
|
30
|
+
if (!Array.isArray(speakers)) {
|
|
31
|
+
throw new Error('Invalid VOICEVOX speakers response');
|
|
32
|
+
}
|
|
33
|
+
return speakers;
|
|
34
|
+
}
|
|
35
|
+
async getVoices() {
|
|
36
|
+
const speakers = await this.getSpeakers();
|
|
37
|
+
return speakers.flatMap((speaker) => speaker.styles.map((style) => ({
|
|
38
|
+
id: style.id,
|
|
39
|
+
name: `${speaker.name} (${style.name})`,
|
|
40
|
+
speakerName: speaker.name,
|
|
41
|
+
styleName: style.name,
|
|
42
|
+
})));
|
|
43
|
+
}
|
|
44
|
+
async getVoiceById(id) {
|
|
45
|
+
const voices = await this.getVoices();
|
|
46
|
+
return voices.find((voice) => voice.id === id);
|
|
47
|
+
}
|
|
48
|
+
async getVoiceIdByName(name) {
|
|
49
|
+
const voices = await this.getVoices();
|
|
50
|
+
return voices.find((voice) => voice.name === name)?.id;
|
|
51
|
+
}
|
|
52
|
+
// Audio
|
|
53
|
+
async getAudio(options) {
|
|
54
|
+
const normalized = normalizeSpeechOptions(options);
|
|
55
|
+
const key = await getCacheKey(normalized);
|
|
56
|
+
return this.getCachedAudio(key, normalized);
|
|
57
|
+
}
|
|
58
|
+
async createAudioRequest(options) {
|
|
59
|
+
const normalized = normalizeSpeechOptions(options);
|
|
60
|
+
const key = await getCacheKey(normalized);
|
|
61
|
+
return {
|
|
62
|
+
key,
|
|
63
|
+
audio: () => this.getCachedAudio(key, normalized),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
async getCachedAudio(key, options) {
|
|
67
|
+
const cached = this.cache.get(key);
|
|
68
|
+
if (cached) {
|
|
69
|
+
return cached;
|
|
70
|
+
}
|
|
71
|
+
const promise = this.fetchAudio(options);
|
|
72
|
+
this.cache.set(key, promise);
|
|
73
|
+
try {
|
|
74
|
+
const audio = await promise;
|
|
75
|
+
this.cache.set(key, audio);
|
|
76
|
+
return audio;
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
if (this.cache.get(key) === promise) {
|
|
80
|
+
this.cache.delete(key);
|
|
81
|
+
}
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async fetchAudio(options) {
|
|
86
|
+
const queryParams = new URLSearchParams({
|
|
87
|
+
text: options.input,
|
|
88
|
+
speaker: String(options.speaker),
|
|
89
|
+
});
|
|
90
|
+
const queryResponse = await this.fetch(`${this.endpoint}/audio_query?${queryParams}`, { method: 'POST' });
|
|
91
|
+
if (!queryResponse.ok) {
|
|
92
|
+
throw await this.createResponseError(queryResponse, 'VOICEVOX audio_query failed');
|
|
93
|
+
}
|
|
94
|
+
const audioQuery = (await queryResponse.json());
|
|
95
|
+
audioQuery.speedScale = options.speed;
|
|
96
|
+
const synthesisResponse = await this.fetch(`${this.endpoint}/synthesis?speaker=${options.speaker}`, {
|
|
97
|
+
method: 'POST',
|
|
98
|
+
headers: {
|
|
99
|
+
'Content-Type': 'application/json',
|
|
100
|
+
},
|
|
101
|
+
body: JSON.stringify(audioQuery),
|
|
102
|
+
});
|
|
103
|
+
if (!synthesisResponse.ok) {
|
|
104
|
+
throw await this.createResponseError(synthesisResponse, 'VOICEVOX synthesis failed');
|
|
105
|
+
}
|
|
106
|
+
return synthesisResponse.blob();
|
|
107
|
+
}
|
|
108
|
+
// HTTP
|
|
109
|
+
async fetch(url, options) {
|
|
110
|
+
try {
|
|
111
|
+
return await fetch(url, options);
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
const path = new URL(url).pathname;
|
|
115
|
+
throw new Error(`VOICEVOX request failed for ${path}. Is the proxy running?`, { cause: error });
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// Errors
|
|
119
|
+
// TODO: When proxy will get implemented we should remove body.detail
|
|
120
|
+
async createResponseError(response, fallback) {
|
|
121
|
+
let message = '';
|
|
122
|
+
try {
|
|
123
|
+
const body = await response.json();
|
|
124
|
+
if (typeof body === 'object' &&
|
|
125
|
+
body !== null &&
|
|
126
|
+
'error' in body &&
|
|
127
|
+
typeof body.error === 'string') {
|
|
128
|
+
message = body.error;
|
|
129
|
+
}
|
|
130
|
+
else if (typeof body === 'object' &&
|
|
131
|
+
body !== null &&
|
|
132
|
+
'detail' in body &&
|
|
133
|
+
typeof body.detail === 'string') {
|
|
134
|
+
message = body.detail;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
// The response body is not valid JSON.
|
|
139
|
+
}
|
|
140
|
+
if (!message) {
|
|
141
|
+
message = `${fallback}: ${response.status} ${response.statusText}`;
|
|
142
|
+
}
|
|
143
|
+
return new Error(message);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,WAAW,EACX,sBAAsB,GAGtB,MAAM,WAAW,CAAC;AAcnB,MAAM,OAAO,mBAAmB;IAQ/B,YAAY,QAAQ,GAAG,wBAAwB;QALvC,UAAK,GAAG,IAAI,GAAG,EAAgC,CAAC;QAMvD,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IAC9C,CAAC;IAED,WAAW;IAEX,KAAK,CAAC,WAAW;QAChB,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YACnB,OAAO,IAAI,CAAC,QAAQ,CAAC;QACtB,CAAC;QAED,IAAI,IAAI,CAAC,eAAe,EAAE,CAAC;YAC1B,OAAO,IAAI,CAAC,eAAe,CAAC;QAC7B,CAAC;QAED,IAAI,CAAC,eAAe,GAAG,IAAI,CAAC,aAAa,EAAE,CAAC;QAE5C,IAAI,CAAC;YACJ,IAAI,CAAC,QAAQ,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC;YAC3C,OAAO,IAAI,CAAC,QAAQ,CAAC;QACtB,CAAC;gBAAS,CAAC;YACV,IAAI,CAAC,eAAe,GAAG,SAAS,CAAC;QAClC,CAAC;IACF,CAAC;IAEO,KAAK,CAAC,aAAa;QAC1B,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,K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|
+
export interface VoicevoxStyle {
|
|
2
|
+
readonly name: string;
|
|
3
|
+
readonly id: number;
|
|
4
|
+
readonly type: string;
|
|
5
|
+
}
|
|
6
|
+
export interface VoicevoxSpeaker {
|
|
7
|
+
readonly name: string;
|
|
8
|
+
readonly speaker_uuid: string;
|
|
9
|
+
readonly styles: readonly VoicevoxStyle[];
|
|
10
|
+
readonly version: string;
|
|
11
|
+
readonly supported_features: {
|
|
12
|
+
readonly permitted_synthesis_morphing: 'ALL' | 'SELF_ONLY' | 'NOTHING';
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export interface VoicevoxSpeechOptions {
|
|
16
|
+
input: string;
|
|
17
|
+
speaker: number;
|
|
18
|
+
speed?: number;
|
|
19
|
+
}
|
|
20
|
+
export declare function normalizeSpeechOptions(options: VoicevoxSpeechOptions): Required<VoicevoxSpeechOptions>;
|
|
21
|
+
export declare function getCacheKey(options: VoicevoxSpeechOptions): Promise<string>;
|
|
22
|
+
//# sourceMappingURL=core.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,aAAa;IAC7B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,eAAe;IAC/B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,MAAM,EAAE,SAAS,aAAa,EAAE,CAAC;IAC1C,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,kBAAkB,EAAE;QAC5B,QAAQ,CAAC,4BAA4B,EAAE,KAAK,GAAG,WAAW,GAAG,SAAS,CAAC;KACvE,CAAC;CACF;AAED,MAAM,WAAW,qBAAqB;IACrC,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CACf;AAED,wBAAgB,sBAAsB,CACrC,OAAO,EAAE,qBAAqB,GAC5B,QAAQ,CAAC,qBAAqB,CAAC,CAUjC;AAED,wBAAsB,WAAW,CAChC,OAAO,EAAE,qBAAqB,GAC5B,OAAO,CAAC,MAAM,CAAC,CAYjB"}
|
package/lib/core.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function normalizeSpeechOptions(options) {
|
|
2
|
+
function normalize(value) {
|
|
3
|
+
return value.replace(/[^\S\r\n]+/g, ' ').trim();
|
|
4
|
+
}
|
|
5
|
+
return {
|
|
6
|
+
input: normalize(options.input),
|
|
7
|
+
speaker: options.speaker,
|
|
8
|
+
speed: options.speed ?? 1,
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export async function getCacheKey(options) {
|
|
12
|
+
const normalized = normalizeSpeechOptions(options);
|
|
13
|
+
const payload = JSON.stringify(Object.values(normalized));
|
|
14
|
+
const data = new TextEncoder().encode(payload);
|
|
15
|
+
const hash = await crypto.subtle.digest('SHA-256', data);
|
|
16
|
+
return Array.from(new Uint8Array(hash))
|
|
17
|
+
.map(function (byte) {
|
|
18
|
+
return byte.toString(16).padStart(2, '0');
|
|
19
|
+
})
|
|
20
|
+
.join('');
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=core.js.map
|
package/lib/core.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.js","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAsBA,MAAM,UAAU,sBAAsB,CACrC,OAA8B;IAE9B,SAAS,SAAS,CAAC,KAAa;QAC/B,OAAO,KAAK,CAAC,OAAO,CAAC,aAAa,EAAE,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;IACjD,CAAC;IAED,OAAO;QACN,KAAK,EAAE,SAAS,CAAC,OAAO,CAAC,KAAK,CAAC;QAC/B,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,KAAK,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;KACzB,CAAC;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,WAAW,CAChC,OAA8B;IAE9B,MAAM,UAAU,GAAG,sBAAsB,CAAC,OAAO,CAAC,CAAC;IACnD,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC;IAE1D,MAAM,IAAI,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAC/C,MAAM,IAAI,GAAG,MAAM,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IAEzD,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC,CAAC;SACrC,GAAG,CAAC,UAAU,IAAI;QAClB,OAAO,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IAC3C,CAAC,CAAC;SACD,IAAI,CAAC,EAAE,CAAC,CAAC;AACZ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vdegenne/voicevox",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.18",
|
|
4
4
|
"description": "Snar reactive controller TS helper to interact with VoiceVox TTS server.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
8
|
-
"
|
|
9
|
-
"
|
|
8
|
+
"types": "./lib/client.d.ts",
|
|
9
|
+
"import": "./lib/client.js"
|
|
10
|
+
},
|
|
11
|
+
"./core": {
|
|
12
|
+
"types": "./lib/core.d.ts",
|
|
13
|
+
"import": "./lib/core.js"
|
|
10
14
|
}
|
|
11
15
|
},
|
|
12
16
|
"files": [
|
|
@@ -21,18 +25,15 @@
|
|
|
21
25
|
"clean": "if-file-deleted",
|
|
22
26
|
"files": [
|
|
23
27
|
"./tsconfig.json",
|
|
24
|
-
"./src"
|
|
28
|
+
"./src/"
|
|
25
29
|
],
|
|
26
30
|
"output": [
|
|
27
|
-
"./lib"
|
|
31
|
+
"./lib/"
|
|
28
32
|
]
|
|
29
33
|
}
|
|
30
34
|
},
|
|
31
35
|
"devDependencies": {
|
|
32
36
|
"typescript": "^7.0.2",
|
|
33
|
-
"wireit": "^0.
|
|
34
|
-
},
|
|
35
|
-
"dependencies": {
|
|
36
|
-
"@snar/lit": "^1.8.1"
|
|
37
|
+
"wireit": "^0.15.0"
|
|
37
38
|
}
|
|
38
39
|
}
|
package/lib/index.d.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { ReactiveController } from '@snar/lit';
|
|
2
|
-
interface VoicevoxStyle {
|
|
3
|
-
id: number;
|
|
4
|
-
name: string;
|
|
5
|
-
}
|
|
6
|
-
interface VoicevoxSpeaker {
|
|
7
|
-
name: string;
|
|
8
|
-
speaker_uuid: string;
|
|
9
|
-
styles: VoicevoxStyle[];
|
|
10
|
-
}
|
|
11
|
-
interface PlayOptions {
|
|
12
|
-
voiceId?: number;
|
|
13
|
-
speed?: number;
|
|
14
|
-
volume?: number;
|
|
15
|
-
}
|
|
16
|
-
export declare class VoicevoxClient extends ReactiveController {
|
|
17
|
-
state: 'disconnected' | 'connecting' | 'connection_error' | 'connected';
|
|
18
|
-
speakers: VoicevoxSpeaker[];
|
|
19
|
-
host: string;
|
|
20
|
-
port: number;
|
|
21
|
-
constructor({ host, port, }?: {
|
|
22
|
-
host?: string;
|
|
23
|
-
port?: number;
|
|
24
|
-
});
|
|
25
|
-
private get endpoint();
|
|
26
|
-
private cache;
|
|
27
|
-
private currentAudios;
|
|
28
|
-
private pendingPlays;
|
|
29
|
-
connect({ reconnect }?: {
|
|
30
|
-
reconnect?: boolean;
|
|
31
|
-
}): Promise<void>;
|
|
32
|
-
disconnect(): void;
|
|
33
|
-
play(sentence: string, { voiceId, speed, volume }?: PlayOptions): Promise<void>;
|
|
34
|
-
togglePlay(sentence: string, options?: PlayOptions): Promise<void>;
|
|
35
|
-
private fetchAudio;
|
|
36
|
-
private playBlob;
|
|
37
|
-
private stop;
|
|
38
|
-
private getCacheKey;
|
|
39
|
-
getRandomVoiceId(): number;
|
|
40
|
-
getVoiceTitles(): string[];
|
|
41
|
-
getVoiceTitleFromId(voiceId: number): string | undefined;
|
|
42
|
-
}
|
|
43
|
-
export {};
|
|
44
|
-
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,kBAAkB,EAAQ,MAAM,WAAW,CAAC;AAEpD,UAAU,aAAa;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;CACb;AAED,UAAU,eAAe;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;IACrB,MAAM,EAAE,aAAa,EAAE,CAAC;CACxB;AAED,UAAU,WAAW;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,MAAM,CAAC;CAChB;AAID,qBAAa,cAAe,SAAQ,kBAAkB;IAC5C,KAAK,EACb,cAAc,GAAG,YAAY,GAAG,kBAAkB,GAAG,WAAW,CACjD;IAEP,QAAQ,EAAE,eAAe,EAAE,CAAM;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IAEtB,YAAY,EACX,IAAkB,EAClB,IAAY,GACZ,GAAE;QACF,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,IAAI,CAAC,EAAE,MAAM,CAAC;KACT,EAKL;IAED,OAAO,KAAK,QAAQ,GAEnB;IAED,OAAO,CAAC,KAAK,CAAkC;IAE/C,OAAO,CAAC,aAAa,CAMjB;IAEJ,OAAO,CAAC,YAAY,CAAqB;IAEnC,OAAO,CAAC,EAAC,SAAiB,EAAC,GAAE;QAAC,SAAS,CAAC,EAAE,OAAO,CAAA;KAAM,iBAqB5D;IAED,UAAU,SAKT;IAEK,IAAI,CACT,QAAQ,EAAE,MAAM,EAChB,EAAC,OAAW,EAAE,KAAS,EAAE,MAAU,EAAC,GAAE,WAAgB,iBAkDtD;IAEK,UAAU,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,GAAE,WAAgB,iBAO3D;YAEa,UAAU;YAsCV,QAAQ;IAyDtB,OAAO,CAAC,IAAI;IA0BZ,OAAO,CAAC,WAAW;IAQnB,gBAAgB,WAQf;IAED,cAAc,aAUb;IAED,mBAAmB,CAAC,OAAO,EAAE,MAAM,sBAElC;CACD"}
|
package/lib/index.js
DELETED
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
|
-
import { ReactiveController, state } from '@snar/lit';
|
|
8
|
-
export class VoicevoxClient extends ReactiveController {
|
|
9
|
-
constructor({ host = '127.0.0.1', port = 50021, } = {}) {
|
|
10
|
-
super();
|
|
11
|
-
this.state = 'disconnected';
|
|
12
|
-
this.speakers = [];
|
|
13
|
-
this.cache = new Map();
|
|
14
|
-
this.currentAudios = new Map();
|
|
15
|
-
this.pendingPlays = new Set();
|
|
16
|
-
this.host = host;
|
|
17
|
-
this.port = port;
|
|
18
|
-
}
|
|
19
|
-
get endpoint() {
|
|
20
|
-
return `http://${this.host}:${this.port}`;
|
|
21
|
-
}
|
|
22
|
-
async connect({ reconnect = false } = {}) {
|
|
23
|
-
if (this.state === 'connected' && !reconnect) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
this.state = 'connecting';
|
|
27
|
-
try {
|
|
28
|
-
const response = await fetch(`${this.endpoint}/speakers`);
|
|
29
|
-
if (!response.ok) {
|
|
30
|
-
throw new Error(`VOICEVOX returned HTTP ${response.status}`);
|
|
31
|
-
}
|
|
32
|
-
this.speakers = await response.json();
|
|
33
|
-
this.state = 'connected';
|
|
34
|
-
}
|
|
35
|
-
catch (error) {
|
|
36
|
-
this.speakers = [];
|
|
37
|
-
this.state = 'connection_error';
|
|
38
|
-
throw error;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
disconnect() {
|
|
42
|
-
this.stop();
|
|
43
|
-
this.speakers = [];
|
|
44
|
-
this.state = 'disconnected';
|
|
45
|
-
}
|
|
46
|
-
async play(sentence, { voiceId = 0, speed = 1, volume = 1 } = {}) {
|
|
47
|
-
if (this.state !== 'connected') {
|
|
48
|
-
throw new Error('VOICEVOX is not connected');
|
|
49
|
-
}
|
|
50
|
-
const key = this.getCacheKey(sentence, voiceId, speed);
|
|
51
|
-
if (this.pendingPlays.has(key)) {
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
const current = this.currentAudios.get(key);
|
|
55
|
-
if (current) {
|
|
56
|
-
current.audio.currentTime = 0;
|
|
57
|
-
current.audio.volume = Math.max(0, Math.min(1, volume));
|
|
58
|
-
await current.audio.play();
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
this.pendingPlays.add(key);
|
|
62
|
-
try {
|
|
63
|
-
let cached = this.cache.get(key);
|
|
64
|
-
if (!cached) {
|
|
65
|
-
const promise = this.fetchAudio(sentence, voiceId, speed);
|
|
66
|
-
this.cache.set(key, promise);
|
|
67
|
-
try {
|
|
68
|
-
const audio = await promise;
|
|
69
|
-
this.cache.set(key, audio);
|
|
70
|
-
cached = audio;
|
|
71
|
-
}
|
|
72
|
-
catch (error) {
|
|
73
|
-
this.cache.delete(key);
|
|
74
|
-
throw error;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
const audio = cached instanceof Promise ? await cached : cached;
|
|
78
|
-
this.pendingPlays.delete(key);
|
|
79
|
-
await this.playBlob(audio, key, volume);
|
|
80
|
-
}
|
|
81
|
-
catch (error) {
|
|
82
|
-
this.pendingPlays.delete(key);
|
|
83
|
-
throw error;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
async togglePlay(sentence, options = {}) {
|
|
87
|
-
if (this.currentAudios.size > 0) {
|
|
88
|
-
this.stop();
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
return this.play(sentence, options);
|
|
92
|
-
}
|
|
93
|
-
async fetchAudio(sentence, voiceId, speed) {
|
|
94
|
-
const queryResponse = await fetch(`${this.endpoint}/audio_query?text=${encodeURIComponent(sentence)}&speaker=${voiceId}`, {
|
|
95
|
-
method: 'POST',
|
|
96
|
-
});
|
|
97
|
-
if (!queryResponse.ok) {
|
|
98
|
-
throw new Error(`VOICEVOX audio_query failed: HTTP ${queryResponse.status}`);
|
|
99
|
-
}
|
|
100
|
-
const query = await queryResponse.json();
|
|
101
|
-
query.speedScale = speed;
|
|
102
|
-
const synthesisResponse = await fetch(`${this.endpoint}/synthesis?speaker=${voiceId}`, {
|
|
103
|
-
method: 'POST',
|
|
104
|
-
headers: {
|
|
105
|
-
'Content-Type': 'application/json',
|
|
106
|
-
},
|
|
107
|
-
body: JSON.stringify(query),
|
|
108
|
-
});
|
|
109
|
-
if (!synthesisResponse.ok) {
|
|
110
|
-
throw new Error(`VOICEVOX synthesis failed: HTTP ${synthesisResponse.status}`);
|
|
111
|
-
}
|
|
112
|
-
return synthesisResponse.blob();
|
|
113
|
-
}
|
|
114
|
-
async playBlob(blob, key, volume) {
|
|
115
|
-
const url = URL.createObjectURL(blob);
|
|
116
|
-
const audio = new Audio(url);
|
|
117
|
-
audio.volume = Math.max(0, Math.min(1, volume));
|
|
118
|
-
this.currentAudios.set(key, {
|
|
119
|
-
audio,
|
|
120
|
-
resolve: () => { },
|
|
121
|
-
});
|
|
122
|
-
try {
|
|
123
|
-
await audio.play();
|
|
124
|
-
await new Promise((resolve, reject) => {
|
|
125
|
-
const current = this.currentAudios.get(key);
|
|
126
|
-
if (current?.audio === audio) {
|
|
127
|
-
current.resolve = resolve;
|
|
128
|
-
}
|
|
129
|
-
audio.addEventListener('ended', () => {
|
|
130
|
-
if (this.currentAudios.get(key)?.audio === audio) {
|
|
131
|
-
this.currentAudios.delete(key);
|
|
132
|
-
}
|
|
133
|
-
URL.revokeObjectURL(url);
|
|
134
|
-
resolve();
|
|
135
|
-
}, { once: true });
|
|
136
|
-
audio.addEventListener('error', () => {
|
|
137
|
-
if (this.currentAudios.get(key)?.audio === audio) {
|
|
138
|
-
this.currentAudios.delete(key);
|
|
139
|
-
}
|
|
140
|
-
URL.revokeObjectURL(url);
|
|
141
|
-
reject(new Error('Audio playback failed'));
|
|
142
|
-
}, { once: true });
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
catch (error) {
|
|
146
|
-
if (this.currentAudios.get(key)?.audio === audio) {
|
|
147
|
-
this.currentAudios.delete(key);
|
|
148
|
-
}
|
|
149
|
-
URL.revokeObjectURL(url);
|
|
150
|
-
throw error;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
stop(key) {
|
|
154
|
-
if (key) {
|
|
155
|
-
const current = this.currentAudios.get(key);
|
|
156
|
-
if (!current) {
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
current.audio.pause();
|
|
160
|
-
current.audio.currentTime = 0;
|
|
161
|
-
this.currentAudios.delete(key);
|
|
162
|
-
current.resolve();
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
for (const current of this.currentAudios.values()) {
|
|
166
|
-
current.audio.pause();
|
|
167
|
-
current.audio.currentTime = 0;
|
|
168
|
-
current.resolve();
|
|
169
|
-
}
|
|
170
|
-
this.currentAudios.clear();
|
|
171
|
-
}
|
|
172
|
-
getCacheKey(sentence, voiceId, speed) {
|
|
173
|
-
return `${voiceId}:${speed}:${sentence}`;
|
|
174
|
-
}
|
|
175
|
-
getRandomVoiceId() {
|
|
176
|
-
const styles = this.speakers.flatMap((speaker) => speaker.styles);
|
|
177
|
-
if (styles.length === 0) {
|
|
178
|
-
throw new Error('VOICEVOX has no available voices');
|
|
179
|
-
}
|
|
180
|
-
return styles[Math.floor(Math.random() * styles.length)].id;
|
|
181
|
-
}
|
|
182
|
-
getVoiceTitles() {
|
|
183
|
-
const titles = [];
|
|
184
|
-
for (const speaker of this.speakers) {
|
|
185
|
-
for (const style of speaker.styles) {
|
|
186
|
-
titles[style.id] = `${speaker.name} - ${style.name}`;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
return titles;
|
|
190
|
-
}
|
|
191
|
-
getVoiceTitleFromId(voiceId) {
|
|
192
|
-
return this.getVoiceTitles()[voiceId];
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
__decorate([
|
|
196
|
-
state()
|
|
197
|
-
], VoicevoxClient.prototype, "state", void 0);
|
|
198
|
-
__decorate([
|
|
199
|
-
state()
|
|
200
|
-
], VoicevoxClient.prototype, "speakers", void 0);
|
|
201
|
-
__decorate([
|
|
202
|
-
state()
|
|
203
|
-
], VoicevoxClient.prototype, "host", void 0);
|
|
204
|
-
__decorate([
|
|
205
|
-
state()
|
|
206
|
-
], VoicevoxClient.prototype, "port", void 0);
|
|
207
|
-
// export const voicevox = new VoicevoxClient();
|
|
208
|
-
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
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