2
0

resource_importer_wav.cpp 17 KB

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