Debian Codegraph @

Drivers/Speech/eSpeak-NG/speech.c

raw
1 /*
2 * BRLTTY - A background process providing access to the console screen (when in
3 * text mode) for a blind person using a refreshable braille display.
4 *
5 * Copyright (C) 1995-2026 by The BRLTTY Developers.
6 *
7 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
8 *
9 * This is free software, placed under the terms of the
10 * GNU Lesser General Public License, as published by the Free Software
11 * Foundation; either version 2.1 of the License, or (at your option) any
12 * later version. Please see the file LICENSE-LGPL for details.
13 *
14 * Web Page: http://brltty.app/
15 *
16 * This software is maintained by Dave Mielke <[email protected]>.
17 */
18
19 #include "prologue.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "log.h"
26 #include "parse.h"
27
28 typedef enum {
29 PARM_PATH,
30 PARM_PUNCTLIST,
31 PARM_VOICE,
32 PARM_MAXRATE
33 } DriverParameter;
34 #define SPKPARMS "path", "punctlist", "voice", "maxrate"
35
36 #include "spk_driver.h"
37
38 #include <espeak-ng/speak_lib.h>
39
40 static int maxrate = espeakRATE_MAXIMUM;
41
42 static void
43 spk_say(SpeechSynthesizer *spk, const unsigned char *buffer, size_t length, size_t count, const unsigned char *attributes)
44 {
45 int result;
46
47 /* add 1 to the length in order to pass along the trailing zero */
48 result = espeak_Synth(buffer, length+1, 0, POS_CHARACTER, 0,
49 espeakCHARS_UTF8, NULL, (void *)spk);
50 if (result != EE_OK)
51 logMessage(LOG_ERR, "eSpeak-NG: Synth() returned error %d", result);
52 }
53
54 static void
55 spk_mute(SpeechSynthesizer *spk)
56 {
57 espeak_Cancel();
58 }
59
60 static int SynthCallback(short *audio, int numsamples, espeak_EVENT *events)
61 {
62 SpeechSynthesizer *spk = events->user_data;
63
64 while (events->type != espeakEVENT_LIST_TERMINATED) {
65 if (events->type == espeakEVENT_WORD)
66 tellSpeechLocation(spk, events->text_position - 1);
67 if (events->type == espeakEVENT_MSG_TERMINATED)
68 tellSpeechFinished(spk);
69 events++;
70 }
71 return 0;
72 }
73
74 static void
75 spk_drain(SpeechSynthesizer *spk)
76 {
77 espeak_Synchronize();
78 }
79
80 static void
81 spk_setVolume(SpeechSynthesizer *spk, unsigned char setting)
82 {
83 int volume = getIntegerSpeechVolume(setting, 50);
84 espeak_SetParameter(espeakVOLUME, volume, 0);
85 }
86
87 static void
88 spk_setRate(SpeechSynthesizer *spk, unsigned char setting)
89 {
90 int h_range = (maxrate - espeakRATE_MINIMUM)/2;
91 int rate = getIntegerSpeechRate(setting, h_range) + espeakRATE_MINIMUM;
92 espeak_SetParameter(espeakRATE, rate, 0);
93 }
94
95 static void
96 spk_setPitch(SpeechSynthesizer *spk, unsigned char setting)
97 {
98 int pitch = getIntegerSpeechPitch(setting, 50);
99 espeak_SetParameter(espeakPITCH, pitch, 0);
100 }
101
102 static void
103 spk_setPunctuation(SpeechSynthesizer *spk, SpeechPunctuation setting)
104 {
105 espeak_PUNCT_TYPE punct;
106
107 if (setting <= SPK_PUNCTUATION_NONE)
108 punct = espeakPUNCT_NONE;
109 else if (setting == SPK_PUNCTUATION_SOME)
110 punct = espeakPUNCT_SOME;
111 else
112 punct = espeakPUNCT_ALL;
113
114 espeak_SetParameter(espeakPUNCTUATION, punct, 0);
115 }
116
117 static int spk_construct(SpeechSynthesizer *spk, char **parameters)
118 {
119 const char *data_path, *voicename, *punctlist;
120 int result;
121
122 spk->setVolume = spk_setVolume;
123 spk->setRate = spk_setRate;
124 spk->setPitch = spk_setPitch;
125 spk->setPunctuation = spk_setPunctuation;
126 spk->drain = spk_drain;
127
128 logMessage(LOG_INFO, "eSpeak-NG version %s", espeak_Info(NULL));
129
130 data_path = parameters[PARM_PATH];
131 if (data_path && !*data_path)
132 data_path = NULL;
133 result = espeak_Initialize(AUDIO_OUTPUT_PLAYBACK, 0, data_path, 0);
134 if (result < 0) {
135 logMessage(LOG_ERR, "eSpeak-NG: initialization failed");
136 return 0;
137 }
138
139 voicename = parameters[PARM_VOICE];
140 if(!voicename || !*voicename)
141 voicename = "en";
142 result = espeak_SetVoiceByName(voicename);
143 if (result != EE_OK) {
144 espeak_VOICE voice_select;
145 memset(&voice_select, 0, sizeof(voice_select));
146 voice_select.languages = voicename;
147 result = espeak_SetVoiceByProperties(&voice_select);
148 }
149 if (result != EE_OK) {
150 logMessage(LOG_ERR, "eSpeak-NG: unable to load voice '%s'", voicename);
151 return 0;
152 }
153
154 punctlist = parameters[PARM_PUNCTLIST];
155 if (punctlist && *punctlist) {
156 wchar_t w_punctlist[strlen(punctlist) + 1];
157 int i = 0;
158 while ((w_punctlist[i] = punctlist[i]) != 0) i++;
159 espeak_SetPunctuationList(w_punctlist);
160 }
161
162 if (parameters[PARM_MAXRATE]) {
163 int val = atoi(parameters[PARM_MAXRATE]);
164 if (val > espeakRATE_MINIMUM) maxrate = val;
165 }
166
167 espeak_SetSynthCallback(SynthCallback);
168
169 return 1;
170 }
171
172 static void spk_destruct(SpeechSynthesizer *spk)
173 {
174 espeak_Cancel();
175 espeak_Terminate();
176 }