### Install node-edge-tts using npm Source: https://github.com/schneehertz/node-edge-tts/blob/master/README.md This snippet shows the command to install the node-edge-tts module using npm. It's a prerequisite for using the library in a Node.js project. ```bash npm install node-edge-tts ``` -------------------------------- ### Command Line Interface for Text-to-Speech Source: https://context7.com/schneehertz/node-edge-tts/llms.txt Illustrates how to use the node-edge-tts module as a command-line tool to convert text to speech. Examples include basic usage, specifying voice and language, custom parameters, subtitle generation, and using a proxy. ```bash # Basic usage with default Chinese voice npx node-edge-tts -t 'Hello world' # Specify voice, language, and output file npx node-edge-tts -t 'Hello world' -v 'en-US-AriaNeural' -l 'en-US' -f './hello.mp3' # With custom parameters and subtitle generation npx node-edge-tts \ -t 'Welcome to the application' \ -f './welcome.mp3' \ -v 'en-GB-SoniaNeural' \ -l 'en-GB' \ -o 'audio-24khz-96kbitrate-mono-mp3' \ --pitch '+5%' \ --rate '+10%' \ --volume '-20%' \ -s true \ --timeout 15000 # Using proxy npx node-edge-tts -t 'Hello world' -p 'http://localhost:7890' -f './output.mp3' ``` -------------------------------- ### Use node-edge-tts from the command line Source: https://github.com/schneehertz/node-edge-tts/blob/master/README.md This snippet demonstrates how to use node-edge-tts directly from the command line using npx. It allows quick conversion of text to speech without a Node.js project setup. The '-t' flag specifies the text to convert. ```bash npx node-edge-tts -t 'Hello world' ``` -------------------------------- ### ES Module Import for EdgeTTS Source: https://context7.com/schneehertz/node-edge-tts/llms.txt Demonstrates how to import and use the EdgeTTS class in modern JavaScript/TypeScript projects using ES module syntax. This example shows setting up the TTS instance and a function for converting text to speech. ```typescript import { EdgeTTS } from 'node-edge-tts'; import path from 'path'; const tts = new EdgeTTS({ voice: 'en-US-JennyNeural', lang: 'en-US', outputFormat: 'audio-24khz-48kbitrate-mono-mp3' }); async function convertTextToSpeech(text: string, filename: string) { try { await tts.ttsPromise(text, path.resolve(__dirname, filename)); return { success: true, path: filename }; } catch (error) { return { success: false, error: error.message }; } } // Usage await convertTextToSpeech('Hello from TypeScript', 'output.mp3'); ``` -------------------------------- ### Example subtitle JSON format Source: https://github.com/schneehertz/node-edge-tts/blob/master/README.md This snippet displays the JSON structure for subtitle files generated by node-edge-tts. Each object in the array represents a segment of speech, including the text ('part') and its start and end timestamps in milliseconds. This format is useful for synchronizing captions with audio. ```json [ { "part": "node-edge-tts ", "start": 100, "end": 1287 }, { "part": "is ", "start": 1287, "end": 1450 }, { "part": "a ", "start": 1450, "end": 1500 }, { "part": "module ", "start": 1500, "end": 2037 }, { "part": "that ", "start": 2037, "end": 2350 }, { "part": "utilizes ", "start": 2350, "end": 3162 }, { "part": "Microsoft ", "start": 3162, "end": 3762 }, { "part": "Edge's ", "start": 3762, "end": 4212 }, { "part": "online ", "start": 4212, "end": 4750 }, { "part": "TTS (", "start": 4750, "end": 5450 }, { "part": "Text-to-Speech) ", "start": 5600, "end": 6637 }, { "part": "service ", "start": 6800, "end": 7387 }, { "part": "on ", "start": 7387, "end": 7600 }, { "part": "Node.", "start": 7600, "end": 7950 }, { "part": "js", "start": 8012, "end": 8762 } ] ``` -------------------------------- ### Error Handling for Text-to-Speech with node-edge-tts Source: https://context7.com/schneehertz/node-edge-tts/llms.txt Implement robust error handling for TTS operations, managing connection failures, timeouts, and invalid configurations. This example provides a `safeTTSConversion` function to catch and report errors gracefully. ```javascript const { EdgeTTS } = require('node-edge-tts'); const tts = new EdgeTTS({ voice: 'en-US-AriaNeural', lang: 'en-US', timeout: 5000 }); async function safeTTSConversion(text, outputPath) { try { await tts.ttsPromise(text, outputPath); return { success: true, message: `Audio saved to ${outputPath}` }; } catch (error) { if (error === 'Timed out') { return { success: false, error: 'timeout', message: 'Request timed out. Try increasing timeout or check network connection.' }; } else if (error.code === 'ECONNREFUSED') { return { success: false, error: 'connection', message: 'Could not connect to TTS service. Check network or proxy settings.' }; } else { return { success: false, error: 'unknown', message: `TTS conversion failed: ${error.message || error}` }; } } } (async () => { const result = await safeTTSConversion('Test message', './test.mp3'); if (result.success) { console.log('Success:', result.message); } else { console.error('Error:', result.message); // Implement retry logic or fallback mechanism } })(); ``` -------------------------------- ### Customize Voice Characteristics with node-edge-tts Source: https://context7.com/schneehertz/node-edge-tts/llms.txt Adjust voice pitch, speaking rate, and volume to create distinct speech effects. This example demonstrates creating robotic and excited voice profiles using the EdgeTTS class. ```javascript const { EdgeTTS } = require('node-edge-tts'); // Create robotic voice effect (lower pitch, slower rate) const robotTTS = new EdgeTTS({ voice: 'en-US-GuyNeural', lang: 'en-US', pitch: '-30%', rate: '-20%', volume: '+0%' }); // Create excited voice effect (higher pitch, faster rate) const excitedTTS = new EdgeTTS({ voice: 'en-US-JennyNeural', lang: 'en-US', pitch: '+15%', rate: '+30%', volume: '+20%' }); (async () => { const text = 'The quick brown fox jumps over the lazy dog'; await robotTTS.ttsPromise(text, './robot_voice.mp3'); console.log('Robot voice created'); await excitedTTS.ttsPromise(text, './excited_voice.mp3'); console.log('Excited voice created'); })(); ``` -------------------------------- ### Convert Text to Speech and Save Audio Asynchronously Source: https://context7.com/schneehertz/node-edge-tts/llms.txt Shows how to convert text to speech and save the resulting audio file asynchronously using the ttsPromise method. This example includes optional subtitle generation in JSON format and error handling. ```javascript const { EdgeTTS } = require('node-edge-tts'); const path = require('path'); const tts = new EdgeTTS({ voice: 'pt-BR-ThalitaNeural', lang: 'pt-BR', outputFormat: 'audio-24khz-96kbitrate-mono-mp3', saveSubtitles: true, timeout: 20000 }); (async () => { try { const text = `No Brasil, a diversidade cultural é uma das características mais marcantes. Com influências indígenas, africanas e europeias, o país desenvolveu uma mistura única de tradições.`; const outputPath = path.join(__dirname, 'output.mp3'); await tts.ttsPromise(text, outputPath); console.log('Audio file created successfully'); // If saveSubtitles is true, a JSON file is created at output.mp3.json } catch (error) { console.error('TTS conversion failed:', error); } })(); ``` -------------------------------- ### Subtitle Generation with Word-Level Timing Source: https://context7.com/schneehertz/node-edge-tts/llms.txt Explains how to generate subtitles with word-level timing information alongside audio files using the `saveSubtitles` option. The example shows reading the generated JSON subtitle file and accessing timing data for each word. ```javascript const { EdgeTTS } = require('node-edge-tts'); const fs = require('fs'); const tts = new EdgeTTS({ voice: 'zh-CN-XiaoyiNeural', lang: 'zh-CN', saveSubtitles: true }); (async () => { const text = 'node-edge-tts is a module that utilizes Microsoft Edge online TTS service'; await tts.ttsPromise(text, './output.mp3'); // Read the generated subtitle file const subtitles = JSON.parse(fs.readFileSync('./output.mp3.json', 'utf-8')); // Subtitle format: // [ // { "part": "node-edge-tts ", "start": 100, "end": 1287 }, // { "part": "is ", "start": 1287, "end": 1450 }, // { "part": "a ", "start": 1450, "end": 1500 }, // { "part": "module ", "start": 1500, "end": 2037 } // ] // Times are in milliseconds console.log(`Generated ${subtitles.length} subtitle entries`); subtitles.forEach(sub => { console.log(`"${sub.part}" from ${sub.start}ms to ${sub.end}ms`); }); })(); ``` -------------------------------- ### Initialize EdgeTTS Instance with Configuration Source: https://context7.com/schneehertz/node-edge-tts/llms.txt Demonstrates how to create a new EdgeTTS instance with custom voice, language, output format, and other speech synthesis parameters. It also shows creating an instance with default settings. ```typescript const { EdgeTTS } = require('node-edge-tts'); // Create instance with custom configuration const tts = new EdgeTTS({ voice: 'en-US-AriaNeural', lang: 'en-US', outputFormat: 'audio-24khz-96kbitrate-mono-mp3', saveSubtitles: true, proxy: 'http://localhost:7890', pitch: '-10%', rate: '+10%', volume: '-50%', timeout: 10000 }); // Create instance with defaults (Chinese voice) const defaultTTS = new EdgeTTS(); ``` -------------------------------- ### Command line usage options for node-edge-tts Source: https://github.com/schneehertz/node-edge-tts/blob/master/README.md This section outlines the available command-line options for node-edge-tts. It includes flags for text input, output file path, voice selection, language, output format, pitch, rate, volume, subtitle saving, proxy configuration, and request timeout. Default values are provided for most options. ```bash Usage: npx node-edge-tts [options] Options: --help Show help [boolean] --version Show version number [boolean] -t, --text The text to be converted to speech [string] [required] -f, --filepath The output file path [string] [default: "./output.mp3"] -v, --voice The voice to be used [string] [default: "zh-CN-XiaoyiNeural"] -l, --lang The language to be used [string] [default: "zh-CN"] -o, --outputFormat The output format [string] [default: "audio-24khz-48kbitrate-mono-mp3"] --pitch The pitch of the voice [string] [default: "default"] -r, --rate The rate of the voice [string] [default: "default"] --volume The volume of the voice [string] [default: "default"] -s, --saveSubtitles Whether to save subtitles [boolean] [default: false] -p, --proxy example: http://localhost:7890 [string] --timeout The timeout of the request [number] [default: 10000] Examples: npx node-edge-tts -t 'Hello world' -f './output.mp3' ``` -------------------------------- ### Configure EdgeTTS options Source: https://github.com/schneehertz/node-edge-tts/blob/master/README.md This snippet shows how to instantiate the EdgeTTS class with various configuration options. These options allow customization of voice, language, output format, subtitle saving, proxy, pitch, rate, volume, and timeout. The comments provide links to Microsoft's documentation for available speech configurations, noting potential limitations. ```javascript const tts = new EdgeTTS({ voice: 'en-US-AriaNeural', lang: 'en-US', outputFormat: 'audio-24khz-96kbitrate-mono-mp3', saveSubtitles: true, proxy: 'http://localhost:7890', pitch: '-10%', rate: '+10%', volume: '-50%', timeout: 10000 }) ``` -------------------------------- ### Basic module usage of node-edge-tts Source: https://github.com/schneehertz/node-edge-tts/blob/master/README.md This snippet illustrates the basic usage of the EdgeTTS class in a Node.js project. It involves creating an instance of EdgeTTS and then calling the ttsPromise method to convert text to speech and save it to a file. The 'await' keyword indicates that this is an asynchronous operation. ```javascript const tts = new EdgeTTS() await tts.ttsPromise('Hello world', path_to_audiofile_with_extension) ``` -------------------------------- ### Import EdgeTTS class in Node.js (ES Modules) Source: https://github.com/schneehertz/node-edge-tts/blob/master/README.md This snippet demonstrates how to import the EdgeTTS class using the ES Modules (import/export) syntax. This is the modern standard for JavaScript modules and is used in newer Node.js projects. ```javascript import { EdgeTTS } from 'node-edge-tts' ``` -------------------------------- ### Multi-Language Speech Generation with node-edge-tts Source: https://context7.com/schneehertz/node-edge-tts/llms.txt Generate speech in multiple languages using region-specific neural voices for natural pronunciation. This snippet shows how to configure and use EdgeTTS for different languages. ```javascript const { EdgeTTS } = require('node-edge-tts'); const languages = [ { lang: 'en-US', voice: 'en-US-AriaNeural', text: 'Hello, how are you?' }, { lang: 'zh-CN', voice: 'zh-CN-XiaoyiNeural', text: '你好,你好吗?' }, { lang: 'es-ES', voice: 'es-ES-ElviraNeural', text: '¿Hola, cómo estás?' }, { lang: 'fr-FR', voice: 'fr-FR-DeniseNeural', text: 'Bonjour, comment allez-vous?' }, { lang: 'de-DE', voice: 'de-DE-KatjaNeural', text: 'Hallo, wie geht es dir?' }, { lang: 'ja-JP', voice: 'ja-JP-NanamiNeural', text: 'こんにちは、お元気ですか?' } ]; (async () => { for (const config of languages) { const tts = new EdgeTTS({ voice: config.voice, lang: config.lang, outputFormat: 'audio-24khz-48kbitrate-mono-mp3' }); const filename = `./greeting_${config.lang}.mp3`; await tts.ttsPromise(config.text, filename); console.log(`Created ${filename}`); } })(); ``` -------------------------------- ### Import EdgeTTS class in Node.js (CommonJS) Source: https://github.com/schneehertz/node-edge-tts/blob/master/README.md This snippet shows how to import the EdgeTTS class using the CommonJS module system in Node.js. This is the standard way to include modules in older Node.js projects. ```javascript const { EdgeTTS } = require('node-edge-tts') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.