ConfManager.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <stdexcept>
  42. #include <algorithm>
  43. #include "util_log.h"
  44. #include "StringTokenizer.h"
  45. #include "Deleter.h"
  46. #include "ConfManager.h"
  47. #include "ConfGroup.h"
  48. #include "util_converter.h"
  49. #include "JConf.h"
  50. namespace sinsy
  51. {
  52. namespace
  53. {
  54. const std::string UTF_8_STRS("utf_8, utf8, utf-8");
  55. const std::string SHIFT_JIS_STRS("shift_jis, shift-jis, sjis");
  56. const std::string EUC_JP_STRS("euc-jp, euc_jp, eucjp");
  57. const std::string CODE_SEPARATOR = "|";
  58. };
  59. /*!
  60. constructor
  61. */
  62. ConfManager::ConfManager() : uJConf(NULL), sJConf(NULL), eJConf(NULL), jConfs(NULL)
  63. {
  64. }
  65. /*!
  66. destructor
  67. */
  68. ConfManager::~ConfManager()
  69. {
  70. clear();
  71. }
  72. /*!
  73. @internal
  74. clear all confs
  75. */
  76. void ConfManager::clear()
  77. {
  78. std::for_each(deleteList.begin(), deleteList.end(), Deleter<IConf>());
  79. deleteList.clear();
  80. uJConf = NULL;
  81. sJConf = NULL;
  82. eJConf = NULL;
  83. jConfs = NULL;
  84. confList.clear();
  85. }
  86. /*!
  87. @internal
  88. add Japanese conf
  89. */
  90. void ConfManager::addJConf(IConf* conf)
  91. {
  92. if (!jConfs) {
  93. jConfs = new ConfGroup();
  94. deleteList.push_back(jConfs);
  95. confList.push_back(jConfs);
  96. }
  97. jConfs->add(conf);
  98. }
  99. /*!
  100. set languages
  101. (Currently, you can set only Japanese (j))
  102. */
  103. bool ConfManager::setLanguages(const std::string& languages, const std::string& dirPath)
  104. {
  105. clear();
  106. const std::string::const_iterator itrEnd(languages.end());
  107. for (std::string::const_iterator itr(languages.begin()); itrEnd != itr; ++itr) {
  108. char lang(*itr);
  109. switch (lang) {
  110. case 'j' : { // Japanese
  111. const std::string TABLE_UTF_8(dirPath + "/japanese.utf_8.table");
  112. const std::string CONF_UTF_8(dirPath + "/japanese.utf_8.conf");
  113. const std::string TABLE_SHIFT_JIS(dirPath + "/japanese.shift_jis.table");
  114. const std::string CONF_SHIFT_JIS(dirPath + "/japanese.shift_jis.conf");
  115. const std::string TABLE_EUC_JP(dirPath + "/japanese.euc_jp.table");
  116. const std::string CONF_EUC_JP(dirPath + "/japanese.euc_jp.conf");
  117. const std::string MACRON_TABLE(dirPath + "/japanese.macron");
  118. uJConf = new JConf(UTF_8_STRS);
  119. sJConf = new JConf(SHIFT_JIS_STRS);
  120. eJConf = new JConf(EUC_JP_STRS);
  121. // utf-8
  122. if (!uJConf->read(TABLE_UTF_8, CONF_UTF_8, MACRON_TABLE)) {
  123. ERR_MSG("Cannot read Japanese table or config or macron file : " << TABLE_UTF_8 << ", " << CONF_UTF_8);
  124. delete uJConf;
  125. uJConf = NULL;
  126. return false;
  127. }
  128. addJConf(uJConf);
  129. deleteList.push_back(uJConf);
  130. // shift_jis
  131. if (!sJConf->read(TABLE_SHIFT_JIS, CONF_SHIFT_JIS, MACRON_TABLE)) {
  132. ERR_MSG("Cannot read Japanese table or config or macron file :" << TABLE_SHIFT_JIS << ", " << CONF_SHIFT_JIS);
  133. delete sJConf;
  134. sJConf = NULL;
  135. return false;
  136. }
  137. addJConf(sJConf);
  138. deleteList.push_back(sJConf);
  139. // euc-jp
  140. if (!eJConf->read(TABLE_EUC_JP, CONF_EUC_JP, MACRON_TABLE)) {
  141. ERR_MSG("Cannot read Japanese table or config or macron file : " << TABLE_EUC_JP << ", " << CONF_EUC_JP);
  142. delete eJConf;
  143. eJConf = NULL;
  144. return false;
  145. }
  146. addJConf(eJConf);
  147. deleteList.push_back(eJConf);
  148. break;
  149. }
  150. default :
  151. ERR_MSG("Unknown language flag : " << lang);
  152. return false;
  153. }
  154. }
  155. return true;
  156. }
  157. /*!
  158. set default confs
  159. */
  160. void ConfManager::setDefaultConfs(ConfGroup& confs) const
  161. {
  162. confs.clear();
  163. const ConfList::const_iterator itrEnd(confList.end());
  164. for (ConfList::const_iterator itr(confList.begin()); itrEnd != itr ; ++itr) {
  165. confs.add(*itr);
  166. }
  167. // unknown conf
  168. confs.add(&uConf);
  169. return;
  170. }
  171. }; // namespace sinsy