say.js Documentation

repository·master·Indexed 23 days ago

https://github.com/marak/say.js

A text-to-speech (TTS) module for Node.js that provides a unified API to speak text or export audio files across macOS, Windows, and Linux. It supports custom voices, speed control, and WAV file export on macOS and Windows. Linux support requires the Festival speech synthesis system, and Windows requires PowerShell.

Tokens
1.2K
Snippets
5
Records
11
Agent score
31%

What's inside say.js

  1. Configure macOS voices

    master

    In macOS, voices are associated with different localities. You can list all available voices and their localities using the system say command in your terminal:

    say -v "?"

    Example voices: Alex (default) or Samantha (Siri).

  2. Use say.js for text-to-speech

    master

    You can use say.js to speak text using the system's default voice and speed, or specify a custom voice and speed. You can also override the platform detection manually if needed.

    To use the default settings:

    const say = require('say')
    say.speak('Hello!')

    To use a specific voice and speed (where 1 is 100%, 0.5 is 50%, etc.):

    say.speak("What's up, dog?", 'Alex', 0.5)

    To execute a callback once the speech is finished:

    say.speak("What's up, dog?", 'Good News', 1.0, (err) => {
      if (err) {
        return console.error(err)
      }
      console.log('Text has been spoken.')
    });
    // automatically pick platform
    const say = require('say')
    
    // or, override the platform
    const Say = require('say').Say
    const say = new Say('darwin' || 'win32' || 'linux')
    
    // Use default system voice and speed
    say.speak('Hello!')
    
    // Stop the text currently being spoken
    say.stop()
    
    // More complex example (with an OS X voice) and slow speed
    say.speak("What's up, dog?", 'Alex', 0.5)
    
    // Fire a callback once the text has completed being spoken
    say.speak("What's up, dog?", 'Good News', 1.0, (err) => {
      if (err) {
        return console.error(err)
      }
    
      console.log('Text has been spoken.')
    });
  3. Export spoken audio to a WAV file

    master

    On macOS and Windows, you can export spoken text directly to a .wav file. This method requires a filename and accepts an optional voice and speed.

    say.export("I'm sorry, Dave.", 'Cellos', 0.75, 'hal.wav', (err) => {
      if (err) {
        return console.error(err)
      }
      console.log('Text has been saved to hal.wav.')
    })
    say.export("I'm sorry, Dave.", 'Cellos', 0.75, 'hal.wav', (err) => {
      if (err) {
        return console.error(err)
      }
    
      console.log('Text has been saved to hal.wav.')
    })
  4. Reference the say.js API methods

    master

    The following methods are available on the say instance:

    • say.speak(text, voice || null, speed || null, callback || null): Speaks the provided text. Speed is a multiplier (e.g., 1 = 100%, 0.5 = 50%).
    • say.export(text, voice || null, speed || null, filename, callback || null): (macOS/Windows only) Exports the spoken text to a WAV file.
    • say.stop(callback || null): Stops the text currently being spoken.
    • say.getInstalledVoices(callback): Retrieves a list of installed system voices via a callback.
  5. Instantiate a specific platform with the Say class

    master
    If you need to target a specific platform or are running in an environment where process.platform does not match your target, you can use the Say class directly. You must pass the platform string to the constructor. The available platform constants are exported via say.platforms.
  6. Use the say.js singleton for text-to-speech

    master
    The default export of say.js is a singleton instance of the Say class. This instance automatically detects your current operating system (darwin, linux, or win32) and initializes the appropriate platform-specific driver. This is the easiest way to use the library for immediate text-to-speech functionality.