00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "pent_include.h"
00019 #include "SpeechFlex.h"
00020 #include "AudioSample.h"
00021 #include "util.h"
00022
00023
00024 DEFINE_RUNTIME_CLASSTYPE_CODE(SpeechFlex, SoundFlex);
00025
00026 SpeechFlex::SpeechFlex(IDataSource* ds) : SoundFlex(ds)
00027 {
00028 uint32 size = getRawSize(0);
00029 uint8* buf = getRawObject(0);
00030
00031 Pentagram::istring strings(reinterpret_cast<char*>(buf), size);
00032 std::vector<Pentagram::istring> s;
00033 Pentagram::SplitString(strings, 0, s);
00034
00035 for (unsigned int i = 0; i < s.size(); ++i) {
00036 Pentagram::TabsToSpaces(s[i], 1);
00037 Pentagram::TrimSpaces(s[i]);
00038
00039
00040
00041 phrases.push_back(s[i]);
00042 }
00043
00044 delete [] buf;
00045
00046 }
00047
00048 SpeechFlex::~SpeechFlex(void)
00049 {
00050 }
00051
00052 int SpeechFlex::getIndexForPhrase(const std::string &phrase,
00053 uint32 start, uint32& end) const
00054 {
00055 std::vector<Pentagram::istring>::const_iterator it;
00056 int i = 1;
00057
00058 std::string text = phrase.substr(start);
00059 Pentagram::TabsToSpaces(text, 1);
00060
00061 std::string::size_type pos1 = text.find_first_not_of(' ');
00062 if (pos1 == std::string::npos) return 0;
00063
00064 std::string::size_type pos2 = text.find_last_not_of(' ');
00065 text = text.substr(pos1, pos2-pos1+1);
00066
00067
00068
00069 for(it = phrases.begin(); it != phrases.end(); ++it)
00070 {
00071 if (text.find(*it) == 0) {
00072
00073 end = (*it).size() + start + pos1;
00074 if (end >= start + pos2)
00075 end = phrase.size();
00076 return i;
00077 }
00078 i++;
00079 }
00080
00081
00082
00083 return 0;
00084 }
00085
00086 uint32 SpeechFlex::getSpeechLength(const std::string &phrase)
00087 {
00088 uint32 start = 0, end = 0;
00089 uint32 length = 0;
00090
00091 while (end < phrase.size()) {
00092 start = end;
00093 int index = getIndexForPhrase(phrase, start, end);
00094 if (!index) break;
00095
00096 Pentagram::AudioSample* sample = getSample(index);
00097 if (!sample) break;
00098
00099 uint32 samples = sample->getLength();
00100 uint32 rate = sample->getRate();
00101 bool stereo = sample->isStereo();
00102 if (stereo) rate *= 2;
00103
00104 length += (samples * 1000) / rate;
00105 length += 33;
00106 }
00107
00108 return length;
00109 }