2017-03-05 15:47:28 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* resource_importer_wav.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
#ifndef RESOURCE_IMPORTER_WAV_H
|
|
|
|
#define RESOURCE_IMPORTER_WAV_H
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2019-02-12 13:30:56 +01:00
|
|
|
#include "core/io/resource_importer.h"
|
2017-02-03 02:51:26 +01:00
|
|
|
|
|
|
|
class ResourceImporterWAV : public ResourceImporter {
|
2019-03-19 19:35:57 +01:00
|
|
|
GDCLASS(ResourceImporterWAV, ResourceImporter);
|
|
|
|
|
2017-02-03 02:51:26 +01:00
|
|
|
public:
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual String get_importer_name() const override;
|
|
|
|
virtual String get_visible_name() const override;
|
|
|
|
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
|
|
|
|
virtual String get_save_extension() const override;
|
|
|
|
virtual String get_resource_type() const override;
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual int get_preset_count() const override;
|
|
|
|
virtual String get_preset_name(int p_idx) const override;
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2021-11-14 18:02:38 +01:00
|
|
|
virtual void get_import_options(const String &p_path, List<ImportOption> *r_options, int p_preset = 0) const override;
|
2022-05-13 15:04:37 +02:00
|
|
|
virtual bool get_option_visibility(const String &p_path, const String &p_option, const HashMap<StringName, Variant> &p_options) const override;
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2020-02-17 22:06:54 +01:00
|
|
|
static void _compress_ima_adpcm(const Vector<float> &p_data, Vector<uint8_t> &dst_data) {
|
2018-01-18 16:03:53 +01:00
|
|
|
static const int16_t _ima_adpcm_step_table[89] = {
|
|
|
|
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
|
|
|
|
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
|
|
|
|
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
|
|
|
|
130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
|
|
|
|
337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
|
|
|
|
876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
|
|
|
|
2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
|
|
|
|
5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
|
|
|
|
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int8_t _ima_adpcm_index_table[16] = {
|
|
|
|
-1, -1, -1, -1, 2, 4, 6, 8,
|
|
|
|
-1, -1, -1, -1, 2, 4, 6, 8
|
|
|
|
};
|
|
|
|
|
|
|
|
int datalen = p_data.size();
|
|
|
|
int datamax = datalen;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (datalen & 1) {
|
2018-01-18 16:03:53 +01:00
|
|
|
datalen++;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-01-18 16:03:53 +01:00
|
|
|
|
|
|
|
dst_data.resize(datalen / 2 + 4);
|
2020-02-17 22:06:54 +01:00
|
|
|
uint8_t *w = dst_data.ptrw();
|
2018-01-18 16:03:53 +01:00
|
|
|
|
|
|
|
int i, step_idx = 0, prev = 0;
|
2020-02-17 22:06:54 +01:00
|
|
|
uint8_t *out = w;
|
2018-01-18 16:03:53 +01:00
|
|
|
const float *in = p_data.ptr();
|
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
// Initial value is zero.
|
2018-01-18 16:03:53 +01:00
|
|
|
*(out++) = 0;
|
|
|
|
*(out++) = 0;
|
2022-02-16 13:56:32 +01:00
|
|
|
// Table index initial value.
|
2018-01-18 16:03:53 +01:00
|
|
|
*(out++) = 0;
|
2022-02-16 13:56:32 +01:00
|
|
|
// Unused.
|
2018-01-18 16:03:53 +01:00
|
|
|
*(out++) = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < datalen; i++) {
|
|
|
|
int step, diff, vpdiff, mask;
|
|
|
|
uint8_t nibble;
|
|
|
|
int16_t xm_sample;
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (i >= datamax) {
|
2018-01-18 16:03:53 +01:00
|
|
|
xm_sample = 0;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2018-01-18 16:03:53 +01:00
|
|
|
xm_sample = CLAMP(in[i] * 32767.0, -32768, 32767);
|
|
|
|
}
|
|
|
|
|
|
|
|
diff = (int)xm_sample - prev;
|
|
|
|
|
|
|
|
nibble = 0;
|
|
|
|
step = _ima_adpcm_step_table[step_idx];
|
|
|
|
vpdiff = step >> 3;
|
|
|
|
if (diff < 0) {
|
|
|
|
nibble = 8;
|
|
|
|
diff = -diff;
|
|
|
|
}
|
|
|
|
mask = 4;
|
|
|
|
while (mask) {
|
|
|
|
if (diff >= step) {
|
|
|
|
nibble |= mask;
|
|
|
|
diff -= step;
|
|
|
|
vpdiff += step;
|
|
|
|
}
|
|
|
|
|
|
|
|
step >>= 1;
|
|
|
|
mask >>= 1;
|
2022-02-16 13:56:32 +01:00
|
|
|
}
|
2018-01-18 16:03:53 +01:00
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (nibble & 8) {
|
2018-01-18 16:03:53 +01:00
|
|
|
prev -= vpdiff;
|
2020-05-14 16:41:43 +02:00
|
|
|
} else {
|
2018-01-18 16:03:53 +01:00
|
|
|
prev += vpdiff;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2018-01-18 16:03:53 +01:00
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
prev = CLAMP(prev, -32768, 32767);
|
2018-01-18 16:03:53 +01:00
|
|
|
|
|
|
|
step_idx += _ima_adpcm_index_table[nibble];
|
2022-02-16 13:56:32 +01:00
|
|
|
step_idx = CLAMP(step_idx, 0, 88);
|
2018-01-18 16:03:53 +01:00
|
|
|
|
|
|
|
if (i & 1) {
|
|
|
|
*out |= nibble << 4;
|
|
|
|
out++;
|
|
|
|
} else {
|
|
|
|
*out = nibble;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-03 02:51:26 +01:00
|
|
|
|
2022-05-13 15:04:37 +02:00
|
|
|
virtual Error 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 = nullptr, Variant *r_metadata = nullptr) override;
|
2017-02-03 02:51:26 +01:00
|
|
|
|
|
|
|
ResourceImporterWAV();
|
|
|
|
};
|
|
|
|
|
2022-02-16 13:56:32 +01:00
|
|
|
#endif // RESOURCE_IMPORTER_WAV_H
|