| 1 |
/* |
| 2 |
* Copyright (C) 2005 to 2013 by Jonathan Duddington |
| 3 |
* email: [email protected] |
| 4 |
* Copyright (C) 2015-2016 Reece H. Dunn |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 3 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, see: <http://www.gnu.org/licenses/>. |
| 18 |
*/ |
| 19 |
|
| 20 |
// this version keeps wavemult window as a constant fraction |
| 21 |
// of the cycle length - but that spreads out the HF peaks too much |
| 22 |
|
| 23 |
#include "espeak__config.h" |
| 24 |
|
| 25 |
#include <math.h> |
| 26 |
#include <stdbool.h> |
| 27 |
#include <stdint.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#include "espeak_ng.h" |
| 33 |
#include "speak_lib.h" |
| 34 |
|
| 35 |
#include "wavegen.h" |
| 36 |
#include "common.h" // for espeak_rand |
| 37 |
#include "synthesize.h" // for WGEN_DATA, RESONATOR, frame_t |
| 38 |
#include "mbrola.h" // for MbrolaFill, MbrolaReset, mbrola... |
| 39 |
|
| 40 |
#include "melder.h" // for tracing |
| 41 |
|
| 42 |
#if USE_KLATT |
| 43 |
#include "klatt.h" |
| 44 |
#endif |
| 45 |
|
| 46 |
#if USE_LIBSONIC |
| 47 |
#include "sonic.h" |
| 48 |
#endif |
| 49 |
|
| 50 |
#include "sintab.h" |
| 51 |
#include "speech.h" |
| 52 |
|
| 53 |
static void SetSynth(int length, int modn, frame_t *fr1, frame_t *fr2, voice_t *v); |
| 54 |
|
| 55 |
static voice_t *wvoice = NULL; |
| 56 |
|
| 57 |
static int option_harmonic1 = 10; |
| 58 |
static int flutter_amp = 64; |
| 59 |
|
| 60 |
static int general_amplitude = 60; |
| 61 |
static int consonant_amp = 26; |
| 62 |
|
| 63 |
int embedded_value[N_EMBEDDED_VALUES]; |
| 64 |
|
| 65 |
static int PHASE_INC_FACTOR; |
| 66 |
int samplerate = 0; // this is set by Wavegeninit() |
| 67 |
|
| 68 |
static wavegen_peaks_t peaks[N_PEAKS]; |
| 69 |
static int peak_harmonic[N_PEAKS]; |
| 70 |
static int peak_height[N_PEAKS]; |
| 71 |
|
| 72 |
int echo_head; |
| 73 |
int echo_tail; |
| 74 |
int echo_amp = 0; |
| 75 |
short echo_buf[N_ECHO_BUF]; |
| 76 |
static int echo_length = 0; // period (in sample\) to ensure completion of echo at the end of speech, set in WavegenSetEcho() |
| 77 |
|
| 78 |
static int voicing; |
| 79 |
static RESONATOR rbreath[N_PEAKS]; |
| 80 |
|
| 81 |
#define N_LOWHARM 30 |
| 82 |
#define MAX_HARMONIC 400 // 400 * 50Hz = 20 kHz, more than enough |
| 83 |
static int harm_inc[N_LOWHARM]; // only for these harmonics do we interpolate amplitude between steps |
| 84 |
static int *harmspect; |
| 85 |
static int hswitch = 0; |
| 86 |
static int hspect[2][MAX_HARMONIC]; // 2 copies, we interpolate between then |
| 87 |
|
| 88 |
static int nsamples = 0; // number to do |
| 89 |
static int modulation_type = 0; |
| 90 |
static int glottal_flag = 0; |
| 91 |
static int glottal_reduce = 0; |
| 92 |
|
| 93 |
static WGEN_DATA wdata; |
| 94 |
|
| 95 |
static int amp_ix; |
| 96 |
static int amp_inc; |
| 97 |
static unsigned char *amplitude_env = NULL; |
| 98 |
|
| 99 |
static int samplecount = 0; // number done |
| 100 |
static int samplecount_start = 0; // count at start of this segment |
| 101 |
static int end_wave = 0; // continue to end of wave cycle |
| 102 |
static int wavephase; |
| 103 |
static int phaseinc; |
| 104 |
static int cycle_samples; // number of samples in a cycle at current pitch |
| 105 |
static int cbytes; |
| 106 |
static int hf_factor; |
| 107 |
|
| 108 |
static double minus_pi_t; |
| 109 |
static double two_pi_t; |
| 110 |
|
| 111 |
unsigned char *out_ptr; |
| 112 |
unsigned char *out_end; |
| 113 |
|
| 114 |
espeak_ng_OUTPUT_HOOKS* output_hooks = NULL; |
| 115 |
static int const_f0 = 0; |
| 116 |
|
| 117 |
// the queue of operations passed to wavegen from sythesize |
| 118 |
intptr_t wcmdq[N_WCMDQ][4]; |
| 119 |
int wcmdq_head = 0; |
| 120 |
int wcmdq_tail = 0; |
| 121 |
|
| 122 |
// pitch,speed, |
| 123 |
const int embedded_default[N_EMBEDDED_VALUES] = { 0, 50, espeakRATE_NORMAL, 100, 50, 0, 0, 0, espeakRATE_NORMAL, 0, 0, 0, 0, 0, 0 }; |
| 124 |
static const int embedded_max[N_EMBEDDED_VALUES] = { 0, 0x7fff, 2000, 300, 99, 99, 99, 0, 2000, 0, 0, 0, 0, 4, 0 }; |
| 125 |
|
| 126 |
#if USE_LIBSONIC |
| 127 |
static sonicStream sonicSpeedupStream = NULL; |
| 128 |
static double sonicSpeed = 1.0; |
| 129 |
#endif |
| 130 |
|
| 131 |
// 1st index=roughness |
| 132 |
// 2nd index=modulation_type |
| 133 |
// value: bits 0-3 amplitude (16ths), bits 4-7 every n cycles |
| 134 |
#define N_ROUGHNESS 8 |
| 135 |
static const unsigned char modulation_tab[N_ROUGHNESS][8] = { |
| 136 |
{ 0, 0x00, 0x00, 0x00, 0, 0x46, 0xf2, 0x29 }, |
| 137 |
{ 0, 0x2f, 0x00, 0x2f, 0, 0x45, 0xf2, 0x29 }, |
| 138 |
{ 0, 0x2f, 0x00, 0x2e, 0, 0x45, 0xf2, 0x28 }, |
| 139 |
{ 0, 0x2e, 0x00, 0x2d, 0, 0x34, 0xf2, 0x28 }, |
| 140 |
{ 0, 0x2d, 0x2d, 0x2c, 0, 0x34, 0xf2, 0x28 }, |
| 141 |
{ 0, 0x2b, 0x2b, 0x2b, 0, 0x34, 0xf2, 0x28 }, |
| 142 |
{ 0, 0x2a, 0x2a, 0x2a, 0, 0x34, 0xf2, 0x28 }, |
| 143 |
{ 0, 0x29, 0x29, 0x29, 0, 0x34, 0xf2, 0x28 }, |
| 144 |
}; |
| 145 |
|
| 146 |
// Flutter table, to add natural variations to the pitch |
| 147 |
#define N_FLUTTER 0x170 |
| 148 |
static int Flutter_inc; |
| 149 |
static const unsigned char Flutter_tab[N_FLUTTER] = { |
| 150 |
0x80, 0x9b, 0xb5, 0xcb, 0xdc, 0xe8, 0xed, 0xec, |
| 151 |
0xe6, 0xdc, 0xce, 0xbf, 0xb0, 0xa3, 0x98, 0x90, |
| 152 |
0x8c, 0x8b, 0x8c, 0x8f, 0x92, 0x94, 0x95, 0x92, |
| 153 |
0x8c, 0x83, 0x78, 0x69, 0x59, 0x49, 0x3c, 0x31, |
| 154 |
0x2a, 0x29, 0x2d, 0x36, 0x44, 0x56, 0x69, 0x7d, |
| 155 |
0x8f, 0x9f, 0xaa, 0xb1, 0xb2, 0xad, 0xa4, 0x96, |
| 156 |
0x87, 0x78, 0x69, 0x5c, 0x53, 0x4f, 0x4f, 0x55, |
| 157 |
0x5e, 0x6b, 0x7a, 0x88, 0x96, 0xa2, 0xab, 0xb0, |
| 158 |
|
| 159 |
0xb1, 0xae, 0xa8, 0xa0, 0x98, 0x91, 0x8b, 0x88, |
| 160 |
0x89, 0x8d, 0x94, 0x9d, 0xa8, 0xb2, 0xbb, 0xc0, |
| 161 |
0xc1, 0xbd, 0xb4, 0xa5, 0x92, 0x7c, 0x63, 0x4a, |
| 162 |
0x32, 0x1e, 0x0e, 0x05, 0x02, 0x05, 0x0f, 0x1e, |
| 163 |
0x30, 0x44, 0x59, 0x6d, 0x7f, 0x8c, 0x96, 0x9c, |
| 164 |
0x9f, 0x9f, 0x9d, 0x9b, 0x99, 0x99, 0x9c, 0xa1, |
| 165 |
0xa9, 0xb3, 0xbf, 0xca, 0xd5, 0xdc, 0xe0, 0xde, |
| 166 |
0xd8, 0xcc, 0xbb, 0xa6, 0x8f, 0x77, 0x60, 0x4b, |
| 167 |
|
| 168 |
0x3a, 0x2e, 0x28, 0x29, 0x2f, 0x3a, 0x48, 0x59, |
| 169 |
0x6a, 0x7a, 0x86, 0x90, 0x94, 0x95, 0x91, 0x89, |
| 170 |
0x80, 0x75, 0x6b, 0x62, 0x5c, 0x5a, 0x5c, 0x61, |
| 171 |
0x69, 0x74, 0x80, 0x8a, 0x94, 0x9a, 0x9e, 0x9d, |
| 172 |
0x98, 0x90, 0x86, 0x7c, 0x71, 0x68, 0x62, 0x60, |
| 173 |
0x63, 0x6b, 0x78, 0x88, 0x9b, 0xaf, 0xc2, 0xd2, |
| 174 |
0xdf, 0xe6, 0xe7, 0xe2, 0xd7, 0xc6, 0xb2, 0x9c, |
| 175 |
0x84, 0x6f, 0x5b, 0x4b, 0x40, 0x39, 0x37, 0x38, |
| 176 |
|
| 177 |
0x3d, 0x43, 0x4a, 0x50, 0x54, 0x56, 0x55, 0x52, |
| 178 |
0x4d, 0x48, 0x42, 0x3f, 0x3e, 0x41, 0x49, 0x56, |
| 179 |
0x67, 0x7c, 0x93, 0xab, 0xc3, 0xd9, 0xea, 0xf6, |
| 180 |
0xfc, 0xfb, 0xf4, 0xe7, 0xd5, 0xc0, 0xaa, 0x94, |
| 181 |
0x80, 0x71, 0x64, 0x5d, 0x5a, 0x5c, 0x61, 0x68, |
| 182 |
0x70, 0x77, 0x7d, 0x7f, 0x7f, 0x7b, 0x74, 0x6b, |
| 183 |
0x61, 0x57, 0x4e, 0x48, 0x46, 0x48, 0x4e, 0x59, |
| 184 |
0x66, 0x75, 0x84, 0x93, 0x9f, 0xa7, 0xab, 0xaa, |
| 185 |
|
| 186 |
0xa4, 0x99, 0x8b, 0x7b, 0x6a, 0x5b, 0x4e, 0x46, |
| 187 |
0x43, 0x45, 0x4d, 0x5a, 0x6b, 0x7f, 0x92, 0xa6, |
| 188 |
0xb8, 0xc5, 0xcf, 0xd3, 0xd2, 0xcd, 0xc4, 0xb9, |
| 189 |
0xad, 0xa1, 0x96, 0x8e, 0x89, 0x87, 0x87, 0x8a, |
| 190 |
0x8d, 0x91, 0x92, 0x91, 0x8c, 0x84, 0x78, 0x68, |
| 191 |
0x55, 0x41, 0x2e, 0x1c, 0x0e, 0x05, 0x01, 0x05, |
| 192 |
0x0f, 0x1f, 0x34, 0x4d, 0x68, 0x81, 0x9a, 0xb0, |
| 193 |
0xc1, 0xcd, 0xd3, 0xd3, 0xd0, 0xc8, 0xbf, 0xb5, |
| 194 |
|
| 195 |
0xab, 0xa4, 0x9f, 0x9c, 0x9d, 0xa0, 0xa5, 0xaa, |
| 196 |
0xae, 0xb1, 0xb0, 0xab, 0xa3, 0x96, 0x87, 0x76, |
| 197 |
0x63, 0x51, 0x42, 0x36, 0x2f, 0x2d, 0x31, 0x3a, |
| 198 |
0x48, 0x59, 0x6b, 0x7e, 0x8e, 0x9c, 0xa6, 0xaa, |
| 199 |
0xa9, 0xa3, 0x98, 0x8a, 0x7b, 0x6c, 0x5d, 0x52, |
| 200 |
0x4a, 0x48, 0x4a, 0x50, 0x5a, 0x67, 0x75, 0x82 |
| 201 |
}; |
| 202 |
|
| 203 |
// waveform shape table for HF peaks, formants 6,7,8 |
| 204 |
#define N_WAVEMULT 128 |
| 205 |
static int wavemult_offset = 0; |
| 206 |
static int wavemult_max = 0; |
| 207 |
|
| 208 |
// the presets are for 22050 Hz sample rate. |
| 209 |
// A different rate will need to recalculate the presets in WavegenInit() |
| 210 |
static unsigned char wavemult[N_WAVEMULT] = { |
| 211 |
0, 0, 0, 2, 3, 5, 8, 11, 14, 18, 22, 27, 32, 37, 43, 49, |
| 212 |
55, 62, 69, 76, 83, 90, 98, 105, 113, 121, 128, 136, 144, 152, 159, 166, |
| 213 |
174, 181, 188, 194, 201, 207, 213, 218, 224, 228, 233, 237, 240, 244, 246, 249, |
| 214 |
251, 252, 253, 253, 253, 253, 252, 251, 249, 246, 244, 240, 237, 233, 228, 224, |
| 215 |
218, 213, 207, 201, 194, 188, 181, 174, 166, 159, 152, 144, 136, 128, 121, 113, |
| 216 |
105, 98, 90, 83, 76, 69, 62, 55, 49, 43, 37, 32, 27, 22, 18, 14, |
| 217 |
11, 8, 5, 3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 218 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 219 |
}; |
| 220 |
|
| 221 |
// set from y = pow(2,x) * 128, x=-1 to 1 |
| 222 |
#define MAX_PITCH_VALUE 101 |
| 223 |
static const unsigned char pitch_adjust_tab[MAX_PITCH_VALUE+1] = { |
| 224 |
64, 65, 66, 67, 68, 69, 70, 71, |
| 225 |
72, 73, 74, 75, 76, 77, 78, 79, |
| 226 |
80, 81, 82, 83, 84, 86, 87, 88, |
| 227 |
89, 91, 92, 93, 94, 96, 97, 98, |
| 228 |
100, 101, 103, 104, 105, 107, 108, 110, |
| 229 |
111, 113, 115, 116, 118, 119, 121, 123, |
| 230 |
124, 126, 128, 130, 132, 133, 135, 137, |
| 231 |
139, 141, 143, 145, 147, 149, 151, 153, |
| 232 |
155, 158, 160, 162, 164, 167, 169, 171, |
| 233 |
174, 176, 179, 181, 184, 186, 189, 191, |
| 234 |
194, 197, 199, 202, 205, 208, 211, 214, |
| 235 |
217, 220, 223, 226, 229, 232, 236, 239, |
| 236 |
242, 246, 249, 252, 254, 255 |
| 237 |
}; |
| 238 |
|
| 239 |
void WcmdqStop(void) |
| 240 |
{ |
| 241 |
wcmdq_head = 0; |
| 242 |
wcmdq_tail = 0; |
| 243 |
|
| 244 |
#if USE_LIBSONIC |
| 245 |
if (sonicSpeedupStream != NULL) { |
| 246 |
sonicDestroyStream(sonicSpeedupStream); |
| 247 |
sonicSpeedupStream = NULL; |
| 248 |
} |
| 249 |
#endif |
| 250 |
|
| 251 |
#if USE_MBROLA |
| 252 |
if (mbrola_name[0] != 0) |
| 253 |
MbrolaReset(); |
| 254 |
#endif |
| 255 |
} |
| 256 |
|
| 257 |
int WcmdqFree(void) |
| 258 |
{ |
| 259 |
int i; |
| 260 |
i = wcmdq_head - wcmdq_tail; |
| 261 |
if (i <= 0) i += N_WCMDQ; |
| 262 |
return i; |
| 263 |
} |
| 264 |
|
| 265 |
int WcmdqUsed(void) |
| 266 |
{ |
| 267 |
return N_WCMDQ - WcmdqFree(); |
| 268 |
} |
| 269 |
|
| 270 |
void WcmdqInc(void) |
| 271 |
{ |
| 272 |
wcmdq_tail++; |
| 273 |
if (wcmdq_tail >= N_WCMDQ) wcmdq_tail = 0; |
| 274 |
//TRACE |
| 275 |
trace (U"Tail now ", wcmdq_tail); |
| 276 |
} |
| 277 |
|
| 278 |
static void WcmdqIncHead(void) |
| 279 |
{ |
| 280 |
MAKE_MEM_UNDEFINED(&wcmdq[wcmdq_head], sizeof(wcmdq[wcmdq_head])); |
| 281 |
wcmdq_head++; |
| 282 |
if (wcmdq_head >= N_WCMDQ) wcmdq_head = 0; |
| 283 |
//TRACE |
| 284 |
trace (U"Head now ", wcmdq_head); |
| 285 |
} |
| 286 |
|
| 287 |
#define PEAKSHAPEW 256 |
| 288 |
|
| 289 |
static const unsigned char pk_shape1[PEAKSHAPEW+1] = { |
| 290 |
255, 254, 254, 254, 254, 254, 253, 253, 252, 251, 251, 250, 249, 248, 247, 246, |
| 291 |
245, 244, 242, 241, 239, 238, 236, 234, 233, 231, 229, 227, 225, 223, 220, 218, |
| 292 |
216, 213, 211, 209, 207, 205, 203, 201, 199, 197, 195, 193, 191, 189, 187, 185, |
| 293 |
183, 180, 178, 176, 173, 171, 169, 166, 164, 161, 159, 156, 154, 151, 148, 146, |
| 294 |
143, 140, 138, 135, 132, 129, 126, 123, 120, 118, 115, 112, 108, 105, 102, 99, |
| 295 |
96, 95, 93, 91, 90, 88, 86, 85, 83, 82, 80, 79, 77, 76, 74, 73, |
| 296 |
72, 70, 69, 68, 67, 66, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, |
| 297 |
55, 54, 53, 52, 52, 51, 50, 50, 49, 48, 48, 47, 47, 46, 46, 46, |
| 298 |
45, 45, 45, 44, 44, 44, 44, 44, 44, 44, 43, 43, 43, 43, 44, 43, |
| 299 |
42, 42, 41, 40, 40, 39, 38, 38, 37, 36, 36, 35, 35, 34, 33, 33, |
| 300 |
32, 32, 31, 30, 30, 29, 29, 28, 28, 27, 26, 26, 25, 25, 24, 24, |
| 301 |
23, 23, 22, 22, 21, 21, 20, 20, 19, 19, 18, 18, 18, 17, 17, 16, |
| 302 |
16, 15, 15, 15, 14, 14, 13, 13, 13, 12, 12, 11, 11, 11, 10, 10, |
| 303 |
10, 9, 9, 9, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 5, 5, |
| 304 |
5, 5, 4, 4, 4, 4, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, |
| 305 |
2, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 306 |
0 |
| 307 |
}; |
| 308 |
|
| 309 |
static const unsigned char pk_shape2[PEAKSHAPEW+1] = { |
| 310 |
255, 254, 254, 254, 254, 254, 254, 254, 254, 254, 253, 253, 253, 253, 252, 252, |
| 311 |
252, 251, 251, 251, 250, 250, 249, 249, 248, 248, 247, 247, 246, 245, 245, 244, |
| 312 |
243, 243, 242, 241, 239, 237, 235, 233, 231, 229, 227, 225, 223, 221, 218, 216, |
| 313 |
213, 211, 208, 205, 203, 200, 197, 194, 191, 187, 184, 181, 178, 174, 171, 167, |
| 314 |
163, 160, 156, 152, 148, 144, 140, 136, 132, 127, 123, 119, 114, 110, 105, 100, |
| 315 |
96, 94, 91, 88, 86, 83, 81, 78, 76, 74, 71, 69, 66, 64, 62, 60, |
| 316 |
57, 55, 53, 51, 49, 47, 44, 42, 40, 38, 36, 34, 32, 30, 29, 27, |
| 317 |
25, 23, 21, 19, 18, 16, 14, 12, 11, 9, 7, 6, 4, 3, 1, 0, |
| 318 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 319 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 320 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 321 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 322 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 323 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 324 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 325 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 326 |
0 |
| 327 |
}; |
| 328 |
|
| 329 |
static const unsigned char *pk_shape; |
| 330 |
|
| 331 |
void WavegenInit(int rate, int wavemult_fact) |
| 332 |
{ |
| 333 |
int ix; |
| 334 |
double x; |
| 335 |
|
| 336 |
if (wavemult_fact == 0) |
| 337 |
wavemult_fact = 60; // default |
| 338 |
|
| 339 |
wvoice = NULL; |
| 340 |
samplerate = rate; |
| 341 |
PHASE_INC_FACTOR = 0x8000000 / samplerate; // assumes pitch is Hz*32 |
| 342 |
Flutter_inc = (64 * samplerate)/rate; |
| 343 |
samplecount = 0; |
| 344 |
nsamples = 0; |
| 345 |
wavephase = 0x7fffffff; |
| 346 |
|
| 347 |
wdata.amplitude = 32; |
| 348 |
wdata.amplitude_fmt = 100; |
| 349 |
|
| 350 |
for (ix = 0; ix < N_EMBEDDED_VALUES; ix++) |
| 351 |
embedded_value[ix] = embedded_default[ix]; |
| 352 |
|
| 353 |
// set up window to generate a spread of harmonics from a |
| 354 |
// single peak for HF peaks |
| 355 |
wavemult_max = (samplerate * wavemult_fact)/(256 * 50); |
| 356 |
if (wavemult_max > N_WAVEMULT) wavemult_max = N_WAVEMULT; |
| 357 |
|
| 358 |
wavemult_offset = wavemult_max/2; |
| 359 |
|
| 360 |
if (samplerate != 22050) { |
| 361 |
// wavemult table has preset values for 22050 Hz, we only need to |
| 362 |
// recalculate them if we have a different sample rate |
| 363 |
for (ix = 0; ix < wavemult_max; ix++) { |
| 364 |
x = 127*(1.0 - cos((M_PI*2)*ix/wavemult_max)); |
| 365 |
wavemult[ix] = (int)x; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
pk_shape = pk_shape2; |
| 370 |
|
| 371 |
#if USE_KLATT |
| 372 |
KlattInit(); |
| 373 |
#endif |
| 374 |
} |
| 375 |
|
| 376 |
void WavegenFini(void) |
| 377 |
{ |
| 378 |
#if USE_KLATT |
| 379 |
KlattFini(); |
| 380 |
#endif |
| 381 |
} |
| 382 |
|
| 383 |
int GetAmplitude(void) |
| 384 |
{ |
| 385 |
int amp; |
| 386 |
|
| 387 |
// normal, none, reduced, moderate, strong |
| 388 |
static const unsigned char amp_emphasis[5] = { 16, 16, 10, 16, 22 }; |
| 389 |
|
| 390 |
amp = (embedded_value[EMBED_A])*55/100; |
| 391 |
general_amplitude = amp * amp_emphasis[embedded_value[EMBED_F]] / 16; |
| 392 |
return general_amplitude; |
| 393 |
} |
| 394 |
|
| 395 |
static void WavegenSetEcho(void) |
| 396 |
{ |
| 397 |
if (wvoice == NULL) |
| 398 |
return; |
| 399 |
|
| 400 |
int delay; |
| 401 |
int amp; |
| 402 |
|
| 403 |
voicing = wvoice->voicing; |
| 404 |
delay = wvoice->echo_delay; |
| 405 |
amp = wvoice->echo_amp; |
| 406 |
|
| 407 |
if (delay >= N_ECHO_BUF) |
| 408 |
delay = N_ECHO_BUF-1; |
| 409 |
if (amp > 100) |
| 410 |
amp = 100; |
| 411 |
|
| 412 |
memset(echo_buf, 0, sizeof(echo_buf)); |
| 413 |
echo_tail = 0; |
| 414 |
|
| 415 |
if (embedded_value[EMBED_H] > 0) { |
| 416 |
// set echo from an embedded command in the text |
| 417 |
amp = embedded_value[EMBED_H]; |
| 418 |
delay = 130; |
| 419 |
} |
| 420 |
|
| 421 |
if (delay == 0) |
| 422 |
amp = 0; |
| 423 |
|
| 424 |
echo_head = (delay * samplerate)/1000; |
| 425 |
echo_length = echo_head; // ensure completion of echo at the end of speech. Use 1 delay period? |
| 426 |
if (amp == 0) |
| 427 |
echo_length = 0; |
| 428 |
if (amp > 20) |
| 429 |
echo_length = echo_head * 2; // perhaps allow 2 echo periods if the echo is loud. |
| 430 |
|
| 431 |
// echo_amp units are 1/256ths of the amplitude of the original sound. |
| 432 |
echo_amp = amp; |
| 433 |
// compensate (partially) for increase in amplitude due to echo |
| 434 |
general_amplitude = GetAmplitude(); |
| 435 |
general_amplitude = ((general_amplitude * (500-amp))/500); |
| 436 |
} |
| 437 |
|
| 438 |
int PeaksToHarmspect(wavegen_peaks_t *wavegenPeaks, int pitch, int *htab, int control) |
| 439 |
{ |
| 440 |
if (wvoice == NULL) |
| 441 |
return 1; |
| 442 |
|
| 443 |
// Calculate the amplitude of each harmonics from the formants |
| 444 |
// Only for formants 0 to 5 |
| 445 |
|
| 446 |
// control 0=initial call, 1=every 64 cycles |
| 447 |
|
| 448 |
// pitch and freqs are Hz<<16 |
| 449 |
|
| 450 |
int f; |
| 451 |
wavegen_peaks_t *p; |
| 452 |
int fp; // centre freq of peak |
| 453 |
int fhi; // high freq of peak |
| 454 |
int h; // harmonic number |
| 455 |
int pk; |
| 456 |
int hmax; |
| 457 |
int hmax_samplerate; // highest harmonic allowed for the samplerate |
| 458 |
int x; |
| 459 |
int h1; |
| 460 |
|
| 461 |
// initialise as much of *out as we will need |
| 462 |
hmax = (wavegenPeaks[wvoice->n_harmonic_peaks].freq + wavegenPeaks[wvoice->n_harmonic_peaks].right)/pitch; |
| 463 |
if (hmax >= MAX_HARMONIC) |
| 464 |
hmax = MAX_HARMONIC-1; |
| 465 |
|
| 466 |
// restrict highest harmonic to half the samplerate |
| 467 |
hmax_samplerate = (((samplerate * 19)/40) << 16)/pitch; // only 95% of Nyquist freq |
| 468 |
|
| 469 |
if (hmax > hmax_samplerate) |
| 470 |
hmax = hmax_samplerate; |
| 471 |
|
| 472 |
for (h = 0; h <= hmax; h++) |
| 473 |
htab[h] = 0; |
| 474 |
|
| 475 |
for (pk = 0; pk <= wvoice->n_harmonic_peaks; pk++) { |
| 476 |
p = &wavegenPeaks[pk]; |
| 477 |
if ((p->height == 0) || (fp = p->freq) == 0) |
| 478 |
continue; |
| 479 |
|
| 480 |
fhi = p->freq + p->right; |
| 481 |
h = ((p->freq - p->left) / pitch) + 1; |
| 482 |
if (h <= 0) h = 1; |
| 483 |
|
| 484 |
for (f = pitch*h; f < fp; f += pitch) |
| 485 |
htab[h++] += pk_shape[(fp-f)/(p->left>>8)] * p->height; |
| 486 |
for (; f < fhi; f += pitch) |
| 487 |
htab[h++] += pk_shape[(f-fp)/(p->right>>8)] * p->height; |
| 488 |
} |
| 489 |
|
| 490 |
int y; |
| 491 |
int h2; |
| 492 |
// increase bass |
| 493 |
y = wavegenPeaks[1].height * 10; // addition as a multiple of 1/256s |
| 494 |
h2 = (1000<<16)/pitch; // decrease until 1000Hz |
| 495 |
if (h2 > 0) { |
| 496 |
x = y/h2; |
| 497 |
h = 1; |
| 498 |
while (y > 0) { |
| 499 |
htab[h++] += y; |
| 500 |
y -= x; |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
// find the nearest harmonic for HF peaks where we don't use shape |
| 505 |
for (; pk < N_PEAKS; pk++) { |
| 506 |
x = wavegenPeaks[pk].height >> 14; |
| 507 |
peak_height[pk] = (x * x * 5)/2; |
| 508 |
|
| 509 |
// find the nearest harmonic for HF peaks where we don't use shape |
| 510 |
if (control == 0) { |
| 511 |
// set this initially, but make changes only at the quiet point |
| 512 |
peak_harmonic[pk] = wavegenPeaks[pk].freq / pitch; |
| 513 |
} |
| 514 |
// only use harmonics up to half the samplerate |
| 515 |
if (peak_harmonic[pk] >= hmax_samplerate) |
| 516 |
peak_height[pk] = 0; |
| 517 |
} |
| 518 |
|
| 519 |
// convert from the square-rooted values |
| 520 |
f = 0; |
| 521 |
for (h = 0; h <= hmax; h++, f += pitch) { |
| 522 |
x = htab[h] >> 15; |
| 523 |
htab[h] = (x * x) >> 8; |
| 524 |
|
| 525 |
int ix; |
| 526 |
if ((ix = (f >> 19)) < N_TONE_ADJUST) |
| 527 |
htab[h] = (htab[h] * wvoice->tone_adjust[ix]) >> 13; // index tone_adjust with Hz/8 |
| 528 |
} |
| 529 |
|
| 530 |
// adjust the amplitude of the first harmonic, affects tonal quality |
| 531 |
h1 = htab[1] * option_harmonic1; |
| 532 |
htab[1] = h1/8; |
| 533 |
|
| 534 |
// calc intermediate increments of LF harmonics |
| 535 |
if (control & 1) { |
| 536 |
for (h = 1; h < N_LOWHARM; h++) |
| 537 |
harm_inc[h] = (htab[h] - harmspect[h]) >> 3; |
| 538 |
} |
| 539 |
|
| 540 |
return hmax; // highest harmonic number |
| 541 |
} |
| 542 |
|
| 543 |
static void AdvanceParameters(void) |
| 544 |
{ |
| 545 |
// Called every 64 samples to increment the formant freq, height, and widths |
| 546 |
if (wvoice == NULL) |
| 547 |
return; |
| 548 |
|
| 549 |
int x; |
| 550 |
int ix; |
| 551 |
static int Flutter_ix = 0; |
| 552 |
|
| 553 |
// advance the pitch |
| 554 |
wdata.pitch_ix += wdata.pitch_inc; |
| 555 |
if ((ix = wdata.pitch_ix>>8) > 127) ix = 127; |
| 556 |
x = wdata.pitch_env[ix] * wdata.pitch_range; |
| 557 |
if (wdata.pitch_env) x = wdata.pitch_env[ix] * wdata.pitch_range; |
| 558 |
wdata.pitch = (x>>8) + wdata.pitch_base; |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
amp_ix += amp_inc; |
| 563 |
|
| 564 |
/* add pitch flutter */ |
| 565 |
if (Flutter_ix >= (N_FLUTTER*64)) |
| 566 |
Flutter_ix = 0; |
| 567 |
x = ((int)(Flutter_tab[Flutter_ix >> 6])-0x80) * flutter_amp; |
| 568 |
Flutter_ix += Flutter_inc; |
| 569 |
wdata.pitch += x; |
| 570 |
|
| 571 |
if(const_f0) |
| 572 |
wdata.pitch = (const_f0<<12); |
| 573 |
|
| 574 |
if (wdata.pitch < 102400) |
| 575 |
wdata.pitch = 102400; // min pitch, 25 Hz (25 << 12) |
| 576 |
|
| 577 |
if (samplecount == samplecount_start) |
| 578 |
return; |
| 579 |
|
| 580 |
for (ix = 0; ix <= wvoice->n_harmonic_peaks; ix++) { |
| 581 |
peaks[ix].freq1 += peaks[ix].freq_inc; |
| 582 |
peaks[ix].freq = (int)peaks[ix].freq1; |
| 583 |
peaks[ix].height1 += peaks[ix].height_inc; |
| 584 |
if ((peaks[ix].height = (int)peaks[ix].height1) < 0) |
| 585 |
peaks[ix].height = 0; |
| 586 |
peaks[ix].left1 += peaks[ix].left_inc; |
| 587 |
peaks[ix].left = (int)peaks[ix].left1; |
| 588 |
if (ix < 3) { |
| 589 |
peaks[ix].right1 += peaks[ix].right_inc; |
| 590 |
peaks[ix].right = (int)peaks[ix].right1; |
| 591 |
} else |
| 592 |
peaks[ix].right = peaks[ix].left; |
| 593 |
} |
| 594 |
for (; ix < 8; ix++) { |
| 595 |
// formants 6,7,8 don't have a width parameter |
| 596 |
if (ix < 7) { |
| 597 |
peaks[ix].freq1 += peaks[ix].freq_inc; |
| 598 |
peaks[ix].freq = (int)peaks[ix].freq1; |
| 599 |
} |
| 600 |
peaks[ix].height1 += peaks[ix].height_inc; |
| 601 |
if ((peaks[ix].height = (int)peaks[ix].height1) < 0) |
| 602 |
peaks[ix].height = 0; |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
static double resonator(RESONATOR *r, double input) |
| 607 |
{ |
| 608 |
double x; |
| 609 |
|
| 610 |
x = r->a * input + r->b * r->x1 + r->c * r->x2; |
| 611 |
r->x2 = r->x1; |
| 612 |
r->x1 = x; |
| 613 |
|
| 614 |
return x; |
| 615 |
} |
| 616 |
|
| 617 |
static void setresonator(RESONATOR *rp, int freq, int bwidth, int init) |
| 618 |
{ |
| 619 |
// freq Frequency of resonator in Hz |
| 620 |
// bwidth Bandwidth of resonator in Hz |
| 621 |
// init Initialize internal data |
| 622 |
|
| 623 |
double x; |
| 624 |
double arg; |
| 625 |
|
| 626 |
if (init) { |
| 627 |
rp->x1 = 0; |
| 628 |
rp->x2 = 0; |
| 629 |
} |
| 630 |
|
| 631 |
arg = minus_pi_t * bwidth; |
| 632 |
x = exp(arg); |
| 633 |
|
| 634 |
rp->c = -(x * x); |
| 635 |
|
| 636 |
arg = two_pi_t * freq; |
| 637 |
rp->b = x * cos(arg) * 2.0; |
| 638 |
|
| 639 |
rp->a = 1.0 - rp->b - rp->c; |
| 640 |
} |
| 641 |
|
| 642 |
void InitBreath(void) |
| 643 |
{ |
| 644 |
int ix; |
| 645 |
|
| 646 |
minus_pi_t = -M_PI / samplerate; |
| 647 |
two_pi_t = -2.0 * minus_pi_t; |
| 648 |
|
| 649 |
for (ix = 0; ix < N_PEAKS; ix++) |
| 650 |
setresonator(&rbreath[ix], 2000, 200, 1); |
| 651 |
} |
| 652 |
|
| 653 |
static void SetBreath(void) |
| 654 |
{ |
| 655 |
int pk; |
| 656 |
|
| 657 |
if (wvoice == NULL || wvoice->breath[0] == 0) |
| 658 |
return; |
| 659 |
|
| 660 |
for (pk = 1; pk < N_PEAKS; pk++) { |
| 661 |
if (wvoice->breath[pk] != 0) { |
| 662 |
// breath[0] indicates that some breath formants are needed |
| 663 |
// set the freq from the current synthesis formant and the width from the voice data |
| 664 |
setresonator(&rbreath[pk], peaks[pk].freq >> 16, wvoice->breathw[pk], 0); |
| 665 |
} |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
static int ApplyBreath(void) |
| 670 |
{ |
| 671 |
if (wvoice == NULL) |
| 672 |
return 0; |
| 673 |
|
| 674 |
int value = 0; |
| 675 |
int noise; |
| 676 |
int ix; |
| 677 |
|
| 678 |
// use two random numbers, for alternate formants |
| 679 |
noise = espeak_rand(-0x2000, 0x1fff); |
| 680 |
|
| 681 |
for (ix = 1; ix < N_PEAKS; ix++) { |
| 682 |
int amp; |
| 683 |
if ((amp = wvoice->breath[ix]) != 0) { |
| 684 |
amp *= (peaks[ix].height >> 14); |
| 685 |
value += (int)resonator(&rbreath[ix], noise) * amp; |
| 686 |
} |
| 687 |
} |
| 688 |
return value; |
| 689 |
} |
| 690 |
|
| 691 |
static int Wavegen(int length, int modulation, bool resume, frame_t *fr1, frame_t *fr2, voice_t *wavegenVoice) |
| 692 |
{ |
| 693 |
if (resume == false) |
| 694 |
SetSynth(length, modulation, fr1, fr2, wavegenVoice); |
| 695 |
|
| 696 |
if (wavegenVoice == NULL) |
| 697 |
return 0; |
| 698 |
|
| 699 |
unsigned short waveph; |
| 700 |
unsigned short theta; |
| 701 |
int total; |
| 702 |
int h; |
| 703 |
int ix; |
| 704 |
int z, z1, z2; |
| 705 |
int echo; |
| 706 |
int ov; |
| 707 |
static int maxh, maxh2; |
| 708 |
int pk; |
| 709 |
signed char c; |
| 710 |
int sample; |
| 711 |
int amp; |
| 712 |
int modn_amp = 1, modn_period; |
| 713 |
static int agc = 256; |
| 714 |
static int h_switch_sign = 0; |
| 715 |
static int cycle_count = 0; |
| 716 |
static int amplitude2 = 0; // adjusted for pitch |
| 717 |
|
| 718 |
// continue until the output buffer is full, or |
| 719 |
// the required number of samples have been produced |
| 720 |
|
| 721 |
for (;;) { |
| 722 |
if ((end_wave == 0) && (samplecount == nsamples)) |
| 723 |
return 0; |
| 724 |
|
| 725 |
if ((samplecount & 0x3f) == 0) { |
| 726 |
// every 64 samples, adjust the parameters |
| 727 |
if (samplecount == 0) { |
| 728 |
hswitch = 0; |
| 729 |
harmspect = hspect[0]; |
| 730 |
maxh2 = PeaksToHarmspect(peaks, wdata.pitch<<4, hspect[0], 0); |
| 731 |
|
| 732 |
// adjust amplitude to compensate for fewer harmonics at higher pitch |
| 733 |
amplitude2 = (wdata.amplitude * (wdata.pitch >> 8) * wdata.amplitude_fmt)/(10000 << 3); |
| 734 |
|
| 735 |
// switch sign of harmonics above about 900Hz, to reduce max peak amplitude |
| 736 |
h_switch_sign = 890 / (wdata.pitch >> 12); |
| 737 |
} else |
| 738 |
AdvanceParameters(); |
| 739 |
|
| 740 |
// pitch is Hz<<12 |
| 741 |
phaseinc = (wdata.pitch>>7) * PHASE_INC_FACTOR; |
| 742 |
cycle_samples = samplerate/(wdata.pitch >> 12); // sr/(pitch*2) |
| 743 |
hf_factor = wdata.pitch >> 11; |
| 744 |
|
| 745 |
maxh = maxh2; |
| 746 |
harmspect = hspect[hswitch]; |
| 747 |
hswitch ^= 1; |
| 748 |
maxh2 = PeaksToHarmspect(peaks, wdata.pitch<<4, hspect[hswitch], 1); |
| 749 |
|
| 750 |
SetBreath(); |
| 751 |
} else if ((samplecount & 0x07) == 0) { |
| 752 |
for (h = 1; h < N_LOWHARM && h <= maxh2 && h <= maxh; h++) |
| 753 |
harmspect[h] += harm_inc[h]; |
| 754 |
|
| 755 |
// bring automatic gain control back towards unity |
| 756 |
if (agc < 256) agc++; |
| 757 |
} |
| 758 |
|
| 759 |
samplecount++; |
| 760 |
|
| 761 |
if (wavephase > 0) { |
| 762 |
wavephase += phaseinc; |
| 763 |
if (wavephase < 0) { |
| 764 |
// sign has changed, reached a quiet point in the waveform |
| 765 |
cbytes = wavemult_offset - (cycle_samples)/2; |
| 766 |
if (samplecount > nsamples) |
| 767 |
return 0; |
| 768 |
|
| 769 |
cycle_count++; |
| 770 |
|
| 771 |
for (pk = wavegenVoice->n_harmonic_peaks+1; pk < N_PEAKS; pk++) { |
| 772 |
// find the nearest harmonic for HF peaks where we don't use shape |
| 773 |
peak_harmonic[pk] = ((peaks[pk].freq / (wdata.pitch*8)) + 1) / 2; |
| 774 |
} |
| 775 |
|
| 776 |
// adjust amplitude to compensate for fewer harmonics at higher pitch |
| 777 |
amplitude2 = (wdata.amplitude * (wdata.pitch >> 8) * wdata.amplitude_fmt)/(10000 << 3); |
| 778 |
|
| 779 |
if (glottal_flag > 0) { |
| 780 |
if (glottal_flag == 3) { |
| 781 |
if ((nsamples-samplecount) < (cycle_samples*2)) { |
| 782 |
// Vowel before glottal-stop. |
| 783 |
// This is the start of the penultimate cycle, reduce its amplitude |
| 784 |
glottal_flag = 2; |
| 785 |
amplitude2 = (amplitude2 * glottal_reduce)/256; |
| 786 |
} |
| 787 |
} else if (glottal_flag == 4) { |
| 788 |
// Vowel following a glottal-stop. |
| 789 |
// This is the start of the second cycle, reduce its amplitude |
| 790 |
glottal_flag = 2; |
| 791 |
amplitude2 = (amplitude2 * glottal_reduce)/256; |
| 792 |
} else |
| 793 |
glottal_flag--; |
| 794 |
} |
| 795 |
|
| 796 |
if (amplitude_env != NULL) { |
| 797 |
// amplitude envelope is only used for creaky voice effect on certain vowels/tones |
| 798 |
if ((ix = amp_ix>>8) > 127) ix = 127; |
| 799 |
amp = amplitude_env[ix]; |
| 800 |
amplitude2 = (amplitude2 * amp)/128; |
| 801 |
} |
| 802 |
|
| 803 |
// introduce roughness into the sound by reducing the amplitude of |
| 804 |
modn_period = 0; |
| 805 |
if (voice->roughness < N_ROUGHNESS) { |
| 806 |
modn_period = modulation_tab[voice->roughness][modulation_type]; |
| 807 |
modn_amp = modn_period & 0xf; |
| 808 |
modn_period = modn_period >> 4; |
| 809 |
} |
| 810 |
|
| 811 |
if (modn_period != 0) { |
| 812 |
if (modn_period == 0xf) { |
| 813 |
// just once */ |
| 814 |
amplitude2 = (amplitude2 * modn_amp)/16; |
| 815 |
modulation_type = 0; |
| 816 |
} else { |
| 817 |
// reduce amplitude every [modn_period} cycles |
| 818 |
if ((cycle_count % modn_period) == 0) |
| 819 |
amplitude2 = (amplitude2 * modn_amp)/16; |
| 820 |
} |
| 821 |
} |
| 822 |
} |
| 823 |
} else |
| 824 |
wavephase += phaseinc; |
| 825 |
waveph = (unsigned short)(wavephase >> 16); |
| 826 |
total = 0; |
| 827 |
|
| 828 |
// apply HF peaks, formants 6,7,8 |
| 829 |
// add a single harmonic and then spread this my multiplying by a |
| 830 |
// window. This is to reduce the processing power needed to add the |
| 831 |
// higher frequence harmonics. |
| 832 |
cbytes++; |
| 833 |
if (cbytes >= 0 && cbytes < wavemult_max) { |
| 834 |
for (pk = wavegenVoice->n_harmonic_peaks+1; pk < N_PEAKS; pk++) { |
| 835 |
theta = peak_harmonic[pk] * waveph; |
| 836 |
total += (long)sin_tab[theta >> 5] * peak_height[pk]; |
| 837 |
} |
| 838 |
|
| 839 |
// spread the peaks by multiplying by a window |
| 840 |
total = (long)(total / hf_factor) * wavemult[cbytes]; |
| 841 |
} |
| 842 |
|
| 843 |
// apply main peaks, formants 0 to 5 |
| 844 |
theta = waveph; |
| 845 |
|
| 846 |
for (h = 1; h <= h_switch_sign; h++) { |
| 847 |
total += ((int)sin_tab[theta >> 5] * harmspect[h]); |
| 848 |
theta += waveph; |
| 849 |
} |
| 850 |
while (h <= maxh) { |
| 851 |
total -= ((int)sin_tab[theta >> 5] * harmspect[h]); |
| 852 |
theta += waveph; |
| 853 |
h++; |
| 854 |
} |
| 855 |
|
| 856 |
if (voicing != 64) |
| 857 |
total = (total >> 6) * voicing; |
| 858 |
|
| 859 |
if (wavegenVoice->breath[0]) |
| 860 |
total += ApplyBreath(); |
| 861 |
|
| 862 |
// mix with sampled wave if required |
| 863 |
z2 = 0; |
| 864 |
if (wdata.mix_wavefile_ix < wdata.n_mix_wavefile) { |
| 865 |
if (wdata.mix_wave_scale == 0) { |
| 866 |
// a 16 bit sample |
| 867 |
c = wdata.mix_wavefile[wdata.mix_wavefile_ix+wdata.mix_wavefile_offset+1]; |
| 868 |
sample = wdata.mix_wavefile[wdata.mix_wavefile_ix+wdata.mix_wavefile_offset] + (c * 256); |
| 869 |
wdata.mix_wavefile_ix += 2; |
| 870 |
} else { |
| 871 |
// a 8 bit sample, scaled |
| 872 |
sample = (signed char)wdata.mix_wavefile[wdata.mix_wavefile_offset+wdata.mix_wavefile_ix++] * wdata.mix_wave_scale; |
| 873 |
} |
| 874 |
z2 = (sample * wdata.amplitude_v) >> 10; |
| 875 |
z2 = (z2 * wdata.mix_wave_amp)/32; |
| 876 |
|
| 877 |
if ((wdata.mix_wavefile_ix + wdata.mix_wavefile_offset) >= wdata.mix_wavefile_max) // reached the end of available WAV data |
| 878 |
wdata.mix_wavefile_offset -= (wdata.mix_wavefile_max*3)/4; |
| 879 |
} |
| 880 |
|
| 881 |
z1 = z2 + (((total>>8) * amplitude2) >> 13); |
| 882 |
|
| 883 |
echo = (echo_buf[echo_tail++] * echo_amp); |
| 884 |
z1 += echo >> 8; |
| 885 |
if (echo_tail >= N_ECHO_BUF) |
| 886 |
echo_tail = 0; |
| 887 |
|
| 888 |
z = (z1 * agc) >> 8; |
| 889 |
|
| 890 |
// check for overflow, 16bit signed samples |
| 891 |
if (z >= 32768) { |
| 892 |
ov = 8388608/z1 - 1; // 8388608 is 2^23, i.e. max value * 256 |
| 893 |
if (ov < agc) agc = ov; // set agc to number of 1/256ths to multiply the sample by |
| 894 |
z = (z1 * agc) >> 8; // reduce sample by agc value to prevent overflow |
| 895 |
} else if (z <= -32768) { |
| 896 |
ov = -8388608/z1 - 1; |
| 897 |
if (ov < agc) agc = ov; |
| 898 |
z = (z1 * agc) >> 8; |
| 899 |
} |
| 900 |
*out_ptr++ = z; |
| 901 |
*out_ptr++ = z >> 8; |
| 902 |
if(output_hooks && output_hooks->outputVoiced) output_hooks->outputVoiced(z); |
| 903 |
|
| 904 |
echo_buf[echo_head++] = z; |
| 905 |
if (echo_head >= N_ECHO_BUF) |
| 906 |
echo_head = 0; |
| 907 |
|
| 908 |
if (out_ptr + 2 > out_end) |
| 909 |
return 1; |
| 910 |
} |
| 911 |
} |
| 912 |
|
| 913 |
static int PlaySilence(int length, bool resume) |
| 914 |
{ |
| 915 |
static int n_samples; |
| 916 |
|
| 917 |
nsamples = 0; |
| 918 |
samplecount = 0; |
| 919 |
wavephase = 0x7fffffff; |
| 920 |
|
| 921 |
if (length == 0) |
| 922 |
return 0; |
| 923 |
|
| 924 |
if (resume == false) |
| 925 |
n_samples = length; |
| 926 |
|
| 927 |
int value = 0; |
| 928 |
while (n_samples-- > 0) { |
| 929 |
value = (echo_buf[echo_tail++] * echo_amp) >> 8; |
| 930 |
|
| 931 |
if (echo_tail >= N_ECHO_BUF) |
| 932 |
echo_tail = 0; |
| 933 |
|
| 934 |
*out_ptr++ = value; |
| 935 |
*out_ptr++ = value >> 8; |
| 936 |
if(output_hooks && output_hooks->outputSilence) output_hooks->outputSilence(value); |
| 937 |
|
| 938 |
echo_buf[echo_head++] = value; |
| 939 |
if (echo_head >= N_ECHO_BUF) |
| 940 |
echo_head = 0; |
| 941 |
|
| 942 |
if (out_ptr + 2 > out_end) |
| 943 |
return 1; |
| 944 |
} |
| 945 |
return 0; |
| 946 |
} |
| 947 |
|
| 948 |
static int PlayWave(int length, bool resume, unsigned char *data, int scale, int amp) |
| 949 |
{ |
| 950 |
static int n_samples; |
| 951 |
static int ix = 0; |
| 952 |
int value; |
| 953 |
signed char c; |
| 954 |
|
| 955 |
if (resume == false) { |
| 956 |
n_samples = length; |
| 957 |
ix = 0; |
| 958 |
} |
| 959 |
|
| 960 |
nsamples = 0; |
| 961 |
samplecount = 0; |
| 962 |
|
| 963 |
while (n_samples-- > 0) { |
| 964 |
if (scale == 0) { |
| 965 |
// 16 bits data |
| 966 |
c = data[ix+1]; |
| 967 |
value = data[ix] + (c * 256); |
| 968 |
ix += 2; |
| 969 |
} else { |
| 970 |
// 8 bit data, shift by the specified scale factor |
| 971 |
value = (signed char)data[ix++] * scale; |
| 972 |
} |
| 973 |
value *= (consonant_amp * general_amplitude); // reduce strength of consonant |
| 974 |
value = value >> 10; |
| 975 |
value = (value * amp)/32; |
| 976 |
|
| 977 |
value += ((echo_buf[echo_tail++] * echo_amp) >> 8); |
| 978 |
|
| 979 |
if (value > 32767) |
| 980 |
value = 32767; |
| 981 |
else if (value < -32768) |
| 982 |
value = -32768; |
| 983 |
|
| 984 |
if (echo_tail >= N_ECHO_BUF) |
| 985 |
echo_tail = 0; |
| 986 |
|
| 987 |
out_ptr[0] = value; |
| 988 |
out_ptr[1] = value >> 8; |
| 989 |
if(output_hooks && output_hooks->outputUnvoiced) output_hooks->outputUnvoiced(value); |
| 990 |
out_ptr += 2; |
| 991 |
|
| 992 |
echo_buf[echo_head++] = (value*3)/4; |
| 993 |
if (echo_head >= N_ECHO_BUF) |
| 994 |
echo_head = 0; |
| 995 |
|
| 996 |
if (out_ptr + 2 > out_end) |
| 997 |
return 1; |
| 998 |
} |
| 999 |
return 0; |
| 1000 |
} |
| 1001 |
|
| 1002 |
static int SetWithRange0(int value, int max) |
| 1003 |
{ |
| 1004 |
if (value < 0) |
| 1005 |
return 0; |
| 1006 |
if (value > max) |
| 1007 |
return max; |
| 1008 |
return value; |
| 1009 |
} |
| 1010 |
|
| 1011 |
static void SetPitchFormants(void) |
| 1012 |
{ |
| 1013 |
if (wvoice == NULL) |
| 1014 |
return; |
| 1015 |
|
| 1016 |
int ix; |
| 1017 |
int factor = 256; |
| 1018 |
int pitch_value; |
| 1019 |
|
| 1020 |
// adjust formants to give better results for a different voice pitch |
| 1021 |
if ((pitch_value = embedded_value[EMBED_P]) > MAX_PITCH_VALUE) |
| 1022 |
pitch_value = MAX_PITCH_VALUE; |
| 1023 |
|
| 1024 |
if (pitch_value > 50) { |
| 1025 |
// only adjust if the pitch is higher than normal |
| 1026 |
factor = 256 + (25 * (pitch_value - 50))/50; |
| 1027 |
} |
| 1028 |
|
| 1029 |
for (ix = 0; ix <= 5; ix++) |
| 1030 |
wvoice->freq[ix] = (wvoice->freq2[ix] * factor)/256; |
| 1031 |
|
| 1032 |
factor = embedded_value[EMBED_T]*3; |
| 1033 |
wvoice->height[0] = (wvoice->height2[0] * (256 - factor*2))/256; |
| 1034 |
wvoice->height[1] = (wvoice->height2[1] * (256 - factor))/256; |
| 1035 |
} |
| 1036 |
|
| 1037 |
void SetEmbedded(int control, int value) |
| 1038 |
{ |
| 1039 |
// there was an embedded command in the text at this point |
| 1040 |
int sign = 0; |
| 1041 |
int command; |
| 1042 |
|
| 1043 |
command = control & 0x1f; |
| 1044 |
if ((control & 0x60) == 0x60) |
| 1045 |
sign = -1; |
| 1046 |
else if ((control & 0x60) == 0x40) |
| 1047 |
sign = 1; |
| 1048 |
|
| 1049 |
if (command < N_EMBEDDED_VALUES) { |
| 1050 |
if (sign == 0) |
| 1051 |
embedded_value[command] = value; |
| 1052 |
else |
| 1053 |
embedded_value[command] += (value * sign); |
| 1054 |
embedded_value[command] = SetWithRange0(embedded_value[command], embedded_max[command]); |
| 1055 |
} |
| 1056 |
|
| 1057 |
switch (command) |
| 1058 |
{ |
| 1059 |
case EMBED_T: |
| 1060 |
WavegenSetEcho(); // and drop through to case P |
| 1061 |
case EMBED_P: |
| 1062 |
SetPitchFormants(); |
| 1063 |
break; |
| 1064 |
case EMBED_A: // amplitude |
| 1065 |
general_amplitude = GetAmplitude(); |
| 1066 |
break; |
| 1067 |
case EMBED_F: // emphasis |
| 1068 |
general_amplitude = GetAmplitude(); |
| 1069 |
break; |
| 1070 |
case EMBED_H: |
| 1071 |
WavegenSetEcho(); |
| 1072 |
break; |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
void WavegenSetVoice(voice_t *v) |
| 1077 |
{ |
| 1078 |
static voice_t v2; |
| 1079 |
|
| 1080 |
memcpy(&v2, v, sizeof(v2)); |
| 1081 |
wvoice = &v2; |
| 1082 |
|
| 1083 |
if (v->peak_shape == 0) |
| 1084 |
pk_shape = pk_shape1; |
| 1085 |
else |
| 1086 |
pk_shape = pk_shape2; |
| 1087 |
|
| 1088 |
consonant_amp = (v->consonant_amp * 26) /100; |
| 1089 |
if (samplerate <= 11000) { |
| 1090 |
consonant_amp = consonant_amp*2; // emphasize consonants at low sample rates |
| 1091 |
option_harmonic1 = 6; |
| 1092 |
} |
| 1093 |
WavegenSetEcho(); |
| 1094 |
SetPitchFormants(); |
| 1095 |
MarkerEvent(espeakEVENT_SAMPLERATE, 0, wvoice->samplerate, 0, out_ptr); |
| 1096 |
} |
| 1097 |
|
| 1098 |
static void SetAmplitude(int length, unsigned char *amp_env, int value) |
| 1099 |
{ |
| 1100 |
if (wvoice == NULL) |
| 1101 |
return; |
| 1102 |
|
| 1103 |
amp_ix = 0; |
| 1104 |
if (length == 0) |
| 1105 |
amp_inc = 0; |
| 1106 |
else |
| 1107 |
amp_inc = (256 * ENV_LEN * STEPSIZE)/length; |
| 1108 |
|
| 1109 |
wdata.amplitude = (value * general_amplitude)/16; |
| 1110 |
wdata.amplitude_v = (wdata.amplitude * wvoice->consonant_ampv * 15)/100; // for wave mixed with voiced sounds |
| 1111 |
|
| 1112 |
amplitude_env = amp_env; |
| 1113 |
} |
| 1114 |
|
| 1115 |
void SetPitch2(voice_t *voicet, int pitch1, int pitch2, int *pitch_base, int *pitch_range) |
| 1116 |
{ |
| 1117 |
int base; |
| 1118 |
int range; |
| 1119 |
int pitch_value; |
| 1120 |
|
| 1121 |
if (pitch1 > pitch2) { |
| 1122 |
int x; |
| 1123 |
x = pitch1; // swap values |
| 1124 |
pitch1 = pitch2; |
| 1125 |
pitch2 = x; |
| 1126 |
} |
| 1127 |
|
| 1128 |
if ((pitch_value = embedded_value[EMBED_P]) > MAX_PITCH_VALUE) |
| 1129 |
pitch_value = MAX_PITCH_VALUE; |
| 1130 |
pitch_value -= embedded_value[EMBED_T]; // adjust tone for announcing punctuation |
| 1131 |
if (pitch_value < 0) |
| 1132 |
pitch_value = 0; |
| 1133 |
|
| 1134 |
base = (voicet->pitch_base * pitch_adjust_tab[pitch_value])/128; |
| 1135 |
range = (voicet->pitch_range * embedded_value[EMBED_R])/50; |
| 1136 |
|
| 1137 |
// compensate for change in pitch when the range is narrowed or widened |
| 1138 |
base -= (range - voicet->pitch_range)*18; |
| 1139 |
|
| 1140 |
*pitch_base = base + (pitch1 * range)/2; |
| 1141 |
*pitch_range = base + (pitch2 * range)/2 - *pitch_base; |
| 1142 |
} |
| 1143 |
|
| 1144 |
static void SetPitch(int length, unsigned char *env, int pitch1, int pitch2) |
| 1145 |
{ |
| 1146 |
if (wvoice == NULL) |
| 1147 |
return; |
| 1148 |
|
| 1149 |
// length in samples |
| 1150 |
|
| 1151 |
if ((wdata.pitch_env = env) == NULL) |
| 1152 |
wdata.pitch_env = env_fall; // default |
| 1153 |
|
| 1154 |
wdata.pitch_ix = 0; |
| 1155 |
if (length == 0) |
| 1156 |
wdata.pitch_inc = 0; |
| 1157 |
else |
| 1158 |
wdata.pitch_inc = (256 * ENV_LEN * STEPSIZE)/length; |
| 1159 |
|
| 1160 |
SetPitch2(wvoice, pitch1, pitch2, &wdata.pitch_base, &wdata.pitch_range); |
| 1161 |
// set initial pitch |
| 1162 |
wdata.pitch = ((wdata.pitch_env[0] * wdata.pitch_range) >>8) + wdata.pitch_base; // Hz << 12 |
| 1163 |
|
| 1164 |
flutter_amp = wvoice->flutter; |
| 1165 |
} |
| 1166 |
|
| 1167 |
static void SetSynth(int length, int modn, frame_t *fr1, frame_t *fr2, voice_t *v) |
| 1168 |
{ |
| 1169 |
if (wvoice == NULL || v == NULL) |
| 1170 |
return; |
| 1171 |
|
| 1172 |
int ix; |
| 1173 |
double next; |
| 1174 |
int length2; |
| 1175 |
int length4; |
| 1176 |
int qix; |
| 1177 |
static const int glottal_reduce_tab1[4] = { 0x30, 0x30, 0x40, 0x50 }; // vowel before [?], amp * 1/256 |
| 1178 |
static const int glottal_reduce_tab2[4] = { 0x90, 0xa0, 0xb0, 0xc0 }; // vowel after [?], amp * 1/256 |
| 1179 |
|
| 1180 |
end_wave = 1; |
| 1181 |
|
| 1182 |
// any additional information in the param1 ? |
| 1183 |
modulation_type = modn & 0xff; |
| 1184 |
|
| 1185 |
glottal_flag = 0; |
| 1186 |
if (modn & 0x400) { |
| 1187 |
glottal_flag = 3; // before a glottal stop |
| 1188 |
glottal_reduce = glottal_reduce_tab1[(modn >> 8) & 3]; |
| 1189 |
} |
| 1190 |
if (modn & 0x800) { |
| 1191 |
glottal_flag = 4; // after a glottal stop |
| 1192 |
glottal_reduce = glottal_reduce_tab2[(modn >> 8) & 3]; |
| 1193 |
} |
| 1194 |
|
| 1195 |
for (qix = wcmdq_head+1;; qix++) { |
| 1196 |
if (qix >= N_WCMDQ) qix = 0; |
| 1197 |
if (qix == wcmdq_tail) break; |
| 1198 |
|
| 1199 |
int cmd = wcmdq[qix][0]; |
| 1200 |
if (cmd == WCMD_SPECT) { |
| 1201 |
end_wave = 0; // next wave generation is from another spectrum |
| 1202 |
break; |
| 1203 |
} |
| 1204 |
if ((cmd == WCMD_WAVE) || (cmd == WCMD_PAUSE)) |
| 1205 |
break; // next is not from spectrum, so continue until end of wave cycle |
| 1206 |
} |
| 1207 |
|
| 1208 |
// round the length to a multiple of the stepsize |
| 1209 |
length2 = (length + STEPSIZE/2) & ~0x3f; |
| 1210 |
if (length2 == 0) |
| 1211 |
length2 = STEPSIZE; |
| 1212 |
|
| 1213 |
// add this length to any left over from the previous synth |
| 1214 |
samplecount_start = samplecount; |
| 1215 |
nsamples += length2; |
| 1216 |
|
| 1217 |
length4 = length2/4; |
| 1218 |
|
| 1219 |
peaks[7].freq = (7800 * v->freq[7] + v->freqadd[7]*256) << 8; |
| 1220 |
peaks[8].freq = (9000 * v->freq[8] + v->freqadd[8]*256) << 8; |
| 1221 |
|
| 1222 |
for (ix = 0; ix < 8; ix++) { |
| 1223 |
if (ix < 7) { |
| 1224 |
peaks[ix].freq1 = (fr1->ffreq[ix] * v->freq[ix] + v->freqadd[ix]*256) << 8; |
| 1225 |
peaks[ix].freq = (int)peaks[ix].freq1; |
| 1226 |
next = (fr2->ffreq[ix] * v->freq[ix] + v->freqadd[ix]*256) << 8; |
| 1227 |
peaks[ix].freq_inc = ((next - peaks[ix].freq1) * (STEPSIZE/4)) / length4; // lower headroom for fixed point math |
| 1228 |
} |
| 1229 |
|
| 1230 |
peaks[ix].height1 = (fr1->fheight[ix] * v->height[ix]) << 6; |
| 1231 |
peaks[ix].height = (int)peaks[ix].height1; |
| 1232 |
next = (fr2->fheight[ix] * v->height[ix]) << 6; |
| 1233 |
peaks[ix].height_inc = ((next - peaks[ix].height1) * STEPSIZE) / length2; |
| 1234 |
|
| 1235 |
if ((ix <= 5) && (ix <= wvoice->n_harmonic_peaks)) { |
| 1236 |
peaks[ix].left1 = (fr1->fwidth[ix] * v->width[ix]) << 10; |
| 1237 |
peaks[ix].left = (int)peaks[ix].left1; |
| 1238 |
next = (fr2->fwidth[ix] * v->width[ix]) << 10; |
| 1239 |
peaks[ix].left_inc = ((next - peaks[ix].left1) * STEPSIZE) / length2; |
| 1240 |
|
| 1241 |
if (ix < 3) { |
| 1242 |
peaks[ix].right1 = (fr1->fright[ix] * v->width[ix]) << 10; |
| 1243 |
peaks[ix].right = (int)peaks[ix].right1; |
| 1244 |
next = (fr2->fright[ix] * v->width[ix]) << 10; |
| 1245 |
peaks[ix].right_inc = ((next - peaks[ix].right1) * STEPSIZE) / length2; |
| 1246 |
} else |
| 1247 |
peaks[ix].right = peaks[ix].left; |
| 1248 |
} |
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
void Write4Bytes(FILE *f, int value) |
| 1253 |
{ |
| 1254 |
// Write 4 bytes to a file, least significant first |
| 1255 |
int ix; |
| 1256 |
|
| 1257 |
for (ix = 0; ix < 4; ix++) { |
| 1258 |
fputc(value & 0xff, f); |
| 1259 |
value = value >> 8; |
| 1260 |
} |
| 1261 |
} |
| 1262 |
|
| 1263 |
static int WavegenFill2(void) |
| 1264 |
{ |
| 1265 |
// Pick up next wavegen commands from the queue |
| 1266 |
// return: 0 output buffer has been filled |
| 1267 |
// return: 1 input command queue is now empty |
| 1268 |
intptr_t *q; |
| 1269 |
int length; |
| 1270 |
int result; |
| 1271 |
int marker_type; |
| 1272 |
static bool resume = false; |
| 1273 |
static int echo_complete = 0; |
| 1274 |
|
| 1275 |
if (wdata.pitch < 102400) |
| 1276 |
wdata.pitch = 102400; // min pitch, 25 Hz (25 << 12) |
| 1277 |
|
| 1278 |
while (out_ptr < out_end) { |
| 1279 |
if (WcmdqUsed() <= 0) { |
| 1280 |
if (echo_complete > 0) { |
| 1281 |
// continue to play silence until echo is completed |
| 1282 |
resume = PlaySilence(echo_complete, resume); |
| 1283 |
if (resume == true) |
| 1284 |
return 0; // not yet finished |
| 1285 |
} |
| 1286 |
return 1; // queue empty, close sound channel |
| 1287 |
} |
| 1288 |
|
| 1289 |
result = 0; |
| 1290 |
q = wcmdq[wcmdq_head]; |
| 1291 |
length = q[1]; |
| 1292 |
|
| 1293 |
switch (q[0] & 0xff) |
| 1294 |
{ |
| 1295 |
case WCMD_PITCH: |
| 1296 |
SetPitch(length, (unsigned char *)q[2], q[3] >> 16, q[3] & 0xffff); |
| 1297 |
break; |
| 1298 |
case WCMD_PHONEME_ALIGNMENT: |
| 1299 |
{ |
| 1300 |
char* data = (char*)q[1]; |
| 1301 |
output_hooks->outputPhoSymbol(data,q[2]); |
| 1302 |
free(data); |
| 1303 |
} |
| 1304 |
break; |
| 1305 |
case WCMD_PAUSE: |
| 1306 |
if (resume == false) |
| 1307 |
echo_complete -= length; |
| 1308 |
wdata.n_mix_wavefile = 0; |
| 1309 |
wdata.amplitude_fmt = 100; |
| 1310 |
#if USE_KLATT |
| 1311 |
KlattReset(1); |
| 1312 |
#endif |
| 1313 |
result = PlaySilence(length, resume); |
| 1314 |
break; |
| 1315 |
case WCMD_WAVE: |
| 1316 |
echo_complete = echo_length; |
| 1317 |
wdata.n_mix_wavefile = 0; |
| 1318 |
#if USE_KLATT |
| 1319 |
KlattReset(1); |
| 1320 |
#endif |
| 1321 |
result = PlayWave(length, resume, (unsigned char *)q[2], q[3] & 0xff, q[3] >> 8); |
| 1322 |
break; |
| 1323 |
case WCMD_WAVE2: |
| 1324 |
// wave file to be played at the same time as synthesis |
| 1325 |
wdata.mix_wave_amp = q[3] >> 8; |
| 1326 |
wdata.mix_wave_scale = q[3] & 0xff; |
| 1327 |
wdata.n_mix_wavefile = (length & 0xffff); |
| 1328 |
wdata.mix_wavefile_max = (length >> 16) & 0xffff; |
| 1329 |
if (wdata.mix_wave_scale == 0) { |
| 1330 |
wdata.n_mix_wavefile *= 2; |
| 1331 |
wdata.mix_wavefile_max *= 2; |
| 1332 |
} |
| 1333 |
wdata.mix_wavefile_ix = 0; |
| 1334 |
wdata.mix_wavefile_offset = 0; |
| 1335 |
wdata.mix_wavefile = (unsigned char *)q[2]; |
| 1336 |
break; |
| 1337 |
case WCMD_SPECT2: // as WCMD_SPECT but stop any concurrent wave file |
| 1338 |
wdata.n_mix_wavefile = 0; // ... and drop through to WCMD_SPECT case |
| 1339 |
case WCMD_SPECT: |
| 1340 |
echo_complete = echo_length; |
| 1341 |
result = Wavegen(length & 0xffff, q[1] >> 16, resume, (frame_t *)q[2], (frame_t *)q[3], wvoice); |
| 1342 |
break; |
| 1343 |
#if USE_KLATT |
| 1344 |
case WCMD_KLATT2: // as WCMD_SPECT but stop any concurrent wave file |
| 1345 |
wdata.n_mix_wavefile = 0; // ... and drop through to WCMD_SPECT case |
| 1346 |
case WCMD_KLATT: |
| 1347 |
echo_complete = echo_length; |
| 1348 |
result = Wavegen_Klatt(length & 0xffff, resume, (frame_t *)q[2], (frame_t *)q[3], &wdata, wvoice); |
| 1349 |
break; |
| 1350 |
#endif |
| 1351 |
case WCMD_MARKER: { |
| 1352 |
marker_type = q[0] >> 8; |
| 1353 |
//TRACE |
| 1354 |
trace (U"marker ", marker_type, U" ", q [1], U" ", q [2], U" ", q [3]); |
| 1355 |
/* |
| 1356 |
ppgb: |
| 1357 |
These are the possible marker types: |
| 1358 |
espeakEVENT_LIST_TERMINATED = 0, // Retrieval mode: terminates the event list. |
| 1359 |
espeakEVENT_WORD = 1, // Start of word |
| 1360 |
espeakEVENT_SENTENCE = 2, // Start of sentence |
| 1361 |
espeakEVENT_MARK = 3, // Mark |
| 1362 |
espeakEVENT_PLAY = 4, // Audio element |
| 1363 |
espeakEVENT_END = 5, // End of sentence or clause |
| 1364 |
espeakEVENT_MSG_TERMINATED = 6, // End of message |
| 1365 |
espeakEVENT_PHONEME = 7, // Phoneme, if enabled in espeak_Initialize() |
| 1366 |
espeakEVENT_SAMPLERATE = 8 // internal use, set sample rate |
| 1367 |
Of these: |
| 1368 |
- espeakEVENT_PHONEME takes all 8 bytes of the `string` branch of the union `espeak_EVENT.id` (see `speak_lib.h`), |
| 1369 |
which was set in DoPhonemeMarker(). |
| 1370 |
- espeakEVENT_WORD |
| 1371 |
*/ |
| 1372 |
/* |
| 1373 |
Whether the split version occurs always or only in case of espeakEVENT_PHONEME doesn't seme to matter: |
| 1374 |
- here we have only WORD, SENTENCE and PHONEME (also END) |
| 1375 |
- WORD seems to set `id.number`, end there we do see a difference if we are on 64-bit big-endian |
| 1376 |
- however WORD ignores `id.number` (`id.number is used only by SAMPLE_RATE, which doesn't arrive here) |
| 1377 |
- yes, these assumptions are brittle |
| 1378 |
- so we repair only if PHONEME, although we sent a pull request to eSpeak to always split |
| 1379 |
- the eSpeak version will break as soom as WORD starts to interpret `id.number`. |
| 1380 |
*/ |
| 1381 |
/* |
| 1382 |
The incorrect eSpeak version: |
| 1383 |
*/ |
| 1384 |
//MarkerEvent(marker_type, q[1], q[2], q[3], out_ptr); |
| 1385 |
/* |
| 1386 |
The corrected eSpeak-Praat version: |
| 1387 |
*/ |
| 1388 |
Melder_assert (q[1] >= 0); // check that we are allowed to do a signedness conversion |
| 1389 |
Melder_assert (q[1] <= INT32_MAX); // check that we are allowed to convert from a 64-bit intptr_t to a 32-bit int |
| 1390 |
const unsigned int position = (unsigned int) q[1]; |
| 1391 |
if (marker_type == espeakEVENT_PHONEME) { |
| 1392 |
MarkerEvent(marker_type, position, * (int *) & q[2], * ((int *) & q[2] + 1), out_ptr); |
| 1393 |
// ppgb-espeak: split up q [2] into two ints (eSpeak issue #1970, pull request #1973) |
| 1394 |
} else if (marker_type == espeakEVENT_SENTENCE) { |
| 1395 |
MarkerEvent(marker_type, position, 12345, 123456, out_ptr); |
| 1396 |
//MarkerEvent(marker_type, position, q[2], q[3], out_ptr); |
| 1397 |
//MarkerEvent(marker_type, position, * (int *) & q[2], * ((int *) & q[2] + 1), out_ptr); |
| 1398 |
} else if (marker_type == espeakEVENT_WORD) { |
| 1399 |
MarkerEvent(marker_type, position, 1234567, 12345678, out_ptr); |
| 1400 |
//MarkerEvent(marker_type, position, q[2], q[3], out_ptr); |
| 1401 |
//MarkerEvent(marker_type, position, * (int *) & q[2], * ((int *) & q[2] + 1), out_ptr); |
| 1402 |
} else if (marker_type == espeakEVENT_END) { |
| 1403 |
MarkerEvent(marker_type, position, 123456789, 1234567890, out_ptr); |
| 1404 |
//MarkerEvent(marker_type, position, q[2], q[3], out_ptr); |
| 1405 |
//MarkerEvent(marker_type, position, * (int *) & q[2], * ((int *) & q[2] + 1), out_ptr); |
| 1406 |
} else { |
| 1407 |
Melder_crash (U"(WavegenFill2:) Uninterpretable marker type ", marker_type); |
| 1408 |
} |
| 1409 |
break; |
| 1410 |
} case WCMD_AMPLITUDE: |
| 1411 |
SetAmplitude(length, (unsigned char *)q[2], q[3]); |
| 1412 |
break; |
| 1413 |
case WCMD_VOICE: |
| 1414 |
WavegenSetVoice((voice_t *)q[2]); |
| 1415 |
free((voice_t *)q[2]); |
| 1416 |
break; |
| 1417 |
case WCMD_EMBEDDED: |
| 1418 |
SetEmbedded(q[1], q[2]); |
| 1419 |
break; |
| 1420 |
#if USE_MBROLA |
| 1421 |
case WCMD_MBROLA_DATA: |
| 1422 |
if (wvoice != NULL) |
| 1423 |
result = MbrolaFill(length, resume, (general_amplitude * wvoice->voicing)/64); |
| 1424 |
break; |
| 1425 |
#endif |
| 1426 |
case WCMD_FMT_AMPLITUDE: |
| 1427 |
if ((wdata.amplitude_fmt = q[1]) == 0) |
| 1428 |
wdata.amplitude_fmt = 100; // percentage, but value=0 means 100% |
| 1429 |
break; |
| 1430 |
#if USE_LIBSONIC |
| 1431 |
case WCMD_SONIC_SPEED: |
| 1432 |
sonicSpeed = (double)q[1] / 1024; |
| 1433 |
if (sonicSpeedupStream && (sonicSpeed <= 1.0)) { |
| 1434 |
sonicFlushStream(sonicSpeedupStream); |
| 1435 |
int length = (out_end - out_ptr); |
| 1436 |
length = sonicReadShortFromStream(sonicSpeedupStream, (short*)out_ptr, length/2); |
| 1437 |
out_ptr += length * 2; |
| 1438 |
} |
| 1439 |
break; |
| 1440 |
#endif |
| 1441 |
} |
| 1442 |
|
| 1443 |
if (result == 0) { |
| 1444 |
WcmdqIncHead(); |
| 1445 |
resume = false; |
| 1446 |
} else |
| 1447 |
resume = true; |
| 1448 |
} |
| 1449 |
|
| 1450 |
return 0; |
| 1451 |
} |
| 1452 |
|
| 1453 |
#if USE_LIBSONIC |
| 1454 |
// Speed up the audio samples with libsonic. |
| 1455 |
static int SpeedUp(short *outbuf, int length_in, int length_out, int end_of_text) |
| 1456 |
{ |
| 1457 |
if (length_in > 0) { |
| 1458 |
if (sonicSpeedupStream == NULL) |
| 1459 |
sonicSpeedupStream = sonicCreateStream(22050, 1); |
| 1460 |
if (sonicGetSpeed(sonicSpeedupStream) != sonicSpeed) |
| 1461 |
sonicSetSpeed(sonicSpeedupStream, sonicSpeed); |
| 1462 |
|
| 1463 |
sonicWriteShortToStream(sonicSpeedupStream, outbuf, length_in); |
| 1464 |
} |
| 1465 |
|
| 1466 |
if (sonicSpeedupStream == NULL) |
| 1467 |
return 0; |
| 1468 |
|
| 1469 |
if (end_of_text) |
| 1470 |
sonicFlushStream(sonicSpeedupStream); |
| 1471 |
return sonicReadShortFromStream(sonicSpeedupStream, outbuf, length_out); |
| 1472 |
} |
| 1473 |
#endif |
| 1474 |
|
| 1475 |
// Call WavegenFill2, and then speed up the output samples. |
| 1476 |
int WavegenFill(void) |
| 1477 |
{ |
| 1478 |
int finished; |
| 1479 |
#if USE_LIBSONIC |
| 1480 |
unsigned char *p_start; |
| 1481 |
|
| 1482 |
p_start = out_ptr; |
| 1483 |
#endif |
| 1484 |
|
| 1485 |
finished = WavegenFill2(); |
| 1486 |
|
| 1487 |
#if USE_LIBSONIC |
| 1488 |
if (sonicSpeed > 1.0) { |
| 1489 |
int length; |
| 1490 |
int max_length; |
| 1491 |
|
| 1492 |
max_length = (out_end - p_start); |
| 1493 |
length = 2*SpeedUp((short *)p_start, (out_ptr-p_start)/2, max_length/2, finished); |
| 1494 |
out_ptr = p_start + length; |
| 1495 |
|
| 1496 |
if (length >= max_length) |
| 1497 |
finished = 0; // there may be more data to flush |
| 1498 |
} |
| 1499 |
#endif |
| 1500 |
return finished; |
| 1501 |
} |
| 1502 |
|
| 1503 |
#pragma GCC visibility push(default) |
| 1504 |
|
| 1505 |
ESPEAK_NG_API espeak_ng_STATUS |
| 1506 |
espeak_ng_SetOutputHooks(espeak_ng_OUTPUT_HOOKS* hooks) |
| 1507 |
{ |
| 1508 |
output_hooks = hooks; |
| 1509 |
return static_cast<espeak_ng_STATUS> (0); |
| 1510 |
} |
| 1511 |
|
| 1512 |
ESPEAK_NG_API espeak_ng_STATUS |
| 1513 |
espeak_ng_SetConstF0(int f0) |
| 1514 |
{ |
| 1515 |
const_f0 = f0; |
| 1516 |
return ENS_OK; |
| 1517 |
} |
| 1518 |
|
| 1519 |
#pragma GCC visibility pop |