23 #include "synth_thread.h"
25 #include <alsa/asoundlib.h>
26 #include <interfaces/SpeechSynthInterface.h>
27 #include <utils/time/wait.h>
34 extern cst_voice *register_cmu_us_kal(
const char *voxdir);
35 extern void unregister_cmu_us_kal(cst_voice *voice);
46 :
Thread(
"FliteSynthThread",
Thread::OPMODE_WAITFORWAKEUP),
55 voice_ = register_cmu_us_kal(NULL);
62 say(
"Speech synth loaded");
68 unregister_cmu_us_kal(voice_);
108 cst_wave *wave = flite_text_to_wave(text, voice_);
109 cst_wave_save_riff(wave,
"/tmp/test.wav");
114 speechsynth_if_->
write();
120 speechsynth_if_->
write();
124 FliteSynthThread::get_duration(cst_wave *wave)
126 return (
float)cst_wave_num_samples(wave) / (float)cst_wave_sample_rate(wave);
133 FliteSynthThread::play_wave(cst_wave *wave)
136 float duration = get_duration(wave);
138 if ((err = snd_pcm_open(&pcm, cfg_soundcard_.c_str(), SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
139 throw Exception(
"Failed to open PCM: %s", snd_strerror(err));
141 snd_pcm_nonblock(pcm, 0);
142 if ((err = snd_pcm_set_params(pcm,
143 SND_PCM_FORMAT_S16_LE,
144 SND_PCM_ACCESS_RW_INTERLEAVED,
145 cst_wave_num_channels(wave),
146 cst_wave_sample_rate(wave),
148 (
unsigned int)roundf(duration * 1000000.)))
150 throw Exception(
"Playback to set params: %s", snd_strerror(err));
153 snd_pcm_sframes_t frames;
154 frames = snd_pcm_writei(pcm, cst_wave_samples(wave), cst_wave_num_samples(wave));
157 frames = snd_pcm_recover(pcm, frames, 0);
161 }
else if (frames < (
long)cst_wave_num_samples(wave)) {
163 "Short write (expected %li, wrote %li)",
164 (
long)cst_wave_num_samples(wave),
168 TimeWait::wait_systime((
unsigned int)roundf(duration * 1000000.f));
void say(const char *text)
Say something.
FliteSynthThread()
Constructor.
virtual void init()
Initialize the thread.
virtual bool bb_interface_message_received(fawkes::Interface *interface, fawkes::Message *message) noexcept
BlackBoard message received notification.
virtual void loop()
Code to execute in the thread.
virtual void finalize()
Finalize the thread.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
BlackBoard interface listener.
void bbil_add_message_interface(Interface *interface)
Add an interface to the message received watch list.
virtual void unregister_listener(BlackBoardInterfaceListener *listener)
Unregister BB interface listener.
virtual Interface * open_for_writing(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for writing.
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
virtual void close(Interface *interface)=0
Close interface.
Configuration * config
This is the Configuration member used to access the configuration.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
Base class for exceptions in Fawkes.
Base class for all Fawkes BlackBoard interfaces.
bool msgq_first_is()
Check if first message has desired type.
void msgq_pop()
Erase first message from queue.
Message * msgq_first()
Get the first message from the message queue.
void write()
Write from local copy into BlackBoard memory.
bool msgq_empty()
Check if queue is empty.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
Logger * logger
This is the Logger member used to access the logger.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
unsigned int id() const
Get message ID.
SayMessage Fawkes BlackBoard Interface Message.
char * text() const
Get text value.
SpeechSynthInterface Fawkes BlackBoard Interface.
void set_duration(const float new_duration)
Set duration value.
void set_text(const char *new_text)
Set text value.
void set_msgid(const uint32_t new_msgid)
Set msgid value.
void set_final(const bool new_final)
Set final value.
Thread class encapsulation of pthreads.
const char * name() const
Get name of thread.
Fawkes library namespace.