From 8899405c9ae5738fe4cf12539dcda7f8e44d717d Mon Sep 17 00:00:00 2001 From: Ryan Grindstaff Date: Mon, 14 Apr 2025 02:27:25 +0000 Subject: [PATCH] Add --volume option This adds the command-line option to set the volume via --volume=N When --volume is specified, the volume is locked using a new volumeSet flag to prevent it from being overridden by runtime commands (e.g. from the kernel). volumeMultiplier was also changed from 22 to 16, which maps the default volume to ~100%, keeping the volume consistent when --volume isn't provided --- src/cli.c | 13 +++++++++++++ src/espeak.c | 15 ++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/cli.c b/src/cli.c index 8453587..8f8b471 100644 --- a/src/cli.c +++ b/src/cli.c @@ -35,11 +35,15 @@ extern char *defaultVoice; /* Whether to drive ALSA volume */ extern int alsaVolume; +/* default volume */ +extern int defaultVolume; +extern int volumeSet; /* command line options */ const char *shortOptions = "P:V:adhv"; const struct option longOptions[] = { {"pid-path", required_argument, NULL, 'P'}, {"default-voice", required_argument, NULL, 'V'}, + {"volume", required_argument, NULL, 'x'}, {"alsa-volume", no_argument, &alsaVolume, 1}, {"acsint", no_argument, NULL, 'a'}, {"debug", no_argument, NULL, 'd'}, @@ -57,6 +61,7 @@ static void show_help() printf(" --debug, -d\t\t\t\tDebug mode (stay in the foreground).\n"); printf(" --help, -h\t\t\t\tShow this help.\n"); printf(" --version, -v\t\t\t\tDisplay the software version.\n"); + printf(" --volume=[1-150]\t\t\tSet the volume of speakup.\n"); exit(0); } @@ -95,6 +100,14 @@ void process_cli(int argc, char **argv) case 'v': show_version(); break; + case 'x': + volumeSet = 1; + defaultVolume = atoi(optarg); + if (defaultVolume < 1 || defaultVolume > 150) { + fprintf(stderr, "WARNING: Volume must be between 1 and 150\n"); + volumeSet = 0; + } + break; case -1: case 0: break; diff --git a/src/espeak.c b/src/espeak.c index 90bdfc6..747e15e 100644 --- a/src/espeak.c +++ b/src/espeak.c @@ -32,7 +32,8 @@ const int defaultFrequency = 5; const int defaultPitch = 5; const int defaultRange = 5; const int defaultRate = 2; -const int defaultVolume = 5; +int defaultVolume = 5; +int volumeSet = 0; char *defaultVoice = NULL; int alsaVolume = 0; @@ -42,7 +43,7 @@ const int pitchMultiplier = 11; const int rangeMultiplier = 11; const int rateMultiplier = 41; const int rateOffset = 80; -const int volumeMultiplier = 22; +const int volumeMultiplier = 16; volatile int stop_requested = 0; int paused_espeak = 1; @@ -246,11 +247,19 @@ static espeak_ERROR set_volume(struct synth_t *s, int vol, enum adjust_t adj) { espeak_ERROR rc; + if (adj == ADJ_DEC) vol = -vol; if (adj != ADJ_SET) vol += s->volume; - rc = espeak_SetParameter(espeakVOLUME, (vol + 1) * volumeMultiplier, 0); + + /* use the volume if specified by the user */ + if (volumeSet) { + rc = espeak_SetParameter(espeakVOLUME, defaultVolume, 0); + } + else { + rc = espeak_SetParameter(espeakVOLUME, (vol + 1) * volumeMultiplier, 0); + } if (rc == EE_OK) { s->volume = vol; if (alsaVolume)