Dynamics.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 <algorithm>
  42. #include <stdexcept>
  43. #include "Dynamics.h"
  44. #include "util_log.h"
  45. #include "util_string.h"
  46. namespace sinsy
  47. {
  48. namespace
  49. {
  50. const int DYNAMICS_NUM = 11;
  51. const std::string STR_P4 = "p4";
  52. const std::string STR_P3 = "p3";
  53. const std::string STR_P2 = "p2";
  54. const std::string STR_P1 = "p1";
  55. const std::string STR_MP = "mp";
  56. const std::string STR_N = "n";
  57. const std::string STR_MF = "mf";
  58. const std::string STR_F1 = "f1";
  59. const std::string STR_F2 = "f2";
  60. const std::string STR_F3 = "f3";
  61. const std::string STR_F4 = "f4";
  62. const std::string STR_PPPP = "pppp";
  63. const std::string STR_PPP = "ppp";
  64. const std::string STR_PP = "pp";
  65. const std::string STR_P = "p";
  66. const std::string STR_F = "f";
  67. const std::string STR_FF = "ff";
  68. const std::string STR_FFF = "fff";
  69. const std::string STR_FFFF = "ffff";
  70. const std::string DYNAMICSES[] = {
  71. STR_P4, STR_P3, STR_P2, STR_P1, STR_MP, STR_N, STR_MF, STR_F1, STR_F2, STR_F3, STR_F4
  72. };
  73. const std::string DYNAMICS_TAGS[] = {
  74. STR_PPPP, STR_PPP, STR_PP, STR_P, STR_MP, STR_N, STR_MF, STR_F, STR_FF, STR_FFF, STR_FFFF
  75. };
  76. class CompDynamics
  77. {
  78. public:
  79. //! constructor
  80. CompDynamics(const std::string& str) : target(str) {}
  81. //! destructor
  82. virtual ~CompDynamics() {}
  83. //! ...
  84. bool operator()(const std::string& str) const {
  85. return (0 == target.compare(str)) ? true : false;
  86. }
  87. //! target
  88. const std::string& target;
  89. };
  90. };
  91. const Dynamics Dynamics::PPPP(STR_PPPP);
  92. const Dynamics Dynamics::PPP(STR_PPP);
  93. const Dynamics Dynamics::PP(STR_PP);
  94. const Dynamics Dynamics::P(STR_P);
  95. const Dynamics Dynamics::MP(STR_MP);
  96. const Dynamics Dynamics::N(STR_N);
  97. const Dynamics Dynamics::MF(STR_MF);
  98. const Dynamics Dynamics::F(STR_F);
  99. const Dynamics Dynamics::FF(STR_FF);
  100. const Dynamics Dynamics::FFF(STR_FFF);
  101. const Dynamics Dynamics::FFFF(STR_FFFF);
  102. /*!
  103. constructor
  104. */
  105. Dynamics::Dynamics() : value((DYNAMICS_NUM - 1) / 2)
  106. {
  107. }
  108. /*!
  109. constructor
  110. */
  111. Dynamics::Dynamics(const std::string& str)
  112. {
  113. set(str);
  114. }
  115. /*!
  116. copy constructor
  117. */
  118. Dynamics::Dynamics(const Dynamics& obj) : value(obj.value)
  119. {
  120. }
  121. /*!
  122. destructor
  123. */
  124. Dynamics::~Dynamics()
  125. {
  126. }
  127. /*!
  128. assignment operator
  129. */
  130. Dynamics& Dynamics::operator=(const Dynamics & obj)
  131. {
  132. value = obj.value;
  133. return *this;
  134. }
  135. /*!
  136. equal
  137. */
  138. bool Dynamics::operator==(const Dynamics& obj) const
  139. {
  140. return (obj.value == value);
  141. }
  142. /*!
  143. not equal
  144. */
  145. bool Dynamics::operator!=(const Dynamics& obj) const
  146. {
  147. return !(obj == *this);
  148. }
  149. /*!
  150. set value
  151. */
  152. void Dynamics::set(const std::string& _str)
  153. {
  154. std::string s(_str);
  155. toLower(s);
  156. const std::string* itr(std::find_if(DYNAMICSES, DYNAMICSES + DYNAMICS_NUM, CompDynamics(s)));
  157. if (itr < DYNAMICSES + DYNAMICS_NUM) {
  158. value = itr - DYNAMICSES;
  159. return;
  160. }
  161. itr = std::find_if(DYNAMICS_TAGS, DYNAMICS_TAGS + DYNAMICS_NUM, CompDynamics(s));
  162. if (itr < DYNAMICS_TAGS + DYNAMICS_NUM) {
  163. value = itr - DYNAMICS_TAGS;
  164. return;
  165. }
  166. ERR_MSG("Unexpected dynamics : " << s);
  167. throw std::runtime_error("Dynamics::set() invalid argument");
  168. }
  169. /*!
  170. get valie as string
  171. */
  172. const std::string& Dynamics::getStr() const
  173. {
  174. return DYNAMICSES[value];
  175. }
  176. /*!
  177. get tag string
  178. */
  179. const std::string& Dynamics::getTagStr() const
  180. {
  181. return DYNAMICS_TAGS[value];
  182. }
  183. /*!
  184. to string
  185. */
  186. std::ostream& operator<<(std::ostream& os, const Dynamics& dynamics)
  187. {
  188. return os << dynamics.getStr();
  189. }
  190. }; // namespace sinsy