espeak

package module
v0.1.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2020 License: MIT Imports: 13 Imported by: 1

README

PkgGoDev CI status

go-espeak

Golang C bindings for the espeak voice synthesizer.

There is a live demo of its usage at https://go-espeak-demo.djangulo.com, source code in examples/demo.

Sub-package native contains a mostly C implementation, minimizing the amount of Go used. This implementation is slightly faster than the go implementation, with the inconvenience of being a black box from the input to the .wav.

Requirements

  • Go >= 1.15 with cgo support
  • espeak.

Install

Install requirements

Arch

~# pacman -S espeak

Ubuntu

~# apt-get install espeak
Install go-espeak
~$ go get -u github.com/djangulo/go-espeak

Usage

examples/basic-usage.

package main

import (
	"github.com/djangulo/go-espeak"
)

func main() {

	// need to call terminate so espeak can clean itself out
	defer espeak.Terminate()
	params := espeak.NewParameters().WithDir(".")
	espeak.TextToSpeech(
		"Hello World!", // Text to speak
		nil,            // voice to use, nil == DefaultVoice (en-us male)
		"hello.wav",    // if "" or "play", it plays to default audio out
		params,         // Parameters for voice modulation, nil == DefaultParameters
	)

	// get a random spanish voice
	v, _ := espeak.VoiceFromSpec(&espeak.Voice{Languages: "es"})
	espeak.TextToSpeech("¡Hola mundo!", v, "hola.wav", params)
}

Documentation

Overview

Package espeak implements C bindings for the Espeak voice synthesizer. It also provides Go wrappers around espeak's api that allow for creation of custom text synthesis functions.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	DefaultVoice = ENUSMale
	ENUSMale     = &Voice{Name: "english-us", Languages: "en-us", Identifier: "en-us", Gender: Male}
	ESSpainMale  = &Voice{Name: "spanish", Languages: "es", Identifier: "europe/es", Gender: Male}
	ESLatinMale  = &Voice{Name: "spanish-latin-am", Languages: "es-la", Identifier: "es-la", Gender: Male}
	FRFranceMale = &Voice{Name: "french", Languages: "fr-fr", Identifier: "fr", Gender: Male}
)

Default voices.

View Source
var (
	// EErrOK espeak return for not-really-an-error.
	EErrOK = &LibError{0, "OK"}
	// EErrInternal espeak return for internal error
	EErrInternal = &LibError{-1, "Internal error"}
	// EErrBufferFull espeak buffer full error.
	EErrBufferFull = &LibError{1, "Buffer full"}
	// EErrNotFound espeak not found error.
	EErrNotFound = &LibError{2, "Not found"}
	// ErrEmptyText text is empty.
	ErrEmptyText = errors.New("text is empty")
	// ErrUnknown unknown error code.
	ErrUnknown = errors.New("unknown error code")
	// ErrAlreadyInitialized espeak already initialized.
	ErrAlreadyInitialized = errors.New("espeak already initialized")
	// ErrNotInitialized espeak not initialized (call Init).
	ErrNotInitialized = errors.New("espeak not initialized (call Init)")
)

Errors

View Source
var DefaultParameters = &Parameters{
	Rate:                175,
	Volume:              100,
	Pitch:               50,
	Range:               50,
	AnnouncePunctuation: PunctNone,
	AnnounceCapitals:    CapitalNone,
	WordGap:             10,
	Dir:                 os.TempDir(),
}

DefaultParameters for voice modulation.

Functions

func Cancel added in v0.1.8

func Cancel() error

Cancel wrapper around espeak_Cancel. Stop immediately synthesis and audio output of the current text. When this function returns, the audio output is fully stopped and the synthesizer is ready to synthesize a new message.

func ErrFromCode added in v0.1.8

func ErrFromCode(code C.espeak_ERROR) error

ErrFromCode get a Go error from an espeak_ERROR.

func GenSamples added in v0.1.10

func GenSamples(text string, voice *Voice, params *Parameters) ([]int16, error)

GenSamples generates a []int16 sample slice containing the data of text, using voice, modified by params. If params is nil, default parameters are used.

func Init added in v0.1.8

func Init(
	output AudioOutput,
	bufferLength int,
	path *string,
	options InitOption,
) (uintptr, int32, error)

Init wrapper around espeak_Initialize. Returns a uintptr id which the address of the data block (T: *[]int16) acted on, and the sample rate used.

  • output AudioOutput type.
  • bufferLength length in mS of sound buffers passed to the SynthCallback function. If 0 gives a default of 200mS. Only used for output==Retrieval and output == Synchronous.
  • path: the directory which contains the espeak-data directory.
  • options: InitOption to use.

func IsPlaying added in v0.1.8

func IsPlaying() bool

IsPlaying returns whether audio is being played.

func SampleRate added in v0.1.10

func SampleRate() int32

SampleRate return the produced sample rate.

func SetSynthCallback added in v0.1.8

func SetSynthCallback(ptr unsafe.Pointer)

SetSynthCallback to the unsafe.Pointer passed. The underlying C object has to be a a function of signature

int (t_espeak_callback)(short*, int, espeak_EVENT*)

func SetVoiceByName added in v0.1.8

func SetVoiceByName(name string) error

SetVoiceByName wrapper around espeak_SetVoiceByName.

func SetVoiceByProps added in v0.1.8

func SetVoiceByProps(v *Voice) error

SetVoiceByProps wrapper around espeak_SetVoiceByProperties. An *Voice is used to pass criteria to select a voice.

func Synchronize added in v0.1.8

func Synchronize() error

Synchronize wrapper around espeak_Synchronize.

func Synth added in v0.1.8

func Synth(
	text string,
	flags FlagType,
	startPos, endPos uint32,
	posType PositionType,
	uniqueIdent *uint64,
	userData unsafe.Pointer,
) error

Synth wrapper around espeak_Synth.

  • text: text to synthezise.
  • flags: flag values to pass.
  • startPos, endPos: start and end position in the text where speaking starts and ends. If endPos is zero indicates no end position.
  • posType: PositionType to use.
  • uniqueIdent: This must be either NULL, or point to an integer variable to which eSpeak writes a message identifier number. eSpeak includes this number in espeak_EVENT messages which are the result of this call of espeak_Synth().
  • userData: a pointer (or NULL) which will be passed to the callback function in espeak_EVENT messages.

func Terminate added in v0.1.8

func Terminate() error

Terminate closes the espeak connection. It's up to the caller to call this and terminate the function.

func TextToSpeech

func TextToSpeech(text string, voice *Voice, outfile string, params *Parameters) (uint64, error)

TextToSpeech reproduces text, using voice, modified by params. If params is nil, default parameters are used. If outfile is an empty string or "play", the audio is spoken to the system default's audio output; otherwise is appended with .wav and saved to params.Dir/outfile[.wav]. Returns the number of samples written to file, if any.

Example
espeak.TextToSpeech("Hello world!", espeak.DefaultVoice, "play", nil)
// or set an outfile name to save it
// TextToSpeech("Hello world!", ENUSFemale, "hello-world.wav", nil)
Example (CustomVoice)

ExampleTextToSpeech_second show usage with a non-default voice.

// output of
//     ~$ espeak --voices=el
//     Pty Language Age/Gender VoiceName          File          Other Languages
//     5  el             M  greek                europe/el
//     7  el             M  greek-mbrola-1       mb/mb-gr2
greek := espeak.Voice{
	Languages:  "el",
	Gender:     espeak.Male,
	Name:       "greek",
	Identifier: "europe/el",
}
espeak.TextToSpeech("Γειά σου Κόσμε!", &greek, "play", nil)

Types

type Age

type Age int

Age voice age in years, 0 for not specified.

type AudioOutput added in v0.1.8

type AudioOutput uint8

AudioOutput type.

const (
	// Playback plays the audio data, supplies events to the calling program.
	Playback AudioOutput = iota + 1
	// Retrieval supplies audio data and events to the calling program.
	Retrieval
	// Synchronous as Retrieval but doesn't return until synthesis is completed.
	Synchronous
	// SynchPlayback synchronous playback.
	SynchPlayback
)

type Capitals

type Capitals int

Capitals setting to announce capital letters by.

const (
	// CapitalNone announce no capitals.
	CapitalNone Capitals = iota
	// CapitalSoundIcon distinctive sound for capitals.
	CapitalSoundIcon
	// CapitalSpelling spells out "Capital A" for each capital.
	CapitalSpelling
	// CapitalPitchRaise uses a different pitch for capital letters.
	CapitalPitchRaise
)

func (Capitals) String

func (c Capitals) String() string

type FlagType added in v0.1.8

type FlagType uint16

FlagType one-to-one mapping to the espeak flags.

const (
	// CharsAuto 8 bit or UTF8  (this is the default).
	CharsAuto FlagType = iota
	// CharsUTF8 utf-8 encoding.
	CharsUTF8
	// Chars8Bit the 8 bit ISO-8859 character set for the particular language.
	Chars8Bit
	// CharsWChar Wide characters (wchar_t).
	CharsWChar
	// Chars16Bit 16 bit characters.
	Chars16Bit
	// SSML Elements within < > are treated as SSML elements, or if not
	// recognised are ignored.
	SSML FlagType = 0x10
	// Phonemes Text within [[ ]] is treated as phonemes codes (in espeak's
	// Hirshenbaum encoding).
	Phonemes FlagType = 0x100
	// EndPause if set then a sentence pause is added at the end of the text.
	// If not set then this pause is suppressed.
	EndPause FlagType = 0x1000
)

type Gender

type Gender int

Gender voice gender.

const (
	// Unspecified or none.
	Unspecified Gender = iota
	// Male voice variant.
	Male
	// Female voice variant.
	Female
)

func (Gender) MarshalJSON added in v0.1.8

func (g Gender) MarshalJSON() ([]byte, error)

MarshalJSON marshals the Gender into a string.

func (Gender) String added in v0.1.8

func (g Gender) String() string

String implements the stringer interface.

func (*Gender) UnmarshalJSON added in v0.1.8

func (g *Gender) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the JSON.Unmarshaler interface.

type InitOption added in v0.1.8

type InitOption uint8

InitOption initialization options. Beware only PhonemeEvents and PhonemeIPA are the only ones that belong to espeak.

const (
	// PhonemeEvents allow espeakEVENT_PHONEME events.
	PhonemeEvents InitOption = 1 << iota
	// PhonemeIPA espeak events give IPA phoneme names, not eSpeak phoneme names.
	PhonemeIPA
	// UseMbrola allow usage of mbrola voices, excluded by default. This is not an
	// espeak option.
	UseMbrola
)

type LibError added in v0.1.4

type LibError struct {
	// contains filtered or unexported fields
}

LibError analog to espeak_ERROR.

func (*LibError) Error added in v0.1.4

func (e *LibError) Error() string

type Option

type Option func(*Parameters)

Option parameter creatien function.

func WithAnnounceCapitals

func WithAnnounceCapitals(cap Capitals) Option

WithAnnounceCapitals cap.

func WithAnnouncePunctuation

func WithAnnouncePunctuation(punct PunctType) Option

WithAnnouncePunctuation punct.

func WithDir

func WithDir(path string) Option

WithDir path.

func WithPitch

func WithPitch(pitch int) Option

WithPitch pitch.

func WithRange

func WithRange(rng int) Option

WithRange rng.

func WithRate

func WithRate(rate int) Option

WithRate rate.

func WithVolume

func WithVolume(volume int) Option

WithVolume volume.

func WithWordGap

func WithWordGap(wg int) Option

WithWordGap wg.

type Parameters

type Parameters struct {
	// Rate speaking speed in word per minute.  Values 80 to 450. Default 175.
	Rate int
	// Volume in range 0-200 or more.
	// 0=silence, 100=normal full volume, greater values may
	// produce amplitude compression or distortion. Default 100.
	Volume int
	// Pitch base pitch. Range 0-100. Default 50 (normal).
	Pitch int
	// Range pitch range, range 0-100. 0-monotone, 50=normal. Default 50 (normal).
	Range int
	// AnnouncePunctuation settings. See PunctType for details. Default None (0).
	AnnouncePunctuation PunctType
	// AnnounceCapitals settings. See Capitals for details. Default None (0).
	AnnounceCapitals Capitals
	// WordGap pause between words, units of 10mS (at the default speed).
	WordGap int
	// Dir directory path to save .wav files. Default os.TempDir()
	Dir string
	// contains filtered or unexported fields
}

Parameters espeak voice parameters.

func NewParameters

func NewParameters(opts ...Option) *Parameters

NewParameters returns *DefaultParameters modified by opts.

func (*Parameters) PunctuationList

func (p *Parameters) PunctuationList() string

PunctuationList returns the list of punctuation characters (if any).

func (*Parameters) SetPunctuationList

func (p *Parameters) SetPunctuationList(chars string)

SetPunctuationList sets the list of punctuation characters.

func (*Parameters) SetVoiceParams added in v0.1.8

func (p *Parameters) SetVoiceParams() error

SetVoiceParams calls espeak_SetParameter for each of the *Parameters fields.

func (*Parameters) WithAnnounceCapitals added in v0.1.4

func (p *Parameters) WithAnnounceCapitals(cap Capitals) *Parameters

WithAnnounceCapitals cap.

func (*Parameters) WithAnnouncePunctuation added in v0.1.4

func (p *Parameters) WithAnnouncePunctuation(punct PunctType) *Parameters

WithAnnouncePunctuation punct.

func (*Parameters) WithDir added in v0.1.4

func (p *Parameters) WithDir(path string) *Parameters

WithDir path.

func (*Parameters) WithPitch added in v0.1.4

func (p *Parameters) WithPitch(pitch int) *Parameters

WithPitch pitch.

func (*Parameters) WithRange added in v0.1.4

func (p *Parameters) WithRange(rng int) *Parameters

WithRange rng.

func (*Parameters) WithRate added in v0.1.4

func (p *Parameters) WithRate(rate int) *Parameters

WithRate rate.

func (*Parameters) WithVolume added in v0.1.4

func (p *Parameters) WithVolume(volume int) *Parameters

WithVolume volume.

func (*Parameters) WithWordGap added in v0.1.4

func (p *Parameters) WithWordGap(wg int) *Parameters

WithWordGap wg.

type PositionType added in v0.1.8

type PositionType uint8

PositionType determines whether "position" is a number of characters, words, or sentences.

const (
	// Character position type.
	Character PositionType = iota + 1
	// Word position type.
	Word
	// Sentence position type.
	Sentence
)

type PunctType

type PunctType int

PunctType punctuation to announce.

const (
	// PunctNone do not announce any punctuation.
	PunctNone PunctType = 0
	// PunctAll announce all punctuation signs.
	PunctAll PunctType = 1
	// PunctSome only announce punctuation signs as defined by
	// &Parameters.PunctuationList() or set by SetPunctList.
	PunctSome PunctType = 2
)

func (PunctType) String

func (p PunctType) String() string

type Variant

type Variant int

Variant after a list of candidates is produced, scored and sorted, "variant" is used to index that list and choose a voice. variant=0 takes the top voice (i.e. best match). variant=1 takes the next voice, etc

type Voice

type Voice struct {
	Name       string `json:"name,omitempty"`
	Languages  string `json:"languages,omitempty"`
	Identifier string `json:"identifier,omitempty"`
	Gender     Gender `json:"gender,omitempty"`
	Age        Age
	Variant    Variant
}

Voice analogous to C.espeak_VOICE. New voices can be created as long as they're listed in "espeak --voices=<lang>".

func ListVoices added in v0.1.8

func ListVoices(spec *Voice) (voices []*Voice, err error)

ListVoices reads the voice files from espeak-data/voices and returns them in a []*Voice object. If spec is nil, all available voices are listed. If spec is given, then only the voices which are compatible with the spec are listed, and they are listed in preference order. Init must have been called.

func VoiceFromSpec added in v0.1.8

func VoiceFromSpec(spec *Voice) (*Voice, error)

VoiceFromSpec returns a random Voice from the group of voices that matches spec. Is spec is nil, returns a random voice.

func (*Voice) String added in v0.1.8

func (v *Voice) String() string

Directories

Path Synopsis
examples
basic-usage command
custom-tts command
demo command
Package native has espeak native C implementation (called by Go) to synthesize audio or write to .wav.
Package native has espeak native C implementation (called by Go) to synthesize audio or write to .wav.
Package wav implements basic utilities for writing .wav files.
Package wav implements basic utilities for writing .wav files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL