LabelData.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 "util_log.h"
  42. #include "LabelData.h"
  43. namespace sinsy
  44. {
  45. const INT64 LabelData::INVALID_TIME = -1;
  46. namespace
  47. {
  48. const size_t NUM_P = 16;
  49. const size_t NUM_A = 5;
  50. const size_t NUM_B = 5;
  51. const size_t NUM_C = 5;
  52. const size_t NUM_D = 9;
  53. const size_t NUM_E = 60;
  54. const size_t NUM_F = 9;
  55. const size_t NUM_G = 2;
  56. const size_t NUM_H = 2;
  57. const size_t NUM_I = 2;
  58. const size_t NUM_J = 3;
  59. const char* SEPARATOR_P[NUM_P] = {"", "@", "^", "-", "+", "=", "_", "%", "^", "_", "~", "-", "!", "[", "$", "]"};
  60. const char* SEPARATOR_A[NUM_A] = {"/A:", "-", "-", "@", "~"};
  61. const char* SEPARATOR_B[NUM_B] = {"/B:", "_", "_", "@", "|"};
  62. const char* SEPARATOR_C[NUM_C] = {"/C:", "+", "+", "@", "&"};
  63. const char* SEPARATOR_D[NUM_D] = {"/D:", "!", "#", "$", "%", "|", "&", ";", "-"};
  64. const char* SEPARATOR_E[NUM_E] = {
  65. "/E:", "]", "^", "=", "~", "!", "@", "#", "+", "]",
  66. "$", "|", "[", "&", "]", "=", "^", "~", "#", "_",
  67. ";", "$", "&", "%", "[", "|", "]", "-", "^", "+",
  68. "~", "=", "@", "$", "!", "%", "#", "|", "|", "-",
  69. "&", "&", "+", "[", ";", "]", ";", "~", "~", "^",
  70. "^", "@", "[", "#", "=", "!", "~", "+", "!", "^"
  71. };
  72. const char* SEPARATOR_F[NUM_F] = {"/F:", "#", "#", "-", "$", "$", "+", "%", ";"};
  73. const char* SEPARATOR_G[NUM_G] = {"/G:", "_"};
  74. const char* SEPARATOR_H[NUM_H] = {"/H:", "_"};
  75. const char* SEPARATOR_I[NUM_I] = {"/I:", "_"};
  76. const char* SEPARATOR_J[NUM_J] = {"/J:", "~", "@"};
  77. };
  78. /*!
  79. constructor
  80. */
  81. LabelData::LabelData() : monophoneFlag(false), outputTimeFlag(true), beginTime(INVALID_TIME), endTime(INVALID_TIME)
  82. {
  83. List *p(NULL), *a(NULL), *b(NULL), *c(NULL), *d(NULL), *e(NULL), *f(NULL), *g(NULL), *h(NULL), *i(NULL), *j(NULL);
  84. try {
  85. p = new List(NUM_P, "xx");
  86. a = new List(NUM_A, "xx");
  87. b = new List(NUM_B, "xx");
  88. c = new List(NUM_C, "xx");
  89. d = new List(NUM_D, "xx");
  90. e = new List(NUM_E, "xx");
  91. f = new List(NUM_F, "xx");
  92. g = new List(NUM_G, "xx");
  93. h = new List(NUM_H, "xx");
  94. i = new List(NUM_I, "xx");
  95. j = new List(NUM_J, "xx");
  96. } catch (const std::bad_alloc&) {
  97. delete p;
  98. delete a;
  99. delete b;
  100. delete c;
  101. delete d;
  102. delete e;
  103. delete f;
  104. delete g;
  105. delete h;
  106. delete i;
  107. delete j;
  108. ERR_MSG("Cannot allocate memory");
  109. }
  110. data.insert(std::make_pair('p', p));
  111. data.insert(std::make_pair('a', a));
  112. data.insert(std::make_pair('b', b));
  113. data.insert(std::make_pair('c', c));
  114. data.insert(std::make_pair('d', d));
  115. data.insert(std::make_pair('e', e));
  116. data.insert(std::make_pair('f', f));
  117. data.insert(std::make_pair('g', g));
  118. data.insert(std::make_pair('h', h));
  119. data.insert(std::make_pair('i', i));
  120. data.insert(std::make_pair('j', j));
  121. }
  122. /*!
  123. destructor
  124. */
  125. LabelData::~LabelData()
  126. {
  127. Data::iterator itr(data.begin());
  128. Data::iterator itrEnd(data.end());
  129. for (; itr != itrEnd; ++itr) {
  130. delete itr->second;
  131. }
  132. }
  133. /*!
  134. set monophone flag
  135. */
  136. void LabelData::setMonophoneFlag(bool b)
  137. {
  138. monophoneFlag = b;
  139. }
  140. /*!
  141. set output flag
  142. */
  143. void LabelData::setOutputTimeFlag(bool b)
  144. {
  145. outputTimeFlag = b;
  146. }
  147. /*!
  148. set begin time
  149. */
  150. void LabelData::setBeginTime(double d)
  151. {
  152. beginTime = static_cast<INT64>(d * 1.0e+7);
  153. }
  154. /*!
  155. set end time
  156. */
  157. void LabelData::setEndTime(double d)
  158. {
  159. endTime = static_cast<INT64>(d * 1.0e+7);
  160. }
  161. /*!
  162. get data
  163. */
  164. const std::string& LabelData::get(char category, size_t number) const
  165. {
  166. Data::const_iterator itr(data.find(category));
  167. if (data.end() == itr) {
  168. throw std::runtime_error("LabelData::get() unknown category");
  169. }
  170. const List* list(itr->second);
  171. if ((0 == number) || (list->size() < number)) {
  172. throw std::runtime_error("LabelData::get() number is out of range");
  173. }
  174. return list->at(number - 1);
  175. }
  176. /*!
  177. set data (std::string)
  178. */
  179. template<>
  180. void LabelData::set<std::string>(char category, size_t number, const std::string& value)
  181. {
  182. Data::iterator itr(data.find(category));
  183. if (data.end() == itr) {
  184. throw std::runtime_error("LabelData::set() unknown category");
  185. }
  186. List* list(itr->second);
  187. if ((0 == number) || (list->size() < number)) {
  188. throw std::runtime_error("LabelData::set() number is out of range");
  189. }
  190. list->at(number - 1) = value;
  191. }
  192. /*!
  193. set data (bool)
  194. */
  195. template<>
  196. void LabelData::set<bool>(char category, size_t number, const bool& value)
  197. {
  198. set(category, number, std::string(value ? "1" : "0"));
  199. }
  200. /*!
  201. to stream
  202. */
  203. std::ostream& operator<<(std::ostream& os, const LabelData& obj)
  204. {
  205. if (obj.outputTimeFlag) {
  206. os << obj.beginTime << " " << obj.endTime << " ";
  207. }
  208. LabelData::Data::const_iterator itrP(obj.data.find('p'));
  209. if (obj.data.end() == itrP)
  210. throw std::runtime_error("LabelData::operator<<() p is not found");
  211. LabelData::List& p = *(itrP->second);
  212. LabelData::Data::const_iterator itrA(obj.data.find('a'));
  213. if (obj.data.end() == itrA)
  214. throw std::runtime_error("LabelData::operator<<() a is not found");
  215. LabelData::List& a = *(itrA->second);
  216. LabelData::Data::const_iterator itrB(obj.data.find('b'));
  217. if (obj.data.end() == itrB)
  218. throw std::runtime_error("LabelData::operator<<() b is not found");
  219. LabelData::List& b = *(itrB->second);
  220. LabelData::Data::const_iterator itrC(obj.data.find('c'));
  221. if (obj.data.end() == itrC)
  222. throw std::runtime_error("LabelData::operator<<() c is not found");
  223. LabelData::List& c = *(itrC->second);
  224. LabelData::Data::const_iterator itrD(obj.data.find('d'));
  225. if (obj.data.end() == itrD)
  226. throw std::runtime_error("LabelData::operator<<() d is not found");
  227. LabelData::List& d = *(itrD->second);
  228. LabelData::Data::const_iterator itrE(obj.data.find('e'));
  229. if (obj.data.end() == itrE)
  230. throw std::runtime_error("LabelData::operator<<() e is not found");
  231. LabelData::List& e = *(itrE->second);
  232. LabelData::Data::const_iterator itrF(obj.data.find('f'));
  233. if (obj.data.end() == itrF)
  234. throw std::runtime_error("LabelData::operator<<() f is not found");
  235. LabelData::List& f = *(itrF->second);
  236. LabelData::Data::const_iterator itrG(obj.data.find('g'));
  237. if (obj.data.end() == itrG)
  238. throw std::runtime_error("LabelData::operator<<() g is not found");
  239. LabelData::List& g = *(itrG->second);
  240. LabelData::Data::const_iterator itrH(obj.data.find('h'));
  241. if (obj.data.end() == itrH)
  242. throw std::runtime_error("LabelData::operator<<() h is not found");
  243. LabelData::List& h = *(itrH->second);
  244. LabelData::Data::const_iterator itrI(obj.data.find('i'));
  245. if (obj.data.end() == itrI)
  246. throw std::runtime_error("LabelData::operator<<() i is not found");
  247. LabelData::List& i = *(itrI->second);
  248. LabelData::Data::const_iterator itrJ(obj.data.find('j'));
  249. if (obj.data.end() == itrJ)
  250. throw std::runtime_error("LabelData::operator<<() j is not found");
  251. LabelData::List& j = *(itrJ->second);
  252. if (obj.monophoneFlag) {
  253. os << p[3];
  254. return os;
  255. }
  256. // p
  257. for (size_t idx(0); idx < NUM_P; ++idx) {
  258. os << SEPARATOR_P[idx] << p[idx];
  259. }
  260. // a
  261. for (size_t idx(0); idx < NUM_A; ++idx) {
  262. os << SEPARATOR_A[idx] << a[idx];
  263. }
  264. // b
  265. for (size_t idx(0); idx < NUM_B; ++idx) {
  266. os << SEPARATOR_B[idx] << b[idx];
  267. }
  268. // c
  269. for (size_t idx(0); idx < NUM_C; ++idx) {
  270. os << SEPARATOR_C[idx] << c[idx];
  271. }
  272. // d
  273. for (size_t idx(0); idx < NUM_D; ++idx) {
  274. os << SEPARATOR_D[idx] << d[idx];
  275. }
  276. // e
  277. for (size_t idx(0); idx < NUM_E; ++idx) {
  278. os << SEPARATOR_E[idx] << e[idx];
  279. }
  280. // f
  281. for (size_t idx(0); idx < NUM_F; ++idx) {
  282. os << SEPARATOR_F[idx] << f[idx];
  283. }
  284. // g
  285. for (size_t idx(0); idx < NUM_G; ++idx) {
  286. os << SEPARATOR_G[idx] << g[idx];
  287. }
  288. // h
  289. for (size_t idx(0); idx < NUM_H; ++idx) {
  290. os << SEPARATOR_H[idx] << h[idx];
  291. }
  292. // i
  293. for (size_t idx(0); idx < NUM_I; ++idx) {
  294. os << SEPARATOR_I[idx] << i[idx];
  295. }
  296. // j
  297. for (size_t idx(0); idx < NUM_J; ++idx) {
  298. os << SEPARATOR_J[idx] << j[idx];
  299. }
  300. return os;
  301. }
  302. }; // namespace sinsy