"Dal testo al parlato" mediante le funzioni esterne del API di Speech-Dispatcher

Da Gambas-it.org - Wikipedia.

Speech-Dispatcher è una risorsa per la sintesi vocale che fornisce una interfaccia comune di facile utilizzo sia per applicazioni client (programmi creati per parlare) sia per i sintetizzatori software effettivamente in grado di convertire il testo in parlato. Tale interfaccia comune generale consente ai programmatori di applicazioni client di avere un modo semplice per l'uso di un software di sintesi vocale evitando così di preoccuparsi di particolari dettagli riguardo ai vari sintetizzatori.

Ogni client (applicazione creata per parlare) apre una connessione socket con Speech-Dispatcher e chiama le funzioni di questo. Quando i messaggi testuali arrivano a Speech-Dispatcher, tale dispositivo li analizza, ne legge il testo da pronunciarsi e lo pone in una delle diverse code in base alla priorità del messaggio e ad altri criteri. Quindi esso decide quando, con quali parametri (impostati dal cliente e dall'utente), e su quale sintetizzatore dirà il messaggio. Queste richieste sono gestite dal plug-in (moduli di output) in uscita ai diversi sintetizzatori hardware e software e poi dette ad alta voce.

Per usare le funzioni esterne del API di Speech-Dispatcher, è necessario richiamare la sua libreria condivisa: "libspeechd.so.2.6.0 ".

Mostriamo di seguito un semplice codice per la lettura parlata di un testo in italiano:

Library "libspeechd:2.6.0"

Private Enum SPD_MODE_SINGLE = 0, SPD_MODE_THREADED
Private Enum SPD_IMPORTANT = 1, SPD_MESSAGE, SPD_TEXT, SPD_NOTIFICATION, SPD_PROGRESS
Private Enum SPD_MALE1 = 1, SPD_MALE2, SPD_MALE3, SPD_FEMALE1, SPD_FEMALE2, SPD_FEMALE3, SPD_CHILD_MALE, SPD_CHILD_FEMALE

' SPDConnection* spd_open(char* client_name, char* connection_name, char* user_name, SPDConnectionMode connection_mode)
' Opens a new connection to Speech Dispatcher and returns a socket file descriptor you will use to communicate with Speech Dispatcher.
Private Extern spd_open(client_name As String, connection_name As String, user_name As String, connection_mode As Integer) As Pointer

' int spd_set_voice_type(SPDConnection* connection, SPDVoiceType voice)
' Set a preferred symbolic voice.
' "voice" is one of the following values: SPD_MALE1, SPD_MALE2, SPD_MALE3, SPD_FEMALE1, SPD_FEMALE2, SPD_FEMALE3, SPD_CHILD_MALE, SPD_CHILD_FEMALE
Private Extern spd_set_voice_type(connection As Pointer, voice As Integer) As Integer
 
' int spd_set_synthesis_voice(SPDConnection*, const char *voice_name)
' Set the speech synthesizer voice to use.
Private Extern spd_set_synthesis_voice(connection As Pointer, voice_name As String) As Integer

' int spd_set_language(SPDConnection* connection, char* language)
' Sets the language that should be used for synthesis. "language" is the language code as defined in ISO 639 (“cs”, “en”, "it"...).
Private Extern spd_set_language(connection As Pointer, language As String) As Integer

' int spd_set_volume(SPDConnection* connection, int volume)
' Set the volume of the voice and sounds produced by Speech Dispatcher's output modules.
' "volume" is a number between -100 and +100 which means the lowest and the loudest voice respectively.
Private Extern spd_set_volume(connection As Pointer, volume As Integer) As Integer

' int spd_set_voice_rate(SPDConnection* connection, int rate)
' Set voice speaking rate. "rate" is a number between -100 and +100 which means the slowest and the fastest speech rate respectively.
Private Extern spd_set_voice_rate(connection As Pointer, rateInt As Integer) As Integer

' int spd_set_voice_pitch(SPDConnection* connection, int pitch)
' Set voice pitch. "pitch" is a number between -100 and +100, which means the lowest and the highest pitch respectively.
Private Extern spd_set_voice_pitch(connection As Pointer, pitch As Integer) As Integer

' int spd_sayf(SPDConnection* connection, SPDPriority priority, char* format, ...)
' Sends a message to Speech Dispatcher.
Private Extern spd_say(connection As Pointer, priority As Byte, $format As String) As Integer

' void spd_close(SPDConnection *connection)
' Closes a Speech Dispatcher socket connection, terminates associated threads (if necessary) and frees the memory allocated by spd_open().
Private Extern spd_close(connection As Pointer)


Public Sub Form_Open()

  parla()

End


Public Procedure parla()

 Dim conn As Pointer
 Dim err As Integer
  
' Potrebbe rendersi necessaria la chiusura del processo di "speech-dispatcher" prima di effettuare una nuova esecuzione parlata del testo:
 Shell "killall speech-dispatcher" Wait
  
 conn = spd_open("parla", "main", Null, SPD_MODE_THREADED)
 If conn == 0 Then Error.Raise("Errore !")

 err = spd_set_voice_type(conn, SPD_MALE1)
 Print err

' Imposta la lingua per la lettura parlata del testo:
 err = spd_set_language(conn, "it")
 Print err
  
 err = spd_set_volume(conn, 100)
 Print err
  
 err = spd_set_voice_rate(conn, -10)
 Print err

 err = spd_set_voice_pitch(conn, -50)
 Print err
 
 err = spd_set_synthesis_voice(conn, "italian")
 Print err

' Si imposta il testo da far leggere ad alta voce:
 err = spd_say(conn, SPD_TEXT, "www.gambas-org.it è la comunità italiana dei programmatori Gambas.")
 Print err

' Chiude la connessione socket:
 spd_close(conn)
 
End


Public Sub Button1_Click()
     
 parla()
  
End


Riferimenti