resource_importer_wav.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*************************************************************************/
  2. /* resource_importer_wav.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "resource_importer_wav.h"
  31. #include "core/io/marshalls.h"
  32. #include "core/io/resource_saver.h"
  33. #include "core/os/file_access.h"
  34. #include "scene/resources/audio_stream_sample.h"
  35. String ResourceImporterWAV::get_importer_name() const {
  36. return "wav";
  37. }
  38. String ResourceImporterWAV::get_visible_name() const {
  39. return "Microsoft WAV";
  40. }
  41. void ResourceImporterWAV::get_recognized_extensions(List<String> *p_extensions) const {
  42. p_extensions->push_back("wav");
  43. }
  44. String ResourceImporterWAV::get_save_extension() const {
  45. return "sample";
  46. }
  47. String ResourceImporterWAV::get_resource_type() const {
  48. return "AudioStreamSample";
  49. }
  50. bool ResourceImporterWAV::get_option_visibility(const String &p_option, const Map<StringName, Variant> &p_options) const {
  51. return true;
  52. }
  53. int ResourceImporterWAV::get_preset_count() const {
  54. return 0;
  55. }
  56. String ResourceImporterWAV::get_preset_name(int p_idx) const {
  57. return String();
  58. }
  59. void ResourceImporterWAV::get_import_options(List<ImportOption> *r_options, int p_preset) const {
  60. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "force/8_bit"), false));
  61. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "force/mono"), false));
  62. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "force/max_rate"), false));
  63. r_options->push_back(ImportOption(PropertyInfo(Variant::REAL, "force/max_rate_hz", PROPERTY_HINT_EXP_RANGE, "11025,192000,1"), 44100));
  64. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "edit/trim"), true));
  65. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "edit/normalize"), true));
  66. r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "edit/loop"), false));
  67. r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "compress/mode", PROPERTY_HINT_ENUM, "Disabled,RAM (Ima-ADPCM)"), 0));
  68. }
  69. Error ResourceImporterWAV::import(const String &p_source_file, const String &p_save_path, const Map<StringName, Variant> &p_options, List<String> *r_platform_variants, List<String> *r_gen_files) {
  70. /* STEP 1, READ WAVE FILE */
  71. Error err;
  72. FileAccess *file = FileAccess::open(p_source_file, FileAccess::READ, &err);
  73. ERR_FAIL_COND_V(err != OK, ERR_CANT_OPEN);
  74. /* CHECK RIFF */
  75. char riff[5];
  76. riff[4] = 0;
  77. file->get_buffer((uint8_t *)&riff, 4); //RIFF
  78. if (riff[0] != 'R' || riff[1] != 'I' || riff[2] != 'F' || riff[3] != 'F') {
  79. file->close();
  80. memdelete(file);
  81. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  82. }
  83. /* GET FILESIZE */
  84. file->get_32(); // filesize
  85. /* CHECK WAVE */
  86. char wave[4];
  87. file->get_buffer((uint8_t *)&wave, 4); //RIFF
  88. if (wave[0] != 'W' || wave[1] != 'A' || wave[2] != 'V' || wave[3] != 'E') {
  89. file->close();
  90. memdelete(file);
  91. ERR_EXPLAIN("Not a WAV file (no WAVE RIFF Header)")
  92. ERR_FAIL_V(ERR_FILE_UNRECOGNIZED);
  93. }
  94. int format_bits = 0;
  95. int format_channels = 0;
  96. AudioStreamSample::LoopMode loop = AudioStreamSample::LOOP_DISABLED;
  97. uint16_t compression_code = 1;
  98. bool format_found = false;
  99. bool data_found = false;
  100. int format_freq = 0;
  101. int loop_begin = 0;
  102. int loop_end = 0;
  103. int frames = 0;
  104. Vector<float> data;
  105. while (!file->eof_reached()) {
  106. /* chunk */
  107. char chunkID[4];
  108. file->get_buffer((uint8_t *)&chunkID, 4); //RIFF
  109. /* chunk size */
  110. uint32_t chunksize = file->get_32();
  111. uint32_t file_pos = file->get_position(); //save file pos, so we can skip to next chunk safely
  112. if (file->eof_reached()) {
  113. //ERR_PRINT("EOF REACH");
  114. break;
  115. }
  116. if (chunkID[0] == 'f' && chunkID[1] == 'm' && chunkID[2] == 't' && chunkID[3] == ' ' && !format_found) {
  117. /* IS FORMAT CHUNK */
  118. //Issue: #7755 : Not a bug - usage of other formats (format codes) are unsupported in current importer version.
  119. //Consider revision for engine version 3.0
  120. compression_code = file->get_16();
  121. if (compression_code != 1 && compression_code != 3) {
  122. file->close();
  123. memdelete(file);
  124. ERR_EXPLAIN("Format not supported for WAVE file (not PCM). Save WAVE files as uncompressed PCM instead.");
  125. ERR_FAIL_V(ERR_INVALID_DATA);
  126. }
  127. format_channels = file->get_16();
  128. if (format_channels != 1 && format_channels != 2) {
  129. file->close();
  130. memdelete(file);
  131. ERR_EXPLAIN("Format not supported for WAVE file (not stereo or mono).");
  132. ERR_FAIL_V(ERR_INVALID_DATA);
  133. }
  134. format_freq = file->get_32(); //sampling rate
  135. file->get_32(); // average bits/second (unused)
  136. file->get_16(); // block align (unused)
  137. format_bits = file->get_16(); // bits per sample
  138. if (format_bits % 8 || format_bits == 0) {
  139. file->close();
  140. memdelete(file);
  141. ERR_EXPLAIN("Invalid amount of bits in the sample (should be one of 8, 16, 24 or 32).");
  142. ERR_FAIL_V(ERR_INVALID_DATA);
  143. }
  144. /* Don't need anything else, continue */
  145. format_found = true;
  146. }
  147. if (chunkID[0] == 'd' && chunkID[1] == 'a' && chunkID[2] == 't' && chunkID[3] == 'a' && !data_found) {
  148. /* IS DATA CHUNK */
  149. data_found = true;
  150. if (!format_found) {
  151. ERR_PRINT("'data' chunk before 'format' chunk found.");
  152. break;
  153. }
  154. frames = chunksize;
  155. frames /= format_channels;
  156. frames /= (format_bits >> 3);
  157. /*print_line("chunksize: "+itos(chunksize));
  158. print_line("channels: "+itos(format_channels));
  159. print_line("bits: "+itos(format_bits));
  160. */
  161. int len = frames;
  162. if (format_channels == 2)
  163. len *= 2;
  164. if (format_bits > 8)
  165. len *= 2;
  166. data.resize(frames * format_channels);
  167. if (format_bits == 8) {
  168. for (int i = 0; i < frames * format_channels; i++) {
  169. // 8 bit samples are UNSIGNED
  170. data.write[i] = int8_t(file->get_8() - 128) / 128.f;
  171. }
  172. } else if (format_bits == 32 && compression_code == 3) {
  173. for (int i = 0; i < frames * format_channels; i++) {
  174. //32 bit IEEE Float
  175. data.write[i] = file->get_float();
  176. }
  177. } else if (format_bits == 16) {
  178. for (int i = 0; i < frames * format_channels; i++) {
  179. //16 bit SIGNED
  180. data.write[i] = int16_t(file->get_16()) / 32768.f;
  181. }
  182. } else {
  183. for (int i = 0; i < frames * format_channels; i++) {
  184. //16+ bits samples are SIGNED
  185. // if sample is > 16 bits, just read extra bytes
  186. uint32_t s = 0;
  187. for (int b = 0; b < (format_bits >> 3); b++) {
  188. s |= ((uint32_t)file->get_8()) << (b * 8);
  189. }
  190. s <<= (32 - format_bits);
  191. data.write[i] = (int32_t(s) >> 16) / 32768.f;
  192. }
  193. }
  194. if (file->eof_reached()) {
  195. file->close();
  196. memdelete(file);
  197. ERR_EXPLAIN("Premature end of file.");
  198. ERR_FAIL_V(ERR_FILE_CORRUPT);
  199. }
  200. }
  201. if (chunkID[0] == 's' && chunkID[1] == 'm' && chunkID[2] == 'p' && chunkID[3] == 'l') {
  202. //loop point info!
  203. /**
  204. * Consider exploring next document:
  205. * http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/RIFFNEW.pdf
  206. * Especially on page:
  207. * 16 - 17
  208. * Timestamp:
  209. * 22:38 06.07.2017 GMT
  210. **/
  211. for (int i = 0; i < 10; i++)
  212. file->get_32(); // i wish to know why should i do this... no doc!
  213. // only read 0x00 (loop forward), 0x01 (loop ping-pong) and 0x02 (loop backward)
  214. // Skip anything else because it's not supported, reserved for future uses or sampler specific
  215. // from https://sites.google.com/site/musicgapi/technical-documents/wav-file-format#smpl (loop type values table)
  216. int loop_type = file->get_32();
  217. if (loop_type == 0x00 || loop_type == 0x01 || loop_type == 0x02) {
  218. if (loop_type == 0x00) {
  219. loop = AudioStreamSample::LOOP_FORWARD;
  220. } else if (loop_type == 0x01) {
  221. loop = AudioStreamSample::LOOP_PING_PONG;
  222. } else if (loop_type == 0x02) {
  223. loop = AudioStreamSample::LOOP_BACKWARD;
  224. }
  225. loop_begin = file->get_32();
  226. loop_end = file->get_32();
  227. }
  228. }
  229. file->seek(file_pos + chunksize);
  230. }
  231. file->close();
  232. memdelete(file);
  233. // STEP 2, APPLY CONVERSIONS
  234. bool is16 = format_bits != 8;
  235. int rate = format_freq;
  236. /*
  237. print_line("Input Sample: ");
  238. print_line("\tframes: " + itos(frames));
  239. print_line("\tformat_channels: " + itos(format_channels));
  240. print_line("\t16bits: " + itos(is16));
  241. print_line("\trate: " + itos(rate));
  242. print_line("\tloop: " + itos(loop));
  243. print_line("\tloop begin: " + itos(loop_begin));
  244. print_line("\tloop end: " + itos(loop_end));
  245. */
  246. //apply frequency limit
  247. bool limit_rate = p_options["force/max_rate"];
  248. int limit_rate_hz = p_options["force/max_rate_hz"];
  249. if (limit_rate && rate > limit_rate_hz && rate > 0 && frames > 0) {
  250. // resample!
  251. int new_data_frames = (int)(frames * (float)limit_rate_hz / (float)rate);
  252. Vector<float> new_data;
  253. new_data.resize(new_data_frames * format_channels);
  254. for (int c = 0; c < format_channels; c++) {
  255. float frac = .0f;
  256. int ipos = 0;
  257. for (int i = 0; i < new_data_frames; i++) {
  258. //simple cubic interpolation should be enough.
  259. float mu = frac;
  260. float y0 = data[MAX(0, ipos - 1) * format_channels + c];
  261. float y1 = data[ipos * format_channels + c];
  262. float y2 = data[MIN(frames - 1, ipos + 1) * format_channels + c];
  263. float y3 = data[MIN(frames - 1, ipos + 2) * format_channels + c];
  264. float mu2 = mu * mu;
  265. float a0 = y3 - y2 - y0 + y1;
  266. float a1 = y0 - y1 - a0;
  267. float a2 = y2 - y0;
  268. float a3 = y1;
  269. float res = (a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3);
  270. new_data.write[i * format_channels + c] = res;
  271. // update position and always keep fractional part within ]0...1]
  272. // in order to avoid 32bit floating point precision errors
  273. frac += (float)rate / (float)limit_rate_hz;
  274. int tpos = (int)Math::floor(frac);
  275. ipos += tpos;
  276. frac -= tpos;
  277. }
  278. }
  279. if (loop) {
  280. loop_begin = (int)(loop_begin * (float)new_data_frames / (float)frames);
  281. loop_end = (int)(loop_end * (float)new_data_frames / (float)frames);
  282. }
  283. data = new_data;
  284. rate = limit_rate_hz;
  285. frames = new_data_frames;
  286. }
  287. bool normalize = p_options["edit/normalize"];
  288. if (normalize) {
  289. float max = 0;
  290. for (int i = 0; i < data.size(); i++) {
  291. float amp = Math::abs(data[i]);
  292. if (amp > max)
  293. max = amp;
  294. }
  295. if (max > 0) {
  296. float mult = 1.0 / max;
  297. for (int i = 0; i < data.size(); i++) {
  298. data.write[i] *= mult;
  299. }
  300. }
  301. }
  302. bool trim = p_options["edit/trim"];
  303. if (trim && !loop && format_channels > 0) {
  304. int first = 0;
  305. int last = (frames * format_channels) - 1;
  306. bool found = false;
  307. float limit = Math::db2linear((float)-30);
  308. for (int i = 0; i < data.size(); i++) {
  309. float amp = Math::abs(data[i]);
  310. if (!found && amp > limit) {
  311. first = i;
  312. found = true;
  313. }
  314. if (found && amp > limit) {
  315. last = i;
  316. }
  317. }
  318. first /= format_channels;
  319. last /= format_channels;
  320. if (first < last) {
  321. Vector<float> new_data;
  322. new_data.resize((last - first + 1) * format_channels);
  323. for (int i = first * format_channels; i < (last + 1) * format_channels; i++) {
  324. new_data.write[i - first * format_channels] = data[i];
  325. }
  326. data = new_data;
  327. frames = data.size() / format_channels;
  328. }
  329. }
  330. bool make_loop = p_options["edit/loop"];
  331. if (make_loop && !loop) {
  332. loop = AudioStreamSample::LOOP_FORWARD;
  333. loop_begin = 0;
  334. loop_end = frames;
  335. }
  336. int compression = p_options["compress/mode"];
  337. bool force_mono = p_options["force/mono"];
  338. if (force_mono && format_channels == 2) {
  339. Vector<float> new_data;
  340. new_data.resize(data.size() / 2);
  341. for (int i = 0; i < frames; i++) {
  342. new_data.write[i] = (data[i * 2 + 0] + data[i * 2 + 1]) / 2.0;
  343. }
  344. data = new_data;
  345. format_channels = 1;
  346. }
  347. bool force_8_bit = p_options["force/8_bit"];
  348. if (force_8_bit) {
  349. is16 = false;
  350. }
  351. PoolVector<uint8_t> dst_data;
  352. AudioStreamSample::Format dst_format;
  353. if (compression == 1) {
  354. dst_format = AudioStreamSample::FORMAT_IMA_ADPCM;
  355. if (format_channels == 1) {
  356. _compress_ima_adpcm(data, dst_data);
  357. } else {
  358. //byte interleave
  359. Vector<float> left;
  360. Vector<float> right;
  361. int tframes = data.size() / 2;
  362. left.resize(tframes);
  363. right.resize(tframes);
  364. for (int i = 0; i < tframes; i++) {
  365. left.write[i] = data[i * 2 + 0];
  366. right.write[i] = data[i * 2 + 1];
  367. }
  368. PoolVector<uint8_t> bleft;
  369. PoolVector<uint8_t> bright;
  370. _compress_ima_adpcm(left, bleft);
  371. _compress_ima_adpcm(right, bright);
  372. int dl = bleft.size();
  373. dst_data.resize(dl * 2);
  374. PoolVector<uint8_t>::Write w = dst_data.write();
  375. PoolVector<uint8_t>::Read rl = bleft.read();
  376. PoolVector<uint8_t>::Read rr = bright.read();
  377. for (int i = 0; i < dl; i++) {
  378. w[i * 2 + 0] = rl[i];
  379. w[i * 2 + 1] = rr[i];
  380. }
  381. }
  382. } else {
  383. dst_format = is16 ? AudioStreamSample::FORMAT_16_BITS : AudioStreamSample::FORMAT_8_BITS;
  384. dst_data.resize(data.size() * (is16 ? 2 : 1));
  385. {
  386. PoolVector<uint8_t>::Write w = dst_data.write();
  387. int ds = data.size();
  388. for (int i = 0; i < ds; i++) {
  389. if (is16) {
  390. int16_t v = CLAMP(data[i] * 32768, -32768, 32767);
  391. encode_uint16(v, &w[i * 2]);
  392. } else {
  393. int8_t v = CLAMP(data[i] * 128, -128, 127);
  394. w[i] = v;
  395. }
  396. }
  397. }
  398. }
  399. Ref<AudioStreamSample> sample;
  400. sample.instance();
  401. sample->set_data(dst_data);
  402. sample->set_format(dst_format);
  403. sample->set_mix_rate(rate);
  404. sample->set_loop_mode(loop);
  405. sample->set_loop_begin(loop_begin);
  406. sample->set_loop_end(loop_end);
  407. sample->set_stereo(format_channels == 2);
  408. ResourceSaver::save(p_save_path + ".sample", sample);
  409. return OK;
  410. }
  411. ResourceImporterWAV::ResourceImporterWAV() {
  412. }