00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef PHONETIC_SYMBOL
00018 #define PHONETIC_SYMBOL
00019
00020 #include <iostream>
00021 #include <sndfile.h>
00022 #include <string.h>
00023
00024 using namespace std;
00025
00026 namespace ekho {
00027 typedef struct SymbolCode {
00028 const char *name;
00029 unsigned short code;
00030 } SymbolCode;
00031
00032 class PhoneticSymbol {
00033 public:
00034 PhoneticSymbol(void): symbol(0), mPcm(0), mSize(0) {};
00035 PhoneticSymbol(const char *sym):
00036 symbol(sym), mPcm(0), mSize(0) {};
00037
00038
00039
00040
00041
00042
00043
00044 ~PhoneticSymbol(void) {
00045
00046
00047
00048
00049
00050
00051 if (mPcm) {
00052 delete[] mPcm;
00053 mPcm = 0;
00054 }
00055 };
00056
00057 const char *symbol;
00058
00059 void setPcm(char *pcm, const int size) {
00060 if (mPcm) {
00061 delete mPcm;
00062 }
00063 mPcm = pcm;
00064 mSize = size;
00065 };
00066
00067 inline const char* getPcm(int &size) {
00068 return getPcm("", "wav", size);
00069 };
00070
00071 inline const char* getPcm(const char *wavDir, const char *postfix, int &size) {
00072 SF_INFO sfinfo;
00073 return getPcm(wavDir, postfix, size, sfinfo);
00074 };
00075
00076 const char* getPcm(const char *wavDir, const char *postfix, int &size, SF_INFO &sfinfo) {
00077 if (!mPcm) {
00078 memset(&sfinfo, 0, sizeof(SF_INFO));
00079
00080 string wav_file = wavDir;
00081 wav_file += "/";
00082 wav_file += this->symbol;
00083 wav_file += ".";
00084 wav_file += postfix;
00085
00086 SNDFILE *sndfile = sf_open(wav_file.c_str(), SFM_READ, &sfinfo);
00087 if (!sndfile) {
00088
00089
00090 } else {
00091 sfinfo.channels = 1;
00092 int samples = 0;
00093
00094
00095 switch (sfinfo.format & SF_FORMAT_SUBMASK) {
00096 case SF_FORMAT_VORBIS:
00097 case SF_FORMAT_GSM610:
00098 case SF_FORMAT_PCM_16:
00099 mSize = (int)sfinfo.frames * 2;
00100 mPcm = new char[mSize];
00101 samples = (int)sf_readf_short(sndfile, (short int*)mPcm, sfinfo.frames);
00102 break;
00103 case SF_FORMAT_PCM_S8:
00104 case SF_FORMAT_PCM_U8:
00105 default:
00106 cerr << "Unknown soundfile format: " << (sfinfo.format & SF_FORMAT_SUBMASK) << endl;
00107 }
00108
00109 if (samples != sfinfo.frames) {
00110 cerr << "Fail to read " << wav_file.c_str() << ": " << samples <<
00111 " frames out of " << sfinfo.frames << " have been read." << endl;
00112 }
00113
00114 sf_close(sndfile);
00115 }
00116 }
00117
00118 size = mSize;
00119 return mPcm;
00120 };
00121
00122 private:
00123 char *mPcm;
00124 int mSize;
00125 };
00126 }
00127 #endif