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