sample.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*************************************************************************/
  2. /* sample.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "sample.h"
  30. void Sample::_set_data(const Dictionary& p_data) {
  31. ERR_FAIL_COND(!p_data.has("packing"));
  32. String packing = p_data["packing"];
  33. if (packing=="raw") {
  34. ERR_FAIL_COND( !p_data.has("stereo"));
  35. ERR_FAIL_COND( !p_data.has("format"));
  36. ERR_FAIL_COND( !p_data.has("length"));
  37. bool stereo=p_data["stereo"];
  38. int length=p_data["length"];
  39. Format fmt;
  40. String fmtstr=p_data["format"];
  41. if (fmtstr=="pcm8")
  42. fmt=FORMAT_PCM8;
  43. else if (fmtstr=="pcm16")
  44. fmt=FORMAT_PCM16;
  45. else if (fmtstr=="ima_adpcm")
  46. fmt=FORMAT_IMA_ADPCM;
  47. else {
  48. ERR_EXPLAIN("Invalid format for sample: "+fmtstr);
  49. ERR_FAIL();
  50. }
  51. ERR_FAIL_COND(!p_data.has("data"));
  52. create(fmt,stereo,length);
  53. set_data(p_data["data"]);
  54. } else {
  55. ERR_EXPLAIN("Invalid packing for sample data: "+packing);
  56. ERR_FAIL();
  57. }
  58. }
  59. Dictionary Sample::_get_data() const {
  60. Dictionary d;
  61. switch(get_format()) {
  62. case FORMAT_PCM8: d["format"]="pcm8"; break;
  63. case FORMAT_PCM16: d["format"]="pcm16"; break;
  64. case FORMAT_IMA_ADPCM: d["format"]="ima_adpcm"; break;
  65. }
  66. d["stereo"]=is_stereo();
  67. d["length"]=get_length();
  68. d["packing"]="raw";
  69. d["data"]=get_data();
  70. return d;
  71. }
  72. void Sample::create(Format p_format, bool p_stereo, int p_length) {
  73. if (p_length<1)
  74. return;
  75. if (sample.is_valid())
  76. AudioServer::get_singleton()->free(sample);
  77. mix_rate=44100;
  78. stereo=p_stereo;
  79. length=p_length;
  80. format=p_format;
  81. loop_format=LOOP_NONE;
  82. loop_begin=0;
  83. loop_end=0;
  84. sample=AudioServer::get_singleton()->sample_create((AudioServer::SampleFormat)p_format,p_stereo,p_length);
  85. }
  86. Sample::Format Sample::get_format() const {
  87. return format;
  88. }
  89. bool Sample::is_stereo() const {
  90. return stereo;
  91. }
  92. int Sample::get_length() const {
  93. return length;
  94. }
  95. void Sample::set_data(const DVector<uint8_t>& p_buffer) {
  96. if (sample.is_valid())
  97. AudioServer::get_singleton()->sample_set_data(sample,p_buffer);
  98. }
  99. DVector<uint8_t> Sample::get_data() const {
  100. if (sample.is_valid())
  101. return AudioServer::get_singleton()->sample_get_data(sample);
  102. return DVector<uint8_t>();
  103. }
  104. void Sample::set_mix_rate(int p_rate) {
  105. mix_rate=p_rate;
  106. if (sample.is_valid())
  107. return AudioServer::get_singleton()->sample_set_mix_rate(sample,mix_rate);
  108. }
  109. int Sample::get_mix_rate() const {
  110. return mix_rate;
  111. }
  112. void Sample::set_loop_format(LoopFormat p_format) {
  113. if (sample.is_valid())
  114. AudioServer::get_singleton()->sample_set_loop_format(sample,(AudioServer::SampleLoopFormat)p_format);
  115. loop_format=p_format;
  116. }
  117. Sample::LoopFormat Sample::get_loop_format() const {
  118. return loop_format;
  119. }
  120. void Sample::set_loop_begin(int p_pos) {
  121. if (sample.is_valid())
  122. AudioServer::get_singleton()->sample_set_loop_begin(sample,p_pos);
  123. loop_begin=p_pos;
  124. }
  125. int Sample::get_loop_begin() const {
  126. return loop_begin;
  127. }
  128. void Sample::set_loop_end(int p_pos) {
  129. if (sample.is_valid())
  130. AudioServer::get_singleton()->sample_set_loop_end(sample,p_pos);
  131. loop_end=p_pos;
  132. }
  133. int Sample::get_loop_end() const {
  134. return loop_end;
  135. }
  136. RID Sample::get_rid() const {
  137. return sample;
  138. }
  139. void Sample::_bind_methods(){
  140. ObjectTypeDB::bind_method(_MD("create","format","stereo","length"),&Sample::create);
  141. ObjectTypeDB::bind_method(_MD("get_format"),&Sample::get_format);
  142. ObjectTypeDB::bind_method(_MD("is_stereo"),&Sample::is_stereo);
  143. ObjectTypeDB::bind_method(_MD("get_length"),&Sample::get_length);
  144. ObjectTypeDB::bind_method(_MD("set_data","data"),&Sample::set_data);
  145. ObjectTypeDB::bind_method(_MD("get_data"),&Sample::get_data);
  146. ObjectTypeDB::bind_method(_MD("set_mix_rate","hz"),&Sample::set_mix_rate);
  147. ObjectTypeDB::bind_method(_MD("get_mix_rate"),&Sample::get_mix_rate);
  148. ObjectTypeDB::bind_method(_MD("set_loop_format","format"),&Sample::set_loop_format);
  149. ObjectTypeDB::bind_method(_MD("get_loop_format"),&Sample::get_loop_format);
  150. ObjectTypeDB::bind_method(_MD("set_loop_begin","pos"),&Sample::set_loop_begin);
  151. ObjectTypeDB::bind_method(_MD("get_loop_begin"),&Sample::get_loop_begin);
  152. ObjectTypeDB::bind_method(_MD("set_loop_end","pos"),&Sample::set_loop_end);
  153. ObjectTypeDB::bind_method(_MD("get_loop_end"),&Sample::get_loop_end);
  154. ObjectTypeDB::bind_method(_MD("_set_data"),&Sample::_set_data);
  155. ObjectTypeDB::bind_method(_MD("_get_data"),&Sample::_get_data);
  156. ADD_PROPERTY( PropertyInfo( Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), _SCS("_set_data"), _SCS("_get_data") );
  157. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "stereo"), _SCS(""), _SCS("is_stereo") );
  158. ADD_PROPERTY( PropertyInfo( Variant::INT, "length"), _SCS(""), _SCS("get_length") );
  159. ADD_PROPERTY( PropertyInfo( Variant::INT, "mix_rate", PROPERTY_HINT_RANGE,"1,192000,1" ), _SCS("set_mix_rate"), _SCS("get_mix_rate") );
  160. ADD_PROPERTY( PropertyInfo( Variant::INT, "loop_format", PROPERTY_HINT_ENUM,"None,Forward,PingPong" ), _SCS("set_loop_format"), _SCS("get_loop_format") );
  161. ADD_PROPERTY( PropertyInfo( Variant::INT, "loop_begin", PROPERTY_HINT_RANGE,"0,"+itos(99999999)+",1"), _SCS("set_loop_begin"), _SCS("get_loop_begin") );
  162. ADD_PROPERTY( PropertyInfo( Variant::INT, "loop_end", PROPERTY_HINT_RANGE,"0,"+itos(99999999)+",1"), _SCS("set_loop_end"), _SCS("get_loop_end") );
  163. BIND_CONSTANT( FORMAT_PCM8 );
  164. BIND_CONSTANT( FORMAT_PCM16 );
  165. BIND_CONSTANT( FORMAT_IMA_ADPCM );
  166. BIND_CONSTANT( LOOP_NONE );
  167. BIND_CONSTANT( LOOP_FORWARD );
  168. BIND_CONSTANT( LOOP_PING_PONG );
  169. }
  170. Sample::Sample() {
  171. format=FORMAT_PCM8;
  172. length=0;
  173. stereo=false;
  174. loop_format=LOOP_NONE;
  175. loop_begin=0;
  176. loop_end=0;
  177. mix_rate=44100;
  178. }
  179. Sample::~Sample() {
  180. if (sample.is_valid())
  181. AudioServer::get_singleton()->free(sample);
  182. }