compilembrola.c
3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Copyright (C) 2005 to 2014 by Jonathan Duddington
* email: jonsd@users.sourceforge.net
* Copyright (C) 2013-2016 Reece H. Dunn
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see: <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <espeak-ng/espeak_ng.h>
#include <espeak/speak_lib.h>
#include "error.h"
#include "phoneme.h"
#include "speech.h"
#include "synthesize.h"
static const char *basename(const char *filename)
{
const char *current = filename + strlen(filename);
while (current != filename && !(*current == '/' || *current == '\\'))
--current;
return current == filename ? current : current + 1;
}
static unsigned int StringToWord(const char *string)
{
// Pack 4 characters into a word
int ix;
unsigned char c;
unsigned int word;
if (string == NULL)
return 0;
word = 0;
for (ix = 0; ix < 4; ix++) {
if (string[ix] == 0) break;
c = string[ix];
word |= (c << (ix*8));
}
return word;
}
#pragma GCC visibility push(default)
espeak_ng_STATUS espeak_ng_CompileMbrolaVoice(const char *filepath, FILE *log, espeak_ng_ERROR_CONTEXT *context)
{
if (!log) log = stderr;
char *p;
FILE *f_in;
FILE *f_out;
int percent;
int n;
int *pw;
int *pw_end;
int count = 0;
int control;
char phoneme[40];
char phoneme2[40];
char name1[40];
char name2[40];
char mbrola_voice[40];
char buf[sizeof(path_home)+30];
int mbrola_ctrl = 20; // volume in 1/16 ths
MBROLA_TAB data[N_PHONEME_TAB];
strcpy(buf, filepath);
if ((f_in = fopen(buf, "r")) == NULL)
return create_file_error_context(context, errno, buf);
while (fgets(buf, sizeof(phoneme), f_in) != NULL) {
buf[sizeof(phoneme)-1] = 0;
if ((p = strstr(buf, "//")) != NULL)
*p = 0; // truncate line at comment
if (memcmp(buf, "volume", 6) == 0) {
mbrola_ctrl = atoi(&buf[6]);
continue;
}
n = sscanf(buf, "%d %s %s %d %s %s", &control, phoneme, phoneme2, &percent, name1, name2);
if (n >= 5) {
data[count].name = StringToWord(phoneme);
if (strcmp(phoneme2, "NULL") == 0)
data[count].next_phoneme = 0;
else if (strcmp(phoneme2, "VWL") == 0)
data[count].next_phoneme = 2;
else
data[count].next_phoneme = StringToWord(phoneme2);
data[count].mbr_name = 0;
data[count].mbr_name2 = 0;
data[count].percent = percent;
data[count].control = control;
if (strcmp(name1, "NULL") != 0)
data[count].mbr_name = StringToWord(name1);
if (n == 6)
data[count].mbr_name2 = StringToWord(name2);
count++;
}
}
fclose(f_in);
strcpy(mbrola_voice, basename(filepath));
sprintf(buf, "%s/mbrola_ph/%s_phtrans", path_home, mbrola_voice);
if ((f_out = fopen(buf, "wb")) == NULL)
return create_file_error_context(context, errno, buf);
data[count].name = 0; // list terminator
Write4Bytes(f_out, mbrola_ctrl);
pw_end = (int *)(&data[count+1]);
for (pw = (int *)data; pw < pw_end; pw++)
Write4Bytes(f_out, *pw);
fclose(f_out);
fprintf(log, "Mbrola translation file: %s -- %d phonemes\n", buf, count);
return ENS_OK;
}
#pragma GCC visibility pop