diff --git a/alsa.c b/alsa.c index 8a81342..6ab8b60 100644 --- a/alsa.c +++ b/alsa.c @@ -35,7 +35,6 @@ static pthread_mutex_t audio_mutex = PTHREAD_MUTEX_INITIALIZER; static snd_pcm_t *handle; static snd_pcm_hw_params_t *params; snd_pcm_status_t *status; -static unsigned int rate = 22050; /* sample rate */ static int dir = 0; void lock_audio_mutex(void) @@ -95,12 +94,10 @@ static int alsa_play_callback(short *audio, int numsamples, return 0; } -int init_audio(void) +int init_audio(unsigned int rate) { int rc; - unsigned saved_rate; - rate = 22050; /* Open PCM device for playback. */ rc = snd_pcm_open(&handle, "default", SND_PCM_STREAM_PLAYBACK, 0); @@ -141,7 +138,6 @@ int init_audio(void) /* One channel */ snd_pcm_hw_params_set_channels(handle, params, 1); - saved_rate = rate; snd_pcm_hw_params_set_rate_near(handle, params, &rate, &dir); /* Write the parameters to the driver */ diff --git a/espeak_sound.c b/espeak_sound.c index c9dcd82..92e6017 100644 --- a/espeak_sound.c +++ b/espeak_sound.c @@ -1,5 +1,5 @@ #include "espeakup.h" -int init_audio(void) +int init_audio(unsigned int rate) { audio_mode = AUDIO_OUTPUT_PLAYBACK; audio_callback = NULL; diff --git a/espeakup.c b/espeakup.c index f323d3c..4ec7580 100644 --- a/espeakup.c +++ b/espeakup.c @@ -95,6 +95,7 @@ void espeakup_sighandler(int sig) int main(int argc, char **argv) { + int rate; pthread_t queue_thread_id; struct synth_t s = { .voice = "", @@ -119,9 +120,6 @@ int main(int argc, char **argv) signal(SIGINT, espeakup_sighandler); signal(SIGTERM, espeakup_sighandler); - if (init_audio() < 0) { - return 5; - } if (!debug) { /* become a daemon */ @@ -135,7 +133,16 @@ int main(int argc, char **argv) } /* initialize espeak */ - espeak_Initialize(audio_mode, 0, NULL, 0); + rate = espeak_Initialize(audio_mode, 0, NULL, 0); + if (rate < 0) { + fprintf(stderr, "Unable to initialize espeak.\n"); + return 5; + } + + if (init_audio((unsigned int) rate) < 0) { + return 6; + } + espeak_SetSynthCallback(audio_callback); /* Setup initial voice parameters */ diff --git a/espeakup.h b/espeakup.h index a289e32..afe75ad 100644 --- a/espeakup.h +++ b/espeakup.h @@ -78,7 +78,7 @@ extern int open_softsynth(void); extern void close_softsynth(void); extern void main_loop(struct synth_t *s); extern void *queue_runner(void *arg); -extern int init_audio(void); +extern int init_audio(unsigned int rate); extern void lock_audio_mutex(void); extern void unlock_audio_mutex(void); extern volatile int stopped;