format code

This commit is contained in:
Alexander Epaneshnikov 2021-06-17 12:57:40 +03:00 committed by William
commit c528913ad9
8 changed files with 72 additions and 86 deletions

View file

@ -44,8 +44,7 @@ const struct option longOptions[] = {
{"debug", no_argument, NULL, 'd'}, {"debug", no_argument, NULL, 'd'},
{"help", no_argument, NULL, 'h'}, {"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'}, {"version", no_argument, NULL, 'v'},
{0, 0, 0, 0} {0, 0, 0, 0}};
};
static void show_help() static void show_help()
{ {

View file

@ -18,12 +18,12 @@
*/ */
#define _GNU_SOURCE #define _GNU_SOURCE
#include <alsa/asoundlib.h>
#include <assert.h> #include <assert.h>
#include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <alsa/asoundlib.h>
#include <math.h>
#include "espeakup.h" #include "espeakup.h"
@ -47,10 +47,10 @@ const int volumeMultiplier = 22;
volatile int stop_requested = 0; volatile int stop_requested = 0;
int paused_espeak = 1; int paused_espeak = 1;
static int callback(short *wav, int numsamples, espeak_EVENT * events) static int callback(short *wav, int numsamples, espeak_EVENT *events)
{ {
int i; int i;
for (i = 0; events[i].type != espeakEVENT_LIST_TERMINATED; i++) { for (i = 0; events[i].type != espeakEVENT_LIST_TERMINATED; i++) {
if (events[i].type == espeakEVENT_MARK) { if (events[i].type == espeakEVENT_MARK) {
int mark = atoi(events[i].id.name); int mark = atoi(events[i].id.name);
if ((mark < 0) || (mark > 255)) if ((mark < 0) || (mark > 255))
@ -62,7 +62,7 @@ static int callback(short *wav, int numsamples, espeak_EVENT * events)
} }
static espeak_ERROR set_frequency(struct synth_t *s, int freq, static espeak_ERROR set_frequency(struct synth_t *s, int freq,
enum adjust_t adj) enum adjust_t adj)
{ {
espeak_ERROR rc; espeak_ERROR rc;
@ -76,8 +76,7 @@ static espeak_ERROR set_frequency(struct synth_t *s, int freq,
return rc; return rc;
} }
static espeak_ERROR set_pitch(struct synth_t *s, int pitch, static espeak_ERROR set_pitch(struct synth_t *s, int pitch, enum adjust_t adj)
enum adjust_t adj)
{ {
espeak_ERROR rc; espeak_ERROR rc;
@ -91,8 +90,7 @@ static espeak_ERROR set_pitch(struct synth_t *s, int pitch,
return rc; return rc;
} }
static espeak_ERROR set_range(struct synth_t *s, int range, static espeak_ERROR set_range(struct synth_t *s, int range, enum adjust_t adj)
enum adjust_t adj)
{ {
espeak_ERROR rc; espeak_ERROR rc;
@ -107,7 +105,7 @@ static espeak_ERROR set_range(struct synth_t *s, int range,
} }
static espeak_ERROR set_punctuation(struct synth_t *s, int punct, static espeak_ERROR set_punctuation(struct synth_t *s, int punct,
enum adjust_t adj) enum adjust_t adj)
{ {
espeak_ERROR rc; espeak_ERROR rc;
@ -121,8 +119,7 @@ static espeak_ERROR set_punctuation(struct synth_t *s, int punct,
return rc; return rc;
} }
static espeak_ERROR set_rate(struct synth_t *s, int rate, static espeak_ERROR set_rate(struct synth_t *s, int rate, enum adjust_t adj)
enum adjust_t adj)
{ {
espeak_ERROR rc; espeak_ERROR rc;
@ -130,8 +127,7 @@ static espeak_ERROR set_rate(struct synth_t *s, int rate,
rate = -rate; rate = -rate;
if (adj != ADJ_SET) if (adj != ADJ_SET)
rate += s->rate; rate += s->rate;
rc = espeak_SetParameter(espeakRATE, rc = espeak_SetParameter(espeakRATE, rate * rateMultiplier + rateOffset, 0);
rate * rateMultiplier + rateOffset, 0);
if (rc == EE_OK) if (rc == EE_OK)
s->rate = rate; s->rate = rate;
return rc; return rc;
@ -143,8 +139,7 @@ static espeak_ERROR set_voice(struct synth_t *s, char *voice)
espeak_VOICE voice_select; espeak_VOICE voice_select;
rc = espeak_SetVoiceByName(voice); rc = espeak_SetVoiceByName(voice);
if (rc != EE_OK) if (rc != EE_OK) {
{
memset(&voice_select, 0, sizeof(voice_select)); memset(&voice_select, 0, sizeof(voice_select));
voice_select.languages = voice; voice_select.languages = voice;
rc = espeak_SetVoiceByProperties(&voice_select); rc = espeak_SetVoiceByProperties(&voice_select);
@ -161,27 +156,23 @@ static void set_alsa_volume(int vol)
int err; int err;
err = snd_mixer_open(&m, 0); err = snd_mixer_open(&m, 0);
if (err < 0) if (err < 0) {
{
fprintf(stderr, "ALSA mixer open error: %s\n", snd_strerror(err)); fprintf(stderr, "ALSA mixer open error: %s\n", snd_strerror(err));
return; return;
} }
err = snd_mixer_attach(m, "default"); err = snd_mixer_attach(m, "default");
if (err < 0) if (err < 0) {
{
fprintf(stderr, "ALSA mixer attach error: %s\n", snd_strerror(err)); fprintf(stderr, "ALSA mixer attach error: %s\n", snd_strerror(err));
return; return;
} }
err = snd_mixer_selem_register(m, NULL, NULL); err = snd_mixer_selem_register(m, NULL, NULL);
if (err < 0) if (err < 0) {
{
fprintf(stderr, "ALSA mixer load error: %s\n", snd_strerror(err)); fprintf(stderr, "ALSA mixer load error: %s\n", snd_strerror(err));
return; return;
} }
err = snd_mixer_load(m); err = snd_mixer_load(m);
if (err < 0) if (err < 0) {
{
fprintf(stderr, "ALSA mixer load error: %s\n", snd_strerror(err)); fprintf(stderr, "ALSA mixer load error: %s\n", snd_strerror(err));
return; return;
} }
@ -192,10 +183,9 @@ static void set_alsa_volume(int vol)
* volume (80%), and make higher values increase ALSA volume, up to * volume (80%), and make higher values increase ALSA volume, up to
* 100%. */ * 100%. */
int volume = (vol+1) * 50 / 10 + 50; int volume = (vol + 1) * 50 / 10 + 50;
for (e = snd_mixer_first_elem(m); e; e = snd_mixer_elem_next(e)) for (e = snd_mixer_first_elem(m); e; e = snd_mixer_elem_next(e)) {
{
if (snd_mixer_elem_get_type(e) != SND_MIXER_ELEM_SIMPLE) if (snd_mixer_elem_get_type(e) != SND_MIXER_ELEM_SIMPLE)
continue; continue;
if (snd_mixer_selem_is_enumerated(e)) if (snd_mixer_selem_is_enumerated(e))
@ -212,13 +202,12 @@ static void set_alsa_volume(int vol)
if (err == 0 && min < max) { if (err == 0 && min < max) {
if (max - min < 2400) { if (max - min < 2400) {
/* 24dB amplitude is too small for using a logscale */ /* 24dB amplitude is too small for using a logscale */
set = min + volume * (max-min) / 100; set = min + volume * (max - min) / 100;
} else { } else {
/* Use a logscale */ /* Use a logscale */
double volf = volume / 100.; double volf = volume / 100.;
if (min != SND_CTL_TLV_DB_GAIN_MUTE) if (min != SND_CTL_TLV_DB_GAIN_MUTE) {
{ double minf = pow(10, (min - max) / 6000.);
double minf = pow(10, (min-max) / 6000.);
volf = volf * (1 - minf) + minf; volf = volf * (1 - minf) + minf;
} }
set = 6000. * log10(volf) + max; set = 6000. * log10(volf) + max;
@ -227,15 +216,14 @@ static void set_alsa_volume(int vol)
} else { } else {
/* No dB setting, try a linear scale */ /* No dB setting, try a linear scale */
snd_mixer_selem_get_playback_volume_range(e, &min, &max); snd_mixer_selem_get_playback_volume_range(e, &min, &max);
set = min + volume * (max-min) / 100; set = min + volume * (max - min) / 100;
snd_mixer_selem_set_playback_volume_all(e, set); snd_mixer_selem_set_playback_volume_all(e, set);
} }
} }
} }
} }
static espeak_ERROR set_volume(struct synth_t *s, int vol, static espeak_ERROR set_volume(struct synth_t *s, int vol, enum adjust_t adj)
enum adjust_t adj)
{ {
espeak_ERROR rc; espeak_ERROR rc;
@ -243,10 +231,8 @@ static espeak_ERROR set_volume(struct synth_t *s, int vol,
vol = -vol; vol = -vol;
if (adj != ADJ_SET) if (adj != ADJ_SET)
vol += s->volume; vol += s->volume;
rc = espeak_SetParameter(espeakVOLUME, (vol + 1) * volumeMultiplier, rc = espeak_SetParameter(espeakVOLUME, (vol + 1) * volumeMultiplier, 0);
0); if (rc == EE_OK) {
if (rc == EE_OK)
{
s->volume = vol; s->volume = vol;
if (alsaVolume) if (alsaVolume)
set_alsa_volume(vol); set_alsa_volume(vol);
@ -276,24 +262,24 @@ static espeak_ERROR speak_text(struct synth_t *s)
int n; int n;
if (s->buf[0] == ' ') if (s->buf[0] == ' ')
n = asprintf(&buf, n = asprintf(&buf,
"<say-as interpret-as=\"tts:char\">&#32;</say-as>"); "<say-as interpret-as=\"tts:char\">&#32;</say-as>");
else else
n = asprintf(&buf, n = asprintf(&buf,
"<say-as interpret-as=\"characters\">%c</say-as>", "<say-as interpret-as=\"characters\">%c</say-as>",
s->buf[0]); s->buf[0]);
if (n == -1) { if (n == -1) {
/* D'oh. Not much to do on allocation failure. /* D'oh. Not much to do on allocation failure.
* Perhaps espeak will happen to say the character */ * Perhaps espeak will happen to say the character */
rc = espeak_Synth(s->buf, s->len + 1, 0, POS_CHARACTER, rc = espeak_Synth(s->buf, s->len + 1, 0, POS_CHARACTER, 0,
0, synth_mode, NULL, NULL); synth_mode, NULL, NULL);
} else { } else {
rc = espeak_Synth(buf, n + 1, 0, POS_CHARACTER, 0, rc = espeak_Synth(buf, n + 1, 0, POS_CHARACTER, 0, espeakSSML, NULL,
espeakSSML, NULL, NULL); NULL);
free(buf); free(buf);
} }
} else } else
rc = espeak_Synth(s->buf, s->len + 1, 0, POS_CHARACTER, 0, rc = espeak_Synth(s->buf, s->len + 1, 0, POS_CHARACTER, 0, synth_mode,
synth_mode, NULL, NULL); NULL, NULL);
return rc; return rc;
} }
@ -361,9 +347,10 @@ static void queue_process_entry(struct synth_t *s)
error = set_frequency(s, current->value, current->adjust); error = set_frequency(s, current->value, current->adjust);
break; break;
case CMD_SET_MARK: case CMD_SET_MARK:
snprintf(markbuff, sizeof(markbuff), "<mark name=\"%d\"/>", current->value); snprintf(markbuff, sizeof(markbuff), "<mark name=\"%d\"/>",
error = espeak_Synth(markbuff, strlen(markbuff)+1, 0, POS_CHARACTER, current->value);
0, espeakSSML, NULL, NULL); error = espeak_Synth(markbuff, strlen(markbuff) + 1, 0, POS_CHARACTER,
0, espeakSSML, NULL, NULL);
break; break;
case CMD_SET_PITCH: case CMD_SET_PITCH:
error = set_pitch(s, current->value, current->adjust); error = set_pitch(s, current->value, current->adjust);
@ -452,14 +439,13 @@ int initialize_espeak(struct synth_t *s)
* The main thread can add items to the queue in exactly two situations: * The main thread can add items to the queue in exactly two situations:
* 1. We are waiting on runner_awake, or * 1. We are waiting on runner_awake, or
* 2. We are processing an entry that has just been removed from the queue. * 2. We are processing an entry that has just been removed from the queue.
*/ */
void *espeak_thread(void *arg) void *espeak_thread(void *arg)
{ {
struct synth_t *s = (struct synth_t *) arg; struct synth_t *s = (struct synth_t *) arg;
pthread_mutex_lock(&queue_guard); pthread_mutex_lock(&queue_guard);
while (should_run) { while (should_run) {
while (should_run && !queue_peek(synth_queue) && !stop_requested) while (should_run && !queue_peek(synth_queue) && !stop_requested)
pthread_cond_wait(&runner_awake, &queue_guard); pthread_cond_wait(&runner_awake, &queue_guard);

View file

@ -18,13 +18,13 @@
*/ */
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/file.h> #include <sys/file.h>
#include <unistd.h>
#include "espeakup.h" #include "espeakup.h"
@ -78,7 +78,7 @@ int espeakup_start_daemon(void)
/* Child */ /* Child */
if (chdir("/") < 0) { if (chdir("/") < 0) {
c = 1; c = 1;
(void)write(fds[1], &c, 1); (void) write(fds[1], &c, 1);
exit(1); exit(1);
} }
return fds[1]; return fds[1];
@ -99,14 +99,12 @@ int espeakup_is_running(void)
} }
if (flock(pidFile, LOCK_EX) < 0) { if (flock(pidFile, LOCK_EX) < 0) {
printf("Can not lock the pid file %s: %s\n", pidPath, printf("Can not lock the pid file %s: %s\n", pidPath, strerror(errno));
strerror(errno));
goto error; goto error;
} }
n = read(pidFile, s, sizeof(s) - 1); n = read(pidFile, s, sizeof(s) - 1);
if (n < 0) { if (n < 0) {
printf("Can not read the pid file %s: %s\n", pidPath, printf("Can not read the pid file %s: %s\n", pidPath, strerror(errno));
strerror(errno));
goto error; goto error;
} }
s[n] = 0; s[n] = 0;
@ -197,13 +195,13 @@ int main(int argc, char **argv)
sigaddset(&sigset, SIGTERM); sigaddset(&sigset, SIGTERM);
sigprocmask(SIG_BLOCK, &sigset, NULL); sigprocmask(SIG_BLOCK, &sigset, NULL);
/* Initialize espeak */ /* Initialize espeak */
if (initialize_espeak(&s) < 0) { if (initialize_espeak(&s) < 0) {
ret = 2; ret = 2;
goto out; goto out;
} }
/* open the softsynth */ /* open the softsynth */
if (open_softsynth() < 0) { if (open_softsynth() < 0) {
ret = 2; ret = 2;
goto out; goto out;
@ -224,7 +222,7 @@ int main(int argc, char **argv)
} }
if (!debug && espeakup_mode == ESPEAKUP_MODE_SPEAKUP) if (!debug && espeakup_mode == ESPEAKUP_MODE_SPEAKUP)
(void)write(fd, &ret, 1); (void) write(fd, &ret, 1);
/* wait for the threads to shut down. */ /* wait for the threads to shut down. */
pthread_join(signal_thread_id, NULL); pthread_join(signal_thread_id, NULL);
@ -240,7 +238,7 @@ out:
if (ret != 1) if (ret != 1)
unlink(pidPath); unlink(pidPath);
if (ret != 0) if (ret != 0)
(void)write(fd, &ret, 1); (void) write(fd, &ret, 1);
/* If ret was 0, the status byte was written before joining /* If ret was 0, the status byte was written before joining
* the threads. */ * the threads. */
} }

View file

@ -21,8 +21,8 @@
#define __ESPEAKUP_H #define __ESPEAKUP_H
/* This was added for gcc 4.3 */ /* This was added for gcc 4.3 */
#include <stddef.h>
#include <pthread.h> #include <pthread.h>
#include <stddef.h>
#include <espeak-ng/speak_lib.h> #include <espeak-ng/speak_lib.h>
@ -30,12 +30,14 @@
#define PACKAGE_BUGREPORT "https://github.com/linux-speakup/espeakup/issues" #define PACKAGE_BUGREPORT "https://github.com/linux-speakup/espeakup/issues"
enum espeakup_mode_t { enum espeakup_mode_t
{
ESPEAKUP_MODE_SPEAKUP, ESPEAKUP_MODE_SPEAKUP,
ESPEAKUP_MODE_ACSINT ESPEAKUP_MODE_ACSINT
}; };
enum command_t { enum command_t
{
CMD_SET_FREQUENCY, CMD_SET_FREQUENCY,
CMD_SET_MARK, CMD_SET_MARK,
CMD_SET_PITCH, CMD_SET_PITCH,
@ -50,7 +52,8 @@ enum command_t {
CMD_UNKNOWN, CMD_UNKNOWN,
}; };
enum adjust_t { enum adjust_t
{
ADJ_DEC, ADJ_DEC,
ADJ_SET, ADJ_SET,
ADJ_INC, ADJ_INC,

View file

@ -20,7 +20,7 @@
#ifndef __QUEUE_H #ifndef __QUEUE_H
#define __QUEUE_H #define __QUEUE_H
struct queue_t; /* An opaque type. */ struct queue_t; /* An opaque type. */
extern struct queue_t *new_queue(void); extern struct queue_t *new_queue(void);
extern int queue_add(struct queue_t *q, void *entry); extern int queue_add(struct queue_t *q, void *entry);

View file

@ -40,7 +40,7 @@ void *signal_thread(void *arg)
sigset_t sigset; sigset_t sigset;
int sig; int sig;
memset(&temp, 0, sizeof (struct sigaction)); memset(&temp, 0, sizeof(struct sigaction));
/* install dummy handlers for the signals we want to process */ /* install dummy handlers for the signals we want to process */
temp.sa_handler = dummy_handler; temp.sa_handler = dummy_handler;
sigemptyset(&temp.sa_mask); sigemptyset(&temp.sa_mask);

View file

@ -17,14 +17,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <ctype.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include <sys/select.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <sys/select.h>
#include <unistd.h>
#include "espeakup.h" #include "espeakup.h"
#include "stringhandling.h" #include "stringhandling.h"
@ -155,8 +155,7 @@ static int process_command(struct synth_t *s, char *buf, int start)
} }
if (cmd != CMD_FLUSH && cmd != CMD_UNKNOWN) { if (cmd != CMD_FLUSH && cmd != CMD_UNKNOWN) {
if (espeakup_mode == ESPEAKUP_MODE_ACSINT if (espeakup_mode == ESPEAKUP_MODE_ACSINT && textAccumulator_l != 0) {
&& textAccumulator_l != 0) {
queue_add_text(textAccumulator, textAccumulator_l); queue_add_text(textAccumulator, textAccumulator_l);
free(textAccumulator); free(textAccumulator);
textAccumulator = initString(&textAccumulator_l); textAccumulator = initString(&textAccumulator_l);
@ -177,7 +176,8 @@ static void process_buffer(struct synth_t *s, char *buf, ssize_t length)
start = 0; start = 0;
end = 0; end = 0;
while (start < length) { while (start < length) {
while ((buf[end] < 0 || buf[end] >= ' ' || buf[end] == '\n') && end < length) while ((buf[end] < 0 || buf[end] >= ' ' || buf[end] == '\n') &&
end < length)
end++; end++;
if (end != start) { if (end != start) {
txtLen = end - start; txtLen = end - start;
@ -192,8 +192,7 @@ static void process_buffer(struct synth_t *s, char *buf, ssize_t length)
} }
} }
static void process_buffer_acsint(struct synth_t *s, char *buf, static void process_buffer_acsint(struct synth_t *s, char *buf, ssize_t length)
ssize_t length)
{ {
int start = 0; int start = 0;
int i; int i;
@ -207,8 +206,8 @@ static void process_buffer_acsint(struct synth_t *s, char *buf,
break; break;
} }
if (i > start) if (i > start)
stringAndBytes(&textAccumulator, &textAccumulator_l, stringAndBytes(&textAccumulator, &textAccumulator_l, buf + start,
buf + start, i - start); i - start);
if (flushIt) { if (flushIt) {
if (textAccumulator != EMPTYSTRING) { if (textAccumulator != EMPTYSTRING) {
queue_add_text(textAccumulator, textAccumulator_l); queue_add_text(textAccumulator, textAccumulator_l);
@ -228,9 +227,10 @@ static void request_espeak_stop(void)
{ {
pthread_mutex_lock(&queue_guard); pthread_mutex_lock(&queue_guard);
stop_requested = 1; stop_requested = 1;
pthread_cond_signal(&runner_awake); /* Wake runner, if necessary. */ pthread_cond_signal(&runner_awake); /* Wake runner, if necessary. */
while (should_run && stop_requested) while (should_run && stop_requested)
pthread_cond_wait(&stop_acknowledged, &queue_guard); /* wait for acknowledgement. */ pthread_cond_wait(&stop_acknowledged,
&queue_guard); /* wait for acknowledgement. */
pthread_mutex_unlock(&queue_guard); pthread_mutex_unlock(&queue_guard);
} }

View file

@ -75,9 +75,9 @@ void stringAndString(char **s, int *l, const char *t)
oldlen = *l; oldlen = *l;
newlen = oldlen + strlen(t); newlen = oldlen + strlen(t);
*l = newlen; *l = newlen;
++newlen; /* room for the 0 */ ++newlen; /* room for the 0 */
x = oldlen ^ newlen; x = oldlen ^ newlen;
if (x > oldlen) { /* must realloc */ if (x > oldlen) { /* must realloc */
newlen |= (newlen >> 1); newlen |= (newlen >> 1);
newlen |= (newlen >> 2); newlen |= (newlen >> 2);
newlen |= (newlen >> 4); newlen |= (newlen >> 4);
@ -98,7 +98,7 @@ void stringAndBytes(char **s, int *l, const char *t, int cnt)
*l = newlen; *l = newlen;
++newlen; ++newlen;
x = oldlen ^ newlen; x = oldlen ^ newlen;
if (x > oldlen) { /* must realloc */ if (x > oldlen) { /* must realloc */
newlen |= (newlen >> 1); newlen |= (newlen >> 1);
newlen |= (newlen >> 2); newlen |= (newlen >> 2);
newlen |= (newlen >> 4); newlen |= (newlen >> 4);