resource_format_wav.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*************************************************************************/
  2. /* resource_format_wav.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 "resource_format_wav.h"
  30. #include "os/file_access.h"
  31. #include "scene/resources/sample.h"
  32. RES ResourceFormatLoaderWAV::load(const String &p_path,const String& p_original_path) {
  33. Error err;
  34. FileAccess *file=FileAccess::open(p_path, FileAccess::READ,&err);
  35. ERR_FAIL_COND_V( err!=OK, RES() );
  36. /* CHECK RIFF */
  37. char riff[5];
  38. riff[4]=0;
  39. file->get_buffer((uint8_t*)&riff,4); //RIFF
  40. if (riff[0]!='R' || riff[1]!='I' || riff[2]!='F' || riff[3]!='F') {
  41. file->close();
  42. memdelete(file);
  43. ERR_FAIL_V( RES() );
  44. }
  45. /* GET FILESIZE */
  46. uint32_t filesize=file->get_32();
  47. /* CHECK WAVE */
  48. char wave[4];
  49. file->get_buffer((uint8_t*)&wave,4); //RIFF
  50. if (wave[0]!='W' || wave[1]!='A' || wave[2]!='V' || wave[3]!='E') {
  51. file->close();
  52. memdelete(file);
  53. ERR_EXPLAIN("Not a WAV file (no WAVE RIFF Header)")
  54. ERR_FAIL_V( RES() );
  55. }
  56. bool format_found=false;
  57. bool data_found=false;
  58. int format_bits=0;
  59. int format_channels=0;
  60. int format_freq=0;
  61. Sample::LoopFormat loop=Sample::LOOP_NONE;
  62. int loop_begin=0;
  63. int loop_end=0;
  64. Ref<Sample> sample( memnew( Sample ) );
  65. while (!file->eof_reached()) {
  66. /* chunk */
  67. char chunkID[4];
  68. file->get_buffer((uint8_t*)&chunkID,4); //RIFF
  69. /* chunk size */
  70. uint32_t chunksize=file->get_32();
  71. uint32_t file_pos=file->get_pos(); //save file pos, so we can skip to next chunk safely
  72. if (file->eof_reached()) {
  73. //ERR_PRINT("EOF REACH");
  74. break;
  75. }
  76. if (chunkID[0]=='f' && chunkID[1]=='m' && chunkID[2]=='t' && chunkID[3]==' ' && !format_found) {
  77. /* IS FORMAT CHUNK */
  78. uint16_t compression_code=file->get_16();
  79. if (compression_code!=1) {
  80. ERR_PRINT("Format not supported for WAVE file (not PCM). Save WAVE files as uncompressed PCM instead.");
  81. break;
  82. }
  83. format_channels=file->get_16();
  84. if (format_channels!=1 && format_channels !=2) {
  85. ERR_PRINT("Format not supported for WAVE file (not stereo or mono)");
  86. break;
  87. }
  88. format_freq=file->get_32(); //sampling rate
  89. file->get_32(); // average bits/second (unused)
  90. file->get_16(); // block align (unused)
  91. format_bits=file->get_16(); // bits per sample
  92. if (format_bits%8) {
  93. ERR_PRINT("Strange number of bits in sample (not 8,16,24,32)");
  94. break;
  95. }
  96. /* Dont need anything else, continue */
  97. format_found=true;
  98. }
  99. if (chunkID[0]=='d' && chunkID[1]=='a' && chunkID[2]=='t' && chunkID[3]=='a' && !data_found) {
  100. /* IS FORMAT CHUNK */
  101. data_found=true;
  102. if (!format_found) {
  103. ERR_PRINT("'data' chunk before 'format' chunk found.");
  104. break;
  105. }
  106. int frames=chunksize;
  107. frames/=format_channels;
  108. frames/=(format_bits>>3);
  109. sample->create(
  110. (format_bits==8) ? Sample::FORMAT_PCM8 : Sample::FORMAT_PCM16,
  111. (format_channels==2)?true:false,
  112. frames );
  113. sample->set_mix_rate( format_freq );
  114. DVector<uint8_t> data;
  115. data.resize(chunksize);
  116. DVector<uint8_t>::Write dataw = data.write();
  117. void * data_ptr = dataw.ptr();
  118. for (int i=0;i<frames;i++) {
  119. for (int c=0;c<format_channels;c++) {
  120. if (format_bits==8) {
  121. // 8 bit samples are UNSIGNED
  122. uint8_t s = file->get_8();
  123. s-=128;
  124. int8_t *sp=(int8_t*)&s;
  125. int8_t *data_ptr8=&((int8_t*)data_ptr)[i*format_channels+c];
  126. *data_ptr8=*sp;
  127. } else {
  128. //16+ bits samples are SIGNED
  129. // if sample is > 16 bits, just read extra bytes
  130. uint32_t data=0;
  131. for (int b=0;b<(format_bits>>3);b++) {
  132. data|=((uint32_t)file->get_8())<<(b*8);
  133. }
  134. data<<=(32-format_bits);
  135. int32_t s=data;
  136. int16_t *data_ptr16=&((int16_t*)data_ptr)[i*format_channels+c];
  137. *data_ptr16=s>>16;
  138. }
  139. }
  140. }
  141. dataw=DVector<uint8_t>::Write();
  142. sample->set_data(data);
  143. if (file->eof_reached()) {
  144. file->close();
  145. memdelete(file);
  146. ERR_EXPLAIN("Premature end of file.");
  147. ERR_FAIL_V(RES());
  148. }
  149. }
  150. if (chunkID[0]=='s' && chunkID[1]=='m' && chunkID[2]=='p' && chunkID[3]=='l') {
  151. //loop point info!
  152. for(int i=0;i<10;i++)
  153. file->get_32(); // i wish to know why should i do this... no doc!
  154. loop=file->get_32()?Sample::LOOP_PING_PONG:Sample::LOOP_FORWARD;
  155. loop_begin=file->get_32();
  156. loop_end=file->get_32();
  157. }
  158. file->seek( file_pos+chunksize );
  159. }
  160. sample->set_loop_format(loop);
  161. sample->set_loop_begin(loop_begin);
  162. sample->set_loop_end(loop_end);
  163. file->close();
  164. memdelete(file);
  165. return sample;
  166. }
  167. void ResourceFormatLoaderWAV::get_recognized_extensions(List<String> *p_extensions) const {
  168. p_extensions->push_back("wav");
  169. }
  170. bool ResourceFormatLoaderWAV::handles_type(const String& p_type) const {
  171. return (p_type=="Sample");
  172. }
  173. String ResourceFormatLoaderWAV::get_resource_type(const String &p_path) const {
  174. if (p_path.extension().to_lower()=="wav")
  175. return "Sample";
  176. return "";
  177. }