[vConnect/trunk/stand2.0] modified: vConnectConverter; 文字列の切り出し処理をStringUtilを使って書き換えた
@@ -1,126 +0,0 @@ | ||
1 | -#include <stdio.h> | |
2 | -#include <string.h> | |
3 | - | |
4 | -#include <string> | |
5 | - | |
6 | -#include "vConnectPhoneme.h" | |
7 | -#include "vConnectConverter.h" | |
8 | -#include "waveFileEx/waveFileEx.h" | |
9 | -#include "time.h" | |
10 | -#include "stand.h" | |
11 | -#include "TextReader.h" | |
12 | -#include "TextWriter.h" | |
13 | - | |
14 | -const int bufSize = 4096; | |
15 | - | |
16 | -using namespace std; | |
17 | -using namespace vconnect; | |
18 | - | |
19 | -bool vConnectConverter::convert( const char *otoIni, const char *dstDir ) | |
20 | -{ | |
21 | - | |
22 | - char buf[bufSize]; | |
23 | - | |
24 | - string srcDir = otoIni; | |
25 | - string dstDir_s = dstDir; | |
26 | - string fileName; | |
27 | - string alias; | |
28 | - srcDir = srcDir.substr( 0, srcDir.rfind( PATH_SEPARATOR ) ); | |
29 | - | |
30 | - float leftBlank, fixedLength, rightBlank, preUtterance, voiceOverlap; | |
31 | - | |
32 | - int count = 0; | |
33 | - | |
34 | - TextReader reader( otoIni, "Shift_JIS" ); | |
35 | - TextWriter writer( (dstDir_s + "oto.ini").c_str(), "Shift_JIS", "\x0D\x0A" ); | |
36 | - while( false == reader.isEOF() ){ | |
37 | - string buffer = reader.readLine(); | |
38 | - if( buffer.length() == 0 ){ | |
39 | - continue; | |
40 | - } | |
41 | - count++; | |
42 | - double *wave; | |
43 | - int waveLength; | |
44 | - waveFileEx waveFile; | |
45 | - vConnectPhoneme phoneme; | |
46 | - | |
47 | - string tmp = buffer; | |
48 | - string line; | |
49 | - fileName = tmp.substr( 0, tmp.find( "=" ) ); | |
50 | - alias = tmp.substr( tmp.find( "=" ) + 1, tmp.find( "," ) ); | |
51 | - line = tmp.substr( tmp.find( "=" ) ); | |
52 | - tmp = tmp.substr( tmp.find( "," ) + 1 ); | |
53 | - | |
54 | - leftBlank = atof( tmp.c_str() ); | |
55 | - tmp = tmp.substr( tmp.find( "," ) + 1 ); | |
56 | - | |
57 | - fixedLength = atof( tmp.c_str() ); | |
58 | - tmp = tmp.substr( tmp.find( "," ) + 1 ); | |
59 | - | |
60 | - rightBlank = atof( tmp.c_str() ); | |
61 | - tmp = tmp.substr( tmp.find( "," ) + 1 ); | |
62 | - | |
63 | - preUtterance = atof( tmp.c_str() ); | |
64 | - tmp = tmp.substr( tmp.find( "," ) + 1 ); | |
65 | - | |
66 | - voiceOverlap = atof( tmp.c_str() ); | |
67 | - | |
68 | - sprintf( buf, "%d", count ); | |
69 | - tmp = buf; | |
70 | - line = tmp + ".vvd" + line; | |
71 | - | |
72 | - fprintf( stderr,"====\n" ); | |
73 | - fprintf( stderr, "Begin analysis : %s @ %s\n", alias.c_str(), fileName.c_str() ); | |
74 | - | |
75 | - if( waveFile.readWaveFile( srcDir + PATH_SEPARATOR + fileName ) != 1 ){ | |
76 | - cout << "error ; can't open the file, " << (srcDir + PATH_SEPARATOR + fileName).c_str() << endl; | |
77 | - continue; | |
78 | - } | |
79 | - waveLength = waveFile.getWaveLength( leftBlank, rightBlank ); | |
80 | - wave = new double[waveLength]; | |
81 | - waveFile.getWaveBuffer( wave, leftBlank, rightBlank, waveLength ); | |
82 | - | |
83 | - // 音量の正規化を行う. | |
84 | - long index = (long)(44100.0 * fixedLength / 1000.0); | |
85 | - double sum1 = 0.0; | |
86 | - double sum2 = 0.0; | |
87 | - // 固定長終了位置の音量を得る. | |
88 | - for( int i = index - 1024; 0 <= i && i < index + 1024 && i < waveLength; i++ ){ | |
89 | - sum1 += wave[i] * wave[i]; | |
90 | - } | |
91 | - // 左ブランク終了位置の音量を得る. | |
92 | - for( int i = 0; i < 2048 && i < waveLength; i++ ){ | |
93 | - sum2 += wave[i] * wave[i]; | |
94 | - } | |
95 | - // 大きい方が正規化のための音量. | |
96 | - sum1 = max( sum1, sum2 ); | |
97 | - sum1 = VOL_NORMALIZE / sqrt( (sum1 / 2048.0) ); | |
98 | - | |
99 | - for( int i = 0; i < waveLength; i++ ){ | |
100 | - wave[i] *= sum1; | |
101 | - } | |
102 | - | |
103 | - fprintf( stderr, " computing wave....\n" ); | |
104 | - clock_t cl = clock(); | |
105 | - phoneme.computeWave( wave, waveLength, 44100, 2.0 ); | |
106 | - fprintf( stderr, " Done. Elapsed time = %d[ms]\n", clock() - cl ); | |
107 | - | |
108 | - fileName = dstDir; | |
109 | - fileName = fileName + buf + ".vvd"; | |
110 | - if( phoneme.writePhoneme( fileName.c_str() ) ){ | |
111 | - fprintf( stderr, "Wrote file : %s\n", fileName.c_str() ); | |
112 | - }else{ | |
113 | - fprintf( stderr, "Error. Failed to write : %s\n", fileName.c_str() ); | |
114 | - } | |
115 | - fprintf( stderr, "====\n" ); | |
116 | - | |
117 | - writer.writeLine( line ); | |
118 | - | |
119 | - delete[] wave; | |
120 | - } | |
121 | - | |
122 | - writer.close(); | |
123 | - reader.close(); | |
124 | - | |
125 | - return true; | |
126 | -} |
@@ -1,20 +1,142 @@ | ||
1 | 1 | #ifndef __vConnectConverter_h__ |
2 | 2 | #define __vConnectConverter_h__ |
3 | 3 | |
4 | -class vConnectConverter { | |
4 | +#include <math.h> | |
5 | +#include "TextReader.h" | |
6 | +#include "TextWriter.h" | |
7 | +#include "waveFileEx/waveFileEx.h" | |
8 | +#include "stand.h" | |
9 | +#include "vConnectPhoneme.h" | |
10 | +#include "StringUtil.h" | |
11 | + | |
12 | +using namespace vconnect; | |
13 | + | |
14 | +class vConnectConverter | |
15 | +{ | |
5 | 16 | public: |
6 | - vConnectConverter(){ } | |
7 | - ~vConnectConverter(){ } | |
17 | + vConnectConverter() | |
18 | + { | |
19 | + } | |
8 | 20 | |
9 | - /// <summary> | |
10 | - /// UTAU 音源形式を STAND 音源形式へ変換します. Unicode に対応していない簡易版です. | |
11 | - /// </summary> | |
12 | - /// <param name="otoIni">分析する音源の oto.ini へのパス.</param> | |
13 | - /// <param name="dstDir">出力先ディレクトリ名.</param> | |
14 | - /// <returns>分析に成功した場合true,それ以外はfalseを返します.</returns> | |
15 | - bool convert(const char *otoIni, const char *dstDir); | |
21 | + ~vConnectConverter() | |
22 | + { | |
23 | + } | |
16 | 24 | |
25 | + /** | |
26 | + * UTAU 音源形式を STAND 音源形式へ変換します. Unicode に対応していない簡易版です. | |
27 | + * @param otoIni 分析する音源の oto.ini へのパス. | |
28 | + * @param dstDir 出力先ディレクトリ名. | |
29 | + * @returns 分析に成功した場合true,それ以外はfalseを返します. | |
30 | + */ | |
31 | + bool convert( const char *otoIni, const char *dstDir ) | |
32 | + { | |
33 | + string srcDir = otoIni; | |
34 | + string dstDir_s = dstDir; | |
35 | + srcDir = srcDir.substr( 0, srcDir.rfind( PATH_SEPARATOR ) ); | |
36 | + int count = 0; | |
37 | + | |
38 | + TextReader reader( otoIni, "Shift_JIS" ); | |
39 | + TextWriter writer( (dstDir_s + "oto.ini").c_str(), "Shift_JIS", "\x0D\x0A" ); | |
40 | + while( false == reader.isEOF() ){ | |
41 | + string buffer = reader.readLine(); | |
42 | + if( buffer.length() == 0 ){ | |
43 | + continue; | |
44 | + } | |
45 | + count++; | |
46 | + | |
47 | + string line = this->processRecord( buffer, count, srcDir, dstDir_s ); | |
48 | + | |
49 | + if( line.length() > 0 ){ | |
50 | + writer.writeLine( line ); | |
51 | + } | |
52 | + } | |
53 | + | |
54 | + writer.close(); | |
55 | + reader.close(); | |
56 | + | |
57 | + return true; | |
58 | + } | |
59 | + | |
17 | 60 | private: |
61 | + /** | |
62 | + * 変換元の oto.ini ファイルの1行分のデータを処理する | |
63 | + * @param record 1 行分のデータ | |
64 | + * @param count oto.ini の何行目のデータかを表す | |
65 | + * @param sourceDirectory 変換元音源のディレクトリ | |
66 | + * @param destinationDirectory 変換後の音源の保存ディレクトリ | |
67 | + * @return 変換後の oto.ini ファイルに書きこむ行データ | |
68 | + */ | |
69 | + string processRecord( std::string record, int count, string sourceDirectory, string destinationDirectory ) | |
70 | + { | |
71 | + list<string> splitted = StringUtil::explode( "=", record, 2 ); | |
72 | + list<string>::iterator i = splitted.begin(); | |
73 | + string fileName = *i++; | |
74 | + string parametersString = *i++; | |
75 | + | |
76 | + list<string> parameters = StringUtil::explode( ",", parametersString ); | |
77 | + list<string>::iterator j = parameters.begin(); | |
78 | + string alias = *j++; | |
79 | + float leftBlank = atof( (*j++).c_str() ); | |
80 | + float fixedLength = atof( (*j++).c_str() ); | |
81 | + float rightBlank = atof( (*j++).c_str() ); | |
82 | + float preUtterance = atof( (*j++).c_str() ); | |
83 | + float voiceOverlap = atof( (*j++).c_str() ); | |
84 | + | |
85 | + char buf[16]; | |
86 | + sprintf( buf, "%d.vvd", count ); | |
87 | + string vvdName = buf; | |
88 | + string line = vvdName + "=" + parametersString; | |
89 | + | |
90 | + cerr << "====" << endl; | |
91 | + cerr << "Begin analysis : " << alias << " @ " << fileName << endl; | |
92 | + | |
93 | + waveFileEx waveFile; | |
94 | + if( waveFile.readWaveFile( sourceDirectory + PATH_SEPARATOR + fileName ) != 1 ){ | |
95 | + cout << "error ; can't open the file, " << (sourceDirectory + PATH_SEPARATOR + fileName).c_str() << endl; | |
96 | + return ""; | |
97 | + } | |
98 | + int waveLength = waveFile.getWaveLength( leftBlank, rightBlank ); | |
99 | + double *wave = new double[waveLength]; | |
100 | + waveFile.getWaveBuffer( wave, leftBlank, rightBlank, waveLength ); | |
101 | + | |
102 | + // 音量の正規化を行う. | |
103 | + long index = (long)(44100.0 * fixedLength / 1000.0); | |
104 | + double sum1 = 0.0; | |
105 | + double sum2 = 0.0; | |
106 | + // 固定長終了位置の音量を得る. | |
107 | + for( int i = index - 1024; 0 <= i && i < index + 1024 && i < waveLength; i++ ){ | |
108 | + sum1 += wave[i] * wave[i]; | |
109 | + } | |
110 | + // 左ブランク終了位置の音量を得る. | |
111 | + for( int i = 0; i < 2048 && i < waveLength; i++ ){ | |
112 | + sum2 += wave[i] * wave[i]; | |
113 | + } | |
114 | + // 大きい方が正規化のための音量. | |
115 | + sum1 = max( sum1, sum2 ); | |
116 | + sum1 = VOL_NORMALIZE / sqrt( (sum1 / 2048.0) ); | |
117 | + | |
118 | + for( int i = 0; i < waveLength; i++ ){ | |
119 | + wave[i] *= sum1; | |
120 | + } | |
121 | + | |
122 | + cerr << " computing wave...." << endl; | |
123 | + clock_t cl = clock(); | |
124 | + vConnectPhoneme phoneme; | |
125 | + phoneme.computeWave( wave, waveLength, 44100, 2.0 ); | |
126 | + cerr << " Done. Elapsed time = " << (clock() - cl) << "[ms]" << endl; | |
127 | + | |
128 | + fileName = destinationDirectory + vvdName; | |
129 | + if( phoneme.writePhoneme( fileName.c_str() ) ){ | |
130 | + cerr << "Wrote file : " << fileName << endl; | |
131 | + }else{ | |
132 | + cerr << "Error. Failed to write : " << fileName << endl; | |
133 | + } | |
134 | + cerr << "====" << endl; | |
135 | + | |
136 | + delete[] wave; | |
137 | + | |
138 | + return line; | |
139 | + } | |
18 | 140 | }; |
19 | 141 | |
20 | 142 | #endif |