diff --git a/espeakup.c b/espeakup.c index 65570f0..0eafc2f 100644 --- a/espeakup.c +++ b/espeakup.c @@ -104,6 +104,16 @@ int main(int argc, char **argv) } } +/* Initialize espeak */ + if (initialize_espeak(&s) < 0) { + return 2; + } + +/* open the softsynth */ + if (open_softsynth() < 0) { + return 2; + } + /* set up the pipe used to wake the espeak thread */ if (pipe(self_pipe_fds) < 0) { perror("Unable to create pipe"); @@ -142,6 +152,7 @@ int main(int argc, char **argv) pthread_join(softsynth_thread_id, NULL); pthread_join(espeak_thread_id, NULL); + espeak_Terminate(); if ( ! debug) unlink(pidPath); return 0; diff --git a/espeakup.h b/espeakup.h index ffe375b..2992aa2 100644 --- a/espeakup.h +++ b/espeakup.h @@ -70,8 +70,11 @@ extern void queue_add(void *entry); extern void queue_remove(void); extern void *queue_peek(void); extern void *signal_thread(void *arg); -extern void *softsynth_thread(void *arg); +extern int initialize_espeak(struct synth_t *s); extern void *espeak_thread(void *arg); +extern int open_softsynth(void); +extern void close_softsynth(void); +extern void *softsynth_thread(void *arg); extern void select_audio_mode(void); extern int init_audio(unsigned int rate); extern void lock_audio_mutex(void); diff --git a/softsynth.c b/softsynth.c index d3fe1b1..4472ff4 100644 --- a/softsynth.c +++ b/softsynth.c @@ -28,10 +28,12 @@ #include "espeakup.h" /* max buffer size */ -const size_t maxBufferSize = 1025; +static const size_t maxBufferSize = 1025; /* synth flush character */ -const int synthFlushChar = 0x18; +static const int synthFlushChar = 0x18; + +static int softFD = 0; static void queue_add_cmd(enum command_t cmd, enum adjust_t adj, int value) { @@ -175,6 +177,24 @@ static void request_espeak_stop(void) pthread_mutex_unlock(&queue_guard); } +int open_softsynth(void) +{ + int rc = 0; + /* open the softsynth. */ + softFD = open("/dev/softsynth", O_RDWR | O_NONBLOCK); + if (softFD < 0) { + perror("Unable to open the softsynth device"); + rc = -1; + } + return rc; +} + +void close_softsynth(void) +{ + if (softFD) + close(softFD); +} + void *softsynth_thread(void *arg) { struct synth_t *s = (struct synth_t *) arg; @@ -183,16 +203,8 @@ void *softsynth_thread(void *arg) char buf[maxBufferSize]; char *cp; int terminalFD = PIPE_READ_FD; - int softFD; int greatestFD; - /* open the softsynth. */ - softFD = open("/dev/softsynth", O_RDWR | O_NONBLOCK); - if (softFD < 0) { - perror("Unable to open the softsynth device"); - should_run = 0; - } - if (terminalFD > softFD) greatestFD = terminalFD; else @@ -231,7 +243,5 @@ void *softsynth_thread(void *arg) } process_buffer(s, buf, length); } - if (softFD) - close(softFD); return NULL; } diff --git a/synth.c b/synth.c index 58e111b..bcee672 100644 --- a/synth.c +++ b/synth.c @@ -206,6 +206,36 @@ static void queue_clear() } } +int initialize_espeak(struct synth_t *s) +{ + 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"); + return -1; + } + + if (init_audio((unsigned int) rate) < 0) { + return -1; + } + + /* 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); + return 0; +} + /* 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 @@ -225,35 +255,9 @@ static void queue_clear() * 1. We are waiting on runner_awake, or * 2. We are processing an entry that has just been removed from the queue. */ - void *espeak_thread(void *arg) { struct synth_t *s = (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 (should_run) {