PhonemeTable.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* ----------------------------------------------------------------- */
  2. /* The HMM-Based Singing Voice Synthesis System "Sinsy" */
  3. /* developed by Sinsy Working Group */
  4. /* http://sinsy.sourceforge.net/ */
  5. /* ----------------------------------------------------------------- */
  6. /* */
  7. /* Copyright (c) 2009-2015 Nagoya Institute of Technology */
  8. /* Department of Computer Science */
  9. /* */
  10. /* All rights reserved. */
  11. /* */
  12. /* Redistribution and use in source and binary forms, with or */
  13. /* without modification, are permitted provided that the following */
  14. /* conditions are met: */
  15. /* */
  16. /* - Redistributions of source code must retain the above copyright */
  17. /* notice, this list of conditions and the following disclaimer. */
  18. /* - Redistributions in binary form must reproduce the above */
  19. /* copyright notice, this list of conditions and the following */
  20. /* disclaimer in the documentation and/or other materials provided */
  21. /* with the distribution. */
  22. /* - Neither the name of the Sinsy working group nor the names of */
  23. /* its contributors may be used to endorse or promote products */
  24. /* derived from this software without specific prior written */
  25. /* permission. */
  26. /* */
  27. /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */
  28. /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */
  29. /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  30. /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
  31. /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS */
  32. /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
  33. /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
  34. /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
  35. /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
  36. /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, */
  37. /* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY */
  38. /* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
  39. /* POSSIBILITY OF SUCH DAMAGE. */
  40. /* ----------------------------------------------------------------- */
  41. #include <fstream>
  42. #include "PhonemeTable.h"
  43. #include "StringTokenizer.h"
  44. #include "util_log.h"
  45. #include "util_string.h"
  46. namespace sinsy
  47. {
  48. /*!
  49. constructor
  50. */
  51. PhonemeTable::Result::Result() : syllable(NULL), phonemeList(NULL)
  52. {
  53. }
  54. /*!
  55. constructor
  56. */
  57. PhonemeTable::Result::Result(const std::string& s, const PhonemeList* p) : syllable(&s), phonemeList(p)
  58. {
  59. }
  60. /*!
  61. copy constructor
  62. */
  63. PhonemeTable::Result::Result(const Result& obj) : syllable(obj.syllable), phonemeList(obj.phonemeList)
  64. {
  65. }
  66. /*!
  67. destructor
  68. */
  69. PhonemeTable::Result::~Result()
  70. {
  71. }
  72. /*!
  73. assignment operator
  74. */
  75. PhonemeTable::Result& PhonemeTable::Result::operator=(const Result & obj)
  76. {
  77. this->syllable = obj.syllable;
  78. this->phonemeList = obj.phonemeList;
  79. return *this;
  80. }
  81. /*!
  82. return true if this result is valid
  83. */
  84. bool PhonemeTable::Result::isValid() const
  85. {
  86. return (NULL != syllable);
  87. }
  88. /*!
  89. get syllable
  90. */
  91. const std::string& PhonemeTable::Result::getSyllable() const
  92. {
  93. return (NULL == syllable) ? NULL_STR : *syllable;
  94. }
  95. /*!
  96. get phoneme list
  97. */
  98. const PhonemeTable::PhonemeList* PhonemeTable::Result::getPhonemeList() const
  99. {
  100. return phonemeList;
  101. }
  102. /*!
  103. get matched length
  104. */
  105. size_t PhonemeTable::Result::getMatchedLength() const
  106. {
  107. return (NULL == syllable) ? 0 : syllable->size();
  108. }
  109. /*!
  110. constructor
  111. */
  112. PhonemeTable::PhonemeTable()
  113. {
  114. }
  115. /*!
  116. destructor
  117. */
  118. PhonemeTable::~PhonemeTable()
  119. {
  120. clear();
  121. }
  122. /*!
  123. clear
  124. */
  125. void PhonemeTable::clear()
  126. {
  127. ConvertTable::iterator itr(convertTable.begin());
  128. ConvertTable::iterator itrEnd(convertTable.end());
  129. for (; itr != itrEnd; ++itr) {
  130. delete itr->second;
  131. }
  132. convertTable.clear();
  133. }
  134. /*!
  135. read from file
  136. @param fname phoneme table file path
  137. @param return true if success
  138. */
  139. bool PhonemeTable::read(const std::string& fname)
  140. {
  141. std::ifstream ifs(fname.c_str());
  142. if (!ifs) {
  143. ERR_MSG("Cannot open phoneme table file : " << fname);
  144. return false;
  145. }
  146. clear();
  147. std::string buffer;
  148. while (!ifs.eof()) {
  149. std::getline(ifs, buffer);
  150. StringTokenizer st(buffer, BLANK_STR);
  151. size_t sz(st.size());
  152. if (0 == sz) {
  153. continue;
  154. } else if (1 == sz) {
  155. ERR_MSG("Wrong phoneme table (value is needed after key) : " << fname);
  156. return false;
  157. }
  158. PhonemeList* pl(new PhonemeList);
  159. pl->reserve(sz - 1);
  160. for (size_t i(1); i < sz; ++i) {
  161. pl->push_back(st.at(i));
  162. }
  163. if (false == convertTable.insert(std::make_pair/*<std::string, PhonemeList*>*/(st.at(0), pl)).second) {
  164. ERR_MSG("Wrong phoneme table (some syllables have same name : " << st.at(0) << ") : " << fname);
  165. delete pl;
  166. return false;
  167. }
  168. }
  169. return true;
  170. }
  171. /*!
  172. find from table
  173. */
  174. PhonemeTable::Result PhonemeTable::find(const std::string& syllable) const
  175. {
  176. for (size_t sz(syllable.size()); 0 < sz ; --sz) {
  177. std::string s(syllable.substr(0, sz));
  178. ConvertTable::const_iterator itr(convertTable.find(s));
  179. if (convertTable.end() != itr) {
  180. return Result(itr->first, itr->second);
  181. }
  182. }
  183. return Result();
  184. }
  185. /*!
  186. return matched result
  187. */
  188. PhonemeTable::Result PhonemeTable::match(const std::string& syllable) const
  189. {
  190. ConvertTable::const_iterator itr(convertTable.find(syllable));
  191. if (convertTable.end() != itr) {
  192. return Result(itr->first, itr->second);
  193. }
  194. return Result();
  195. }
  196. }; // namespace sinsy