htgo-tts

repository·master·Indexed 42 days ago

https://github.com/hegedustibor/htgo-tts

A GoLang package providing Text-to-Speech (TTS) capabilities to convert text into spoken audio. It supports multiple playback handlers, including mplayer and a native implementation using the oto library, and provides methods for direct speech playback, creating speech files, and handling long strings via speech buffers.

Tokens
1.8K
Snippets
11
Records
11
Agent score
3%

What's inside htgo-tts

  1. Update or Remove htgo-tts

    master

    To update the package to the latest version:

    go get -u "github.com/hegedustibor/htgo-tts"

    To remove the package:

    go clean -i "github.com/hegedustibor/htgo-tts"
    go get -u "github.com/hegedustibor/htgo-tts"
    go clean -i "github.com/hegedustibor/htgo-tts"
  2. Initialize the Speech struct

    master

    To use htgo-tts, you must first initialize a Speech struct. This struct defines the configuration for audio storage, language, proxy settings, and the playback handler.

    Required Dependency:

    • mplayer must be installed on your system for audio playback.

    Configuration Fields:

    • Folder: The directory where generated .mp3 files will be stored.
    • Language: The language code for the TTS engine (e.g., "en").
    • Proxy: (Optional) A proxy URL string if you need to route requests through a proxy.
    • Handler: An implementation of handlers.PlayerInterface to handle audio playback. If left nil, it defaults to using handlers.MPlayer.
    speech := htgotts.Speech{
        Folder:   "audio",
        Language: "en",
        Handler:  handlers.MPlayer,
    }
  3. Basic usage of htgo-tts

    master

    To use htgo-tts for basic text-to-speech, import the main package and the voices package. Initialize a Speech struct with a target Folder for audio files and a Language from the voices package, then call .Speak().

    import (
        "github.com/hegedustibor/htgo-tts"
        "github.com/hegedustibor/htgo-tts/voices"
    )
    
    speech := htgotts.Speech{Folder: "audio", Language: voices.English}
    speech.Speak("Your sentence.")
  4. Configure a Proxy in htgo-tts

    master

    If you need to route requests through a proxy, set the Proxy field in the Speech struct with the proxy URL.

    import (
        htgotts "github.com/hegedustibor/htgo-tts"
        voices "github.com/hegedustibor/htgo-tts/voices"
    )
    
    speech := htgotts.Speech{Folder: "audio", Language: voices.English, Proxy: "https://..."}
    speech.Speak("Your sentence.")
  5. Use mplayer as a handler

    master

    By default, htgo-tts can use mplayer to play audio. Ensure mplayer is installed on your system. To explicitly use the MPlayer handler, import the handlers package and assign it to the Handler field in the Speech struct.

    import (
        htgotts "github.com/hegedustibor/htgo-tts"
        handlers "github.com/hegedustibor/htgo-tts/handlers"
        voices "github.com/hegedustibor/htgo-tts/voices"
    )
    
    speech := htgotts.Speech{Folder: "audio", Language: voices.English, Handler: &handlers.MPlayer{}}
    speech.Speak("Your sentence.")
  6. Use the Native handler (without external player)

    master

    If you want to play audio without an external player like mplayer, use the Native handler. This handler utilizes the oto library.

    Note: You must follow the prerequisites for the oto library to use this handler successfully.

    import (
        htgotts "github.com/hegedustibor/htgo-tts"
        handlers "github.com/hegedustibor/htgo-tts/handlers"
        voices "github.com/hegedustibor/htgo-tts/voices"
    )
    
    speech := htgotts.Speech{Folder: "audio", Language: voices.English, Handler: &handlers.Native{}}
    speech.Speak("Your sentence.")
  7. Create a speech buffer with CreateSpeechBuff()

    master

    Use CreateSpeechBuff to generate speech for long strings by chunking the text into 32-character segments. This method downloads all segments, concatenates them into a single buffer, saves the result to the configured Folder, and returns an io.Reader containing the full audio data.

    Note: This is useful for handling text that exceeds the standard TTS character limits.

    // Returns an io.Reader of the combined audio and an error
    reader, err := speech.CreateSpeechBuff("This is a very long string that needs chunking", "long_speech")
    if err != nil {
        // handle error
    }
    // You can now read from 'reader' (e.g., stream it or write to another buffer)
  8. Create a speech file with CreateSpeechFile()

    master

    Use CreateSpeechFile when you want to generate and save an .mp3 file to disk without immediately playing it. It returns the path to the created file.

    Parameters:

    • text: The string to convert to speech.
    • fileName: The desired name for the file (without the .mp3 extension).
    // Returns the path to the file, e.g., "audio/my_speech.mp3"
    filePath, err := speech.CreateSpeechFile("Hello world", "my_speech")
    if err != nil {
        // handle error
    }
  9. Speak text directly using Speak()

    master

    The Speak method is the highest-level API. It performs the following steps:

    1. Generates a unique filename based on the MD5 hash of the text.
    2. Downloads the speech file (if it doesn't already exist in the configured Folder).
    3. Plays the resulting .mp3 file using the configured Handler (defaults to MPlayer).
    err := speech.Speak("Hello, world!")
    if err != nil {
        // handle error
    }
  10. Play an existing audio file with PlaySpeechFile()

    master

    If you already have an .mp3 file in your storage, you can play it directly using PlaySpeechFile. This method uses the configured Handler to execute playback.

    // fileName should be the name without extension or the full path depending on implementation
    // Based on source, it expects the filename used during creation
    err := speech.PlaySpeechFile("my_speech")
    if err != nil {
        // handle error
    }