From d82bfcd09ccd4145d17473bc9ae60af223a5f66e Mon Sep 17 00:00:00 2001 From: Christopher Brannon Date: Wed, 24 Jun 2009 21:58:18 -0500 Subject: [PATCH] Make one thread responsible for handling espeak interaction. Most of the idea for this change came from William: Renamed queue_runner to espeak_thread. Moved espeak initialization and termination to espeak_thread. The while loops that process the queue now use the variable should_run. --- espeakup.c | 26 ++++++++++---------------- espeakup.h | 2 +- queue.c | 43 +++++++++++++++++++++++++++++++++++++++---- 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/espeakup.c b/espeakup.c index 5928bc4..6edaac2 100644 --- a/espeakup.c +++ b/espeakup.c @@ -33,13 +33,6 @@ const char *Version = "0.71"; /* path to our pid file */ const char *pidPath = "/var/run/espeakup.pid"; -/* default voice settings */ -const int defaultFrequency = 5; -const int defaultPitch = 5; -const int defaultRate = 5; -const int defaultVolume = 5; - -char *defaultVoice = NULL; int debug = 0; int self_pipe_fds[2]; @@ -83,7 +76,7 @@ int main(int argc, char **argv) int rate; int err; pthread_t signal_thread_id; - pthread_t queue_thread_id; + pthread_t espeak_thread_id; pthread_t softsynth_thread_id; struct synth_t s = { .voice = "", @@ -125,19 +118,20 @@ int main(int argc, char **argv) sigprocmask(SIG_BLOCK, &sigset, NULL); /* set up the pipe used to wake the reader. */ - if(pipe(self_pipe_fds) < 0) { - perror("Unable to create pipe"); - return 5; - } - - /* Spawn our queue-processing thread. */ - err = pthread_create(&queue_thread_id, NULL, &queue_runner, &s); + if (pipe(self_pipe_fds) < 0) { + perror("Unable to create pipe"); + return 5; + } + + /* Spawn our espeak-interacting thread. */ + err = pthread_create(&espeak_thread_id, NULL, &espeak_thread, &s); if (err != 0) { return 4; } /* Spawn our softsynth thread. */ - err = pthread_create(&softsynth_thread_id, NULL, &softsynth_thread, &s); + err = + pthread_create(&softsynth_thread_id, NULL, &softsynth_thread, &s); if (err != 0) { return 4; } diff --git a/espeakup.h b/espeakup.h index b6ecb4c..1edd35d 100644 --- a/espeakup.h +++ b/espeakup.h @@ -76,7 +76,7 @@ extern espeak_ERROR stop_speech(void); extern espeak_ERROR speak_text(struct synth_t *s); extern void *softsynth_thread(void *arg); extern void stop_runner(void); -extern void *queue_runner(void *arg); +extern void *espeak_thread(void *arg); extern void *signal_thread(void *arg); extern void select_audio_mode(void); extern int init_audio(unsigned int rate); diff --git a/queue.c b/queue.c index 002ad61..1aa5d98 100644 --- a/queue.c +++ b/queue.c @@ -25,6 +25,14 @@ #include "espeakup.h" +/* default voice settings */ +const int defaultFrequency = 5; +const int defaultPitch = 5; +const int defaultRate = 5; +const int defaultVolume = 5; + +char *defaultVoice = NULL; + pthread_cond_t runner_awake = PTHREAD_COND_INITIALIZER; pthread_cond_t stop_acknowledged = PTHREAD_COND_INITIALIZER; pthread_mutex_t queue_guard = PTHREAD_MUTEX_INITIALIZER; @@ -185,7 +193,7 @@ void stop_runner(void) pthread_mutex_unlock(&stop_guard); } -/* queue_runner is the "main" function of our secondary (queue-processing) +/* espeak_thread is the "main" function of our secondary (queue-processing) * thread. * First, lock queue_guard, because it needs to be locked when we call * pthread_cond_wait on the runner_awake condition variable. @@ -205,14 +213,40 @@ void stop_runner(void) * 2. We are processing an entry that has just been removed from the queue. */ -void *queue_runner(void *arg) +void *espeak_thread(void *arg) { struct synth_t *synth = (struct synth_t *) arg; + int rate; + + /* initialize espeak */ + select_audio_mode(); + rate = espeak_Initialize(audio_mode, 0, NULL, 0); + if (rate < 0) { + fprintf(stderr, "Unable to initialize espeak.\n"); + should_run = 0; + } + + if (init_audio((unsigned int) rate) < 0) { + should_run = 0; + } + + /* Setup initial voice parameters */ + if (defaultVoice) { + set_voice(&s, defaultVoice); + free(defaultVoice); + defaultVoice = NULL; + } + set_frequency(&s, defaultFrequency, ADJ_SET); + set_pitch(&s, defaultPitch, ADJ_SET); + set_rate(&s, defaultRate, ADJ_SET); + set_volume(&s, defaultVolume, ADJ_SET); + espeak_SetParameter(espeakCAPITALS, 0, 0); + pthread_mutex_lock(&queue_guard); - while (1) { + while (should_run) { pthread_cond_wait(&runner_awake, &queue_guard); - while (last && ! runner_must_stop ) { + while (should_run && last && !runner_must_stop) { queue_process_entry(synth); pthread_mutex_lock(&queue_guard); } @@ -227,5 +261,6 @@ void *queue_runner(void *arg) } } + espeak_Terminate(); return NULL; }