@andresaya/edge-tts 1.2.1 → 1.2.5

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 CHANGED
@@ -1,95 +1,91 @@
1
- # Edge TTS
2
-
3
- **Edge TTS** is a powerful Text-to-Speech (TTS) package that leverages Microsoft's Edge capabilities. This package allows you to synthesize speech from text and manage voice options easily through a command-line interface (CLI).
4
-
5
- ## Features
6
-
7
- - **Text-to-Speech**: Convert text into natural-sounding speech using Microsoft Edge's TTS capabilities.
8
- - **Multiple Voices**: Access a variety of voices to suit your project's needs.
9
- - **Audio Export Options**: Export synthesized audio in different formats (raw, base64, or directly to a file).
10
- - **Command-Line Interface**: Use a simple CLI for easy access to functionality.
11
- - **Easy Integration**: Modular structure allows for easy inclusion in existing projects.
12
-
13
- ## Installation
14
-
15
- You can install Edge TTS via npm. Run the following command in your terminal:
16
-
17
- ```bash
18
- bun add @andresaya/edge-tts
19
- ```
20
- ```bash
21
- npm install @andresaya/edge-tts
22
- ```
23
-
24
-
25
- ## Usage
26
- Command-Line Interface
27
-
28
- You can install Edge TTS via npm. bun Run the following command in your terminal:
29
-
30
- ```bash
31
- npm install -g @andresaya/edge-tts
32
- ```
33
-
34
- ```bash
35
- bun install --global edge-tts
36
- ```
37
-
38
- To synthesize speech from text, use the following command:
39
- ```bash
40
- edge-tts synthesize -t "Hello, world!" -o hello_world_audio
41
- ```
42
-
43
- To list available voices, run:
44
-
45
- ```bash
46
- edge-tts voice-list
47
- ```
48
-
49
- Integration into Your Project
50
- To use Edge TTS in your Bun project, you can import it like this:
51
-
52
- ```js
53
- import { EdgeTTS } from '@andresaya/edge-tts';
54
-
55
- // Initialize the EdgeTTS service
56
- const tts = new EdgeTTS();
57
-
58
- // Get voices
59
- const voices = await tts.getVoices();
60
- console.log(voices); // Display available voices
61
-
62
- // Synthesize text with options for voice, rate, volume, and pitch
63
- await tts.synthesize("Hello, world!", 'en-US-AriaNeural', {
64
- rate: '0%', // Speech rate (range: -100% to 100%)
65
- volume: '0%', // Speech volume (range: -100% to 100%)
66
- pitch: '0Hz' // Voice pitch (range: -100Hz to 100Hz)
67
- });
68
-
69
- // Export synthesized audio in different formats
70
- tts.toBase64(); // Get audio as base64
71
- await tts.toFile("output_audio"); // Save audio to file
72
- await tts.toRaw(); // Get raw audio buffer
73
- ```
74
-
75
- ## Export Options
76
- After synthesizing speech, you can export the audio in various formats:
77
-
78
- - `toBase64`: Returns the audio as a Base64 string.
79
- - `toFile`: Saves the audio to a specified file (e.g., "output.wav").
80
- - `toRaw`: Returns the raw audio stream.
81
-
82
- ## PHP Version
83
- If you want to use Edge TTS with PHP, you can check out the PHP version of this package, [Edge TTS PHP](https://github.com/andresayac/edge-tts-php)
84
-
85
-
86
- ## License
87
- This project is licensed under the GNU General Public License v3 (GPLv3).
88
-
89
- ## Acknowledgments
90
-
91
- We would like to extend our gratitude to the developers and contributors of the following projects for their inspiration and groundwork:
92
-
93
- * https://github.com/rany2/edge-tts/tree/master/examples
94
- * https://github.com/rany2/edge-tts/blob/master/src/edge_tts/util.py
1
+ # Edge TTS
2
+
3
+ **Edge TTS** is a powerful Text-to-Speech (TTS) package that leverages Microsoft's Edge capabilities. This package allows you to synthesize speech from text and manage voice options easily through a command-line interface (CLI).
4
+
5
+ ## Features
6
+
7
+ - **Text-to-Speech**: Convert text into natural-sounding speech using Microsoft Edge's TTS capabilities.
8
+ - **Multiple Voices**: Access a variety of voices to suit your project's needs.
9
+ - **Audio Export Options**: Export synthesized audio in different formats (raw, base64, or directly to a file).
10
+ - **Command-Line Interface**: Use a simple CLI for easy access to functionality.
11
+ - **Easy Integration**: Modular structure allows for easy inclusion in existing projects.
12
+
13
+ ## Installation
14
+
15
+ You can install Edge TTS via npm. Run the following command in your terminal:
16
+
17
+ ```bash
18
+ bun add @andresaya/edge-tts
19
+ ```
20
+ ```bash
21
+ npm install @andresaya/edge-tts
22
+ ```
23
+
24
+
25
+ ## Usage
26
+ Command-Line Interface
27
+
28
+ You can install Edge TTS via npm. bun Run the following command in your terminal:
29
+
30
+ ```bash
31
+ npm install -g @andresaya/edge-tts
32
+ ```
33
+
34
+ To synthesize speech from text, use the following command:
35
+ ```bash
36
+ edge-tts synthesize -t "Hello, world!" -o hello_world_audio
37
+ ```
38
+
39
+ To list available voices, run:
40
+
41
+ ```bash
42
+ edge-tts voice-list
43
+ ```
44
+
45
+ Integration into Your Project
46
+ To use Edge TTS in your Bun project, you can import it like this:
47
+
48
+ ```js
49
+ import { EdgeTTS } from '@andresaya/edge-tts';
50
+
51
+ // Initialize the EdgeTTS service
52
+ const tts = new EdgeTTS();
53
+
54
+ // Get voices
55
+ const voices = await tts.getVoices();
56
+ console.log(voices); // Display available voices
57
+
58
+ // Synthesize text with options for voice, rate, volume, and pitch
59
+ await tts.synthesize("Hello, world!", 'en-US-AriaNeural', {
60
+ rate: '0%', // Speech rate (range: -100% to 100%)
61
+ volume: '0%', // Speech volume (range: -100% to 100%)
62
+ pitch: '0Hz' // Voice pitch (range: -100Hz to 100Hz)
63
+ });
64
+
65
+ // Export synthesized audio in different formats
66
+ tts.toBase64(); // Get audio as base64
67
+ await tts.toFile("output_audio"); // Save audio to file
68
+ await tts.toRaw(); // Get raw audio buffer
69
+ ```
70
+
71
+ ## Export Options
72
+ After synthesizing speech, you can export the audio in various formats:
73
+
74
+ - `toBase64`: Returns the audio as a Base64 string.
75
+ - `toFile`: Saves the audio to a specified file (e.g., "output.wav").
76
+ - `toRaw`: Returns the raw audio stream.
77
+
78
+ ## PHP Version
79
+ If you want to use Edge TTS with PHP, you can check out the PHP version of this package, [Edge TTS PHP](https://github.com/andresayac/edge-tts-php)
80
+
81
+
82
+ ## License
83
+ This project is licensed under the GNU General Public License v3 (GPLv3).
84
+
85
+ ## Acknowledgments
86
+
87
+ We would like to extend our gratitude to the developers and contributors of the following projects for their inspiration and groundwork:
88
+
89
+ * https://github.com/rany2/edge-tts/tree/master/examples
90
+ * https://github.com/rany2/edge-tts/blob/master/src/edge_tts/util.py
95
91
  * https://github.com/hasscc/hass-edge-tts/blob/main/custom_components/edge_tts/tts.py
package/package.json CHANGED
@@ -1,34 +1,47 @@
1
- {
2
- "name": "@andresaya/edge-tts",
3
- "version": "1.2.1",
4
- "main": "dist/index.js",
5
-
6
-
7
- "scripts": {
8
- "build": "tsc",
9
- "build:bin": "bun scripts/build.ts"
10
- },
11
- "devDependencies": {
12
- "@types/bun": "latest",
13
- "typescript": "^5.0.0"
14
- },
15
- "dependencies": {
16
- "@types/uuid": "^10.0.0",
17
- "commander": "^12.1.0",
18
- "events": "^3.3.0",
19
- "fs": "^0.0.1-security",
20
- "ws": "^8.18.0"
21
- },
22
- "peerDependencies": {
23
- "typescript": "^5.0.0"
24
- },
25
- "files": [
26
- "dist"
27
- ],
28
- "bin": {
29
- "edge-tts": "./dist/cli/edge-tts.js"
30
- },
31
- "engines": {
32
- "bun": ">=0.5.0"
33
- }
1
+ {
2
+ "name": "@andresaya/edge-tts",
3
+ "version": "1.2.5",
4
+ "description": "Edge TTS is a package that allows access to the online text-to-speech service used by Microsoft Edge without the need for Microsoft Edge, Windows, or an API key.",
5
+ "main": "dist/index.js",
6
+ "keywords": [
7
+ "edge",
8
+ "tts",
9
+ "text-to-speech",
10
+ "microsoft",
11
+ "microsoft edge",
12
+ "speech-synthesis"
13
+ ],
14
+ "author": "Andres Aya",
15
+ "license": "GPL-3.0-only",
16
+ "homepage": "https://github.com/andresayac/edge-tts",
17
+ "bugs": {
18
+ "url": "https://github.com/andresayac/edge-tts/issues"
19
+ },
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "build:bin": "bun scripts/build.ts"
23
+ },
24
+ "devDependencies": {
25
+ "@types/bun": "latest",
26
+ "typescript": "^5.6.2"
27
+ },
28
+ "dependencies": {
29
+ "@types/uuid": "^10.0.0",
30
+ "commander": "^12.1.0",
31
+ "events": "^3.3.0",
32
+ "fs": "^0.0.1-security",
33
+ "ws": "^8.18.0"
34
+ },
35
+ "peerDependencies": {
36
+ "typescript": "^5.0.0"
37
+ },
38
+ "files": [
39
+ "dist"
40
+ ],
41
+ "bin": {
42
+ "edge-tts": "./dist/cli/edge-tts.js"
43
+ },
44
+ "engines": {
45
+ "bun": ">=0.5.0"
46
+ }
34
47
  }
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- export {};
@@ -1,18 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- const SynthesizeCommand_1 = require("../commands/SynthesizeCommand");
5
- const VoiceListCommand_1 = require("../commands/VoiceListCommand");
6
- const commander_1 = require("commander");
7
- const program = new commander_1.Command();
8
- program
9
- .version('1.2.0')
10
- .addCommand(SynthesizeCommand_1.SynthesizeCommand)
11
- .addCommand(VoiceListCommand_1.VoiceListCommand);
12
- try {
13
- program.parse();
14
- }
15
- catch (error) {
16
- console.error(`Error: ${error.message}`);
17
- process.exit(1);
18
- }
@@ -1,2 +0,0 @@
1
- import { Command } from 'commander';
2
- export declare const SynthesizeCommand: Command;
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SynthesizeCommand = void 0;
4
- const commander_1 = require("commander");
5
- const EdgeTTS_1 = require("../services/EdgeTTS");
6
- exports.SynthesizeCommand = new commander_1.Command('synthesize')
7
- .description('Edge TTS: synthesize text to audio')
8
- .option('-t, --text <text>', 'Text to convert to audio')
9
- .option('-v, --voice [voice]', 'Voice to use for the audio synthesis', 'en-US-AriaNeural')
10
- .option('-r, --rate [rate]', 'Rate of speech', '0%')
11
- .option('-l, --volume [volume]', 'Volume of speech', '0%')
12
- .option('-p, --pitch [pitch]', 'Pitch of speech', '0Hz')
13
- .option('-o, --output [output]', 'Output file name', `output_${Date.now()}`)
14
- .action(async (options) => {
15
- const { text, voice, pitch, rate, volume, output } = options;
16
- if (!text) {
17
- console.error('Text is required');
18
- process.exit(1);
19
- }
20
- const tts = new EdgeTTS_1.EdgeTTS();
21
- await tts.synthesize(text, voice, { pitch, rate, volume });
22
- await tts.toFile(`${output}`);
23
- console.log(`Audio file generated: ${output}.mp3`);
24
- });
@@ -1,2 +0,0 @@
1
- import { Command } from 'commander';
2
- export declare const VoiceListCommand: Command;
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.VoiceListCommand = void 0;
4
- const commander_1 = require("commander");
5
- const EdgeTTS_1 = require("../services/EdgeTTS");
6
- exports.VoiceListCommand = new commander_1.Command('voice-list')
7
- .description('Get the list of available voices')
8
- .action(async () => {
9
- const tts = new EdgeTTS_1.EdgeTTS();
10
- const voices = await tts.getVoices();
11
- console.log('Lista de voces disponibles:');
12
- voices.forEach((voice) => {
13
- console.log(` - ${voice.ShortName}`);
14
- });
15
- });
@@ -1,5 +0,0 @@
1
- export declare const Constants: {
2
- TRUSTED_CLIENT_TOKEN: string;
3
- WSS_URL: string;
4
- VOICES_URL: string;
5
- };
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Constants = void 0;
4
- exports.Constants = {
5
- TRUSTED_CLIENT_TOKEN: '6A5AA1D4EAFF4E9FB37E23D68491D6F4',
6
- WSS_URL: 'wss://speech.platform.bing.com/consumer/speech/synthesize/readaloud/edge/v1',
7
- VOICES_URL: 'https://speech.platform.bing.com/consumer/speech/synthesize/readaloud/voices/list',
8
- };
package/dist/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export { EdgeTTS } from './services/EdgeTTS';
package/dist/index.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EdgeTTS = void 0;
4
- var EdgeTTS_1 = require("./services/EdgeTTS");
5
- Object.defineProperty(exports, "EdgeTTS", { enumerable: true, get: function () { return EdgeTTS_1.EdgeTTS; } });
@@ -1,17 +0,0 @@
1
- export declare class EdgeTTS {
2
- private audio_stream;
3
- private audio_format;
4
- private ws;
5
- getVoices(): Promise<any[]>;
6
- private generateUUID;
7
- private validatePitch;
8
- private validateRate;
9
- private validateVolume;
10
- synthesize(text: string, voice?: string, options?: any): Promise<void>;
11
- private getSSML;
12
- private buildTTSConfigMessage;
13
- private processAudioData;
14
- toFile(outputPath: string): Promise<void>;
15
- toRaw(): string;
16
- toBase64(): string;
17
- }
@@ -1,118 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.EdgeTTS = void 0;
7
- const ws_1 = __importDefault(require("ws"));
8
- const constants_1 = require("../config/constants");
9
- const fs_1 = require("fs");
10
- class EdgeTTS {
11
- audio_stream = [];
12
- audio_format = 'mp3';
13
- ws;
14
- async getVoices() {
15
- const response = await fetch(`${constants_1.Constants.VOICES_URL}?trustedclienttoken=${constants_1.Constants.TRUSTED_CLIENT_TOKEN}`);
16
- const data = await response.json();
17
- return data.map((voice) => {
18
- delete voice.VoiceTag;
19
- delete voice.SuggestedCodec;
20
- delete voice.Status;
21
- return voice;
22
- });
23
- }
24
- generateUUID() {
25
- return 'xxxxxxxx-xxxx-xxxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
26
- const r = Math.random() * 16 | 0;
27
- const v = c === 'x' ? r : (r & 0x3 | 0x8);
28
- return v.toString(16);
29
- });
30
- }
31
- validatePitch(pitch) {
32
- if (!/^(-?\d{1,3}Hz)$/.test(pitch)) {
33
- throw new Error("Invalid pitch format. Expected format: '-100Hz to 100Hz'.");
34
- }
35
- return pitch;
36
- }
37
- validateRate(rate) {
38
- if (!/^(-?\d{1,3}%)$/.test(rate)) {
39
- throw new Error("Invalid rate format. Expected format: '-100% to 100%'.");
40
- }
41
- return rate;
42
- }
43
- validateVolume(volume) {
44
- if (!/^(-?\d{1,3}%)$/.test(volume)) {
45
- throw new Error("Invalid volume format. Expected format: '-100% to 100%'.");
46
- }
47
- return volume;
48
- }
49
- async synthesize(text, voice = 'en-US-AnaNeural', options = {}) {
50
- return new Promise((resolve, reject) => {
51
- const req_id = this.generateUUID();
52
- this.ws = new ws_1.default(`${constants_1.Constants.WSS_URL}?trustedclienttoken=${constants_1.Constants.TRUSTED_CLIENT_TOKEN}&ConnectionId=${req_id}`);
53
- const SSML_text = this.getSSML(text, voice, options);
54
- this.ws.on('open', () => {
55
- const message = this.buildTTSConfigMessage();
56
- this.ws.send(message);
57
- const speechMessage = `X-RequestId:${req_id}\r\nContent-Type:application/ssml+xml\r\nX-Timestamp:${new Date().toISOString()}Z\r\nPath:ssml\r\n\r\n${SSML_text}`;
58
- this.ws.send(speechMessage);
59
- });
60
- this.ws.on('message', (data) => {
61
- this.processAudioData(data);
62
- });
63
- this.ws.on('close', () => {
64
- resolve();
65
- });
66
- this.ws.on('error', (err) => {
67
- reject(err);
68
- });
69
- });
70
- }
71
- getSSML(text, voice, options = {}) {
72
- options.pitch = options.pitch.replace('hz', 'Hz');
73
- const pitch = this.validatePitch(options.pitch || '0Hz');
74
- const rate = this.validateRate(options.rate || '0%');
75
- const volume = this.validateVolume(options.volume || '0%');
76
- return `<speak version='1.0' xml:lang='en-US'><voice name='${voice}'><prosody pitch='${pitch}' rate='${rate}' volume='${volume}'>${text}</prosody></voice></speak>`;
77
- }
78
- buildTTSConfigMessage() {
79
- return `X-Timestamp:${new Date().toISOString()}Z\r\nContent-Type:application/json; charset=utf-8\r\nPath:speech.config\r\n\r\n` +
80
- `{"context":{"synthesis":{"audio":{"metadataoptions":{"sentenceBoundaryEnabled":false,"wordBoundaryEnabled":true},"outputFormat":"audio-24khz-48kbitrate-mono-mp3"}}}}`;
81
- }
82
- processAudioData(data) {
83
- if (Buffer.isBuffer(data)) {
84
- const needle = Buffer.from("Path:audio\r\n");
85
- const uint8Data = new Uint8Array(data);
86
- const start_ind = uint8Data.indexOf(needle[0]);
87
- if (start_ind !== -1) {
88
- const audioData = data.subarray(start_ind + needle.length);
89
- this.audio_stream.push(audioData);
90
- }
91
- if (data.includes("Path:turn.end")) {
92
- this.ws.close();
93
- }
94
- }
95
- }
96
- async toFile(outputPath) {
97
- if (this.audio_stream.length) {
98
- const audioBuffer = Buffer.concat(this.audio_stream);
99
- const uint8Array = new Uint8Array(audioBuffer);
100
- (0, fs_1.writeFileSync)(`${outputPath}.${this.audio_format}`, uint8Array);
101
- }
102
- else {
103
- throw new Error("No audio data available to save.");
104
- }
105
- }
106
- toRaw() {
107
- if (this.audio_stream.length === 0) {
108
- throw new Error("No audio data available.");
109
- }
110
- const audioBuffer = Buffer.concat(this.audio_stream);
111
- return audioBuffer.toString('base64');
112
- }
113
- toBase64() {
114
- const audioBuffer = Buffer.concat(this.audio_stream);
115
- return audioBuffer.toString('base64');
116
- }
117
- }
118
- exports.EdgeTTS = EdgeTTS;