Documentation
¶
Overview ¶
Package piper synthesizes speech with Piper, a fast local neural text-to-speech system whose voices run on the CPU: no API key.
NewTTS runs a Piper voice in process, in libpiper, Piper's native library. It is bound through ebitengine/purego rather than cgo, so the package always builds and the library is loaded at run time: from Config.LibraryPath, or by its conventional name (libpiper.so, libpiper.dylib, piper.dll) on the loader's default search path. libpiper phonemizes the text with the espeak-ng it is built with, whose data it reads from Config.EspeakDataPath or from share/espeak-ng-data beside the library's lib directory. It runs the voice on the ONNX Runtime it is linked against. `make deps-piper` builds it under .native/, against the ONNX Runtime jargo's VAD and turn models run on, so a process using both loads one copy of the runtime. The dynamic loader gives libpiper the runtime already loaded when there is one, so that runtime has to be at least the version libpiper was built against.
The voice is downloaded once, on first use, from the rhasspy/piper-voices repository on Hugging Face into a cache under the user's cache directory: the model (<voice>.onnx) and its configuration (<voice>.onnx.json).
libpiper is licensed under the GPL-3.0. Distributing an application that loads it may subject the application to the GPL's source-disclosure terms. To keep Piper out of an application's license scope, use NewHTTPTTS, which speaks through a separately installed Piper HTTP server (python -m piper.http_server).
Index ¶
Constants ¶
const LibPathEnv = "JARGO_PIPER_LIB"
LibPathEnv is the environment variable applications and the eval harness read Config.LibraryPath from. This package reads no environment itself.
Variables ¶
var ( // ErrLibraryNotAvailable is returned when libpiper cannot be loaded, so a // caller can tell a missing install from a failure of the voice. ErrLibraryNotAvailable = errors.New("piper: libpiper not available") )
Errors the library reports, and loading it.
Functions ¶
func DefaultCacheDir ¶ added in v1.0.0
DefaultCacheDir is where voices are downloaded when the configuration names no directory: jargo/piper under the user's cache directory.
Types ¶
type Config ¶
type Config struct {
// Voice is the Piper voice to speak in, such as "en_US-ryan-high". Required.
// The voices are listed in the rhasspy/piper-voices repository on Hugging
// Face.
Voice string
// SpeakerID is which speaker to use, for voices trained on several
// speakers; nil uses the voice's first speaker.
SpeakerID *int
// LengthScale is the phoneme length scale: below 1 is faster, above 1 is
// slower. Nil uses the voice's default.
LengthScale *float64
// NoiseScale is the amount of generator noise; nil uses the voice's
// default.
NoiseScale *float64
// NoiseWScale is the amount of phoneme width noise; nil uses the voice's
// default.
NoiseWScale *float64
// LibraryPath is the libpiper shared library to load; empty looks for it
// by its conventional name on the loader's default search path.
LibraryPath string
// EspeakDataPath is the espeak-ng data directory libpiper phonemizes
// with; empty uses share/espeak-ng-data beside the library's lib
// directory, where libpiper installs it, when LibraryPath names the
// library and the directory exists, and the directory espeak-ng was built
// to read otherwise.
EspeakDataPath string
// DownloadDir is where the voice files are stored; empty uses
// DefaultCacheDir. A voice whose model is not there is downloaded into it.
DownloadDir string
// ForceRedownload downloads the voice files again even when they exist,
// when the voice's model is to be downloaded.
ForceRedownload bool
// HTTPClient downloads the voice; nil uses http.DefaultClient.
HTTPClient *http.Client
// SampleRate is the rate the service outputs; 0 uses the voice's own
// rate. The voice's audio is resampled to another rate.
SampleRate int `validate:"gte=0"`
}
Config configures the in-process Piper TTS service.
type HTTPConfig ¶ added in v1.0.0
type HTTPConfig struct {
// BaseURL is the URL of the Piper HTTP server's synthesis endpoint, which
// requests are posted to as is (e.g. http://localhost:5000/synthesize).
// Required. A trailing slash is removed, with a warning.
BaseURL string `validate:"required,url"`
// Voice is the voice the server is asked to speak in, such as
// "en_US-ryan-high"; empty leaves the choice to the server, which uses the
// voice it was started with.
Voice string
// SpeakerID is which speaker to use, for voices trained on several
// speakers; nil leaves it to the server.
SpeakerID *int
// LengthScale is the phoneme length scale: below 1 is faster, above 1 is
// slower. Nil leaves it to the server.
LengthScale *float64
// NoiseScale is the amount of generator noise; nil leaves it to the
// server.
NoiseScale *float64
// NoiseWScale is the amount of phoneme width noise; nil leaves it to the
// server.
NoiseWScale *float64
// SampleRate is the rate the service outputs; 0 uses 22050. The server
// answers at the rate of the voice it speaks in, which its WAV header
// gives, and the audio is resampled to this one.
SampleRate int `validate:"gte=0"`
// Client makes the requests; nil uses a client of its own. The service
// sends through a copy of it, so the client itself is left as it is.
Client *http.Client
}
HTTPConfig configures the Piper HTTP TTS service.
func (HTTPConfig) Validate ¶ added in v1.0.0
func (c HTTPConfig) Validate() error
Validate reports whether the configuration is usable.
type HTTPTTSService ¶ added in v1.0.0
HTTPTTSService is the Piper text-to-speech service NewHTTPTTS builds.
func NewHTTPTTS ¶ added in v1.0.0
func NewHTTPTTS(cfg HTTPConfig) *HTTPTTSService
NewHTTPTTS builds a TTS service speaking through a Piper HTTP server. Each utterance is posted to the server with the voice and the synthesis controls set, and the WAV it answers with is streamed downstream as it arrives, its header removed. NewTTS runs the voice in process instead.
type HTTPTTSSettings ¶ added in v1.0.0
type HTTPTTSSettings TTSSettings
HTTPTTSSettings is the configuration of the Piper HTTP service that can change while the pipeline runs, through a TTSUpdateSettingsFrame. It holds the same fields as TTSSettings. Every request is built from it, so any change, the voice included, applies from the next utterance on.
type TTSService ¶ added in v1.0.0
TTSService is the Piper text-to-speech service NewTTS builds.
func NewTTS ¶
func NewTTS(cfg Config) (*TTSService, error)
NewTTS builds a Piper TTS service, which runs the voice in process and streams the audio of each utterance as libpiper synthesizes it.
The library is loaded, and the voice downloaded (the first time) and loaded here, so a missing library, a missing voice or a voice that fails to load is reported now rather than on the first utterance. A missing library is ErrLibraryNotAvailable.
The synthesis controls (the speaker, the length scale and the two noise scales) are applied to each utterance, so a TTSUpdateSettingsFrame changing them takes effect from the next one. The voice is loaded once, so a change to it is stored but not applied, and is reported as not supported; NewHTTPTTS switches voices while running.
The voice runs on the CPU, so the service calls each synthesis off the session's loop, as it does any synthesizer that does not implement tts.IOAwaiter: the inference runs beside the session rather than holding it up, and each chunk of audio takes the loop as it is handed back.
type TTSSettings ¶ added in v1.0.0
type TTSSettings struct {
settings.TTS
// SpeakerID is which speaker to use, for voices trained on several
// speakers (such as en_US-libritts_r-medium). It is an index from 0 to the
// voice's speaker count minus one, listed in the voice's .onnx.json file.
// Voices with a single speaker ignore it.
SpeakerID settings.Opt[int] `settings:"speaker_id"`
// LengthScale is the phoneme length scale: below 1 is faster, above 1 is
// slower.
LengthScale settings.Opt[float64] `settings:"length_scale"`
// NoiseScale is the amount of generator noise, which varies the audio.
NoiseScale settings.Opt[float64] `settings:"noise_scale"`
// NoiseWScale is the amount of phoneme width noise, which varies the
// speaking style.
NoiseWScale settings.Opt[float64] `settings:"noise_w_scale"`
}
TTSSettings is the configuration of the in-process Piper service that can change while the pipeline runs, through a TTSUpdateSettingsFrame.
The synthesis controls take effect with the next utterance. The voice is loaded once, when the service is built, so a change to it (or to the model or the language) is stored but not applied, and is reported as such.