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