ESpeak Library ported to FB

Linux specific questions.
angros47
Posts: 1424
Joined: Jun 21, 2005 19:04

ESpeak Library ported to FB

Postby angros47 » May 03, 2010 16:44

With this library (used in many linux distros, including Ubuntu) FreeBasic can control speech synthesis.

Here is the include file:

Code: Select all

''
''
'' speak_lib -- header translated with help of SWIG FB wrapper
''
'' NOTICE: This file is part of the FreeBASIC Compiler package and can't
''         be included in other distributions without authorization.
''
''
#ifndef __speak_lib_bi__
#define __speak_lib_bi__

#define ESPEAK_API_REVISION 3

#include once "\crt\wchar.bi"
#inclib "espeak"

enum espeak_EVENT_TYPE
   espeakEVENT_LIST_TERMINATED = 0
   espeakEVENT_WORD = 1
   espeakEVENT_SENTENCE
   espeakEVENT_MARK
   espeakEVENT_PLAY
   espeakEVENT_END
   espeakEVENT_MSG_TERMINATED
   espeakEVENT_PHONEME
end enum

union espeak_EVENT__NESTED__id
   number as integer
   name as zstring ptr
end union

type espeak_EVENT
   type as espeak_EVENT_TYPE
   unique_identifier as uinteger
   text_position as integer
   length as integer
   audio_position as integer
   sample as integer
   user_data as any ptr
   id as espeak_EVENT__NESTED__id
end type


enum espeak_POSITION_TYPE
   POS_CHARACTER = 1
   POS_WORD
   POS_SENTENCE
end enum


enum espeak_AUDIO_OUTPUT
   AUDIO_OUTPUT_PLAYBACK
   AUDIO_OUTPUT_RETRIEVAL
   AUDIO_OUTPUT_SYNCHRONOUS
   AUDIO_OUTPUT_SYNCH_PLAYBACK
end enum


enum espeak_ERROR
   EE_OK = 0
   EE_INTERNAL_ERROR = -1
   EE_BUFFER_FULL = 1
   EE_NOT_FOUND = 2
end enum


declare function espeak_Initialize cdecl alias "espeak_Initialize" (byval output as espeak_AUDIO_OUTPUT, byval buflength as integer, byval path as zstring ptr, byval options as integer) as integer

type t_espeak_callback as integer

declare sub espeak_SetSynthCallback cdecl alias "espeak_SetSynthCallback" (byval SynthCallback as t_espeak_callback ptr)
declare sub espeak_SetUriCallback cdecl alias "espeak_SetUriCallback" (byval UriCallback as function cdecl(byval as integer, byval as zstring ptr, byval as zstring ptr) as integer)

#define espeakCHARS_AUTO 0
#define espeakCHARS_UTF8 1
#define espeakCHARS_8BIT 2
#define espeakCHARS_WCHAR 3
#define espeakSSML &h10
#define espeakPHONEMES &h100
#define espeakENDPAUSE &h1000
#define espeakKEEP_NAMEDATA &h2000

declare function espeak_Synth cdecl alias "espeak_Synth" (text as zstring ptr, byval size as size_t, byval position as uinteger, byval position_type as espeak_POSITION_TYPE, byval end_position as uinteger, byval flags as uinteger, byval unique_identifier as uinteger ptr, byval user_data as any ptr) as espeak_ERROR
declare function espeak_Synth_Mark cdecl alias "espeak_Synth_Mark" (text as zstring ptr, byval size as size_t, byval index_mark as zstring ptr, byval end_position as uinteger, byval flags as uinteger, byval unique_identifier as uinteger ptr, byval user_data as any ptr) as espeak_ERROR
declare function espeak_Key cdecl alias "espeak_Key" (byval key_name as zstring ptr) as espeak_ERROR
declare function espeak_Char cdecl alias "espeak_Char" (byval character as wchar_t) as espeak_ERROR

enum espeak_PARAMETER
   espeakSILENCE = 0
   espeakRATE = 1
   espeakVOLUME = 2
   espeakPITCH = 3
   espeakRANGE = 4
   espeakPUNCTUATION = 5
   espeakCAPITALS = 6
   espeakWORDGAP = 7
   espeakOPTIONS = 8
   espeakINTONATION = 9
   espeakRESERVED1 = 10
   espeakRESERVED2 = 11
   espeakEMPHASIS
   espeakLINELENGTH
   espeakVOICETYPE
   N_SPEECH_PARAM
end enum


enum espeak_PUNCT_TYPE
   espeakPUNCT_NONE = 0
   espeakPUNCT_ALL = 1
   espeakPUNCT_SOME = 2
end enum


declare function espeak_SetParameter cdecl alias "espeak_SetParameter" (byval parameter as espeak_PARAMETER, byval value as integer, byval relative as integer) as espeak_ERROR
declare function espeak_GetParameter cdecl alias "espeak_GetParameter" (byval parameter as espeak_PARAMETER, byval current as integer) as integer
declare function espeak_SetPunctuationList cdecl alias "espeak_SetPunctuationList" (byval punctlist as wchar_t ptr) as espeak_ERROR
declare sub espeak_SetPhonemeTrace cdecl alias "espeak_SetPhonemeTrace" (byval value as integer, byval stream as FILE ptr)
declare sub espeak_CompileDictionary cdecl alias "espeak_CompileDictionary" (byval path as zstring ptr, byval log as FILE ptr)

type espeak_VOICE
   name as zstring ptr
   languages as zstring ptr
   identifier as zstring ptr
   gender as ubyte
   age as ubyte
   variant as ubyte
   xx1 as ubyte
   score as integer
   spare as any ptr
end type

declare function espeak_ListVoices cdecl alias "espeak_ListVoices" (byval voice_spec as espeak_VOICE ptr) as espeak_VOICE ptr ptr
declare function espeak_SetVoiceByName cdecl alias "espeak_SetVoiceByName" (byval name as zstring ptr) as espeak_ERROR
declare function espeak_SetVoiceByProperties cdecl alias "espeak_SetVoiceByProperties" (byval voice_spec as espeak_VOICE ptr) as espeak_ERROR
declare function espeak_GetCurrentVoice cdecl alias "espeak_GetCurrentVoice" () as espeak_VOICE ptr
declare function espeak_Cancel cdecl alias "espeak_Cancel" () as espeak_ERROR
declare function espeak_IsPlaying cdecl alias "espeak_IsPlaying" () as integer
declare function espeak_Synchronize cdecl alias "espeak_Synchronize" () as espeak_ERROR
declare function espeak_Terminate cdecl alias "espeak_Terminate" () as espeak_ERROR
declare function espeak_Info cdecl alias "espeak_Info" (byval ptr as any ptr) as zstring ptr

#endif



Here is a very simple demo:

Code: Select all

#include "speak_lib.bi"


dim as any ptr unique_identifier, user_data ,position_type
dim text as string

text="Hello world"

espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 500, 0, 0)
espeak_SetVoiceByName("default")
espeak_Synth( text, len(text), 0, position_type, 0, espeakCHARS_AUTO, unique_identifier, user_data )
espeak_Synchronize( )


If you get the error message "ld: cannot find -lespeak", you will need to install it with

Code: Select all

sudo apt-get install libespeak-dev


Have fun!
joseywales72
Posts: 206
Joined: Aug 27, 2005 2:02
Location: Istanbul, Turkey

Postby joseywales72 » May 04, 2010 6:42

Thank you. I just tried it on Archlinux, it works like a champ... I'll try it later on a Windows VM. (0.20 FB)
Turkish pronounciation is too weird though. :-)
Anil
angros47
Posts: 1424
Joined: Jun 21, 2005 19:04

Re: ESpeak Library ported to FB

Postby angros47 » Dec 05, 2018 22:34

This version loads the library dynamically, so it doesn't need to install libespeak-dev. Also, it can run even if espeak library is not installed (of course, if the variable espeak is set to 0 it means that speech synthesis commands are not available, and would crash the program if used).

Code: Select all

''
''
'' speak_lib -- header translated with help of SWIG FB wrapper
''
'' NOTICE: This file is part of the FreeBASIC Compiler package and can't
''         be included in other distributions without authorization.
''
''
#ifndef __speak_lib_bi__
#define __speak_lib_bi__

#define ESPEAK_API_REVISION 3

#include once "\crt\wchar.bi"

enum espeak_EVENT_TYPE
   espeakEVENT_LIST_TERMINATED = 0
   espeakEVENT_WORD = 1
   espeakEVENT_SENTENCE
   espeakEVENT_MARK
   espeakEVENT_PLAY
   espeakEVENT_END
   espeakEVENT_MSG_TERMINATED
   espeakEVENT_PHONEME
end enum

union espeak_EVENT__NESTED__id
   number as long
   name as zstring ptr
end union

type espeak_EVENT
   type as espeak_EVENT_TYPE
   unique_identifier as ulong
   text_position as long
   length as long
   audio_position as long
   sample as long
   user_data as any ptr
   id as espeak_EVENT__NESTED__id
end type


enum espeak_POSITION_TYPE
   POS_CHARACTER = 1
   POS_WORD
   POS_SENTENCE
end enum


enum espeak_AUDIO_OUTPUT
   AUDIO_OUTPUT_PLAYBACK
   AUDIO_OUTPUT_RETRIEVAL
   AUDIO_OUTPUT_SYNCHRONOUS
   AUDIO_OUTPUT_SYNCH_PLAYBACK
end enum


enum espeak_ERROR
   EE_OK = 0
   EE_INTERNAL_ERROR = -1
   EE_BUFFER_FULL = 1
   EE_NOT_FOUND = 2
end enum


dim shared espeak_Initialize as function (byval output as espeak_AUDIO_OUTPUT, byval buflength as long, byval path as zstring ptr, byval options as long) as long

type t_espeak_callback as long

dim shared espeak_SetSynthCallback as sub(byval SynthCallback as t_espeak_callback ptr)
dim shared espeak_SetUriCallback as sub (byval UriCallback as function cdecl(byval as long, byval as zstring ptr, byval as zstring ptr) as long)

#define espeakCHARS_AUTO 0
#define espeakCHARS_UTF8 1
#define espeakCHARS_8BIT 2
#define espeakCHARS_WCHAR 3
#define espeakSSML &h10
#define espeakPHONEMES &h100
#define espeakENDPAUSE &h1000
#define espeakKEEP_NAMEDATA &h2000

dim shared espeak_Synth as function (text as zstring ptr, byval size as size_t, byval position as ulong, byval position_type as espeak_POSITION_TYPE, byval end_position as ulong, byval flags as ulong, byval unique_identifier as ulong ptr, byval user_data as any ptr) as espeak_ERROR
dim shared espeak_Synth_Mark as function (text as zstring ptr, byval size as size_t, byval index_mark as zstring ptr, byval end_position as ulong, byval flags as ulong, byval unique_identifier as ulong ptr, byval user_data as any ptr) as espeak_ERROR
dim shared espeak_Key as function (byval key_name as zstring ptr) as espeak_ERROR
dim shared espeak_Char as function (byval character as wchar_t) as espeak_ERROR

enum espeak_PARAMETER
   espeakSILENCE = 0
   espeakRATE = 1
   espeakVOLUME = 2
   espeakPITCH = 3
   espeakRANGE = 4
   espeakPUNCTUATION = 5
   espeakCAPITALS = 6
   espeakWORDGAP = 7
   espeakOPTIONS = 8
   espeakINTONATION = 9
   espeakRESERVED1 = 10
   espeakRESERVED2 = 11
   espeakEMPHASIS
   espeakLINELENGTH
   espeakVOICETYPE
   N_SPEECH_PARAM
end enum


enum espeak_PUNCT_TYPE
   espeakPUNCT_NONE = 0
   espeakPUNCT_ALL = 1
   espeakPUNCT_SOME = 2
end enum


dim shared espeak_SetParameter as function(byval parameter as espeak_PARAMETER, byval value as long, byval relative as long) as espeak_ERROR
dim shared espeak_GetParameter as function(byval parameter as espeak_PARAMETER, byval current as long) as long
dim shared espeak_SetPunctuationList as function (byval punctlist as wchar_t ptr) as espeak_ERROR
dim shared espeak_SetPhonemeTrace  as sub (byval value as long, byval stream as FILE ptr)
dim shared espeak_CompileDictionary as sub(byval path as zstring ptr, byval log as FILE ptr)

type espeak_VOICE
   name as zstring ptr
   languages as zstring ptr
   identifier as zstring ptr
   gender as ubyte
   age as ubyte
   variant as ubyte
   xx1 as ubyte
   score as long
   spare as any ptr
end type

dim shared espeak_ListVoices as function (byval voice_spec as espeak_VOICE ptr) as espeak_VOICE ptr ptr
dim shared espeak_SetVoiceByName as function (byval name as zstring ptr) as espeak_ERROR
dim shared espeak_SetVoiceByProperties as function (byval voice_spec as espeak_VOICE ptr) as espeak_ERROR
dim shared espeak_GetCurrentVoice as function () as espeak_VOICE ptr
dim shared espeak_Cancel as function () as espeak_ERROR
dim shared espeak_IsPlaying as function () as long
dim shared espeak_Synchronize as function () as espeak_ERROR
dim shared espeak_Terminate as function () as espeak_ERROR
dim shared espeak_Info as function (byval ptr as any ptr) as zstring ptr

dim shared espeak as any ptr

#endif


espeak=DyLibLoad("espeak")
if espeak=0 then espeak=DyLibLoad("libespeak.so.1")
if espeak<>0 then
   espeak_Initialize=DyLibSymbol(espeak,"espeak_Initialize")
   espeak_SetSynthCallback=DyLibSymbol(espeak,"espeak_SetSynthCallback")
   espeak_SetUriCallback=DyLibSymbol(espeak,"espeak_SetUriCallback")
   espeak_Synth=DyLibSymbol(espeak,"espeak_Synth")
   espeak_Synth_Mark=DyLibSymbol(espeak,"espeak_Synth_Mark")
   espeak_Key=DyLibSymbol(espeak,"espeak_Key")
   espeak_Char=DyLibSymbol(espeak,"espeak_Char")
   espeak_SetParameter=DyLibSymbol(espeak,"espeak_SetParameter")
   espeak_GetParameter=DyLibSymbol(espeak,"espeak_GetParameter")
   espeak_SetPunctuationList=DyLibSymbol(espeak,"espeak_SetPunctuationList")
   espeak_SetPhonemeTrace=DyLibSymbol(espeak,"espeak_SetPhonemeTrace")
   espeak_CompileDictionary=DyLibSymbol(espeak,"espeak_CompileDictionary")
   espeak_ListVoices=DyLibSymbol(espeak,"espeak_ListVoices")
   espeak_SetVoiceByName=DyLibSymbol(espeak,"espeak_SetVoiceByName")
   espeak_SetVoiceByProperties=DyLibSymbol(espeak,"espeak_SetVoiceByProperties")
   espeak_GetCurrentVoice=DyLibSymbol(espeak,"espeak_GetCurrentVoice")
   espeak_Cancel=DyLibSymbol(espeak,"espeak_Cancel")
   espeak_IsPlaying=DyLibSymbol(espeak,"espeak_IsPlaying")
   espeak_Synchronize=DyLibSymbol(espeak,"espeak_Synchronize")
   espeak_Terminate=DyLibSymbol(espeak,"espeak_Terminate")
   espeak_Terminate=DyLibSymbol(espeak,"espeak_Terminate")
end if


dim as any ptr unique_identifier, user_data
dim as long position_type
dim text as string

text="Hello world"

espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 500, 0, 0)
espeak_SetVoiceByName("default")
espeak_Synth( text, len(text)+1, 0, position_type, 0, espeakCHARS_AUTO, unique_identifier, user_data )
espeak_Synchronize( )

Return to “Linux”

Who is online

Users browsing this forum: No registered users and 2 guests