forked from kbinani/libvsq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLyric.hpp
More file actions
399 lines (372 loc) · 13.4 KB
/
Lyric.hpp
File metadata and controls
399 lines (372 loc) · 13.4 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
* Lyric.h
* Copyright © 2012 kbinani
*
* This file is part of libvsq.
*
* libvsq is free software; you can redistribute it and/or
* modify it under the terms of the BSD License.
*
* libvsq 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.
*/
#ifndef __Lyric_h__
#define __Lyric_h__
#include "vsqglobal.hpp"
#include "StringUtil.hpp"
#include "PhoneticSymbol.hpp"
#include <string>
#include <vector>
VSQ_BEGIN_NAMESPACE
using namespace std;
using namespace VSQ_NS;
/**
* 歌詞ハンドルに格納する歌詞情報を保持するクラス
* @class table
* @name Lyric
*/
class Lyric
{
public:
/**
* 歌詞
* @var string
*/
std::string phrase;// = "a";
/**
* 歌詞ハンドル内に複数の歌詞が入る場合の、この歌詞の長さ分率。デフォルトは 1.0
* @var double
*/
double lengthRatio;// = 1.0;
/**
* 発音記号がプロテクトされた状態かどうか
* @var boolean
*/
bool isProtected;// = false;
/**
* 発音記号のリスト
* @var table
* @access private
* @todo privateにする
*/
std::vector<std::string> _phoneticSymbol;// = { "a" };
/**
* Consonant Adjustment のリスト
* @var table
* @access private
* @todo privateにする
*/
std::vector<int> _consonantAdjustment;// = { 0 };
/**
* @brief 文字列を元に初期化を行う
* @param line 「"あ","a",0.0000,0.0」などのような文字列
*/
explicit Lyric( const std::string &line ){
if( line.size() == 0 ){
phrase = "a";
setPhoneticSymbol( "a" );
lengthRatio = 1.0;
isProtected = false;
setConsonantAdjustment( "0" );
return;
}
int len = line.size();
int indx = 0;
int dquote_count = 0;
string work = "";
string consonantAdjustment = "";
for( int i = 0; i < len; i++ ){
char c = line[i];
if( c == ',' || i + 1 == len ){
if( i + 1 == len ){
work = work + c;
}
if( dquote_count % 2 == 0 ){
// ,の左側に偶数個の"がある場合→,は区切り文字
indx = indx + 1;
string search = "\"";
if( indx == 1 ){
// phrase
work = StringUtil::replace( work, "\"\"", "\"" ); // "は""として保存される
if( work.find( search ) == 0 && work.find_last_of( search ) == (work.size() - search.size()) ){
int l = work.size();
if( l > search.size() * 2 ){
phrase = work.substr( search.size(), l - search.size() * 2 );
}else{
phrase = "a";
}
}else{
phrase = work;
}
work = "";
}else if( indx == 2 ){
// symbols
string symbols = "";
if( (work.find( search ) == 0) && (work.find_last_of( search ) == (work.size() - search.size())) ){
int l = work.size();
if( l > search.size() * 2 ){
symbols = work.substr( search.size(), l - search.size() * 2 );
}else{
symbols = "a";
}
}else{
symbols = work;
}
setPhoneticSymbol( symbols );
work = "";
}else if( indx == 3 ){
// lengthRatio
lengthRatio = atof( work.c_str() );
work = "";
}else{
if( indx - 3 <= _phoneticSymbol.size() ){
// consonant adjustment
if( indx - 3 == 1 ){
consonantAdjustment = consonantAdjustment + work;
}else{
consonantAdjustment = consonantAdjustment + "," + work;
}
}else{
// protected
isProtected = (work == "1");
}
work = "";
}
}else{
// ,の左側に奇数個の"がある場合→,は歌詞等の一部
work = work + "" + c;
}
}else{
work = work + "" + c;
if( c == '"' ){
dquote_count = dquote_count + 1;
}
}
}
setConsonantAdjustment( consonantAdjustment );
}
/**
* @brief 歌詞、発音記号を指定して初期化を行う
* @param phrase 歌詞
* @param phoneticSymbol 発音記号
*/
explicit Lyric( const std::string &phrase, const std::string &phoneticSymbol ){
this->phrase = phrase;
_consonantAdjustment.clear();
this->setPhoneticSymbol( phoneticSymbol );
lengthRatio = 1.0;
isProtected = false;
}
/**
* @brief このオブジェクトと、指定されたオブジェクトが同じかどうかを調べる。
* 音声合成したときに影響のある範囲のフィールドしか比較されない。
* たとえば、<code>isProtected</code> が <code>this</code> と <code>item</code> で違っていても、他が同一であれば <code>true</code> が返る。
* @param item 比較対象のオブジェクト
* @return 比較対象と同じであれば <code>true</code> を、そうでなければ <code>false</code> を返す
*/
bool equalsForSynth( Lyric &item ) const{
if( getPhoneticSymbol() != item.getPhoneticSymbol() ){
return false;
}
if( getConsonantAdjustment() != item.getConsonantAdjustment() ){
return false;
}
return true;
}
/**
* @brief このオブジェクトのインスタンスと、指定されたオブジェクトが同じかどうかを調べる
* @param item (Lyric) 比較対象のオブジェクト
* @return (boolean) 比較対象と同じであれば <code>true</code> を、そうでなければ <code>false</code> を返す
*/
bool equals( Lyric &item ) const{
if( false == equalsForSynth( item ) ){
return false;
}
if( isProtected != item.isProtected ){
return false;
}
if( phrase != item.phrase ){
return false;
}
if( lengthRatio != item.lengthRatio ){
return false;
}
return true;
}
/**
* @brief Get a string value of consonant adjustment, separated by ",".
* @return A string value of consonant adjustment.
*/
const std::string getConsonantAdjustment() const{
const vector<int> arr = getConsonantAdjustmentList();
if( arr.empty() ){
return "";
}
vector<int>::const_iterator i;
ostringstream ret;
for( i = arr.begin(); i != arr.end(); ++i ){
ret << (i == arr.begin() ? "" : ",") << (*i);
}
return ret.str();
}
/**
* @brief Set consonant adjustment with a string, adjustment value concatenated with ",".
* @param value [in] A string of consonant adjustment. (ex. "64,0")
*/
void setConsonantAdjustment( const std::string &value ){
vector<string> spl = StringUtil::explode( ",", value );
vector<int> arr;
vector<string>::iterator i;
for( i = spl.begin(); i != spl.end(); ++i ){
arr.push_back( (int)atoi( (*i).c_str() ) );
}
setConsonantAdjustmentList( arr );
}
/**
* @brief Consonant Adjustment を、整数配列で取得する
* @return Consonant Adjustment を格納した整数の配列
*/
const std::vector<int> getConsonantAdjustmentList() const{
vector<int> _consonantAdjustment = this->_consonantAdjustment;
if( _consonantAdjustment.empty() ){
if( _phoneticSymbol.empty() ){
_consonantAdjustment.clear();
}else{
_consonantAdjustment.clear();
vector<string>::const_iterator i;
for( i = _phoneticSymbol.begin(); i != _phoneticSymbol.end(); ++i ){
int consonantAdjustment;
if( PhoneticSymbol::isConsonant( (*i) ) ){
consonantAdjustment = 64;
}else{
consonantAdjustment = 0;
}
_consonantAdjustment.push_back( consonantAdjustment );
}
}
}
return _consonantAdjustment;
}
/**
* @brief Consonant Adjustment を、整数配列形式で設定する
* @param value Consonant Adjustment を格納した整数の配列
*/
void setConsonantAdjustmentList( const std::vector<int> &value ){
_consonantAdjustment.clear();
vector<int>::const_iterator i;
for( i = value.begin(); i != value.end(); ++i ){
_consonantAdjustment.push_back( (*i) );
}
}
/**
* @brief コピーを作成する
* @return (Lyric) このインスタンスのコピー
*/
Lyric clone() const{
Lyric result( "", "" );
result.phrase = phrase;
result._phoneticSymbol.clear();
for( int i = 0; i < _phoneticSymbol.size(); i++ ){
result._phoneticSymbol.push_back( _phoneticSymbol[i] );
}
result.lengthRatio = lengthRatio;
result._consonantAdjustment.clear();
for( int i = 0; i < _consonantAdjustment.size(); i++ ){
result._consonantAdjustment.push_back( _consonantAdjustment[i] );
}
result.isProtected = isProtected;
return result;
}
/**
* @brief この歌詞の発音記号を取得する
* @return 発音記号
*/
const std::string getPhoneticSymbol() const{
const vector<string> symbol = getPhoneticSymbolList();
if( symbol.empty() ){
return string( "" );
}
ostringstream result;
vector<string>::const_iterator i;
for( i = symbol.begin(); i != symbol.end(); ++i ){
result << (i == symbol.begin() ? "" : " ") << (*i);
}
return result.str();
}
/**
* @brief この歌詞の発音記号を設定する
* @param value 発音記号
*/
void setPhoneticSymbol( const std::string &value ){
string s = StringUtil::replace( value, " ", " " );
_phoneticSymbol = StringUtil::explode( " ", s );
for( int i = 0; i < _phoneticSymbol.size(); i++ ){
_phoneticSymbol[i] = StringUtil::replace( _phoneticSymbol[i], "\\" "\\", "\\" );
}
}
/**
* @brief この歌詞の発音記号の配列を取得する
* @return 発音記号の配列
*/
const std::vector<std::string> getPhoneticSymbolList() const{
vector<string> ret;
vector<string>::const_iterator i;
for( i = _phoneticSymbol.begin(); i != _phoneticSymbol.end(); ++i ){
ret.push_back( (*i) );
}
return ret;
}
/**
* @brief このインスタンスを文字列に変換する
* @param addQuateMark (boolean) 歌詞、発音記号の前後に引用符(")を追加するかどうか
* @return (string) 変換後の文字列
*/
const std::string toString( bool addQuateMark = false ) const{
string quot;
if( addQuateMark ){
quot = "\"";
}else{
quot = "";
}
ostringstream result;
result << quot << phrase << quot << ",";
vector<string> symbol = getPhoneticSymbolList();
string strSymbol = getPhoneticSymbol();
if( false == addQuateMark ){
if( strSymbol.size() == 0 ){
strSymbol = "u:";
}
}
result << quot << strSymbol << quot << "," << lengthRatio;
string escaped = StringUtil::replace( result.str(), "\\" "\\", "\\" );
result.str( "" );
result.clear( ostringstream::goodbit );
result << escaped;
vector<int> consonantAdjustment = _consonantAdjustment;
if( consonantAdjustment.empty() ){
vector<string>::iterator i;
for( i = symbol.begin(); i != symbol.end(); ++i ){
int adjustment;
if( PhoneticSymbol::isConsonant( (*i) ) ){
adjustment = 64;
}else{
adjustment = 0;
}
consonantAdjustment.push_back( adjustment );
}
}
vector<int>::iterator i;
for( i = consonantAdjustment.begin(); i != consonantAdjustment.end(); ++i ){
result << "," << (*i);
}
if( isProtected ){
result << ",1";
}else{
result << ",0";
}
return result.str();
}
};
VSQ_END_NAMESPACE
#endif