Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
synth_thread.cpp
1 
2 /***************************************************************************
3  * synth_thread.cpp - Flite synthesis thread
4  *
5  * Created: Tue Oct 28 14:34:14 2008
6  * Copyright 2008 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "synth_thread.h"
24 
25 #include <interfaces/SpeechSynthInterface.h>
26 #include <utils/time/wait.h>
27 #include <asoundlib.h>
28 #include <cmath>
29 
30 using namespace fawkes;
31 
32 extern "C" {
33  extern cst_voice *register_cmu_us_kal(const char *voxdir);
34  extern void unregister_cmu_us_kal(cst_voice *voice);
35 }
36 
37 /** @class FliteSynthThread "synth_thread.h"
38  * Flite Synthesis Thread.
39  * This thread synthesises audio for text-to-speech using Flite.
40  * @author Tim Niemueller
41  */
42 
43 
44 /** Constructor. */
46  : Thread("FliteSynthThread", Thread::OPMODE_WAITFORWAKEUP),
47  BlackBoardInterfaceListener("FliteSynthThread")
48 {
49 }
50 
51 
52 void
54 {
55  __speechsynth_if = blackboard->open_for_writing<SpeechSynthInterface>("Flite");
56  __voice = register_cmu_us_kal(NULL);
57 
58  bbil_add_message_interface(__speechsynth_if);
60 
61  say("Speech synth loaded");
62 }
63 
64 
65 void
67 {
68  unregister_cmu_us_kal(__voice);
70  blackboard->close(__speechsynth_if);
71 }
72 
73 void
75 {
76  // wait for message(s) to arrive, could take a (little) while after the wakeup
77  while ( __speechsynth_if->msgq_empty() ) {
78  usleep(100);
79  }
80 
81  // process message, blocking
82  // only one at a time, loop() will be run as many times as wakeup() was called
83  if ( ! __speechsynth_if->msgq_empty() ) {
84  if ( __speechsynth_if->msgq_first_is<SpeechSynthInterface::SayMessage>() ) {
86  __speechsynth_if->set_msgid(msg->id());
87  say(msg->text());
88  }
89 
90  __speechsynth_if->msgq_pop();
91  }
92 }
93 
94 
95 bool
97  Message *message) throw()
98 {
99  wakeup();
100  return true;
101 }
102 
103 
104 /** Say something.
105  * @param text text to synthesize and speak.
106  */
107 void
108 FliteSynthThread::say(const char *text)
109 {
110  cst_wave *wave = flite_text_to_wave(text, __voice);
111  cst_wave_save_riff(wave, "/tmp/test.wav");
112 
113  __speechsynth_if->set_text(text);
114  __speechsynth_if->set_final(false);
115  __speechsynth_if->set_duration(get_duration(wave));
116  __speechsynth_if->write();
117 
118  play_wave(wave);
119  delete_wave(wave);
120 
121  __speechsynth_if->set_final(true);
122  __speechsynth_if->write();
123 }
124 
125 
126 float
127 FliteSynthThread::get_duration(cst_wave *wave)
128 {
129  return (float)cst_wave_num_samples(wave) / (float)cst_wave_sample_rate(wave);
130 }
131 
132 
133 /** Play a Flite wave to the default ALSA audio out.
134  * @param wave the wave form to play
135  */
136 void
137 FliteSynthThread::play_wave(cst_wave *wave)
138 {
139  snd_pcm_t *pcm;
140  float duration = get_duration(wave);
141  int err;
142  if ((err = snd_pcm_open(&pcm, "default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
143  throw Exception("Failed to open PCM: %s", snd_strerror(err));
144  }
145  snd_pcm_nonblock(pcm, 0);
146  if ((err = snd_pcm_set_params(pcm,
147  SND_PCM_FORMAT_S16_LE,
148  SND_PCM_ACCESS_RW_INTERLEAVED,
149  cst_wave_num_channels(wave),
150  cst_wave_sample_rate(wave),
151  1,
152  (unsigned int)roundf(duration * 1000000.))) < 0) {
153  throw Exception("Playback to set params: %s", snd_strerror(err));
154  }
155 
156  snd_pcm_sframes_t frames;
157  frames = snd_pcm_writei(pcm, cst_wave_samples(wave), cst_wave_num_samples(wave));
158  if (frames < 0) {
159  logger->log_warn(name(), "snd_pcm_writei failed (frames < 0)");
160  frames = snd_pcm_recover(pcm, frames, 0);
161  }
162  if (frames < 0) {
163  logger->log_warn(name(), "snd_pcm_writei failed: %s", snd_strerror(err));
164  } else if ( frames < (long)cst_wave_num_samples(wave)) {
165  logger->log_warn(name(), "Short write (expected %li, wrote %li)",
166  (long)cst_wave_num_samples(wave), frames);
167  }
168 
169  TimeWait::wait_systime((unsigned int)roundf(duration * 1000000.f));
170  snd_pcm_close(pcm);
171 }
void set_duration(const float new_duration)
Set duration value.
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:43
void set_final(const bool new_final)
Set final value.
bool msgq_empty()
Check if queue is empty.
Definition: interface.cpp:972
virtual bool bb_interface_message_received(fawkes::Interface *interface, fawkes::Message *message)
BlackBoard message received notification.
virtual void init()
Initialize the thread.
FliteSynthThread()
Constructor.
SayMessage Fawkes BlackBoard Interface Message.
virtual void unregister_listener(BlackBoardInterfaceListener *listener)
Unregister BB interface listener.
Definition: blackboard.cpp:200
Thread class encapsulation of pthreads.
Definition: thread.h:42
void write()
Write from local copy into BlackBoard memory.
Definition: interface.cpp:495
unsigned int id() const
Get message ID.
Definition: message.cpp:197
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:44
virtual Interface * open_for_writing(const char *interface_type, const char *identifier)=0
Open interface for writing.
void set_msgid(const uint32_t new_msgid)
Set msgid value.
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
Definition: blackboard.cpp:174
void msgq_pop()
Erase first message from queue.
Definition: interface.cpp:1118
Base class for exceptions in Fawkes.
Definition: exception.h:36
void set_text(const char *new_text)
Set text value.
virtual void finalize()
Finalize the thread.
bool msgq_first_is()
Check if first message has desired type.
Definition: interface.h:305
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
const char * name() const
Get name of thread.
Definition: thread.h:95
void say(const char *text)
Say something.
virtual void loop()
Code to execute in the thread.
Message * msgq_first()
Get the first message from the message queue.
Definition: interface.cpp:1104
void bbil_add_message_interface(Interface *interface)
Add an interface to the message received watch list.
BlackBoard interface listener.
SpeechSynthInterface Fawkes BlackBoard Interface.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Definition: blackboard.h:43
virtual void close(Interface *interface)=0
Close interface.