sample_manager_sw.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*************************************************************************/
  2. /* sample_manager_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 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_manager_sw.h"
  30. #include "print_string.h"
  31. SampleManagerSW::~SampleManagerSW()
  32. {
  33. }
  34. RID SampleManagerMallocSW::sample_create(AS::SampleFormat p_format, bool p_stereo, int p_length) {
  35. ERR_EXPLAIN("IMA-ADPCM and STEREO are not a valid combination for sample format.");
  36. ERR_FAIL_COND_V( p_format == AS::SAMPLE_FORMAT_IMA_ADPCM && p_stereo,RID());
  37. Sample *s = memnew( Sample );
  38. int datalen = p_length;
  39. if (p_stereo)
  40. datalen*=2;
  41. if (p_format==AS::SAMPLE_FORMAT_PCM16)
  42. datalen*=2;
  43. else if (p_format==AS::SAMPLE_FORMAT_IMA_ADPCM) {
  44. if (datalen&1) {
  45. datalen++;
  46. }
  47. datalen/=2;
  48. datalen+=4;
  49. }
  50. #define SAMPLE_EXTRA 16
  51. s->data = memalloc(datalen+SAMPLE_EXTRA); //help the interpolator by allocating a little more..
  52. for(int i=0;i<SAMPLE_EXTRA;i++) {
  53. uint8_t *data = (uint8_t*)s->data;
  54. data[datalen+i]=0;
  55. }
  56. if (!s->data) {
  57. memdelete(s);
  58. ERR_EXPLAIN("Cannot allocate sample of requested size.");
  59. ERR_FAIL_V(RID());
  60. }
  61. s->format=p_format;
  62. s->length=p_length;
  63. s->length_bytes=datalen;
  64. s->stereo=p_stereo;
  65. s->loop_begin=0;
  66. s->loop_end=0;
  67. s->loop_format=AS::SAMPLE_LOOP_NONE;
  68. s->mix_rate=44100;
  69. AudioServer::get_singleton()->lock();
  70. RID rid = sample_owner.make_rid(s);
  71. AudioServer::get_singleton()->unlock();
  72. return rid;
  73. }
  74. void SampleManagerMallocSW::sample_set_description(RID p_sample, const String& p_description) {
  75. Sample *s = sample_owner.get(p_sample);
  76. ERR_FAIL_COND(!s);
  77. s->description=p_description;
  78. }
  79. String SampleManagerMallocSW::sample_get_description(RID p_sample) const {
  80. const Sample *s = sample_owner.get(p_sample);
  81. ERR_FAIL_COND_V(!s,String());
  82. return s->description;
  83. }
  84. AS::SampleFormat SampleManagerMallocSW::sample_get_format(RID p_sample) const {
  85. const Sample *s = sample_owner.get(p_sample);
  86. ERR_FAIL_COND_V(!s,AS::SAMPLE_FORMAT_PCM8);
  87. return s->format;
  88. }
  89. bool SampleManagerMallocSW::sample_is_stereo(RID p_sample) const {
  90. const Sample *s = sample_owner.get(p_sample);
  91. ERR_FAIL_COND_V(!s,false);
  92. return s->stereo;
  93. }
  94. int SampleManagerMallocSW::sample_get_length(RID p_sample) const {
  95. const Sample *s = sample_owner.get(p_sample);
  96. ERR_FAIL_COND_V(!s,-1);
  97. return s->length;
  98. }
  99. void SampleManagerMallocSW::sample_set_data(RID p_sample, const DVector<uint8_t>& p_buffer) {
  100. Sample *s = sample_owner.get(p_sample);
  101. ERR_FAIL_COND(!s);
  102. int buff_size=p_buffer.size();
  103. ERR_FAIL_COND(buff_size==0);
  104. ERR_EXPLAIN("Sample buffer size does not match sample size.");
  105. print_line("len bytes: "+itos(s->length_bytes)+" bufsize: "+itos(buff_size));
  106. ERR_FAIL_COND(s->length_bytes!=buff_size);
  107. DVector<uint8_t>::Read buffer_r=p_buffer.read();
  108. const uint8_t *src = buffer_r.ptr();
  109. uint8_t *dst = (uint8_t*)s->data;
  110. //print_line("set data: "+itos(s->length_bytes));
  111. for(int i=0;i<s->length_bytes;i++) {
  112. dst[i]=src[i];
  113. }
  114. switch(s->format) {
  115. case AS::SAMPLE_FORMAT_PCM8: {
  116. if (s->stereo) {
  117. dst[s->length]=dst[s->length-2];
  118. dst[s->length+1]=dst[s->length-1];
  119. } else {
  120. dst[s->length]=dst[s->length-1];
  121. }
  122. } break;
  123. case AS::SAMPLE_FORMAT_PCM16: {
  124. if (s->stereo) {
  125. dst[s->length]=dst[s->length-4];
  126. dst[s->length+1]=dst[s->length-3];
  127. dst[s->length+2]=dst[s->length-2];
  128. dst[s->length+3]=dst[s->length-1];
  129. } else {
  130. dst[s->length]=dst[s->length-2];
  131. dst[s->length+1]=dst[s->length-1];
  132. }
  133. } break;
  134. }
  135. }
  136. const DVector<uint8_t> SampleManagerMallocSW::sample_get_data(RID p_sample) const {
  137. Sample *s = sample_owner.get(p_sample);
  138. ERR_FAIL_COND_V(!s,DVector<uint8_t>());
  139. DVector<uint8_t> ret_buffer;
  140. ret_buffer.resize(s->length_bytes);
  141. DVector<uint8_t>::Write buffer_w=ret_buffer.write();
  142. uint8_t *dst = buffer_w.ptr();
  143. const uint8_t *src = (const uint8_t*)s->data;
  144. for(int i=0;i<s->length_bytes;i++) {
  145. dst[i]=src[i];
  146. }
  147. buffer_w = DVector<uint8_t>::Write(); //unlock
  148. return ret_buffer;
  149. }
  150. void *SampleManagerMallocSW::sample_get_data_ptr(RID p_sample) const {
  151. const Sample *s = sample_owner.get(p_sample);
  152. ERR_FAIL_COND_V(!s,NULL);
  153. return s->data;
  154. }
  155. void SampleManagerMallocSW::sample_set_mix_rate(RID p_sample,int p_rate) {
  156. ERR_FAIL_COND(p_rate<1);
  157. Sample *s = sample_owner.get(p_sample);
  158. ERR_FAIL_COND(!s);
  159. s->mix_rate=p_rate;
  160. }
  161. int SampleManagerMallocSW::sample_get_mix_rate(RID p_sample) const {
  162. const Sample *s = sample_owner.get(p_sample);
  163. ERR_FAIL_COND_V(!s,-1);
  164. return s->mix_rate;
  165. }
  166. void SampleManagerMallocSW::sample_set_loop_format(RID p_sample,AS::SampleLoopFormat p_format) {
  167. Sample *s = sample_owner.get(p_sample);
  168. ERR_FAIL_COND(!s);
  169. s->loop_format=p_format;
  170. }
  171. AS::SampleLoopFormat SampleManagerMallocSW::sample_get_loop_format(RID p_sample) const {
  172. const Sample *s = sample_owner.get(p_sample);
  173. ERR_FAIL_COND_V(!s,AS::SAMPLE_LOOP_NONE);
  174. return s->loop_format;
  175. }
  176. void SampleManagerMallocSW::sample_set_loop_begin(RID p_sample,int p_pos) {
  177. Sample *s = sample_owner.get(p_sample);
  178. ERR_FAIL_COND(!s);
  179. ERR_FAIL_INDEX(p_pos,s->length);
  180. s->loop_begin=p_pos;
  181. }
  182. int SampleManagerMallocSW::sample_get_loop_begin(RID p_sample) const {
  183. const Sample *s = sample_owner.get(p_sample);
  184. ERR_FAIL_COND_V(!s,-1);
  185. return s->loop_begin;
  186. }
  187. void SampleManagerMallocSW::sample_set_loop_end(RID p_sample,int p_pos) {
  188. Sample *s = sample_owner.get(p_sample);
  189. ERR_FAIL_COND(!s);
  190. if (p_pos>s->length)
  191. p_pos=s->length;
  192. s->loop_end=p_pos;
  193. }
  194. int SampleManagerMallocSW::sample_get_loop_end(RID p_sample) const {
  195. const Sample *s = sample_owner.get(p_sample);
  196. ERR_FAIL_COND_V(!s,-1);
  197. return s->loop_end;
  198. }
  199. bool SampleManagerMallocSW::is_sample(RID p_sample) const {
  200. return sample_owner.owns(p_sample);
  201. }
  202. void SampleManagerMallocSW::free(RID p_sample) {
  203. Sample *s = sample_owner.get(p_sample);
  204. ERR_FAIL_COND(!s);
  205. AudioServer::get_singleton()->lock();
  206. sample_owner.free(p_sample);
  207. AudioServer::get_singleton()->unlock();
  208. memfree(s->data);
  209. memdelete(s);
  210. }
  211. SampleManagerMallocSW::SampleManagerMallocSW() {
  212. }
  213. SampleManagerMallocSW::~SampleManagerMallocSW() {
  214. // check for sample leakage
  215. List<RID> owned_list;
  216. sample_owner.get_owned_list(&owned_list);
  217. while(owned_list.size()) {
  218. Sample *s = sample_owner.get(owned_list.front()->get());
  219. String err="Leaked sample of size: "+itos(s->length_bytes)+" description: "+s->description;
  220. ERR_PRINT(err.utf8().get_data());
  221. free(owned_list.front()->get());
  222. owned_list.pop_front();
  223. }
  224. }