Pitch.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "Pitch.h"
  44. #include "util_string.h"
  45. #include "util_log.h"
  46. namespace sinsy
  47. {
  48. const Pitch::Step Pitch::C = 0;
  49. const Pitch::Step Pitch::Db = 1;
  50. const Pitch::Step Pitch::D = 2;
  51. const Pitch::Step Pitch::Eb = 3;
  52. const Pitch::Step Pitch::E = 4;
  53. const Pitch::Step Pitch::F = 5;
  54. const Pitch::Step Pitch::Gb = 6;
  55. const Pitch::Step Pitch::G = 7;
  56. const Pitch::Step Pitch::Ab = 8;
  57. const Pitch::Step Pitch::A = 9;
  58. const Pitch::Step Pitch::Bb = 10;
  59. const Pitch::Step Pitch::B = 11;
  60. const Pitch::Step Pitch::DEFAULT_STEP = 0;
  61. const Pitch::Octave Pitch::DEFAULT_OCTAVE = 4;
  62. namespace
  63. {
  64. const size_t STEP_NUM = 12;
  65. const std::string STEPS[STEP_NUM] = {"C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B"};
  66. class CompPitch
  67. {
  68. public:
  69. //! constructor
  70. CompPitch(const std::string& str) : target(str) {}
  71. //! destructor
  72. virtual ~CompPitch() {}
  73. //! ...
  74. bool operator()(const std::string& str) const {
  75. return (0 == target.compare(str)) ? true : false;
  76. }
  77. //! target
  78. const std::string& target;
  79. };
  80. };
  81. /*!
  82. constructor
  83. */
  84. Pitch::Pitch() : step(DEFAULT_STEP), octave(DEFAULT_OCTAVE)
  85. {
  86. }
  87. /*!
  88. constructor
  89. */
  90. Pitch::Pitch(Step s, Octave o, int a)
  91. {
  92. set(s, o, a);
  93. }
  94. /*!
  95. constructor
  96. */
  97. Pitch::Pitch(const std::string& s, Octave o, int a)
  98. {
  99. set(s, o, a);
  100. }
  101. /*!
  102. copy constructor
  103. */
  104. Pitch::Pitch(const Pitch& obj) : step(obj.step), octave(obj.octave)
  105. {
  106. }
  107. /*!
  108. destructor
  109. */
  110. Pitch::~Pitch()
  111. {
  112. }
  113. /*!
  114. assignment operator
  115. */
  116. Pitch& Pitch::operator=(const Pitch & obj)
  117. {
  118. if (this != &obj) {
  119. this->step = obj.step;
  120. this->octave = obj.octave;
  121. }
  122. return *this;
  123. }
  124. /*!
  125. equrl
  126. */
  127. bool Pitch::operator==(const Pitch& obj) const
  128. {
  129. if (this == &obj) {
  130. return true;
  131. }
  132. return ((step == obj.step) && (octave == obj.octave)) ? true : false;
  133. }
  134. /*!
  135. not equrl
  136. */
  137. bool Pitch::operator!=(const Pitch& obj) const
  138. {
  139. return !(*this == obj);
  140. }
  141. /*!
  142. set
  143. */
  144. void Pitch::set(Pitch::Step s, Pitch::Octave o, int alter)
  145. {
  146. s += alter;
  147. while (s < 0) {
  148. s += 12;
  149. --o;
  150. }
  151. while (11 < s) {
  152. s -= 12;
  153. ++o;
  154. }
  155. this->step = s;
  156. this->octave = o;
  157. }
  158. /*!
  159. set
  160. */
  161. void Pitch::set(const std::string& s, Pitch::Octave o, int alter)
  162. {
  163. std::string str(s);
  164. toUpper(str);
  165. const std::string* itr(std::find_if(STEPS, STEPS + STEP_NUM, CompPitch(str)));
  166. if (itr < STEPS + STEP_NUM) {
  167. set(itr - STEPS, o, alter);
  168. return;
  169. }
  170. ERR_MSG("Unexpected pitch : " << str << " [step:" << s << ", octave:" << o << ", alter:" << alter << "]");
  171. throw std::runtime_error("Pitch::set() invalid argument");
  172. }
  173. /*!
  174. get step
  175. */
  176. Pitch::Step Pitch::getStep() const
  177. {
  178. return step;
  179. }
  180. /*!
  181. get octave
  182. */
  183. Pitch::Octave Pitch::getOctave() const
  184. {
  185. return octave;
  186. }
  187. /*!
  188. get string of step
  189. */
  190. const std::string& Pitch::getStepStr() const
  191. {
  192. return STEPS[this->step];
  193. }
  194. /*!
  195. addition
  196. */
  197. Pitch& Pitch::operator+=(int i)
  198. {
  199. step += i;
  200. while (step < 0) {
  201. step += 12;
  202. if (0 == octave) {
  203. std::range_error("Pitch octave is out of range : octave < 0");
  204. }
  205. --octave;
  206. }
  207. while (11 < step) {
  208. step -= 12;
  209. ++octave;
  210. }
  211. return *this;
  212. }
  213. /*!
  214. subtraction
  215. */
  216. Pitch& Pitch::operator-=(int i)
  217. {
  218. return (*this) += (-i);
  219. }
  220. /*!
  221. increment
  222. */
  223. Pitch& Pitch::operator++()
  224. {
  225. return (*this) += 1;
  226. }
  227. /*!
  228. decrement
  229. */
  230. Pitch& Pitch::operator--()
  231. {
  232. return (*this) -= 1;
  233. }
  234. /*!
  235. addition
  236. */
  237. Pitch operator+(const Pitch& pitch, int i)
  238. {
  239. Pitch ret(pitch);
  240. return ret += i;
  241. }
  242. /*!
  243. subtraction
  244. */
  245. Pitch operator-(const Pitch& pitch, int i)
  246. {
  247. return pitch + (-i);
  248. }
  249. /*!
  250. increment
  251. */
  252. Pitch operator++(const Pitch& pitch)
  253. {
  254. Pitch ret(pitch);
  255. return pitch + 1;
  256. }
  257. /*!
  258. decrement
  259. */
  260. Pitch operator--(const Pitch& pitch)
  261. {
  262. Pitch ret(pitch);
  263. return pitch - 1;
  264. }
  265. /*!
  266. to stream
  267. */
  268. std::ostream& operator<<(std::ostream& os, const Pitch& pitch)
  269. {
  270. return os << pitch.getStepStr() << pitch.getOctave();
  271. }
  272. }; // namespace sinsy