| 1 |
https://github.com/linux-speakup/espeakup/pull/48 |
| 2 |
|
| 3 |
commit e892e7165102c55bf8e3c71d8bf80ea485fdb440 |
| 4 |
Author: Samuel Thibault <[email protected]> |
| 5 |
Date: Tue Mar 1 01:28:38 2022 +0100 |
| 6 |
|
| 7 |
Throttle on EE_BUFFER_FULL errors |
| 8 |
|
| 9 |
When espeak returns EE_BUFFER_FULL we should just wait a bit before |
| 10 |
retrying. Also, we don't want to lose the current entry. We however want |
| 11 |
to wake up as soon as possible on stop request. |
| 12 |
|
| 13 |
diff --git a/src/espeak.c b/src/espeak.c |
| 14 |
index 135c6b5..b4b58ca 100644 |
| 15 |
--- a/src/espeak.c |
| 16 |
+++ b/src/espeak.c |
| 17 |
@@ -325,16 +325,16 @@ static void reinitialize_espeak(struct synth_t *s) |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
+static struct espeak_entry_t *current = NULL; |
| 22 |
static void queue_process_entry(struct synth_t *s) |
| 23 |
{ |
| 24 |
- espeak_ERROR error; |
| 25 |
+ espeak_ERROR error = EE_OK; |
| 26 |
char markbuff[50]; |
| 27 |
- static struct espeak_entry_t *current = NULL; |
| 28 |
|
| 29 |
if (current != queue_peek(synth_queue)) { |
| 30 |
if (current) |
| 31 |
free_espeak_entry(current); |
| 32 |
- current = (struct espeak_entry_t *) queue_remove(synth_queue); |
| 33 |
+ current = queue_peek(synth_queue); |
| 34 |
} |
| 35 |
pthread_mutex_unlock(&queue_guard); |
| 36 |
|
| 37 |
@@ -386,9 +386,25 @@ static void queue_process_entry(struct synth_t *s) |
| 38 |
break; |
| 39 |
} |
| 40 |
|
| 41 |
+ pthread_mutex_lock(&queue_guard); |
| 42 |
if (error == EE_OK) { |
| 43 |
+ /* Processed, drop it */ |
| 44 |
+ struct espeak_entry_t *unqueued = queue_remove(synth_queue); |
| 45 |
+ assert(unqueued == current); |
| 46 |
free_espeak_entry(current); |
| 47 |
current = NULL; |
| 48 |
+ } else { |
| 49 |
+ if (error == EE_BUFFER_FULL) |
| 50 |
+ { |
| 51 |
+ /* Give speak a little break before retrying */ |
| 52 |
+ struct timespec timeout; |
| 53 |
+ clock_gettime(CLOCK_REALTIME, &timeout); |
| 54 |
+ timeout.tv_sec++; |
| 55 |
+ /* But wake up immediately if we have to stop */ |
| 56 |
+ pthread_cond_timedwait(&wake_stop, &queue_guard, &timeout); |
| 57 |
+ } |
| 58 |
+ else |
| 59 |
+ fprintf(stderr, "espeak error: %d\n", error); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@@ -450,6 +466,7 @@ void *espeak_thread(void *arg) |
| 64 |
pthread_cond_wait(&runner_awake, &queue_guard); |
| 65 |
|
| 66 |
if (stop_requested) { |
| 67 |
+ current = NULL; |
| 68 |
stop_speech(); |
| 69 |
synth_queue_clear(); |
| 70 |
stop_requested = 0; |
| 71 |
@@ -458,7 +475,6 @@ void *espeak_thread(void *arg) |
| 72 |
|
| 73 |
while (should_run && queue_peek(synth_queue) && !stop_requested) { |
| 74 |
queue_process_entry(s); |
| 75 |
- pthread_mutex_lock(&queue_guard); |
| 76 |
} |
| 77 |
} |
| 78 |
pthread_cond_signal(&stop_acknowledged); |
| 79 |
diff --git a/src/espeakup.c b/src/espeakup.c |
| 80 |
index 86b3a88..baeee11 100644 |
| 81 |
--- a/src/espeakup.c |
| 82 |
+++ b/src/espeakup.c |
| 83 |
@@ -40,6 +40,7 @@ volatile int should_run = 1; |
| 84 |
espeak_AUDIO_OUTPUT audio_mode; |
| 85 |
|
| 86 |
pthread_cond_t runner_awake = PTHREAD_COND_INITIALIZER; |
| 87 |
+pthread_cond_t wake_stop = PTHREAD_COND_INITIALIZER; |
| 88 |
pthread_cond_t stop_acknowledged = PTHREAD_COND_INITIALIZER; |
| 89 |
pthread_mutex_t queue_guard = PTHREAD_MUTEX_INITIALIZER; |
| 90 |
|
| 91 |
diff --git a/src/espeakup.h b/src/espeakup.h |
| 92 |
index 58d0f6e..62a5ca8 100644 |
| 93 |
--- a/src/espeakup.h |
| 94 |
+++ b/src/espeakup.h |
| 95 |
@@ -99,6 +99,7 @@ extern int self_pipe_fds[2]; |
| 96 |
#define PIPE_WRITE_FD (self_pipe_fds[1]) |
| 97 |
|
| 98 |
extern pthread_cond_t runner_awake; |
| 99 |
+extern pthread_cond_t wake_stop; |
| 100 |
extern pthread_cond_t stop_acknowledged; |
| 101 |
extern pthread_mutex_t queue_guard; |
| 102 |
|
| 103 |
diff --git a/src/softsynth.c b/src/softsynth.c |
| 104 |
index 9eb75a4..ca2e47e 100644 |
| 105 |
--- a/src/softsynth.c |
| 106 |
+++ b/src/softsynth.c |
| 107 |
@@ -228,6 +228,7 @@ static void request_espeak_stop(void) |
| 108 |
pthread_mutex_lock(&queue_guard); |
| 109 |
stop_requested = 1; |
| 110 |
pthread_cond_signal(&runner_awake); // Wake runner, if necessary. |
| 111 |
+ pthread_cond_signal(&wake_stop); // Wake runner, if necessary. |
| 112 |
while (should_run && stop_requested) |
| 113 |
// wait for acknowledgement. |
| 114 |
pthread_cond_wait(&stop_acknowledged, &queue_guard); |