diff --git a/alsa.c b/alsa.c index ae38798..8f05fd9 100644 --- a/alsa.c +++ b/alsa.c @@ -30,22 +30,13 @@ #include "espeakup.h" static pthread_mutex_t audio_mutex = PTHREAD_MUTEX_INITIALIZER; +static volatile int stopped = 0; static snd_pcm_t *handle; static snd_pcm_hw_params_t *params; snd_pcm_status_t *status; static int dir = 0; -void lock_audio_mutex(void) -{ - pthread_mutex_lock(&audio_mutex); -} - -void unlock_audio_mutex(void) -{ - pthread_mutex_unlock(&audio_mutex); -} - int sound_error(int err, const char *msg) { fprintf(stderr, "%s: %s\n", msg, snd_strerror(err)); @@ -68,14 +59,14 @@ static int alsa_play_callback(short *audio, int numsamples, int to_write; snd_pcm_state_t state; - lock_audio_mutex(); + pthread_mutex_lock(&audio_mutex); if (stopped) { snd_pcm_drop(handle); stopped = 0; - unlock_audio_mutex(); + pthread_mutex_unlock(&audio_mutex); return 1; } - unlock_audio_mutex(); + pthread_mutex_unlock(&audio_mutex); snd_pcm_status(handle, status); state = snd_pcm_status_get_state(status); @@ -165,3 +156,17 @@ int init_audio(unsigned int rate) espeak_SetSynthCallback(alsa_play_callback); return 0; } + +void stop_audio(void) +{ + pthread_mutex_lock(&audio_mutex); + stopped = 1; + pthread_mutex_unlock(&audio_mutex); +} + +void allow_audio(void) +{ + pthread_mutex_lock(&audio_mutex); + stopped = 0; + pthread_mutex_unlock(&audio_mutex); +} diff --git a/espeak_sound.c b/espeak_sound.c index 4c2d6d1..b20f0e2 100644 --- a/espeak_sound.c +++ b/espeak_sound.c @@ -10,17 +10,12 @@ int init_audio(unsigned int rate) return 0; } -/* - * lock_audio_mutex and unlock_audio_mutex are no-ops if we use native - * sound support. The stopped variable is never read; no need to protect it. - */ - -void lock_audio_mutex(void) +void stop_audio(void) { return; } -void unlock_audio_mutex(void) +void allow_audio(void) { return; } diff --git a/espeakup.c b/espeakup.c index fd607a9..8ab88ad 100644 --- a/espeakup.c +++ b/espeakup.c @@ -35,7 +35,6 @@ const char *pidPath = "/var/run/espeakup.pid"; int debug = 0; int self_pipe_fds[2]; -volatile int stopped = 0; volatile int should_run = 1; espeak_AUDIO_OUTPUT audio_mode; diff --git a/espeakup.h b/espeakup.h index 2992aa2..37ae8fd 100644 --- a/espeakup.h +++ b/espeakup.h @@ -77,10 +77,9 @@ 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); -extern void unlock_audio_mutex(void); +extern void stop_audio(void); +extern void allow_audio(void); extern volatile int should_run; -extern volatile int stopped; extern volatile int runner_must_stop; extern int self_pipe_fds[2]; #define PIPE_READ_FD (self_pipe_fds[0]) diff --git a/synth.c b/synth.c index ca9972c..991a11a 100644 --- a/synth.c +++ b/synth.c @@ -125,9 +125,7 @@ static espeak_ERROR set_volume(struct synth_t * s, int vol, enum adjust_t adj) static espeak_ERROR stop_speech(void) { - lock_audio_mutex(); - stopped = 1; - unlock_audio_mutex(); + stop_audio(); return (espeak_Cancel()); } @@ -135,9 +133,7 @@ static espeak_ERROR speak_text(struct synth_t * s) { espeak_ERROR rc; - lock_audio_mutex(); - stopped = 0; - unlock_audio_mutex(); + allow_audio(); rc = espeak_Synth(s->buf, s->len + 1, 0, POS_CHARACTER, 0, 0, NULL, NULL); return rc;