MacronTable.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "MacronTable.h"
  43. #include "StringTokenizer.h"
  44. #include "util_log.h"
  45. #include "util_string.h"
  46. namespace sinsy
  47. {
  48. namespace
  49. {
  50. const std::string MACRON_DELIMITER = ",";
  51. /*!
  52. extract phoneme list from string
  53. */
  54. void extractPhonemeList(const std::string& str, MacronTable::PhonemeList& pl)
  55. {
  56. StringTokenizer st(str, MACRON_DELIMITER);
  57. size_t sz(st.size());
  58. pl.resize(sz);
  59. for (size_t i(0); i < sz; ++i) {
  60. pl[i] = st.at(i);
  61. }
  62. }
  63. };
  64. /*!
  65. constructor
  66. */
  67. MacronTable::MacronTable()
  68. {
  69. }
  70. /*!
  71. destructor
  72. */
  73. MacronTable::~MacronTable()
  74. {
  75. clear();
  76. }
  77. /*!
  78. clear
  79. */
  80. void MacronTable::clear()
  81. {
  82. const ConvertTable::iterator itrEnd(convertTable.end());
  83. ConvertTable::iterator itr(convertTable.begin());
  84. for (; itrEnd != itr; ++itr) {
  85. delete itr->second;
  86. }
  87. convertTable.clear();
  88. }
  89. /*!
  90. read from file
  91. If the file is not exist, output warning message and return true.
  92. @param fname phoneme table file path
  93. @param return true if success
  94. */
  95. bool MacronTable::read(const std::string& fname)
  96. {
  97. std::ifstream ifs(fname.c_str());
  98. if (!ifs) {
  99. WARN_MSG("Cannot open macron table file : " << fname);
  100. return true;
  101. }
  102. clear();
  103. std::string buffer;
  104. while (!ifs.eof()) {
  105. std::getline(ifs, buffer);
  106. StringTokenizer st(buffer, BLANK_STR);
  107. size_t sz(st.size());
  108. if (0 == sz) {
  109. continue;
  110. } else if (3 != sz) {
  111. ERR_MSG("Wrong macron table (" << buffer << ") : " << fname);
  112. return false;
  113. }
  114. // src
  115. PhonemeList pl;
  116. extractPhonemeList(st.at(0), pl);
  117. // dst
  118. Result* result(new Result);
  119. extractPhonemeList(st.at(1), result->forward);
  120. extractPhonemeList(st.at(2), result->backward);
  121. if (false == convertTable.insert(std::make_pair/*<std::vector<std::string>, Result*>*/(pl, result)).second) {
  122. ERR_MSG("Wrong macron table (There is a duplication : " << st.at(0) << ") : " << fname);
  123. delete result;
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. /*!
  130. divide phoneme set
  131. */
  132. bool MacronTable::divide(const PhonemeList& src_, PhonemeList& dst1, PhonemeList& dst2) const
  133. {
  134. PhonemeList src(src_);
  135. dst1.clear();
  136. dst2.clear();
  137. while (!src.empty()) {
  138. ConvertTable::const_iterator itr(convertTable.find(src));
  139. if (convertTable.end() != itr) {
  140. dst1.insert(dst1.end(), itr->second->forward.begin(), itr->second->forward.end());
  141. dst2 = itr->second->backward;
  142. return true;
  143. }
  144. dst1.push_back(src.front());
  145. src.erase(src.begin());
  146. }
  147. dst1.clear();
  148. return false;
  149. }
  150. }; // namespace sinsy