sample.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*************************************************************************/
  2. /* sample.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 "sample.h"
  31. void Sample::_set_data(const Dictionary &p_data) {
  32. ERR_FAIL_COND(!p_data.has("packing"));
  33. String packing = p_data["packing"];
  34. if (packing == "raw") {
  35. ERR_FAIL_COND(!p_data.has("stereo"));
  36. ERR_FAIL_COND(!p_data.has("format"));
  37. ERR_FAIL_COND(!p_data.has("length"));
  38. bool stereo = p_data["stereo"];
  39. int length = p_data["length"];
  40. Format fmt;
  41. String fmtstr = p_data["format"];
  42. if (fmtstr == "pcm8")
  43. fmt = FORMAT_PCM8;
  44. else if (fmtstr == "pcm16")
  45. fmt = FORMAT_PCM16;
  46. else if (fmtstr == "ima_adpcm")
  47. fmt = FORMAT_IMA_ADPCM;
  48. else {
  49. ERR_EXPLAIN("Invalid format for sample: " + fmtstr);
  50. ERR_FAIL();
  51. }
  52. ERR_FAIL_COND(!p_data.has("data"));
  53. create(fmt, stereo, length);
  54. set_data(p_data["data"]);
  55. } else {
  56. ERR_EXPLAIN("Invalid packing for sample data: " + packing);
  57. ERR_FAIL();
  58. }
  59. }
  60. Dictionary Sample::_get_data() const {
  61. Dictionary d;
  62. switch (get_format()) {
  63. case FORMAT_PCM8: d["format"] = "pcm8"; break;
  64. case FORMAT_PCM16: d["format"] = "pcm16"; break;
  65. case FORMAT_IMA_ADPCM: d["format"] = "ima_adpcm"; break;
  66. }
  67. d["stereo"] = is_stereo();
  68. d["length"] = get_length();
  69. d["packing"] = "raw";
  70. d["data"] = get_data();
  71. return d;
  72. }
  73. void Sample::create(Format p_format, bool p_stereo, int p_length) {
  74. if (p_length < 1)
  75. return;
  76. if (sample.is_valid())
  77. AudioServer::get_singleton()->free(sample);
  78. mix_rate = 44100;
  79. stereo = p_stereo;
  80. length = p_length;
  81. format = p_format;
  82. loop_format = LOOP_NONE;
  83. loop_begin = 0;
  84. loop_end = 0;
  85. sample = AudioServer::get_singleton()->sample_create((AudioServer::SampleFormat)p_format, p_stereo, p_length);
  86. }
  87. Sample::Format Sample::get_format() const {
  88. return format;
  89. }
  90. bool Sample::is_stereo() const {
  91. return stereo;
  92. }
  93. int Sample::get_length() const {
  94. return length;
  95. }
  96. void Sample::set_data(const DVector<uint8_t> &p_buffer) {
  97. if (sample.is_valid())
  98. AudioServer::get_singleton()->sample_set_data(sample, p_buffer);
  99. }
  100. DVector<uint8_t> Sample::get_data() const {
  101. if (sample.is_valid())
  102. return AudioServer::get_singleton()->sample_get_data(sample);
  103. return DVector<uint8_t>();
  104. }
  105. void Sample::set_mix_rate(int p_rate) {
  106. mix_rate = p_rate;
  107. if (sample.is_valid())
  108. return AudioServer::get_singleton()->sample_set_mix_rate(sample, mix_rate);
  109. }
  110. int Sample::get_mix_rate() const {
  111. return mix_rate;
  112. }
  113. void Sample::set_loop_format(LoopFormat p_format) {
  114. if (sample.is_valid())
  115. AudioServer::get_singleton()->sample_set_loop_format(sample, (AudioServer::SampleLoopFormat)p_format);
  116. loop_format = p_format;
  117. }
  118. Sample::LoopFormat Sample::get_loop_format() const {
  119. return loop_format;
  120. }
  121. void Sample::set_loop_begin(int p_pos) {
  122. if (sample.is_valid())
  123. AudioServer::get_singleton()->sample_set_loop_begin(sample, p_pos);
  124. loop_begin = p_pos;
  125. }
  126. int Sample::get_loop_begin() const {
  127. return loop_begin;
  128. }
  129. void Sample::set_loop_end(int p_pos) {
  130. if (sample.is_valid())
  131. AudioServer::get_singleton()->sample_set_loop_end(sample, p_pos);
  132. loop_end = p_pos;
  133. }
  134. int Sample::get_loop_end() const {
  135. return loop_end;
  136. }
  137. RID Sample::get_rid() const {
  138. return sample;
  139. }
  140. void Sample::_bind_methods() {
  141. ObjectTypeDB::bind_method(_MD("create", "format", "stereo", "length"), &Sample::create);
  142. ObjectTypeDB::bind_method(_MD("get_format"), &Sample::get_format);
  143. ObjectTypeDB::bind_method(_MD("is_stereo"), &Sample::is_stereo);
  144. ObjectTypeDB::bind_method(_MD("get_length"), &Sample::get_length);
  145. ObjectTypeDB::bind_method(_MD("set_data", "data"), &Sample::set_data);
  146. ObjectTypeDB::bind_method(_MD("get_data"), &Sample::get_data);
  147. ObjectTypeDB::bind_method(_MD("set_mix_rate", "hz"), &Sample::set_mix_rate);
  148. ObjectTypeDB::bind_method(_MD("get_mix_rate"), &Sample::get_mix_rate);
  149. ObjectTypeDB::bind_method(_MD("set_loop_format", "format"), &Sample::set_loop_format);
  150. ObjectTypeDB::bind_method(_MD("get_loop_format"), &Sample::get_loop_format);
  151. ObjectTypeDB::bind_method(_MD("set_loop_begin", "pos"), &Sample::set_loop_begin);
  152. ObjectTypeDB::bind_method(_MD("get_loop_begin"), &Sample::get_loop_begin);
  153. ObjectTypeDB::bind_method(_MD("set_loop_end", "pos"), &Sample::set_loop_end);
  154. ObjectTypeDB::bind_method(_MD("get_loop_end"), &Sample::get_loop_end);
  155. ObjectTypeDB::bind_method(_MD("_set_data"), &Sample::_set_data);
  156. ObjectTypeDB::bind_method(_MD("_get_data"), &Sample::_get_data);
  157. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), _SCS("_set_data"), _SCS("_get_data"));
  158. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stereo"), _SCS(""), _SCS("is_stereo"));
  159. ADD_PROPERTY(PropertyInfo(Variant::INT, "length", PROPERTY_HINT_RANGE, "0,999999999"), _SCS(""), _SCS("get_length"));
  160. ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_rate", PROPERTY_HINT_RANGE, "1,192000,1"), _SCS("set_mix_rate"), _SCS("get_mix_rate"));
  161. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_format", PROPERTY_HINT_ENUM, "None,Forward,PingPong"), _SCS("set_loop_format"), _SCS("get_loop_format"));
  162. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_begin", PROPERTY_HINT_RANGE, "0," + itos(999999999) + ",1"), _SCS("set_loop_begin"), _SCS("get_loop_begin"));
  163. ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_end", PROPERTY_HINT_RANGE, "0," + itos(999999999) + ",1"), _SCS("set_loop_end"), _SCS("get_loop_end"));
  164. BIND_CONSTANT(FORMAT_PCM8);
  165. BIND_CONSTANT(FORMAT_PCM16);
  166. BIND_CONSTANT(FORMAT_IMA_ADPCM);
  167. BIND_CONSTANT(LOOP_NONE);
  168. BIND_CONSTANT(LOOP_FORWARD);
  169. BIND_CONSTANT(LOOP_PING_PONG);
  170. }
  171. Sample::Sample() {
  172. format = FORMAT_PCM8;
  173. length = 0;
  174. stereo = false;
  175. loop_format = LOOP_NONE;
  176. loop_begin = 0;
  177. loop_end = 0;
  178. mix_rate = 44100;
  179. }
  180. Sample::~Sample() {
  181. if (sample.is_valid())
  182. AudioServer::get_singleton()->free(sample);
  183. }