resource_importer_wav.cpp 18 KB

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