util_converter.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_converter.h"
  42. #include "util_log.h"
  43. #include "util_string.h"
  44. #include "StringTokenizer.h"
  45. #include "MultibyteCharRange.h"
  46. #include <iomanip>
  47. namespace sinsy
  48. {
  49. namespace
  50. {
  51. const size_t SCORE_FLAG_NUM = 8;
  52. const char SCORE_FLAG_CHARS[SCORE_FLAG_NUM] = {'\0', '\0', '\0', '#', '$', '@', '^', '%'}; // '\0': Not defined
  53. const ScoreFlag SCORE_FLAG_DISABLE = 0x10;
  54. const ScoreFlag SCORE_FLAG_FALSETTO = 0x08;
  55. const ScoreFlag SCORE_FLAGS[SCORE_FLAG_NUM] = {0x80, 0x40, 0x20, SCORE_FLAG_DISABLE, SCORE_FLAG_FALSETTO, 0x04, 0x02, 0x01};
  56. /*!
  57. find char from str
  58. */
  59. size_t findChar(std::string& str, char c, const MultibyteCharRange* charRange, size_t startIdx = 0)
  60. {
  61. size_t sz(str.size());
  62. size_t idx(startIdx);
  63. while (idx < sz) {
  64. if (c == str[idx]) {
  65. return idx;
  66. }
  67. if (charRange) {
  68. idx += charRange->getCharSize(static_cast<unsigned char>(str[idx]));
  69. } else {
  70. ++idx;
  71. }
  72. }
  73. return std::string::npos;
  74. }
  75. ScoreFlag matchScoreFlag(char c)
  76. {
  77. for (size_t i(0); i < SCORE_FLAG_NUM; ++i) {
  78. if (SCORE_FLAG_CHARS[i] == c) {
  79. return SCORE_FLAGS[i];
  80. }
  81. }
  82. return 0x00;
  83. }
  84. }; // namespace
  85. /*!
  86. analyze score flags
  87. */
  88. ScoreFlag analyzeScoreFlags(std::string& str, const MultibyteCharRange* charRange)
  89. {
  90. ScoreFlag ret(0x00);
  91. size_t idx(0);
  92. while (idx < str.size()) {
  93. ScoreFlag f(matchScoreFlag(str[idx]));
  94. if (0x00 == f) { // not found
  95. if (charRange) {
  96. idx += charRange->getCharSize(static_cast<unsigned char>(str[idx]));
  97. } else {
  98. ++idx;
  99. }
  100. } else { // found
  101. ret |= f;
  102. str.erase(idx, 1);
  103. }
  104. }
  105. return ret;
  106. }
  107. std::string restoreScoreFlag(ScoreFlag flag)
  108. {
  109. if (0 == flag) {
  110. return std::string();
  111. }
  112. std::ostringstream oss;
  113. for (size_t i(0); i < SCORE_FLAG_NUM; ++i) {
  114. if (SCORE_FLAGS[i] & flag) {
  115. oss << SCORE_FLAG_CHARS[i];
  116. }
  117. }
  118. return oss.str();
  119. }
  120. bool isEnableFlag(ScoreFlag flag)
  121. {
  122. return (0x00 == (SCORE_FLAG_DISABLE & flag));
  123. }
  124. std::string getScoreFlagStr(ScoreFlag flag)
  125. {
  126. std::ostringstream oss;
  127. oss << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(flag);
  128. return oss.str();
  129. }
  130. ScoreFlag& setDisableFlag(ScoreFlag& flag)
  131. {
  132. flag |= SCORE_FLAG_DISABLE;
  133. return flag;
  134. }
  135. ScoreFlag& unsetDisableFlag(ScoreFlag& flag)
  136. {
  137. flag &= ~SCORE_FLAG_DISABLE;
  138. return flag;
  139. }
  140. ScoreFlag& setFalsettoFlag(ScoreFlag& flag)
  141. {
  142. flag |= SCORE_FLAG_FALSETTO;
  143. return flag;
  144. }
  145. }; // namespace sinsy