Path: blob/master/scene/resources/audio_stream_wav.cpp
9896 views
/**************************************************************************/1/* audio_stream_wav.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "audio_stream_wav.h"3132#include "core/io/file_access_memory.h"33#include "core/io/marshalls.h"3435const float TRIM_DB_LIMIT = -50;36const int TRIM_FADE_OUT_FRAMES = 500;3738void AudioStreamPlaybackWAV::start(double p_from_pos) {39if (base->format == AudioStreamWAV::FORMAT_IMA_ADPCM) {40//no seeking in IMA_ADPCM41for (int i = 0; i < 2; i++) {42ima_adpcm[i].step_index = 0;43ima_adpcm[i].predictor = 0;44ima_adpcm[i].loop_step_index = 0;45ima_adpcm[i].loop_predictor = 0;46ima_adpcm[i].last_nibble = -1;47ima_adpcm[i].loop_pos = 0x7FFFFFFF;48ima_adpcm[i].window_ofs = 0;49}5051offset = 0;52} else {53seek(p_from_pos);54}5556sign = 1;57active = true;58begin_resample();59}6061void AudioStreamPlaybackWAV::stop() {62active = false;63}6465bool AudioStreamPlaybackWAV::is_playing() const {66return active;67}6869int AudioStreamPlaybackWAV::get_loop_count() const {70return 0;71}7273double AudioStreamPlaybackWAV::get_playback_position() const {74return double(offset) / base->mix_rate;75}7677void AudioStreamPlaybackWAV::seek(double p_time) {78if (base->format == AudioStreamWAV::FORMAT_IMA_ADPCM) {79return; //no seeking in ima-adpcm80}8182double max = base->get_length();83if (p_time < 0) {84p_time = 0;85} else if (p_time >= max) {86p_time = max - 0.001;87}8889offset = int64_t(p_time * base->mix_rate);90}9192template <typename Depth, bool is_stereo, bool is_ima_adpcm, bool is_qoa>93void AudioStreamPlaybackWAV::decode_samples(const Depth *p_src, AudioFrame *p_dst, int64_t &p_offset, int8_t &p_increment, uint32_t p_amount, IMA_ADPCM_State *p_ima_adpcm, QOA_State *p_qoa) {94// this function will be compiled branchless by any decent compiler9596int32_t final = 0, final_r = 0;97while (p_amount) {98p_amount--;99int64_t pos = p_offset << (is_stereo && !is_ima_adpcm && !is_qoa ? 1 : 0);100101if (is_ima_adpcm) {102int64_t sample_pos = pos + p_ima_adpcm[0].window_ofs;103104while (sample_pos > p_ima_adpcm[0].last_nibble) {105static const int16_t _ima_adpcm_step_table[89] = {1067, 8, 9, 10, 11, 12, 13, 14, 16, 17,10719, 21, 23, 25, 28, 31, 34, 37, 41, 45,10850, 55, 60, 66, 73, 80, 88, 97, 107, 118,109130, 143, 157, 173, 190, 209, 230, 253, 279, 307,110337, 371, 408, 449, 494, 544, 598, 658, 724, 796,111876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,1122272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,1135894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,11415289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767115};116117static const int8_t _ima_adpcm_index_table[16] = {118-1, -1, -1, -1, 2, 4, 6, 8,119-1, -1, -1, -1, 2, 4, 6, 8120};121122for (int i = 0; i < (is_stereo ? 2 : 1); i++) {123int16_t nibble, diff, step;124125p_ima_adpcm[i].last_nibble++;126127uint8_t nbb = p_src[(p_ima_adpcm[i].last_nibble >> 1) * (is_stereo ? 2 : 1) + i];128nibble = (p_ima_adpcm[i].last_nibble & 1) ? (nbb >> 4) : (nbb & 0xF);129step = _ima_adpcm_step_table[p_ima_adpcm[i].step_index];130131p_ima_adpcm[i].step_index += _ima_adpcm_index_table[nibble];132if (p_ima_adpcm[i].step_index < 0) {133p_ima_adpcm[i].step_index = 0;134}135if (p_ima_adpcm[i].step_index > 88) {136p_ima_adpcm[i].step_index = 88;137}138139diff = step >> 3;140if (nibble & 1) {141diff += step >> 2;142}143if (nibble & 2) {144diff += step >> 1;145}146if (nibble & 4) {147diff += step;148}149if (nibble & 8) {150diff = -diff;151}152153p_ima_adpcm[i].predictor += diff;154if (p_ima_adpcm[i].predictor < -0x8000) {155p_ima_adpcm[i].predictor = -0x8000;156} else if (p_ima_adpcm[i].predictor > 0x7FFF) {157p_ima_adpcm[i].predictor = 0x7FFF;158}159160/* store loop if there */161if (p_ima_adpcm[i].last_nibble == p_ima_adpcm[i].loop_pos) {162p_ima_adpcm[i].loop_step_index = p_ima_adpcm[i].step_index;163p_ima_adpcm[i].loop_predictor = p_ima_adpcm[i].predictor;164}165166//printf("%i - %i - pred %i\n",int(p_ima_adpcm[i].last_nibble),int(nibble),int(p_ima_adpcm[i].predictor));167}168}169170final = p_ima_adpcm[0].predictor;171if (is_stereo) {172final_r = p_ima_adpcm[1].predictor;173}174175} else if (is_qoa) {176uint32_t new_data_ofs = 8 + pos / QOA_FRAME_LEN * p_qoa->frame_len;177178if (p_qoa->data_ofs != new_data_ofs) {179p_qoa->data_ofs = new_data_ofs;180const uint8_t *ofs_src = (uint8_t *)p_src + p_qoa->data_ofs;181qoa_decode_frame(ofs_src, p_qoa->frame_len, &p_qoa->desc, p_qoa->dec.ptr(), &p_qoa->dec_len);182}183184uint32_t dec_idx = pos % QOA_FRAME_LEN << (is_stereo ? 1 : 0);185186final = p_qoa->dec[dec_idx];187if (is_stereo) {188final_r = p_qoa->dec[dec_idx + 1];189}190191} else {192final = p_src[pos];193if (is_stereo) {194final_r = p_src[pos + 1];195}196if constexpr (sizeof(Depth) == 1) { /* conditions will not exist anymore when compiled! */197final <<= 8;198if (is_stereo) {199final_r <<= 8;200}201}202}203204if (!is_stereo) {205final_r = final; //copy to right channel if stereo206}207208p_dst->left = final / 32767.0;209p_dst->right = final_r / 32767.0;210p_dst++;211212p_offset += p_increment;213}214}215216int AudioStreamPlaybackWAV::_mix_internal(AudioFrame *p_buffer, int p_frames) {217if (base->data.is_empty() || !active) {218for (int i = 0; i < p_frames; i++) {219p_buffer[i] = AudioFrame(0, 0);220}221return 0;222}223224uint32_t len = base->data_bytes;225switch (base->format) {226case AudioStreamWAV::FORMAT_8_BITS:227len /= 1;228break;229case AudioStreamWAV::FORMAT_16_BITS:230len /= 2;231break;232case AudioStreamWAV::FORMAT_IMA_ADPCM:233len *= 2;234break;235case AudioStreamWAV::FORMAT_QOA:236len = qoa.desc.samples * qoa.desc.channels;237break;238}239240if (base->stereo) {241len /= 2;242}243244int64_t loop_begin = base->loop_begin;245int64_t loop_end = base->loop_end;246int64_t begin_limit = (base->loop_mode != AudioStreamWAV::LOOP_DISABLED) ? loop_begin : 0;247int64_t end_limit = (base->loop_mode != AudioStreamWAV::LOOP_DISABLED) ? loop_end : len - 1;248bool is_stereo = base->stereo;249250int32_t todo = p_frames;251252if (base->loop_mode == AudioStreamWAV::LOOP_BACKWARD) {253sign = -1;254}255256int8_t increment = sign;257258//looping259260AudioStreamWAV::LoopMode loop_format = base->loop_mode;261AudioStreamWAV::Format format = base->format;262263/* audio data */264265const uint8_t *data = base->data.ptr();266AudioFrame *dst_buff = p_buffer;267268if (format == AudioStreamWAV::FORMAT_IMA_ADPCM) {269if (loop_format != AudioStreamWAV::LOOP_DISABLED) {270ima_adpcm[0].loop_pos = loop_begin;271ima_adpcm[1].loop_pos = loop_begin;272loop_format = AudioStreamWAV::LOOP_FORWARD;273}274}275276while (todo > 0) {277int64_t limit = 0;278int32_t target = 0, aux = 0;279280/** LOOP CHECKING **/281282if (increment < 0) {283/* going backwards */284285if (loop_format != AudioStreamWAV::LOOP_DISABLED && offset < loop_begin) {286/* loopstart reached */287if (loop_format == AudioStreamWAV::LOOP_PINGPONG) {288/* bounce ping pong */289offset = loop_begin + (loop_begin - offset);290increment = -increment;291sign *= -1;292} else {293/* go to loop-end */294offset = loop_end - (loop_begin - offset);295}296} else {297/* check for sample not reaching beginning */298if (offset < 0) {299active = false;300break;301}302}303} else {304/* going forward */305if (loop_format != AudioStreamWAV::LOOP_DISABLED && offset >= loop_end) {306/* loopend reached */307308if (loop_format == AudioStreamWAV::LOOP_PINGPONG) {309/* bounce ping pong */310offset = loop_end - (offset - loop_end);311increment = -increment;312sign *= -1;313} else {314/* go to loop-begin */315316if (format == AudioStreamWAV::FORMAT_IMA_ADPCM) {317for (int i = 0; i < 2; i++) {318ima_adpcm[i].step_index = ima_adpcm[i].loop_step_index;319ima_adpcm[i].predictor = ima_adpcm[i].loop_predictor;320ima_adpcm[i].last_nibble = loop_begin;321}322offset = loop_begin;323} else {324offset = loop_begin + (offset - loop_end);325}326}327} else {328/* no loop, check for end of sample */329if (offset >= len) {330active = false;331break;332}333}334}335336/** MIXCOUNT COMPUTING **/337338/* next possible limit (looppoints or sample begin/end */339limit = (increment < 0) ? begin_limit : end_limit;340341/* compute what is shorter, the todo or the limit? */342aux = (limit - offset) / increment + 1;343target = (aux < todo) ? aux : todo; /* mix target is the shorter buffer */344345/* check just in case */346if (target <= 0) {347active = false;348break;349}350351todo -= target;352353switch (base->format) {354case AudioStreamWAV::FORMAT_8_BITS: {355if (is_stereo) {356decode_samples<int8_t, true, false, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);357} else {358decode_samples<int8_t, false, false, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);359}360} break;361case AudioStreamWAV::FORMAT_16_BITS: {362if (is_stereo) {363decode_samples<int16_t, true, false, false>((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);364} else {365decode_samples<int16_t, false, false, false>((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);366}367368} break;369case AudioStreamWAV::FORMAT_IMA_ADPCM: {370if (is_stereo) {371decode_samples<int8_t, true, true, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);372} else {373decode_samples<int8_t, false, true, false>((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);374}375376} break;377case AudioStreamWAV::FORMAT_QOA: {378if (is_stereo) {379decode_samples<uint8_t, true, false, true>((uint8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);380} else {381decode_samples<uint8_t, false, false, true>((uint8_t *)data, dst_buff, offset, increment, target, ima_adpcm, &qoa);382}383} break;384}385386dst_buff += target;387}388389if (todo) {390int mixed_frames = p_frames - todo;391//bit was missing from mix392int todo_ofs = p_frames - todo;393for (int i = todo_ofs; i < p_frames; i++) {394p_buffer[i] = AudioFrame(0, 0);395}396return mixed_frames;397}398return p_frames;399}400401float AudioStreamPlaybackWAV::get_stream_sampling_rate() {402return base->mix_rate;403}404405void AudioStreamPlaybackWAV::tag_used_streams() {406base->tag_used(get_playback_position());407}408409void AudioStreamPlaybackWAV::set_is_sample(bool p_is_sample) {410_is_sample = p_is_sample;411}412413bool AudioStreamPlaybackWAV::get_is_sample() const {414return _is_sample;415}416417Ref<AudioSamplePlayback> AudioStreamPlaybackWAV::get_sample_playback() const {418return sample_playback;419}420421void AudioStreamPlaybackWAV::set_sample_playback(const Ref<AudioSamplePlayback> &p_playback) {422sample_playback = p_playback;423if (sample_playback.is_valid()) {424sample_playback->stream_playback = Ref<AudioStreamPlayback>(this);425}426}427428/////////////////////429430void AudioStreamWAV::set_format(Format p_format) {431format = p_format;432}433434AudioStreamWAV::Format AudioStreamWAV::get_format() const {435return format;436}437438void AudioStreamWAV::set_loop_mode(LoopMode p_loop_mode) {439loop_mode = p_loop_mode;440}441442AudioStreamWAV::LoopMode AudioStreamWAV::get_loop_mode() const {443return loop_mode;444}445446void AudioStreamWAV::set_loop_begin(int p_frame) {447loop_begin = p_frame;448}449450int AudioStreamWAV::get_loop_begin() const {451return loop_begin;452}453454void AudioStreamWAV::set_loop_end(int p_frame) {455loop_end = p_frame;456}457458int AudioStreamWAV::get_loop_end() const {459return loop_end;460}461462void AudioStreamWAV::set_mix_rate(int p_hz) {463ERR_FAIL_COND(p_hz == 0);464mix_rate = p_hz;465}466467int AudioStreamWAV::get_mix_rate() const {468return mix_rate;469}470471void AudioStreamWAV::set_stereo(bool p_enable) {472stereo = p_enable;473}474475bool AudioStreamWAV::is_stereo() const {476return stereo;477}478479void AudioStreamWAV::set_tags(const Dictionary &p_tags) {480tags = p_tags;481}482483Dictionary AudioStreamWAV::get_tags() const {484return tags;485}486487double AudioStreamWAV::get_length() const {488int len = data_bytes;489switch (format) {490case AudioStreamWAV::FORMAT_8_BITS:491len /= 1;492break;493case AudioStreamWAV::FORMAT_16_BITS:494len /= 2;495break;496case AudioStreamWAV::FORMAT_IMA_ADPCM:497len *= 2;498break;499case AudioStreamWAV::FORMAT_QOA:500qoa_desc desc = {};501qoa_decode_header(data.ptr(), data_bytes, &desc);502len = desc.samples * desc.channels;503break;504}505506if (stereo) {507len /= 2;508}509510return double(len) / mix_rate;511}512513bool AudioStreamWAV::is_monophonic() const {514return false;515}516517void AudioStreamWAV::set_data(const Vector<uint8_t> &p_data) {518AudioServer::get_singleton()->lock();519520data = p_data;521data_bytes = p_data.size();522523AudioServer::get_singleton()->unlock();524}525526Vector<uint8_t> AudioStreamWAV::get_data() const {527return data;528}529530Error AudioStreamWAV::save_to_wav(const String &p_path) {531if (format == AudioStreamWAV::FORMAT_IMA_ADPCM || format == AudioStreamWAV::FORMAT_QOA) {532WARN_PRINT("Saving IMA_ADPCM and QOA samples is not supported yet");533return ERR_UNAVAILABLE;534}535536int sub_chunk_2_size = data_bytes; //Subchunk2Size = Size of data in bytes537538// Format code539// 1:PCM format (for 8 or 16 bit)540// 3:IEEE float format541int format_code = (format == FORMAT_IMA_ADPCM) ? 3 : 1;542543int n_channels = stereo ? 2 : 1;544545long sample_rate = mix_rate;546547int byte_pr_sample = 0;548switch (format) {549case AudioStreamWAV::FORMAT_8_BITS:550byte_pr_sample = 1;551break;552case AudioStreamWAV::FORMAT_16_BITS:553case AudioStreamWAV::FORMAT_QOA:554byte_pr_sample = 2;555break;556case AudioStreamWAV::FORMAT_IMA_ADPCM:557byte_pr_sample = 4;558break;559}560561String file_path = p_path;562if (file_path.substr(file_path.length() - 4, 4).to_lower() != ".wav") {563file_path += ".wav";564}565566Ref<FileAccess> file = FileAccess::open(file_path, FileAccess::WRITE); //Overrides existing file if present567568ERR_FAIL_COND_V(file.is_null(), ERR_FILE_CANT_WRITE);569570// Create WAV Header571file->store_string("RIFF"); //ChunkID572file->store_32(sub_chunk_2_size + 36); //ChunkSize = 36 + SubChunk2Size (size of entire file minus the 8 bits for this and previous header)573file->store_string("WAVE"); //Format574file->store_string("fmt "); //Subchunk1ID575file->store_32(16); //Subchunk1Size = 16576file->store_16(format_code); //AudioFormat577file->store_16(n_channels); //Number of Channels578file->store_32(sample_rate); //SampleRate579file->store_32(sample_rate * n_channels * byte_pr_sample); //ByteRate580file->store_16(n_channels * byte_pr_sample); //BlockAlign = NumChannels * BytePrSample581file->store_16(byte_pr_sample * 8); //BitsPerSample582file->store_string("data"); //Subchunk2ID583file->store_32(sub_chunk_2_size); //Subchunk2Size584585// Add data586const uint8_t *read_data = data.ptr();587switch (format) {588case AudioStreamWAV::FORMAT_8_BITS:589for (unsigned int i = 0; i < data_bytes; i++) {590uint8_t data_point = (read_data[i] + 128);591file->store_8(data_point);592}593break;594case AudioStreamWAV::FORMAT_16_BITS:595case AudioStreamWAV::FORMAT_QOA:596for (unsigned int i = 0; i < data_bytes / 2; i++) {597uint16_t data_point = decode_uint16(&read_data[i * 2]);598file->store_16(data_point);599}600break;601case AudioStreamWAV::FORMAT_IMA_ADPCM:602//Unimplemented603break;604}605606return OK;607}608609Ref<AudioStreamPlayback> AudioStreamWAV::instantiate_playback() {610Ref<AudioStreamPlaybackWAV> sample;611sample.instantiate();612sample->base = Ref<AudioStreamWAV>(this);613614if (format == AudioStreamWAV::FORMAT_QOA) {615uint32_t ffp = qoa_decode_header(data.ptr(), data_bytes, &sample->qoa.desc);616ERR_FAIL_COND_V(ffp != 8, Ref<AudioStreamPlaybackWAV>());617sample->qoa.frame_len = qoa_max_frame_size(&sample->qoa.desc);618int samples_len = (sample->qoa.desc.samples > QOA_FRAME_LEN ? QOA_FRAME_LEN : sample->qoa.desc.samples);619int dec_len = sample->qoa.desc.channels * samples_len;620sample->qoa.dec.resize(dec_len);621}622623return sample;624}625626String AudioStreamWAV::get_stream_name() const {627return "";628}629630Ref<AudioSample> AudioStreamWAV::generate_sample() const {631Ref<AudioSample> sample;632sample.instantiate();633sample->stream = this;634switch (loop_mode) {635case AudioStreamWAV::LoopMode::LOOP_DISABLED: {636sample->loop_mode = AudioSample::LoopMode::LOOP_DISABLED;637} break;638639case AudioStreamWAV::LoopMode::LOOP_FORWARD: {640sample->loop_mode = AudioSample::LoopMode::LOOP_FORWARD;641} break;642643case AudioStreamWAV::LoopMode::LOOP_PINGPONG: {644sample->loop_mode = AudioSample::LoopMode::LOOP_PINGPONG;645} break;646647case AudioStreamWAV::LoopMode::LOOP_BACKWARD: {648sample->loop_mode = AudioSample::LoopMode::LOOP_BACKWARD;649} break;650}651sample->loop_begin = loop_begin;652sample->loop_end = loop_end;653sample->sample_rate = mix_rate;654return sample;655}656657Ref<AudioStreamWAV> AudioStreamWAV::load_from_buffer(const Vector<uint8_t> &p_stream_data, const Dictionary &p_options) {658// /* STEP 1, READ WAVE FILE */659660Ref<FileAccessMemory> file;661file.instantiate();662Error err = file->open_custom(p_stream_data.ptr(), p_stream_data.size());663ERR_FAIL_COND_V_MSG(err != OK, Ref<AudioStreamWAV>(), "Cannot create memfile for WAV file buffer.");664665/* CHECK RIFF */666char riff[5];667riff[4] = 0;668file->get_buffer((uint8_t *)&riff, 4); //RIFF669670if (riff[0] != 'R' || riff[1] != 'I' || riff[2] != 'F' || riff[3] != 'F') {671ERR_FAIL_V_MSG(Ref<AudioStreamWAV>(), vformat("Not a WAV file. File should start with 'RIFF', but found '%s', in file of size %d bytes", riff, file->get_length()));672}673674/* GET FILESIZE */675676// The file size in header is 8 bytes less than the actual size.677// See https://docs.fileformat.com/audio/wav/678const int FILE_SIZE_HEADER_OFFSET = 8;679uint32_t file_size_header = file->get_32() + FILE_SIZE_HEADER_OFFSET;680uint64_t file_size = file->get_length();681if (file_size != file_size_header) {682WARN_PRINT(vformat("File size %d is %s than the expected size %d.", file_size, file_size > file_size_header ? "larger" : "smaller", file_size_header));683}684685/* CHECK WAVE */686687char wave[5];688wave[4] = 0;689file->get_buffer((uint8_t *)&wave, 4); //WAVE690691if (wave[0] != 'W' || wave[1] != 'A' || wave[2] != 'V' || wave[3] != 'E') {692ERR_FAIL_V_MSG(Ref<AudioStreamWAV>(), vformat("Not a WAV file. Header should contain 'WAVE', but found '%s', in file of size %d bytes", wave, file->get_length()));693}694695// Let users override potential loop points from the WAV.696// We parse the WAV loop points only with "Detect From WAV" (0).697int import_loop_mode = p_options["edit/loop_mode"];698699int format_bits = 0;700int format_channels = 0;701702AudioStreamWAV::LoopMode loop_mode = AudioStreamWAV::LOOP_DISABLED;703uint16_t compression_code = 1;704bool format_found = false;705bool data_found = false;706int format_freq = 0;707int loop_begin = 0;708int loop_end = 0;709int frames = 0;710711Vector<float> data;712713HashMap<String, String> tag_map;714715while (!file->eof_reached()) {716/* chunk */717char chunk_id[4];718file->get_buffer((uint8_t *)&chunk_id, 4); //RIFF719720/* chunk size */721uint32_t chunksize = file->get_32();722uint32_t file_pos = file->get_position(); //save file pos, so we can skip to next chunk safely723724if (file->eof_reached()) {725//ERR_PRINT("EOF REACH");726break;727}728729if (chunk_id[0] == 'f' && chunk_id[1] == 'm' && chunk_id[2] == 't' && chunk_id[3] == ' ' && !format_found) {730/* IS FORMAT CHUNK */731732//Issue: #7755 : Not a bug - usage of other formats (format codes) are unsupported in current importer version.733//Consider revision for engine version 3.0734compression_code = file->get_16();735if (compression_code != 1 && compression_code != 3) {736ERR_FAIL_V_MSG(Ref<AudioStreamWAV>(), "Format not supported for WAVE file (not PCM). Save WAVE files as uncompressed PCM or IEEE float instead.");737}738739format_channels = file->get_16();740if (format_channels != 1 && format_channels != 2) {741ERR_FAIL_V_MSG(Ref<AudioStreamWAV>(), "Format not supported for WAVE file (not stereo or mono).");742}743744format_freq = file->get_32(); //sampling rate745746file->get_32(); // average bits/second (unused)747file->get_16(); // block align (unused)748format_bits = file->get_16(); // bits per sample749750if (format_bits % 8 || format_bits == 0) {751ERR_FAIL_V_MSG(Ref<AudioStreamWAV>(), "Invalid amount of bits in the sample (should be one of 8, 16, 24 or 32).");752}753754if (compression_code == 3 && format_bits % 32) {755ERR_FAIL_V_MSG(Ref<AudioStreamWAV>(), "Invalid amount of bits in the IEEE float sample (should be 32 or 64).");756}757758/* Don't need anything else, continue */759format_found = true;760}761762if (chunk_id[0] == 'd' && chunk_id[1] == 'a' && chunk_id[2] == 't' && chunk_id[3] == 'a' && !data_found) {763/* IS DATA CHUNK */764data_found = true;765766if (!format_found) {767ERR_PRINT("'data' chunk before 'format' chunk found.");768break;769}770771uint64_t remaining_bytes = file_size - file_pos;772frames = chunksize;773if (remaining_bytes < chunksize) {774WARN_PRINT("Data chunk size is smaller than expected. Proceeding with actual data size.");775frames = remaining_bytes;776}777778ERR_FAIL_COND_V(format_channels == 0, Ref<AudioStreamWAV>());779frames /= format_channels;780frames /= (format_bits >> 3);781782/*print_line("chunksize: "+itos(chunksize));783print_line("channels: "+itos(format_channels));784print_line("bits: "+itos(format_bits));785*/786787data.resize(frames * format_channels);788789if (compression_code == 1) {790if (format_bits == 8) {791for (int i = 0; i < frames * format_channels; i++) {792// 8 bit samples are UNSIGNED793794data.write[i] = int8_t(file->get_8() - 128) / 128.f;795}796} else if (format_bits == 16) {797for (int i = 0; i < frames * format_channels; i++) {798//16 bit SIGNED799800data.write[i] = int16_t(file->get_16()) / 32768.f;801}802} else {803for (int i = 0; i < frames * format_channels; i++) {804//16+ bits samples are SIGNED805// if sample is > 16 bits, just read extra bytes806807uint32_t s = 0;808for (int b = 0; b < (format_bits >> 3); b++) {809s |= ((uint32_t)file->get_8()) << (b * 8);810}811s <<= (32 - format_bits);812813data.write[i] = (int32_t(s) >> 16) / 32768.f;814}815}816} else if (compression_code == 3) {817if (format_bits == 32) {818for (int i = 0; i < frames * format_channels; i++) {819//32 bit IEEE Float820821data.write[i] = file->get_float();822}823} else if (format_bits == 64) {824for (int i = 0; i < frames * format_channels; i++) {825//64 bit IEEE Float826827data.write[i] = file->get_double();828}829}830}831832// This is commented out due to some weird edge case seemingly in FileAccessMemory, doesn't seem to have any side effects though.833// if (file->eof_reached()) {834// ERR_FAIL_V_MSG(Ref<AudioStreamWAV>(), "Premature end of file.");835// }836}837838if (import_loop_mode == 0 && chunk_id[0] == 's' && chunk_id[1] == 'm' && chunk_id[2] == 'p' && chunk_id[3] == 'l') {839// Loop point info!840841/**842* Consider exploring next document:843* http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/RIFFNEW.pdf844* Especially on page:845* 16 - 17846* Timestamp:847* 22:38 06.07.2017 GMT848**/849850for (int i = 0; i < 10; i++) {851file->get_32(); // i wish to know why should i do this... no doc!852}853854// only read 0x00 (loop forward), 0x01 (loop ping-pong) and 0x02 (loop backward)855// Skip anything else because it's not supported, reserved for future uses or sampler specific856// from https://sites.google.com/site/musicgapi/technical-documents/wav-file-format#smpl (loop type values table)857int loop_type = file->get_32();858if (loop_type == 0x00 || loop_type == 0x01 || loop_type == 0x02) {859if (loop_type == 0x00) {860loop_mode = AudioStreamWAV::LOOP_FORWARD;861} else if (loop_type == 0x01) {862loop_mode = AudioStreamWAV::LOOP_PINGPONG;863} else if (loop_type == 0x02) {864loop_mode = AudioStreamWAV::LOOP_BACKWARD;865}866loop_begin = file->get_32();867loop_end = file->get_32();868}869}870871if (chunk_id[0] == 'L' && chunk_id[1] == 'I' && chunk_id[2] == 'S' && chunk_id[3] == 'T') {872// RIFF 'LIST' chunk.873// See https://www.recordingblogs.com/wiki/list-chunk-of-a-wave-file874875char list_id[4];876file->get_buffer((uint8_t *)&list_id, 4);877uint32_t end_of_chunk = file_pos + chunksize - 8;878879if (list_id[0] == 'I' && list_id[1] == 'N' && list_id[2] == 'F' && list_id[3] == 'O') {880// 'INFO' list type.881// The size of an entry can be arbitrary.882while (file->get_position() < end_of_chunk) {883char info_id[4];884file->get_buffer((uint8_t *)&info_id, 4);885886uint32_t text_size = file->get_32();887if (text_size == 0) {888continue;889}890891Vector<char> text;892text.resize(text_size);893file->get_buffer((uint8_t *)&text[0], text_size);894895// Skip padding byte if text_size is odd896if (text_size & 1) {897file->get_8();898}899900// The data is always an ASCII string. ASCII is a subset of UTF-8.901String tag;902tag.append_utf8(&info_id[0], 4);903904String tag_value;905tag_value.append_utf8(&text[0], text_size);906907tag_map[tag] = tag_value;908}909}910}911912// Move to the start of the next chunk. Note that RIFF requires a padding byte for odd913// chunk sizes.914file->seek(file_pos + chunksize + (chunksize & 1));915}916917// STEP 2, APPLY CONVERSIONS918919bool is16 = format_bits != 8;920int rate = format_freq;921922/*923print_line("Input Sample: ");924print_line("\tframes: " + itos(frames));925print_line("\tformat_channels: " + itos(format_channels));926print_line("\t16bits: " + itos(is16));927print_line("\trate: " + itos(rate));928print_line("\tloop: " + itos(loop));929print_line("\tloop begin: " + itos(loop_begin));930print_line("\tloop end: " + itos(loop_end));931*/932933//apply frequency limit934935bool limit_rate = p_options["force/max_rate"];936int limit_rate_hz = p_options["force/max_rate_hz"];937if (limit_rate && rate > limit_rate_hz && rate > 0 && frames > 0) {938// resample!939int new_data_frames = (int)(frames * (float)limit_rate_hz / (float)rate);940941Vector<float> new_data;942new_data.resize(new_data_frames * format_channels);943for (int c = 0; c < format_channels; c++) {944float frac = 0.0;945int ipos = 0;946947for (int i = 0; i < new_data_frames; i++) {948// Cubic interpolation should be enough.949950float y0 = data[MAX(0, ipos - 1) * format_channels + c];951float y1 = data[ipos * format_channels + c];952float y2 = data[MIN(frames - 1, ipos + 1) * format_channels + c];953float y3 = data[MIN(frames - 1, ipos + 2) * format_channels + c];954955new_data.write[i * format_channels + c] = Math::cubic_interpolate(y1, y2, y0, y3, frac);956957// update position and always keep fractional part within ]0...1]958// in order to avoid 32bit floating point precision errors959960frac += (float)rate / (float)limit_rate_hz;961int tpos = (int)Math::floor(frac);962ipos += tpos;963frac -= tpos;964}965}966967if (loop_mode) {968loop_begin = (int)(loop_begin * (float)new_data_frames / (float)frames);969loop_end = (int)(loop_end * (float)new_data_frames / (float)frames);970}971972data = new_data;973rate = limit_rate_hz;974frames = new_data_frames;975}976977bool normalize = p_options["edit/normalize"];978979if (normalize) {980float max = 0.0;981for (int i = 0; i < data.size(); i++) {982float amp = Math::abs(data[i]);983if (amp > max) {984max = amp;985}986}987988if (max > 0) {989float mult = 1.0 / max;990for (int i = 0; i < data.size(); i++) {991data.write[i] *= mult;992}993}994}995996bool trim = p_options["edit/trim"];997998if (trim && (loop_mode == AudioStreamWAV::LOOP_DISABLED) && format_channels > 0) {999int first = 0;1000int last = (frames / format_channels) - 1;1001bool found = false;1002float limit = Math::db_to_linear(TRIM_DB_LIMIT);10031004for (int i = 0; i < data.size() / format_channels; i++) {1005float amp_channel_sum = 0.0;1006for (int j = 0; j < format_channels; j++) {1007amp_channel_sum += Math::abs(data[(i * format_channels) + j]);1008}10091010float amp = Math::abs(amp_channel_sum / (float)format_channels);10111012if (!found && amp > limit) {1013first = i;1014found = true;1015}10161017if (found && amp > limit) {1018last = i;1019}1020}10211022if (first < last) {1023Vector<float> new_data;1024new_data.resize((last - first) * format_channels);1025for (int i = first; i < last; i++) {1026float fade_out_mult = 1.0;10271028if (last - i < TRIM_FADE_OUT_FRAMES) {1029fade_out_mult = ((float)(last - i - 1) / (float)TRIM_FADE_OUT_FRAMES);1030}10311032for (int j = 0; j < format_channels; j++) {1033new_data.write[((i - first) * format_channels) + j] = data[(i * format_channels) + j] * fade_out_mult;1034}1035}10361037data = new_data;1038frames = data.size() / format_channels;1039}1040}10411042if (import_loop_mode >= 2) {1043loop_mode = (AudioStreamWAV::LoopMode)(import_loop_mode - 1);1044loop_begin = p_options["edit/loop_begin"];1045loop_end = p_options["edit/loop_end"];1046// Wrap around to max frames, so `-1` can be used to select the end, etc.1047if (loop_begin < 0) {1048loop_begin = CLAMP(loop_begin + frames, 0, frames - 1);1049}1050if (loop_end < 0) {1051loop_end = CLAMP(loop_end + frames, 0, frames - 1);1052}1053}10541055int compression = p_options["compress/mode"];1056bool force_mono = p_options["force/mono"];10571058if (force_mono && format_channels == 2) {1059Vector<float> new_data;1060new_data.resize(data.size() / 2);1061for (int i = 0; i < frames; i++) {1062new_data.write[i] = (data[i * 2 + 0] + data[i * 2 + 1]) / 2.0;1063}10641065data = new_data;1066format_channels = 1;1067}10681069bool force_8_bit = p_options["force/8_bit"];1070if (force_8_bit) {1071is16 = false;1072}10731074Vector<uint8_t> dst_data;1075AudioStreamWAV::Format dst_format;10761077if (compression == 1) {1078dst_format = AudioStreamWAV::FORMAT_IMA_ADPCM;1079if (format_channels == 1) {1080_compress_ima_adpcm(data, dst_data);1081} else {1082//byte interleave1083Vector<float> left;1084Vector<float> right;10851086int tframes = data.size() / 2;1087left.resize(tframes);1088right.resize(tframes);10891090for (int i = 0; i < tframes; i++) {1091left.write[i] = data[i * 2 + 0];1092right.write[i] = data[i * 2 + 1];1093}10941095Vector<uint8_t> bleft;1096Vector<uint8_t> bright;10971098_compress_ima_adpcm(left, bleft);1099_compress_ima_adpcm(right, bright);11001101int dl = bleft.size();1102dst_data.resize(dl * 2);11031104uint8_t *w = dst_data.ptrw();1105const uint8_t *rl = bleft.ptr();1106const uint8_t *rr = bright.ptr();11071108for (int i = 0; i < dl; i++) {1109w[i * 2 + 0] = rl[i];1110w[i * 2 + 1] = rr[i];1111}1112}11131114} else if (compression == 2) {1115dst_format = AudioStreamWAV::FORMAT_QOA;11161117qoa_desc desc = {};1118desc.samplerate = rate;1119desc.samples = frames;1120desc.channels = format_channels;11211122_compress_qoa(data, dst_data, &desc);1123} else {1124dst_format = is16 ? AudioStreamWAV::FORMAT_16_BITS : AudioStreamWAV::FORMAT_8_BITS;1125dst_data.resize(data.size() * (is16 ? 2 : 1));1126{1127uint8_t *w = dst_data.ptrw();11281129int ds = data.size();1130for (int i = 0; i < ds; i++) {1131if (is16) {1132int16_t v = CLAMP(data[i] * 32768, -32768, 32767);1133encode_uint16(v, &w[i * 2]);1134} else {1135int8_t v = CLAMP(data[i] * 128, -128, 127);1136w[i] = v;1137}1138}1139}1140}11411142Ref<AudioStreamWAV> sample;1143sample.instantiate();1144sample->set_data(dst_data);1145sample->set_format(dst_format);1146sample->set_mix_rate(rate);1147sample->set_loop_mode(loop_mode);1148sample->set_loop_begin(loop_begin);1149sample->set_loop_end(loop_end);1150sample->set_stereo(format_channels == 2);11511152if (!tag_map.is_empty()) {1153// Used to make the metadata tags more unified across different AudioStreams.1154// See https://www.recordingblogs.com/wiki/list-chunk-of-a-wave-file1155// https://wiki.hydrogenaudio.org/index.php?title=Tag_Mapping#Mapping_Tables1156HashMap<String, String> tag_id_remaps;1157tag_id_remaps.reserve(15);1158tag_id_remaps["IARL"] = "location";1159tag_id_remaps["IART"] = "artist";1160tag_id_remaps["ICMS"] = "organization";1161tag_id_remaps["ICMT"] = "comment";1162tag_id_remaps["ICNT"] = "releasecountry";1163tag_id_remaps["ICOP"] = "copyright";1164tag_id_remaps["ICRD"] = "date";1165tag_id_remaps["IENC"] = "encodedby";1166tag_id_remaps["IENG"] = "engineer";1167tag_id_remaps["IFRM"] = "tracktotal";1168tag_id_remaps["IGNR"] = "genre";1169tag_id_remaps["IKEY"] = "keywords";1170tag_id_remaps["ILNG"] = "language";1171tag_id_remaps["IMED"] = "media";1172tag_id_remaps["IMUS"] = "composer";1173tag_id_remaps["INAM"] = "title";1174tag_id_remaps["IPRD"] = "album";1175tag_id_remaps["IPRO"] = "producer";1176tag_id_remaps["IPRT"] = "tracknumber";1177tag_id_remaps["ISBJ"] = "description";1178tag_id_remaps["ISFT"] = "encoder";1179tag_id_remaps["ISRF"] = "media";1180tag_id_remaps["ITCH"] = "encodedby";1181tag_id_remaps["ITRK"] = "tracknumber";1182tag_id_remaps["IWRI"] = "author";1183tag_id_remaps["TLEN"] = "length";1184Dictionary tag_dictionary;1185for (const KeyValue<String, String> &E : tag_map) {1186HashMap<String, String>::ConstIterator remap = tag_id_remaps.find(E.key);1187String tag_key = E.key;1188if (remap) {1189tag_key = remap->value;1190}11911192tag_dictionary[tag_key] = E.value;1193}1194sample->set_tags(tag_dictionary);1195}11961197return sample;1198}11991200Ref<AudioStreamWAV> AudioStreamWAV::load_from_file(const String &p_path, const Dictionary &p_options) {1201const Vector<uint8_t> stream_data = FileAccess::get_file_as_bytes(p_path);1202ERR_FAIL_COND_V_MSG(stream_data.is_empty(), Ref<AudioStreamWAV>(), vformat("Cannot open file '%s'.", p_path));1203return load_from_buffer(stream_data, p_options);1204}12051206void AudioStreamWAV::_bind_methods() {1207ClassDB::bind_static_method("AudioStreamWAV", D_METHOD("load_from_buffer", "stream_data", "options"), &AudioStreamWAV::load_from_buffer, DEFVAL(Dictionary()));1208ClassDB::bind_static_method("AudioStreamWAV", D_METHOD("load_from_file", "path", "options"), &AudioStreamWAV::load_from_file, DEFVAL(Dictionary()));12091210ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamWAV::set_data);1211ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamWAV::get_data);12121213ClassDB::bind_method(D_METHOD("set_format", "format"), &AudioStreamWAV::set_format);1214ClassDB::bind_method(D_METHOD("get_format"), &AudioStreamWAV::get_format);12151216ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &AudioStreamWAV::set_loop_mode);1217ClassDB::bind_method(D_METHOD("get_loop_mode"), &AudioStreamWAV::get_loop_mode);12181219ClassDB::bind_method(D_METHOD("set_loop_begin", "loop_begin"), &AudioStreamWAV::set_loop_begin);1220ClassDB::bind_method(D_METHOD("get_loop_begin"), &AudioStreamWAV::get_loop_begin);12211222ClassDB::bind_method(D_METHOD("set_loop_end", "loop_end"), &AudioStreamWAV::set_loop_end);1223ClassDB::bind_method(D_METHOD("get_loop_end"), &AudioStreamWAV::get_loop_end);12241225ClassDB::bind_method(D_METHOD("set_mix_rate", "mix_rate"), &AudioStreamWAV::set_mix_rate);1226ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioStreamWAV::get_mix_rate);12271228ClassDB::bind_method(D_METHOD("set_stereo", "stereo"), &AudioStreamWAV::set_stereo);1229ClassDB::bind_method(D_METHOD("is_stereo"), &AudioStreamWAV::is_stereo);12301231ClassDB::bind_method(D_METHOD("set_tags", "tags"), &AudioStreamWAV::set_tags);1232ClassDB::bind_method(D_METHOD("get_tags"), &AudioStreamWAV::get_tags);12331234ClassDB::bind_method(D_METHOD("save_to_wav", "path"), &AudioStreamWAV::save_to_wav);12351236ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_data", "get_data");1237ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA ADPCM,Quite OK Audio"), "set_format", "get_format");1238ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "Disabled,Forward,Ping-Pong,Backward"), "set_loop_mode", "get_loop_mode");1239ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_begin"), "set_loop_begin", "get_loop_begin");1240ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_end"), "set_loop_end", "get_loop_end");1241ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_rate"), "set_mix_rate", "get_mix_rate");1242ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stereo"), "set_stereo", "is_stereo");1243ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "tags", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_tags", "get_tags");12441245BIND_ENUM_CONSTANT(FORMAT_8_BITS);1246BIND_ENUM_CONSTANT(FORMAT_16_BITS);1247BIND_ENUM_CONSTANT(FORMAT_IMA_ADPCM);1248BIND_ENUM_CONSTANT(FORMAT_QOA);12491250BIND_ENUM_CONSTANT(LOOP_DISABLED);1251BIND_ENUM_CONSTANT(LOOP_FORWARD);1252BIND_ENUM_CONSTANT(LOOP_PINGPONG);1253BIND_ENUM_CONSTANT(LOOP_BACKWARD);1254}125512561257