cp_envelope.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*************************************************************************/
  2. /* cp_envelope.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "cp_envelope.h"
  31. CPEnvelope::CPEnvelope() {
  32. reset();
  33. }
  34. void CPEnvelope::reset() {
  35. on = false;
  36. carry = false;
  37. loop_on = false;
  38. loop_begin_node = 0;
  39. loop_end_node = 0;
  40. sustain_loop_on = false;
  41. sustain_loop_begin_node = 0;
  42. sustain_loop_end_node = 0;
  43. node_count = 0;
  44. }
  45. int CPEnvelope::get_height_at_pos(int pos) {
  46. if (node_count && pos > node[node_count - 1].tick_offset)
  47. return node[node_count - 1].value;
  48. int begin_x, begin_y;
  49. int end_x, end_y, xdif;
  50. int count = 0;
  51. int limit = -1;
  52. if (node_count < 2) return NO_POINT;
  53. while ((count < node_count) && (limit == -1)) {
  54. if (node[count].tick_offset >= pos) limit = count;
  55. count++;
  56. }
  57. if (pos == 0) return node[0].value;
  58. if (limit == -1) return NO_POINT;
  59. begin_x = node[limit - 1].tick_offset;
  60. end_x = node[limit].tick_offset;
  61. begin_y = node[limit - 1].value;
  62. end_y = node[limit].value;
  63. xdif = end_x - begin_x;
  64. return begin_y + ((pos - begin_x) * (end_y - begin_y)) / (xdif ? xdif : 1);
  65. }
  66. /*
  67. int CPEnvelope::get_fx_height_at_pos(int pos) {
  68. if (node_count && pos>node[node_count-1].tick_offset)
  69. return node[node_count-1].value<<FX_HEIGHT_BITS;
  70. int begin_x,begin_y;
  71. int end_x,end_y,xdif;
  72. int count=0;
  73. int limit=-1;
  74. if (node_count<2) return NO_POINT;
  75. while ((count<node_count) && (limit==-1)) {
  76. if (node[count].tick_offset>=pos) limit=count;
  77. count++;
  78. }
  79. if (pos==0) return node[0].value<<FX_HEIGHT_BITS;
  80. if (limit==-1) return NO_POINT;
  81. begin_x=node[limit-1].tick_offset;
  82. end_x=node[limit].tick_offset;
  83. begin_y=node[limit-1].value;
  84. end_y=node[limit].value;
  85. xdif=end_x-begin_x;
  86. return (begin_y<<FX_HEIGHT_BITS)+((pos-begin_x)*(end_y-begin_y)*(int)(1<<FX_HEIGHT_BITS))/(xdif?xdif:1);
  87. }
  88. */
  89. float CPEnvelope::get_interp_height_at_pos(float pos) {
  90. if (node_count && pos > node[node_count - 1].tick_offset)
  91. return node[node_count - 1].value;
  92. int begin_x, begin_y;
  93. int end_x, end_y, xdif;
  94. int count = 0;
  95. int limit = -1;
  96. if (node_count < 2) return NO_POINT;
  97. while ((count < node_count) && (limit == -1)) {
  98. if (node[count].tick_offset >= pos) limit = count;
  99. count++;
  100. }
  101. if (pos == 0) return node[0].value;
  102. if (limit == -1) return NO_POINT;
  103. begin_x = node[limit - 1].tick_offset;
  104. end_x = node[limit].tick_offset;
  105. begin_y = node[limit - 1].value;
  106. end_y = node[limit].value;
  107. xdif = end_x - begin_x;
  108. return begin_y + ((pos - begin_x) * (end_y - begin_y)) / (xdif ? xdif : 1);
  109. }
  110. void CPEnvelope::set_position(int p_node, int p_x, int p_y) {
  111. if (p_node >= node_count) return;
  112. if (p_node == 0) {
  113. p_x = 0;
  114. } else if (p_x <= node[p_node - 1].tick_offset) {
  115. p_x = node[p_node - 1].tick_offset + 1;
  116. } else if ((p_node < (node_count - 1)) && (p_x >= node[p_node + 1].tick_offset)) {
  117. p_x = node[p_node + 1].tick_offset - 1;
  118. }
  119. if (p_x >= 9999) p_x = 9999;
  120. if (p_y > max_value) p_y = max_value;
  121. if (p_y < min_value) p_y = min_value;
  122. node[p_node].tick_offset = p_x;
  123. node[p_node].value = p_y;
  124. }
  125. int CPEnvelope::add_position(int p_x, int p_y, bool p_move_loops) {
  126. if (node_count == MAX_POINTS) return -1;
  127. int i, new_node;
  128. // if this is assigning an existing node, let's quit.
  129. for (i = 0; i < node_count; i++)
  130. if (p_x == node[i].tick_offset) return -1;
  131. i = 0;
  132. while ((i < node_count) && (p_x >= node[i].tick_offset))
  133. i++;
  134. new_node = i;
  135. node_count++;
  136. if (p_move_loops) {
  137. if (loop_begin_node >= new_node) loop_begin_node++;
  138. if (loop_end_node >= new_node) loop_end_node++;
  139. if (sustain_loop_begin_node >= new_node) sustain_loop_begin_node++;
  140. if (sustain_loop_end_node >= new_node) sustain_loop_end_node++;
  141. }
  142. for (i = node_count - 1; i > new_node; i--)
  143. node[i] = node[i - 1];
  144. set_position(new_node, p_x, p_y);
  145. return new_node;
  146. }
  147. void CPEnvelope::set_loop_begin(int pos) {
  148. if ((pos < 0) || (pos >= node_count)) return;
  149. loop_begin_node = pos;
  150. if (loop_end_node < loop_begin_node) loop_end_node = loop_begin_node;
  151. }
  152. void CPEnvelope::set_loop_end(int pos) {
  153. if ((pos < 0) || (pos >= node_count)) return;
  154. loop_end_node = pos;
  155. if (loop_end_node < loop_begin_node) loop_begin_node = loop_end_node;
  156. }
  157. void CPEnvelope::set_sustain_loop_begin(int pos) {
  158. if ((pos < 0) || (pos >= node_count)) return;
  159. sustain_loop_begin_node = pos;
  160. if (sustain_loop_end_node < sustain_loop_begin_node) sustain_loop_end_node = sustain_loop_begin_node;
  161. }
  162. void CPEnvelope::set_sustain_loop_end(int pos) {
  163. if ((pos < 0) || (pos >= node_count)) return;
  164. sustain_loop_end_node = pos;
  165. if (sustain_loop_end_node < sustain_loop_begin_node) sustain_loop_begin_node = sustain_loop_end_node;
  166. }
  167. void CPEnvelope::set_loop_enabled(bool p_enabled) {
  168. loop_on = p_enabled;
  169. }
  170. bool CPEnvelope::is_loop_enabled() {
  171. return loop_on;
  172. }
  173. void CPEnvelope::set_sustain_loop_enabled(bool p_enabled) {
  174. sustain_loop_on = p_enabled;
  175. }
  176. bool CPEnvelope::is_sustain_loop_enabled() {
  177. return sustain_loop_on;
  178. }
  179. void CPEnvelope::del_position(int p_node) {
  180. if ((node_count < 3) || (p_node <= 0) || (p_node >= node_count)) return;
  181. int i;
  182. if (loop_begin_node >= p_node) loop_begin_node--;
  183. if (loop_end_node >= p_node) loop_end_node--;
  184. if (sustain_loop_begin_node >= p_node) sustain_loop_begin_node--;
  185. if (sustain_loop_end_node >= p_node) sustain_loop_end_node--;
  186. for (i = p_node; i < node_count - 1; i++)
  187. node[i] = node[i + 1];
  188. node_count--;
  189. }
  190. uint8_t CPEnvelope::get_loop_begin() {
  191. return loop_begin_node;
  192. }
  193. uint8_t CPEnvelope::get_loop_end() {
  194. return loop_end_node;
  195. }
  196. uint8_t CPEnvelope::get_sustain_loop_begin() {
  197. return sustain_loop_begin_node;
  198. }
  199. uint8_t CPEnvelope::get_sustain_loop_end() {
  200. return sustain_loop_end_node;
  201. }
  202. void CPEnvelope::set_enabled(bool p_enabled) {
  203. on = p_enabled;
  204. }
  205. bool CPEnvelope::is_enabled() {
  206. return on;
  207. }
  208. void CPEnvelope::set_carry_enabled(bool p_enabled) {
  209. carry = p_enabled;
  210. }
  211. bool CPEnvelope::is_carry_enabled() {
  212. return carry;
  213. }
  214. uint8_t CPEnvelope::get_node_count() {
  215. return node_count;
  216. }
  217. const CPEnvelope::Point &CPEnvelope::get_node(int p_idx) {
  218. if (p_idx < 0 || p_idx >= node_count)
  219. return node[node_count - 1];
  220. return node[p_idx];
  221. }