resource_importer_wav.cpp 17 KB

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