SpeechFlex.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2003-2006  The Pentagram Team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 #include "pent_include.h"
00019 #include "SpeechFlex.h"
00020 #include "AudioSample.h"
00021 #include "util.h"
00022 
00023 // p_dynamic_class stuff 
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 //              pout << "Found string: \"" << s[i] << "\"" << std::endl;
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 //      pout << "Looking for string: \"" << text << "\"" << std::endl;
00068 
00069         for(it = phrases.begin(); it != phrases.end(); ++it)
00070         {
00071                 if (text.find(*it) == 0) {
00072 //                      pout << "Found: " << i << std::endl;
00073                         end = (*it).size() + start + pos1;
00074                         if (end >= start + pos2)
00075                                 end = phrase.size();
00076                         return i;
00077                 }
00078                 i++;
00079         }
00080 
00081 //      pout << "Not found" << std::endl;
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; // one engine frame of overhead between speech samples
00106         }
00107 
00108         return length;
00109 }

Generated on Fri Jul 27 22:27:40 2007 for pentagram by  doxygen 1.4.7