From 4889659227221f137da0bd926ddb6cd867bbd632 Mon Sep 17 00:00:00 2001 From: DeeJayLSP Date: Sat, 23 Jul 2022 11:34:36 -0300 Subject: Rename AudioStreamSample to a more discoverable name --- doc/classes/AudioEffectRecord.xml | 6 +- doc/classes/AudioStream.xml | 2 +- doc/classes/AudioStreamSample.xml | 71 --- doc/classes/AudioStreamWAV.xml | 71 +++ editor/editor_asset_installer.cpp | 2 +- editor/icons/AudioStreamSample.svg | 1 - editor/icons/AudioStreamWAV.svg | 1 + editor/import/resource_importer_wav.cpp | 26 +- editor/project_converter_3_to_4.cpp | 3 +- scene/register_scene_types.cpp | 5 +- scene/resources/audio_stream_sample.cpp | 667 -------------------------- scene/resources/audio_stream_sample.h | 159 ------ scene/resources/audio_stream_wav.cpp | 667 ++++++++++++++++++++++++++ scene/resources/audio_stream_wav.h | 159 ++++++ servers/audio/effects/audio_effect_record.cpp | 20 +- servers/audio/effects/audio_effect_record.h | 10 +- servers/audio_server.cpp | 2 +- servers/audio_server.h | 2 +- 18 files changed, 938 insertions(+), 936 deletions(-) delete mode 100644 doc/classes/AudioStreamSample.xml create mode 100644 doc/classes/AudioStreamWAV.xml delete mode 100644 editor/icons/AudioStreamSample.svg create mode 100644 editor/icons/AudioStreamWAV.svg delete mode 100644 scene/resources/audio_stream_sample.cpp delete mode 100644 scene/resources/audio_stream_sample.h create mode 100644 scene/resources/audio_stream_wav.cpp create mode 100644 scene/resources/audio_stream_wav.h diff --git a/doc/classes/AudioEffectRecord.xml b/doc/classes/AudioEffectRecord.xml index 9728011bb2..32a6aea340 100644 --- a/doc/classes/AudioEffectRecord.xml +++ b/doc/classes/AudioEffectRecord.xml @@ -14,7 +14,7 @@ - + Returns the recorded sample. @@ -34,8 +34,8 @@ - - Specifies the format in which the sample will be recorded. See [enum AudioStreamSample.Format] for available formats. + + Specifies the format in which the sample will be recorded. See [enum AudioStreamWAV.Format] for available formats. diff --git a/doc/classes/AudioStream.xml b/doc/classes/AudioStream.xml index 68f64505d0..0793f2efef 100644 --- a/doc/classes/AudioStream.xml +++ b/doc/classes/AudioStream.xml @@ -4,7 +4,7 @@ Base class for audio streams. - Base class for audio streams. Audio streams are used for sound effects and music playback, and support WAV (via [AudioStreamSample]) and OGG (via [AudioStreamOGGVorbis]) file formats. + Base class for audio streams. Audio streams are used for sound effects and music playback, and support WAV (via [AudioStreamWAV]) and OGG (via [AudioStreamOGGVorbis]) file formats. $DOCS_URL/tutorials/audio/audio_streams.html diff --git a/doc/classes/AudioStreamSample.xml b/doc/classes/AudioStreamSample.xml deleted file mode 100644 index 62f27ce876..0000000000 --- a/doc/classes/AudioStreamSample.xml +++ /dev/null @@ -1,71 +0,0 @@ - - - - Stores audio data loaded from WAV files. - - - AudioStreamSample stores sound samples loaded from WAV files. To play the stored sound, use an [AudioStreamPlayer] (for non-positional audio) or [AudioStreamPlayer2D]/[AudioStreamPlayer3D] (for positional audio). The sound can be looped. - This class can also be used to store dynamically-generated PCM audio data. See also [AudioStreamGenerator] for procedural audio generation. - - - - - - - - - Saves the AudioStreamSample as a WAV file to [code]path[/code]. Samples with IMA ADPCM format can't be saved. - [b]Note:[/b] A [code].wav[/code] extension is automatically appended to [code]path[/code] if it is missing. - - - - - - Contains the audio data in bytes. - [b]Note:[/b] This property expects signed PCM8 data. To convert unsigned PCM8 to signed PCM8, subtract 128 from each byte. - - - Audio format. See [enum Format] constants for values. - - - The loop start point (in number of samples, relative to the beginning of the sample). This information will be imported automatically from the WAV file if present. - - - The loop end point (in number of samples, relative to the beginning of the sample). This information will be imported automatically from the WAV file if present. - - - The loop mode. This information will be imported automatically from the WAV file if present. See [enum LoopMode] constants for values. - - - The sample rate for mixing this audio. Higher values require more storage space, but result in better quality. - In games, common sample rates in use are [code]11025[/code], [code]16000[/code], [code]22050[/code], [code]32000[/code], [code]44100[/code], and [code]48000[/code]. - According to the [url=https://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem]Nyquist-Shannon sampling theorem[/url], there is no quality difference to human hearing when going past 40,000 Hz (since most humans can only hear up to ~20,000 Hz, often less). If you are using lower-pitched sounds such as voices, lower sample rates such as [code]32000[/code] or [code]22050[/code] may be usable with no loss in quality. - - - If [code]true[/code], audio is stereo. - - - - - 8-bit audio codec. - - - 16-bit audio codec. - - - Audio is compressed using IMA ADPCM. - - - Audio does not loop. - - - Audio loops the data between [member loop_begin] and [member loop_end], playing forward only. - - - Audio loops the data between [member loop_begin] and [member loop_end], playing back and forth. - - - Audio loops the data between [member loop_begin] and [member loop_end], playing backward only. - - - diff --git a/doc/classes/AudioStreamWAV.xml b/doc/classes/AudioStreamWAV.xml new file mode 100644 index 0000000000..17595aec2f --- /dev/null +++ b/doc/classes/AudioStreamWAV.xml @@ -0,0 +1,71 @@ + + + + Stores audio data loaded from WAV files. + + + AudioStreamWAV stores sound samples loaded from WAV files. To play the stored sound, use an [AudioStreamPlayer] (for non-positional audio) or [AudioStreamPlayer2D]/[AudioStreamPlayer3D] (for positional audio). The sound can be looped. + This class can also be used to store dynamically-generated PCM audio data. See also [AudioStreamGenerator] for procedural audio generation. + + + + + + + + + Saves the AudioStreamWAV as a WAV file to [code]path[/code]. Samples with IMA ADPCM format can't be saved. + [b]Note:[/b] A [code].wav[/code] extension is automatically appended to [code]path[/code] if it is missing. + + + + + + Contains the audio data in bytes. + [b]Note:[/b] This property expects signed PCM8 data. To convert unsigned PCM8 to signed PCM8, subtract 128 from each byte. + + + Audio format. See [enum Format] constants for values. + + + The loop start point (in number of samples, relative to the beginning of the sample). This information will be imported automatically from the WAV file if present. + + + The loop end point (in number of samples, relative to the beginning of the sample). This information will be imported automatically from the WAV file if present. + + + The loop mode. This information will be imported automatically from the WAV file if present. See [enum LoopMode] constants for values. + + + The sample rate for mixing this audio. Higher values require more storage space, but result in better quality. + In games, common sample rates in use are [code]11025[/code], [code]16000[/code], [code]22050[/code], [code]32000[/code], [code]44100[/code], and [code]48000[/code]. + According to the [url=https://en.wikipedia.org/wiki/Nyquist%E2%80%93Shannon_sampling_theorem]Nyquist-Shannon sampling theorem[/url], there is no quality difference to human hearing when going past 40,000 Hz (since most humans can only hear up to ~20,000 Hz, often less). If you are using lower-pitched sounds such as voices, lower sample rates such as [code]32000[/code] or [code]22050[/code] may be usable with no loss in quality. + + + If [code]true[/code], audio is stereo. + + + + + 8-bit audio codec. + + + 16-bit audio codec. + + + Audio is compressed using IMA ADPCM. + + + Audio does not loop. + + + Audio loops the data between [member loop_begin] and [member loop_end], playing forward only. + + + Audio loops the data between [member loop_begin] and [member loop_end], playing back and forth. + + + Audio loops the data between [member loop_begin] and [member loop_end], playing backward only. + + + diff --git a/editor/editor_asset_installer.cpp b/editor/editor_asset_installer.cpp index 8fa486408e..aea962f344 100644 --- a/editor/editor_asset_installer.cpp +++ b/editor/editor_asset_installer.cpp @@ -100,7 +100,7 @@ void EditorAssetInstaller::open(const String &p_path, int p_depth) { extension_guess["tga"] = tree->get_theme_icon(SNAME("ImageTexture"), SNAME("EditorIcons")); extension_guess["webp"] = tree->get_theme_icon(SNAME("ImageTexture"), SNAME("EditorIcons")); - extension_guess["wav"] = tree->get_theme_icon(SNAME("AudioStreamSample"), SNAME("EditorIcons")); + extension_guess["wav"] = tree->get_theme_icon(SNAME("AudioStreamWAV"), SNAME("EditorIcons")); extension_guess["ogg"] = tree->get_theme_icon(SNAME("AudioStreamOGGVorbis"), SNAME("EditorIcons")); extension_guess["mp3"] = tree->get_theme_icon(SNAME("AudioStreamMP3"), SNAME("EditorIcons")); diff --git a/editor/icons/AudioStreamSample.svg b/editor/icons/AudioStreamSample.svg deleted file mode 100644 index 2e54de9faa..0000000000 --- a/editor/icons/AudioStreamSample.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/editor/icons/AudioStreamWAV.svg b/editor/icons/AudioStreamWAV.svg new file mode 100644 index 0000000000..2e54de9faa --- /dev/null +++ b/editor/icons/AudioStreamWAV.svg @@ -0,0 +1 @@ + diff --git a/editor/import/resource_importer_wav.cpp b/editor/import/resource_importer_wav.cpp index f0ba1eb7a1..3a47bfb29f 100644 --- a/editor/import/resource_importer_wav.cpp +++ b/editor/import/resource_importer_wav.cpp @@ -33,7 +33,7 @@ #include "core/io/file_access.h" #include "core/io/marshalls.h" #include "core/io/resource_saver.h" -#include "scene/resources/audio_stream_sample.h" +#include "scene/resources/audio_stream_wav.h" const float TRIM_DB_LIMIT = -50; const int TRIM_FADE_OUT_FRAMES = 500; @@ -55,7 +55,7 @@ String ResourceImporterWAV::get_save_extension() const { } String ResourceImporterWAV::get_resource_type() const { - return "AudioStreamSample"; + return "AudioStreamWAV"; } bool ResourceImporterWAV::get_option_visibility(const String &p_path, const String &p_option, const HashMap &p_options) const { @@ -86,7 +86,7 @@ void ResourceImporterWAV::get_import_options(const String &p_path, Listpush_back(ImportOption(PropertyInfo(Variant::FLOAT, "force/max_rate_hz", PROPERTY_HINT_RANGE, "11025,192000,1,exp"), 44100)); r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "edit/trim"), false)); r_options->push_back(ImportOption(PropertyInfo(Variant::BOOL, "edit/normalize"), false)); - // Keep the `edit/loop_mode` enum in sync with AudioStreamSample::LoopMode (note: +1 offset due to "Detect From WAV"). + // Keep the `edit/loop_mode` enum in sync with AudioStreamWAV::LoopMode (note: +1 offset due to "Detect From WAV"). 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)); r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "edit/loop_begin"), 0)); r_options->push_back(ImportOption(PropertyInfo(Variant::INT, "edit/loop_end"), -1)); @@ -130,7 +130,7 @@ Error ResourceImporterWAV::import(const String &p_source_file, const String &p_s int format_bits = 0; int format_channels = 0; - AudioStreamSample::LoopMode loop_mode = AudioStreamSample::LOOP_DISABLED; + AudioStreamWAV::LoopMode loop_mode = AudioStreamWAV::LOOP_DISABLED; uint16_t compression_code = 1; bool format_found = false; bool data_found = false; @@ -282,11 +282,11 @@ Error ResourceImporterWAV::import(const String &p_source_file, const String &p_s int loop_type = file->get_32(); if (loop_type == 0x00 || loop_type == 0x01 || loop_type == 0x02) { if (loop_type == 0x00) { - loop_mode = AudioStreamSample::LOOP_FORWARD; + loop_mode = AudioStreamWAV::LOOP_FORWARD; } else if (loop_type == 0x01) { - loop_mode = AudioStreamSample::LOOP_PINGPONG; + loop_mode = AudioStreamWAV::LOOP_PINGPONG; } else if (loop_type == 0x02) { - loop_mode = AudioStreamSample::LOOP_BACKWARD; + loop_mode = AudioStreamWAV::LOOP_BACKWARD; } loop_begin = file->get_32(); loop_end = file->get_32(); @@ -386,7 +386,7 @@ Error ResourceImporterWAV::import(const String &p_source_file, const String &p_s bool trim = p_options["edit/trim"]; - if (trim && (loop_mode != AudioStreamSample::LOOP_DISABLED) && format_channels > 0) { + if (trim && (loop_mode != AudioStreamWAV::LOOP_DISABLED) && format_channels > 0) { int first = 0; int last = (frames / format_channels) - 1; bool found = false; @@ -431,7 +431,7 @@ Error ResourceImporterWAV::import(const String &p_source_file, const String &p_s } if (import_loop_mode >= 2) { - loop_mode = (AudioStreamSample::LoopMode)(import_loop_mode - 1); + loop_mode = (AudioStreamWAV::LoopMode)(import_loop_mode - 1); loop_begin = p_options["edit/loop_begin"]; loop_end = p_options["edit/loop_end"]; // Wrap around to max frames, so `-1` can be used to select the end, etc. @@ -463,10 +463,10 @@ Error ResourceImporterWAV::import(const String &p_source_file, const String &p_s } Vector dst_data; - AudioStreamSample::Format dst_format; + AudioStreamWAV::Format dst_format; if (compression == 1) { - dst_format = AudioStreamSample::FORMAT_IMA_ADPCM; + dst_format = AudioStreamWAV::FORMAT_IMA_ADPCM; if (format_channels == 1) { _compress_ima_adpcm(data, dst_data); } else { @@ -503,7 +503,7 @@ Error ResourceImporterWAV::import(const String &p_source_file, const String &p_s } } else { - dst_format = is16 ? AudioStreamSample::FORMAT_16_BITS : AudioStreamSample::FORMAT_8_BITS; + dst_format = is16 ? AudioStreamWAV::FORMAT_16_BITS : AudioStreamWAV::FORMAT_8_BITS; dst_data.resize(data.size() * (is16 ? 2 : 1)); { uint8_t *w = dst_data.ptrw(); @@ -521,7 +521,7 @@ Error ResourceImporterWAV::import(const String &p_source_file, const String &p_s } } - Ref sample; + Ref sample; sample.instantiate(); sample->set_data(dst_data); sample->set_format(dst_format); diff --git a/editor/project_converter_3_to_4.cpp b/editor/project_converter_3_to_4.cpp index 6437e19404..5be6e9d059 100644 --- a/editor/project_converter_3_to_4.cpp +++ b/editor/project_converter_3_to_4.cpp @@ -120,7 +120,7 @@ static const char *enum_renames[][2] = { { "JOINT_PIN", "JOINT_TYPE_PIN" }, // PhysicsServer2D { "JOINT_SLIDER", "JOINT_TYPE_SLIDER" }, // PhysicsServer3D { "KEY_CONTROL", "KEY_CTRL" }, // Globals - { "LOOP_PING_PONG", "LOOP_PINGPONG" }, //AudioStreamSample + { "LOOP_PING_PONG", "LOOP_PINGPONG" }, // AudioStreamWAV { "MATH_RAND", "MATH_RANDF_RANGE" }, // VisualScriptBuiltinFunc { "MATH_RANDOM", "MATH_RANDI_RANGE" }, // VisualScriptBuiltinFunc { "MATH_STEPIFY", "MATH_STEP_DECIMALS" }, // VisualScriptBuiltinFunc @@ -1251,6 +1251,7 @@ static const char *class_renames[][2] = { { "AnimationTreePlayer", "AnimationTree" }, { "Area", "Area3D" }, // Be careful, this will be used everywhere { "AudioStreamRandomPitch", "AudioStreamRandomizer" }, + { "AudioStreamSample", "AudioStreamWAV" }, { "BakedLightmap", "LightmapGI" }, { "BakedLightmapData", "LightmapGIData" }, { "BitmapFont", "FontFile" }, diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index 09a283ea53..74b6f8aa01 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -141,7 +141,7 @@ #include "scene/multiplayer/scene_replication_interface.h" #include "scene/multiplayer/scene_rpc_interface.h" #include "scene/resources/animation_library.h" -#include "scene/resources/audio_stream_sample.h" +#include "scene/resources/audio_stream_wav.h" #include "scene/resources/bit_map.h" #include "scene/resources/bone_map.h" #include "scene/resources/box_shape_3d.h" @@ -903,7 +903,7 @@ void register_scene_types() { GDREGISTER_CLASS(AudioStreamPlayer3D); #endif GDREGISTER_ABSTRACT_CLASS(VideoStream); - GDREGISTER_CLASS(AudioStreamSample); + GDREGISTER_CLASS(AudioStreamWAV); OS::get_singleton()->yield(); // may take time to init @@ -1090,6 +1090,7 @@ void register_scene_types() { ClassDB::add_compatibility_class("World", "World3D"); // Renamed during 4.0 alpha, added to ease transition between alphas. + ClassDB::add_compatibility_class("AudioStreamSample", "AudioStreamWAV"); ClassDB::add_compatibility_class("StreamCubemap", "CompressedCubemap"); ClassDB::add_compatibility_class("StreamCubemapArray", "CompressedCubemapArray"); ClassDB::add_compatibility_class("StreamTexture2D", "CompressedTexture2D"); diff --git a/scene/resources/audio_stream_sample.cpp b/scene/resources/audio_stream_sample.cpp deleted file mode 100644 index dcd36284d4..0000000000 --- a/scene/resources/audio_stream_sample.cpp +++ /dev/null @@ -1,667 +0,0 @@ -/*************************************************************************/ -/* audio_stream_sample.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* 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. */ -/*************************************************************************/ - -#include "audio_stream_sample.h" - -#include "core/io/file_access.h" -#include "core/io/marshalls.h" - -void AudioStreamPlaybackSample::start(float p_from_pos) { - if (base->format == AudioStreamSample::FORMAT_IMA_ADPCM) { - //no seeking in IMA_ADPCM - for (int i = 0; i < 2; i++) { - ima_adpcm[i].step_index = 0; - ima_adpcm[i].predictor = 0; - ima_adpcm[i].loop_step_index = 0; - ima_adpcm[i].loop_predictor = 0; - ima_adpcm[i].last_nibble = -1; - ima_adpcm[i].loop_pos = 0x7FFFFFFF; - ima_adpcm[i].window_ofs = 0; - } - - offset = 0; - } else { - seek(p_from_pos); - } - - sign = 1; - active = true; -} - -void AudioStreamPlaybackSample::stop() { - active = false; -} - -bool AudioStreamPlaybackSample::is_playing() const { - return active; -} - -int AudioStreamPlaybackSample::get_loop_count() const { - return 0; -} - -float AudioStreamPlaybackSample::get_playback_position() const { - return float(offset >> MIX_FRAC_BITS) / base->mix_rate; -} - -void AudioStreamPlaybackSample::seek(float p_time) { - if (base->format == AudioStreamSample::FORMAT_IMA_ADPCM) { - return; //no seeking in ima-adpcm - } - - float max = base->get_length(); - if (p_time < 0) { - p_time = 0; - } else if (p_time >= max) { - p_time = max - 0.001; - } - - offset = uint64_t(p_time * base->mix_rate) << MIX_FRAC_BITS; -} - -template -void AudioStreamPlaybackSample::do_resample(const Depth *p_src, AudioFrame *p_dst, int64_t &offset, int32_t &increment, uint32_t amount, IMA_ADPCM_State *ima_adpcm) { - // this function will be compiled branchless by any decent compiler - - int32_t final, final_r, next, next_r; - while (amount) { - amount--; - int64_t pos = offset >> MIX_FRAC_BITS; - if (is_stereo && !is_ima_adpcm) { - pos <<= 1; - } - - if (is_ima_adpcm) { - int64_t sample_pos = pos + ima_adpcm[0].window_ofs; - - while (sample_pos > ima_adpcm[0].last_nibble) { - 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 - }; - - for (int i = 0; i < (is_stereo ? 2 : 1); i++) { - int16_t nibble, diff, step; - - ima_adpcm[i].last_nibble++; - const uint8_t *src_ptr = (const uint8_t *)base->data; - src_ptr += AudioStreamSample::DATA_PAD; - - uint8_t nbb = src_ptr[(ima_adpcm[i].last_nibble >> 1) * (is_stereo ? 2 : 1) + i]; - nibble = (ima_adpcm[i].last_nibble & 1) ? (nbb >> 4) : (nbb & 0xF); - step = _ima_adpcm_step_table[ima_adpcm[i].step_index]; - - ima_adpcm[i].step_index += _ima_adpcm_index_table[nibble]; - if (ima_adpcm[i].step_index < 0) { - ima_adpcm[i].step_index = 0; - } - if (ima_adpcm[i].step_index > 88) { - ima_adpcm[i].step_index = 88; - } - - diff = step >> 3; - if (nibble & 1) { - diff += step >> 2; - } - if (nibble & 2) { - diff += step >> 1; - } - if (nibble & 4) { - diff += step; - } - if (nibble & 8) { - diff = -diff; - } - - ima_adpcm[i].predictor += diff; - if (ima_adpcm[i].predictor < -0x8000) { - ima_adpcm[i].predictor = -0x8000; - } else if (ima_adpcm[i].predictor > 0x7FFF) { - ima_adpcm[i].predictor = 0x7FFF; - } - - /* store loop if there */ - if (ima_adpcm[i].last_nibble == ima_adpcm[i].loop_pos) { - ima_adpcm[i].loop_step_index = ima_adpcm[i].step_index; - ima_adpcm[i].loop_predictor = ima_adpcm[i].predictor; - } - - //printf("%i - %i - pred %i\n",int(ima_adpcm[i].last_nibble),int(nibble),int(ima_adpcm[i].predictor)); - } - } - - final = ima_adpcm[0].predictor; - if (is_stereo) { - final_r = ima_adpcm[1].predictor; - } - - } else { - final = p_src[pos]; - if (is_stereo) { - final_r = p_src[pos + 1]; - } - - if (sizeof(Depth) == 1) { /* conditions will not exist anymore when compiled! */ - final <<= 8; - if (is_stereo) { - final_r <<= 8; - } - } - - if (is_stereo) { - next = p_src[pos + 2]; - next_r = p_src[pos + 3]; - } else { - next = p_src[pos + 1]; - } - - if (sizeof(Depth) == 1) { - next <<= 8; - if (is_stereo) { - next_r <<= 8; - } - } - - int32_t frac = int64_t(offset & MIX_FRAC_MASK); - - final = final + ((next - final) * frac >> MIX_FRAC_BITS); - if (is_stereo) { - final_r = final_r + ((next_r - final_r) * frac >> MIX_FRAC_BITS); - } - } - - if (!is_stereo) { - final_r = final; //copy to right channel if stereo - } - - p_dst->l = final / 32767.0; - p_dst->r = final_r / 32767.0; - p_dst++; - - offset += increment; - } -} - -int AudioStreamPlaybackSample::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) { - if (!base->data || !active) { - for (int i = 0; i < p_frames; i++) { - p_buffer[i] = AudioFrame(0, 0); - } - return 0; - } - - int len = base->data_bytes; - switch (base->format) { - case AudioStreamSample::FORMAT_8_BITS: - len /= 1; - break; - case AudioStreamSample::FORMAT_16_BITS: - len /= 2; - break; - case AudioStreamSample::FORMAT_IMA_ADPCM: - len *= 2; - break; - } - - if (base->stereo) { - len /= 2; - } - - /* some 64-bit fixed point precaches */ - - int64_t loop_begin_fp = ((int64_t)base->loop_begin << MIX_FRAC_BITS); - int64_t loop_end_fp = ((int64_t)base->loop_end << MIX_FRAC_BITS); - int64_t length_fp = ((int64_t)len << MIX_FRAC_BITS); - int64_t begin_limit = (base->loop_mode != AudioStreamSample::LOOP_DISABLED) ? loop_begin_fp : 0; - int64_t end_limit = (base->loop_mode != AudioStreamSample::LOOP_DISABLED) ? loop_end_fp : length_fp; - bool is_stereo = base->stereo; - - int32_t todo = p_frames; - - if (base->loop_mode == AudioStreamSample::LOOP_BACKWARD) { - sign = -1; - } - - float base_rate = AudioServer::get_singleton()->get_mix_rate(); - float srate = base->mix_rate; - srate *= p_rate_scale; - float playback_speed_scale = AudioServer::get_singleton()->get_playback_speed_scale(); - float fincrement = (srate * playback_speed_scale) / base_rate; - int32_t increment = int32_t(MAX(fincrement * MIX_FRAC_LEN, 1)); - increment *= sign; - - //looping - - AudioStreamSample::LoopMode loop_format = base->loop_mode; - AudioStreamSample::Format format = base->format; - - /* audio data */ - - uint8_t *dataptr = (uint8_t *)base->data; - const void *data = dataptr + AudioStreamSample::DATA_PAD; - AudioFrame *dst_buff = p_buffer; - - if (format == AudioStreamSample::FORMAT_IMA_ADPCM) { - if (loop_format != AudioStreamSample::LOOP_DISABLED) { - ima_adpcm[0].loop_pos = loop_begin_fp >> MIX_FRAC_BITS; - ima_adpcm[1].loop_pos = loop_begin_fp >> MIX_FRAC_BITS; - loop_format = AudioStreamSample::LOOP_FORWARD; - } - } - - while (todo > 0) { - int64_t limit = 0; - int32_t target = 0, aux = 0; - - /** LOOP CHECKING **/ - - if (increment < 0) { - /* going backwards */ - - if (loop_format != AudioStreamSample::LOOP_DISABLED && offset < loop_begin_fp) { - /* loopstart reached */ - if (loop_format == AudioStreamSample::LOOP_PINGPONG) { - /* bounce ping pong */ - offset = loop_begin_fp + (loop_begin_fp - offset); - increment = -increment; - sign *= -1; - } else { - /* go to loop-end */ - offset = loop_end_fp - (loop_begin_fp - offset); - } - } else { - /* check for sample not reaching beginning */ - if (offset < 0) { - active = false; - break; - } - } - } else { - /* going forward */ - if (loop_format != AudioStreamSample::LOOP_DISABLED && offset >= loop_end_fp) { - /* loopend reached */ - - if (loop_format == AudioStreamSample::LOOP_PINGPONG) { - /* bounce ping pong */ - offset = loop_end_fp - (offset - loop_end_fp); - increment = -increment; - sign *= -1; - } else { - /* go to loop-begin */ - - if (format == AudioStreamSample::FORMAT_IMA_ADPCM) { - for (int i = 0; i < 2; i++) { - ima_adpcm[i].step_index = ima_adpcm[i].loop_step_index; - ima_adpcm[i].predictor = ima_adpcm[i].loop_predictor; - ima_adpcm[i].last_nibble = loop_begin_fp >> MIX_FRAC_BITS; - } - offset = loop_begin_fp; - } else { - offset = loop_begin_fp + (offset - loop_end_fp); - } - } - } else { - /* no loop, check for end of sample */ - if (offset >= length_fp) { - active = false; - break; - } - } - } - - /** MIXCOUNT COMPUTING **/ - - /* next possible limit (looppoints or sample begin/end */ - limit = (increment < 0) ? begin_limit : end_limit; - - /* compute what is shorter, the todo or the limit? */ - aux = (limit - offset) / increment + 1; - target = (aux < todo) ? aux : todo; /* mix target is the shorter buffer */ - - /* check just in case */ - if (target <= 0) { - active = false; - break; - } - - todo -= target; - - switch (base->format) { - case AudioStreamSample::FORMAT_8_BITS: { - if (is_stereo) { - do_resample((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm); - } else { - do_resample((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm); - } - } break; - case AudioStreamSample::FORMAT_16_BITS: { - if (is_stereo) { - do_resample((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm); - } else { - do_resample((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm); - } - - } break; - case AudioStreamSample::FORMAT_IMA_ADPCM: { - if (is_stereo) { - do_resample((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm); - } else { - do_resample((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm); - } - - } break; - } - - dst_buff += target; - } - - if (todo) { - int mixed_frames = p_frames - todo; - //bit was missing from mix - int todo_ofs = p_frames - todo; - for (int i = todo_ofs; i < p_frames; i++) { - p_buffer[i] = AudioFrame(0, 0); - } - return mixed_frames; - } - return p_frames; -} - -void AudioStreamPlaybackSample::tag_used_streams() { - base->tag_used(get_playback_position()); -} - -AudioStreamPlaybackSample::AudioStreamPlaybackSample() {} - -///////////////////// - -void AudioStreamSample::set_format(Format p_format) { - format = p_format; -} - -AudioStreamSample::Format AudioStreamSample::get_format() const { - return format; -} - -void AudioStreamSample::set_loop_mode(LoopMode p_loop_mode) { - loop_mode = p_loop_mode; -} - -AudioStreamSample::LoopMode AudioStreamSample::get_loop_mode() const { - return loop_mode; -} - -void AudioStreamSample::set_loop_begin(int p_frame) { - loop_begin = p_frame; -} - -int AudioStreamSample::get_loop_begin() const { - return loop_begin; -} - -void AudioStreamSample::set_loop_end(int p_frame) { - loop_end = p_frame; -} - -int AudioStreamSample::get_loop_end() const { - return loop_end; -} - -void AudioStreamSample::set_mix_rate(int p_hz) { - ERR_FAIL_COND(p_hz == 0); - mix_rate = p_hz; -} - -int AudioStreamSample::get_mix_rate() const { - return mix_rate; -} - -void AudioStreamSample::set_stereo(bool p_enable) { - stereo = p_enable; -} - -bool AudioStreamSample::is_stereo() const { - return stereo; -} - -float AudioStreamSample::get_length() const { - int len = data_bytes; - switch (format) { - case AudioStreamSample::FORMAT_8_BITS: - len /= 1; - break; - case AudioStreamSample::FORMAT_16_BITS: - len /= 2; - break; - case AudioStreamSample::FORMAT_IMA_ADPCM: - len *= 2; - break; - } - - if (stereo) { - len /= 2; - } - - return float(len) / mix_rate; -} - -bool AudioStreamSample::is_monophonic() const { - return false; -} - -void AudioStreamSample::set_data(const Vector &p_data) { - AudioServer::get_singleton()->lock(); - if (data) { - memfree(data); - data = nullptr; - data_bytes = 0; - } - - int datalen = p_data.size(); - if (datalen) { - const uint8_t *r = p_data.ptr(); - int alloc_len = datalen + DATA_PAD * 2; - data = memalloc(alloc_len); //alloc with some padding for interpolation - memset(data, 0, alloc_len); - uint8_t *dataptr = (uint8_t *)data; - memcpy(dataptr + DATA_PAD, r, datalen); - data_bytes = datalen; - } - - AudioServer::get_singleton()->unlock(); -} - -Vector AudioStreamSample::get_data() const { - Vector pv; - - if (data) { - pv.resize(data_bytes); - { - uint8_t *w = pv.ptrw(); - uint8_t *dataptr = (uint8_t *)data; - memcpy(w, dataptr + DATA_PAD, data_bytes); - } - } - - return pv; -} - -Error AudioStreamSample::save_to_wav(const String &p_path) { - if (format == AudioStreamSample::FORMAT_IMA_ADPCM) { - WARN_PRINT("Saving IMA_ADPC samples are not supported yet"); - return ERR_UNAVAILABLE; - } - - int sub_chunk_2_size = data_bytes; //Subchunk2Size = Size of data in bytes - - // Format code - // 1:PCM format (for 8 or 16 bit) - // 3:IEEE float format - int format_code = (format == FORMAT_IMA_ADPCM) ? 3 : 1; - - int n_channels = stereo ? 2 : 1; - - long sample_rate = mix_rate; - - int byte_pr_sample = 0; - switch (format) { - case AudioStreamSample::FORMAT_8_BITS: - byte_pr_sample = 1; - break; - case AudioStreamSample::FORMAT_16_BITS: - byte_pr_sample = 2; - break; - case AudioStreamSample::FORMAT_IMA_ADPCM: - byte_pr_sample = 4; - break; - } - - String file_path = p_path; - if (!(file_path.substr(file_path.length() - 4, 4) == ".wav")) { - file_path += ".wav"; - } - - Ref file = FileAccess::open(file_path, FileAccess::WRITE); //Overrides existing file if present - - ERR_FAIL_COND_V(file.is_null(), ERR_FILE_CANT_WRITE); - - // Create WAV Header - file->store_string("RIFF"); //ChunkID - file->store_32(sub_chunk_2_size + 36); //ChunkSize = 36 + SubChunk2Size (size of entire file minus the 8 bits for this and previous header) - file->store_string("WAVE"); //Format - file->store_string("fmt "); //Subchunk1ID - file->store_32(16); //Subchunk1Size = 16 - file->store_16(format_code); //AudioFormat - file->store_16(n_channels); //Number of Channels - file->store_32(sample_rate); //SampleRate - file->store_32(sample_rate * n_channels * byte_pr_sample); //ByteRate - file->store_16(n_channels * byte_pr_sample); //BlockAlign = NumChannels * BytePrSample - file->store_16(byte_pr_sample * 8); //BitsPerSample - file->store_string("data"); //Subchunk2ID - file->store_32(sub_chunk_2_size); //Subchunk2Size - - // Add data - Vector data = get_data(); - const uint8_t *read_data = data.ptr(); - switch (format) { - case AudioStreamSample::FORMAT_8_BITS: - for (unsigned int i = 0; i < data_bytes; i++) { - uint8_t data_point = (read_data[i] + 128); - file->store_8(data_point); - } - break; - case AudioStreamSample::FORMAT_16_BITS: - for (unsigned int i = 0; i < data_bytes / 2; i++) { - uint16_t data_point = decode_uint16(&read_data[i * 2]); - file->store_16(data_point); - } - break; - case AudioStreamSample::FORMAT_IMA_ADPCM: - //Unimplemented - break; - } - - return OK; -} - -Ref AudioStreamSample::instantiate_playback() { - Ref sample; - sample.instantiate(); - sample->base = Ref(this); - return sample; -} - -String AudioStreamSample::get_stream_name() const { - return ""; -} - -void AudioStreamSample::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamSample::set_data); - ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamSample::get_data); - - ClassDB::bind_method(D_METHOD("set_format", "format"), &AudioStreamSample::set_format); - ClassDB::bind_method(D_METHOD("get_format"), &AudioStreamSample::get_format); - - ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &AudioStreamSample::set_loop_mode); - ClassDB::bind_method(D_METHOD("get_loop_mode"), &AudioStreamSample::get_loop_mode); - - ClassDB::bind_method(D_METHOD("set_loop_begin", "loop_begin"), &AudioStreamSample::set_loop_begin); - ClassDB::bind_method(D_METHOD("get_loop_begin"), &AudioStreamSample::get_loop_begin); - - ClassDB::bind_method(D_METHOD("set_loop_end", "loop_end"), &AudioStreamSample::set_loop_end); - ClassDB::bind_method(D_METHOD("get_loop_end"), &AudioStreamSample::get_loop_end); - - ClassDB::bind_method(D_METHOD("set_mix_rate", "mix_rate"), &AudioStreamSample::set_mix_rate); - ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioStreamSample::get_mix_rate); - - ClassDB::bind_method(D_METHOD("set_stereo", "stereo"), &AudioStreamSample::set_stereo); - ClassDB::bind_method(D_METHOD("is_stereo"), &AudioStreamSample::is_stereo); - - ClassDB::bind_method(D_METHOD("save_to_wav", "path"), &AudioStreamSample::save_to_wav); - - ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_data", "get_data"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA-ADPCM"), "set_format", "get_format"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "Disabled,Forward,Ping-Pong,Backward"), "set_loop_mode", "get_loop_mode"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_begin"), "set_loop_begin", "get_loop_begin"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_end"), "set_loop_end", "get_loop_end"); - ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_rate"), "set_mix_rate", "get_mix_rate"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stereo"), "set_stereo", "is_stereo"); - - BIND_ENUM_CONSTANT(FORMAT_8_BITS); - BIND_ENUM_CONSTANT(FORMAT_16_BITS); - BIND_ENUM_CONSTANT(FORMAT_IMA_ADPCM); - - BIND_ENUM_CONSTANT(LOOP_DISABLED); - BIND_ENUM_CONSTANT(LOOP_FORWARD); - BIND_ENUM_CONSTANT(LOOP_PINGPONG); - BIND_ENUM_CONSTANT(LOOP_BACKWARD); -} - -AudioStreamSample::AudioStreamSample() {} - -AudioStreamSample::~AudioStreamSample() { - if (data) { - memfree(data); - data = nullptr; - data_bytes = 0; - } -} diff --git a/scene/resources/audio_stream_sample.h b/scene/resources/audio_stream_sample.h deleted file mode 100644 index 2e694cffe2..0000000000 --- a/scene/resources/audio_stream_sample.h +++ /dev/null @@ -1,159 +0,0 @@ -/*************************************************************************/ -/* audio_stream_sample.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* 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. */ -/*************************************************************************/ - -#ifndef AUDIO_STREAM_SAMPLE_H -#define AUDIO_STREAM_SAMPLE_H - -#include "servers/audio/audio_stream.h" - -class AudioStreamSample; - -class AudioStreamPlaybackSample : public AudioStreamPlayback { - GDCLASS(AudioStreamPlaybackSample, AudioStreamPlayback); - enum { - MIX_FRAC_BITS = 13, - MIX_FRAC_LEN = (1 << MIX_FRAC_BITS), - MIX_FRAC_MASK = MIX_FRAC_LEN - 1, - }; - - struct IMA_ADPCM_State { - int16_t step_index = 0; - int32_t predictor = 0; - /* values at loop point */ - int16_t loop_step_index = 0; - int32_t loop_predictor = 0; - int32_t last_nibble = 0; - int32_t loop_pos = 0; - int32_t window_ofs = 0; - } ima_adpcm[2]; - - int64_t offset = 0; - int sign = 1; - bool active = false; - friend class AudioStreamSample; - Ref base; - - template - void do_resample(const Depth *p_src, AudioFrame *p_dst, int64_t &offset, int32_t &increment, uint32_t amount, IMA_ADPCM_State *ima_adpcm); - -public: - virtual void start(float p_from_pos = 0.0) override; - virtual void stop() override; - virtual bool is_playing() const override; - - virtual int get_loop_count() const override; //times it looped - - virtual float get_playback_position() const override; - virtual void seek(float p_time) override; - - virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override; - - virtual void tag_used_streams() override; - - AudioStreamPlaybackSample(); -}; - -class AudioStreamSample : public AudioStream { - GDCLASS(AudioStreamSample, AudioStream); - RES_BASE_EXTENSION("sample") - -public: - enum Format { - FORMAT_8_BITS, - FORMAT_16_BITS, - FORMAT_IMA_ADPCM - }; - - // Keep the ResourceImporterWAV `edit/loop_mode` enum hint in sync with these options. - enum LoopMode { - LOOP_DISABLED, - LOOP_FORWARD, - LOOP_PINGPONG, - LOOP_BACKWARD - }; - -private: - friend class AudioStreamPlaybackSample; - - enum { - DATA_PAD = 16 //padding for interpolation - }; - - Format format = FORMAT_8_BITS; - LoopMode loop_mode = LOOP_DISABLED; - bool stereo = false; - int loop_begin = 0; - int loop_end = 0; - int mix_rate = 44100; - void *data = nullptr; - uint32_t data_bytes = 0; - -protected: - static void _bind_methods(); - -public: - void set_format(Format p_format); - Format get_format() const; - - void set_loop_mode(LoopMode p_loop_mode); - LoopMode get_loop_mode() const; - - void set_loop_begin(int p_frame); - int get_loop_begin() const; - - void set_loop_end(int p_frame); - int get_loop_end() const; - - void set_mix_rate(int p_hz); - int get_mix_rate() const; - - void set_stereo(bool p_enable); - bool is_stereo() const; - - virtual float get_length() const override; //if supported, otherwise return 0 - - virtual bool is_monophonic() const override; - - void set_data(const Vector &p_data); - Vector get_data() const; - - Error save_to_wav(const String &p_path); - - virtual Ref instantiate_playback() override; - virtual String get_stream_name() const override; - - AudioStreamSample(); - ~AudioStreamSample(); -}; - -VARIANT_ENUM_CAST(AudioStreamSample::Format) -VARIANT_ENUM_CAST(AudioStreamSample::LoopMode) - -#endif // AUDIO_STREAM_SAMPLE_H diff --git a/scene/resources/audio_stream_wav.cpp b/scene/resources/audio_stream_wav.cpp new file mode 100644 index 0000000000..a87c8272ea --- /dev/null +++ b/scene/resources/audio_stream_wav.cpp @@ -0,0 +1,667 @@ +/*************************************************************************/ +/* audio_stream_wav.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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. */ +/*************************************************************************/ + +#include "audio_stream_wav.h" + +#include "core/io/file_access.h" +#include "core/io/marshalls.h" + +void AudioStreamPlaybackWAV::start(float p_from_pos) { + if (base->format == AudioStreamWAV::FORMAT_IMA_ADPCM) { + //no seeking in IMA_ADPCM + for (int i = 0; i < 2; i++) { + ima_adpcm[i].step_index = 0; + ima_adpcm[i].predictor = 0; + ima_adpcm[i].loop_step_index = 0; + ima_adpcm[i].loop_predictor = 0; + ima_adpcm[i].last_nibble = -1; + ima_adpcm[i].loop_pos = 0x7FFFFFFF; + ima_adpcm[i].window_ofs = 0; + } + + offset = 0; + } else { + seek(p_from_pos); + } + + sign = 1; + active = true; +} + +void AudioStreamPlaybackWAV::stop() { + active = false; +} + +bool AudioStreamPlaybackWAV::is_playing() const { + return active; +} + +int AudioStreamPlaybackWAV::get_loop_count() const { + return 0; +} + +float AudioStreamPlaybackWAV::get_playback_position() const { + return float(offset >> MIX_FRAC_BITS) / base->mix_rate; +} + +void AudioStreamPlaybackWAV::seek(float p_time) { + if (base->format == AudioStreamWAV::FORMAT_IMA_ADPCM) { + return; //no seeking in ima-adpcm + } + + float max = base->get_length(); + if (p_time < 0) { + p_time = 0; + } else if (p_time >= max) { + p_time = max - 0.001; + } + + offset = uint64_t(p_time * base->mix_rate) << MIX_FRAC_BITS; +} + +template +void AudioStreamPlaybackWAV::do_resample(const Depth *p_src, AudioFrame *p_dst, int64_t &offset, int32_t &increment, uint32_t amount, IMA_ADPCM_State *ima_adpcm) { + // this function will be compiled branchless by any decent compiler + + int32_t final, final_r, next, next_r; + while (amount) { + amount--; + int64_t pos = offset >> MIX_FRAC_BITS; + if (is_stereo && !is_ima_adpcm) { + pos <<= 1; + } + + if (is_ima_adpcm) { + int64_t sample_pos = pos + ima_adpcm[0].window_ofs; + + while (sample_pos > ima_adpcm[0].last_nibble) { + 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 + }; + + for (int i = 0; i < (is_stereo ? 2 : 1); i++) { + int16_t nibble, diff, step; + + ima_adpcm[i].last_nibble++; + const uint8_t *src_ptr = (const uint8_t *)base->data; + src_ptr += AudioStreamWAV::DATA_PAD; + + uint8_t nbb = src_ptr[(ima_adpcm[i].last_nibble >> 1) * (is_stereo ? 2 : 1) + i]; + nibble = (ima_adpcm[i].last_nibble & 1) ? (nbb >> 4) : (nbb & 0xF); + step = _ima_adpcm_step_table[ima_adpcm[i].step_index]; + + ima_adpcm[i].step_index += _ima_adpcm_index_table[nibble]; + if (ima_adpcm[i].step_index < 0) { + ima_adpcm[i].step_index = 0; + } + if (ima_adpcm[i].step_index > 88) { + ima_adpcm[i].step_index = 88; + } + + diff = step >> 3; + if (nibble & 1) { + diff += step >> 2; + } + if (nibble & 2) { + diff += step >> 1; + } + if (nibble & 4) { + diff += step; + } + if (nibble & 8) { + diff = -diff; + } + + ima_adpcm[i].predictor += diff; + if (ima_adpcm[i].predictor < -0x8000) { + ima_adpcm[i].predictor = -0x8000; + } else if (ima_adpcm[i].predictor > 0x7FFF) { + ima_adpcm[i].predictor = 0x7FFF; + } + + /* store loop if there */ + if (ima_adpcm[i].last_nibble == ima_adpcm[i].loop_pos) { + ima_adpcm[i].loop_step_index = ima_adpcm[i].step_index; + ima_adpcm[i].loop_predictor = ima_adpcm[i].predictor; + } + + //printf("%i - %i - pred %i\n",int(ima_adpcm[i].last_nibble),int(nibble),int(ima_adpcm[i].predictor)); + } + } + + final = ima_adpcm[0].predictor; + if (is_stereo) { + final_r = ima_adpcm[1].predictor; + } + + } else { + final = p_src[pos]; + if (is_stereo) { + final_r = p_src[pos + 1]; + } + + if (sizeof(Depth) == 1) { /* conditions will not exist anymore when compiled! */ + final <<= 8; + if (is_stereo) { + final_r <<= 8; + } + } + + if (is_stereo) { + next = p_src[pos + 2]; + next_r = p_src[pos + 3]; + } else { + next = p_src[pos + 1]; + } + + if (sizeof(Depth) == 1) { + next <<= 8; + if (is_stereo) { + next_r <<= 8; + } + } + + int32_t frac = int64_t(offset & MIX_FRAC_MASK); + + final = final + ((next - final) * frac >> MIX_FRAC_BITS); + if (is_stereo) { + final_r = final_r + ((next_r - final_r) * frac >> MIX_FRAC_BITS); + } + } + + if (!is_stereo) { + final_r = final; //copy to right channel if stereo + } + + p_dst->l = final / 32767.0; + p_dst->r = final_r / 32767.0; + p_dst++; + + offset += increment; + } +} + +int AudioStreamPlaybackWAV::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) { + if (!base->data || !active) { + for (int i = 0; i < p_frames; i++) { + p_buffer[i] = AudioFrame(0, 0); + } + return 0; + } + + int len = base->data_bytes; + switch (base->format) { + case AudioStreamWAV::FORMAT_8_BITS: + len /= 1; + break; + case AudioStreamWAV::FORMAT_16_BITS: + len /= 2; + break; + case AudioStreamWAV::FORMAT_IMA_ADPCM: + len *= 2; + break; + } + + if (base->stereo) { + len /= 2; + } + + /* some 64-bit fixed point precaches */ + + int64_t loop_begin_fp = ((int64_t)base->loop_begin << MIX_FRAC_BITS); + int64_t loop_end_fp = ((int64_t)base->loop_end << MIX_FRAC_BITS); + int64_t length_fp = ((int64_t)len << MIX_FRAC_BITS); + int64_t begin_limit = (base->loop_mode != AudioStreamWAV::LOOP_DISABLED) ? loop_begin_fp : 0; + int64_t end_limit = (base->loop_mode != AudioStreamWAV::LOOP_DISABLED) ? loop_end_fp : length_fp; + bool is_stereo = base->stereo; + + int32_t todo = p_frames; + + if (base->loop_mode == AudioStreamWAV::LOOP_BACKWARD) { + sign = -1; + } + + float base_rate = AudioServer::get_singleton()->get_mix_rate(); + float srate = base->mix_rate; + srate *= p_rate_scale; + float playback_speed_scale = AudioServer::get_singleton()->get_playback_speed_scale(); + float fincrement = (srate * playback_speed_scale) / base_rate; + int32_t increment = int32_t(MAX(fincrement * MIX_FRAC_LEN, 1)); + increment *= sign; + + //looping + + AudioStreamWAV::LoopMode loop_format = base->loop_mode; + AudioStreamWAV::Format format = base->format; + + /* audio data */ + + uint8_t *dataptr = (uint8_t *)base->data; + const void *data = dataptr + AudioStreamWAV::DATA_PAD; + AudioFrame *dst_buff = p_buffer; + + if (format == AudioStreamWAV::FORMAT_IMA_ADPCM) { + if (loop_format != AudioStreamWAV::LOOP_DISABLED) { + ima_adpcm[0].loop_pos = loop_begin_fp >> MIX_FRAC_BITS; + ima_adpcm[1].loop_pos = loop_begin_fp >> MIX_FRAC_BITS; + loop_format = AudioStreamWAV::LOOP_FORWARD; + } + } + + while (todo > 0) { + int64_t limit = 0; + int32_t target = 0, aux = 0; + + /** LOOP CHECKING **/ + + if (increment < 0) { + /* going backwards */ + + if (loop_format != AudioStreamWAV::LOOP_DISABLED && offset < loop_begin_fp) { + /* loopstart reached */ + if (loop_format == AudioStreamWAV::LOOP_PINGPONG) { + /* bounce ping pong */ + offset = loop_begin_fp + (loop_begin_fp - offset); + increment = -increment; + sign *= -1; + } else { + /* go to loop-end */ + offset = loop_end_fp - (loop_begin_fp - offset); + } + } else { + /* check for sample not reaching beginning */ + if (offset < 0) { + active = false; + break; + } + } + } else { + /* going forward */ + if (loop_format != AudioStreamWAV::LOOP_DISABLED && offset >= loop_end_fp) { + /* loopend reached */ + + if (loop_format == AudioStreamWAV::LOOP_PINGPONG) { + /* bounce ping pong */ + offset = loop_end_fp - (offset - loop_end_fp); + increment = -increment; + sign *= -1; + } else { + /* go to loop-begin */ + + if (format == AudioStreamWAV::FORMAT_IMA_ADPCM) { + for (int i = 0; i < 2; i++) { + ima_adpcm[i].step_index = ima_adpcm[i].loop_step_index; + ima_adpcm[i].predictor = ima_adpcm[i].loop_predictor; + ima_adpcm[i].last_nibble = loop_begin_fp >> MIX_FRAC_BITS; + } + offset = loop_begin_fp; + } else { + offset = loop_begin_fp + (offset - loop_end_fp); + } + } + } else { + /* no loop, check for end of sample */ + if (offset >= length_fp) { + active = false; + break; + } + } + } + + /** MIXCOUNT COMPUTING **/ + + /* next possible limit (looppoints or sample begin/end */ + limit = (increment < 0) ? begin_limit : end_limit; + + /* compute what is shorter, the todo or the limit? */ + aux = (limit - offset) / increment + 1; + target = (aux < todo) ? aux : todo; /* mix target is the shorter buffer */ + + /* check just in case */ + if (target <= 0) { + active = false; + break; + } + + todo -= target; + + switch (base->format) { + case AudioStreamWAV::FORMAT_8_BITS: { + if (is_stereo) { + do_resample((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm); + } else { + do_resample((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm); + } + } break; + case AudioStreamWAV::FORMAT_16_BITS: { + if (is_stereo) { + do_resample((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm); + } else { + do_resample((int16_t *)data, dst_buff, offset, increment, target, ima_adpcm); + } + + } break; + case AudioStreamWAV::FORMAT_IMA_ADPCM: { + if (is_stereo) { + do_resample((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm); + } else { + do_resample((int8_t *)data, dst_buff, offset, increment, target, ima_adpcm); + } + + } break; + } + + dst_buff += target; + } + + if (todo) { + int mixed_frames = p_frames - todo; + //bit was missing from mix + int todo_ofs = p_frames - todo; + for (int i = todo_ofs; i < p_frames; i++) { + p_buffer[i] = AudioFrame(0, 0); + } + return mixed_frames; + } + return p_frames; +} + +void AudioStreamPlaybackWAV::tag_used_streams() { + base->tag_used(get_playback_position()); +} + +AudioStreamPlaybackWAV::AudioStreamPlaybackWAV() {} + +///////////////////// + +void AudioStreamWAV::set_format(Format p_format) { + format = p_format; +} + +AudioStreamWAV::Format AudioStreamWAV::get_format() const { + return format; +} + +void AudioStreamWAV::set_loop_mode(LoopMode p_loop_mode) { + loop_mode = p_loop_mode; +} + +AudioStreamWAV::LoopMode AudioStreamWAV::get_loop_mode() const { + return loop_mode; +} + +void AudioStreamWAV::set_loop_begin(int p_frame) { + loop_begin = p_frame; +} + +int AudioStreamWAV::get_loop_begin() const { + return loop_begin; +} + +void AudioStreamWAV::set_loop_end(int p_frame) { + loop_end = p_frame; +} + +int AudioStreamWAV::get_loop_end() const { + return loop_end; +} + +void AudioStreamWAV::set_mix_rate(int p_hz) { + ERR_FAIL_COND(p_hz == 0); + mix_rate = p_hz; +} + +int AudioStreamWAV::get_mix_rate() const { + return mix_rate; +} + +void AudioStreamWAV::set_stereo(bool p_enable) { + stereo = p_enable; +} + +bool AudioStreamWAV::is_stereo() const { + return stereo; +} + +float AudioStreamWAV::get_length() const { + int len = data_bytes; + switch (format) { + case AudioStreamWAV::FORMAT_8_BITS: + len /= 1; + break; + case AudioStreamWAV::FORMAT_16_BITS: + len /= 2; + break; + case AudioStreamWAV::FORMAT_IMA_ADPCM: + len *= 2; + break; + } + + if (stereo) { + len /= 2; + } + + return float(len) / mix_rate; +} + +bool AudioStreamWAV::is_monophonic() const { + return false; +} + +void AudioStreamWAV::set_data(const Vector &p_data) { + AudioServer::get_singleton()->lock(); + if (data) { + memfree(data); + data = nullptr; + data_bytes = 0; + } + + int datalen = p_data.size(); + if (datalen) { + const uint8_t *r = p_data.ptr(); + int alloc_len = datalen + DATA_PAD * 2; + data = memalloc(alloc_len); //alloc with some padding for interpolation + memset(data, 0, alloc_len); + uint8_t *dataptr = (uint8_t *)data; + memcpy(dataptr + DATA_PAD, r, datalen); + data_bytes = datalen; + } + + AudioServer::get_singleton()->unlock(); +} + +Vector AudioStreamWAV::get_data() const { + Vector pv; + + if (data) { + pv.resize(data_bytes); + { + uint8_t *w = pv.ptrw(); + uint8_t *dataptr = (uint8_t *)data; + memcpy(w, dataptr + DATA_PAD, data_bytes); + } + } + + return pv; +} + +Error AudioStreamWAV::save_to_wav(const String &p_path) { + if (format == AudioStreamWAV::FORMAT_IMA_ADPCM) { + WARN_PRINT("Saving IMA_ADPC samples are not supported yet"); + return ERR_UNAVAILABLE; + } + + int sub_chunk_2_size = data_bytes; //Subchunk2Size = Size of data in bytes + + // Format code + // 1:PCM format (for 8 or 16 bit) + // 3:IEEE float format + int format_code = (format == FORMAT_IMA_ADPCM) ? 3 : 1; + + int n_channels = stereo ? 2 : 1; + + long sample_rate = mix_rate; + + int byte_pr_sample = 0; + switch (format) { + case AudioStreamWAV::FORMAT_8_BITS: + byte_pr_sample = 1; + break; + case AudioStreamWAV::FORMAT_16_BITS: + byte_pr_sample = 2; + break; + case AudioStreamWAV::FORMAT_IMA_ADPCM: + byte_pr_sample = 4; + break; + } + + String file_path = p_path; + if (!(file_path.substr(file_path.length() - 4, 4) == ".wav")) { + file_path += ".wav"; + } + + Ref file = FileAccess::open(file_path, FileAccess::WRITE); //Overrides existing file if present + + ERR_FAIL_COND_V(file.is_null(), ERR_FILE_CANT_WRITE); + + // Create WAV Header + file->store_string("RIFF"); //ChunkID + file->store_32(sub_chunk_2_size + 36); //ChunkSize = 36 + SubChunk2Size (size of entire file minus the 8 bits for this and previous header) + file->store_string("WAVE"); //Format + file->store_string("fmt "); //Subchunk1ID + file->store_32(16); //Subchunk1Size = 16 + file->store_16(format_code); //AudioFormat + file->store_16(n_channels); //Number of Channels + file->store_32(sample_rate); //SampleRate + file->store_32(sample_rate * n_channels * byte_pr_sample); //ByteRate + file->store_16(n_channels * byte_pr_sample); //BlockAlign = NumChannels * BytePrSample + file->store_16(byte_pr_sample * 8); //BitsPerSample + file->store_string("data"); //Subchunk2ID + file->store_32(sub_chunk_2_size); //Subchunk2Size + + // Add data + Vector data = get_data(); + const uint8_t *read_data = data.ptr(); + switch (format) { + case AudioStreamWAV::FORMAT_8_BITS: + for (unsigned int i = 0; i < data_bytes; i++) { + uint8_t data_point = (read_data[i] + 128); + file->store_8(data_point); + } + break; + case AudioStreamWAV::FORMAT_16_BITS: + for (unsigned int i = 0; i < data_bytes / 2; i++) { + uint16_t data_point = decode_uint16(&read_data[i * 2]); + file->store_16(data_point); + } + break; + case AudioStreamWAV::FORMAT_IMA_ADPCM: + //Unimplemented + break; + } + + return OK; +} + +Ref AudioStreamWAV::instantiate_playback() { + Ref sample; + sample.instantiate(); + sample->base = Ref(this); + return sample; +} + +String AudioStreamWAV::get_stream_name() const { + return ""; +} + +void AudioStreamWAV::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_data", "data"), &AudioStreamWAV::set_data); + ClassDB::bind_method(D_METHOD("get_data"), &AudioStreamWAV::get_data); + + ClassDB::bind_method(D_METHOD("set_format", "format"), &AudioStreamWAV::set_format); + ClassDB::bind_method(D_METHOD("get_format"), &AudioStreamWAV::get_format); + + ClassDB::bind_method(D_METHOD("set_loop_mode", "loop_mode"), &AudioStreamWAV::set_loop_mode); + ClassDB::bind_method(D_METHOD("get_loop_mode"), &AudioStreamWAV::get_loop_mode); + + ClassDB::bind_method(D_METHOD("set_loop_begin", "loop_begin"), &AudioStreamWAV::set_loop_begin); + ClassDB::bind_method(D_METHOD("get_loop_begin"), &AudioStreamWAV::get_loop_begin); + + ClassDB::bind_method(D_METHOD("set_loop_end", "loop_end"), &AudioStreamWAV::set_loop_end); + ClassDB::bind_method(D_METHOD("get_loop_end"), &AudioStreamWAV::get_loop_end); + + ClassDB::bind_method(D_METHOD("set_mix_rate", "mix_rate"), &AudioStreamWAV::set_mix_rate); + ClassDB::bind_method(D_METHOD("get_mix_rate"), &AudioStreamWAV::get_mix_rate); + + ClassDB::bind_method(D_METHOD("set_stereo", "stereo"), &AudioStreamWAV::set_stereo); + ClassDB::bind_method(D_METHOD("is_stereo"), &AudioStreamWAV::is_stereo); + + ClassDB::bind_method(D_METHOD("save_to_wav", "path"), &AudioStreamWAV::save_to_wav); + + ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_data", "get_data"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "format", PROPERTY_HINT_ENUM, "8-Bit,16-Bit,IMA-ADPCM"), "set_format", "get_format"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_mode", PROPERTY_HINT_ENUM, "Disabled,Forward,Ping-Pong,Backward"), "set_loop_mode", "get_loop_mode"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_begin"), "set_loop_begin", "get_loop_begin"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "loop_end"), "set_loop_end", "get_loop_end"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "mix_rate"), "set_mix_rate", "get_mix_rate"); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "stereo"), "set_stereo", "is_stereo"); + + BIND_ENUM_CONSTANT(FORMAT_8_BITS); + BIND_ENUM_CONSTANT(FORMAT_16_BITS); + BIND_ENUM_CONSTANT(FORMAT_IMA_ADPCM); + + BIND_ENUM_CONSTANT(LOOP_DISABLED); + BIND_ENUM_CONSTANT(LOOP_FORWARD); + BIND_ENUM_CONSTANT(LOOP_PINGPONG); + BIND_ENUM_CONSTANT(LOOP_BACKWARD); +} + +AudioStreamWAV::AudioStreamWAV() {} + +AudioStreamWAV::~AudioStreamWAV() { + if (data) { + memfree(data); + data = nullptr; + data_bytes = 0; + } +} diff --git a/scene/resources/audio_stream_wav.h b/scene/resources/audio_stream_wav.h new file mode 100644 index 0000000000..d800388d96 --- /dev/null +++ b/scene/resources/audio_stream_wav.h @@ -0,0 +1,159 @@ +/*************************************************************************/ +/* audio_stream_wav.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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. */ +/*************************************************************************/ + +#ifndef AUDIO_STREAM_WAV_H +#define AUDIO_STREAM_WAV_H + +#include "servers/audio/audio_stream.h" + +class AudioStreamWAV; + +class AudioStreamPlaybackWAV : public AudioStreamPlayback { + GDCLASS(AudioStreamPlaybackWAV, AudioStreamPlayback); + enum { + MIX_FRAC_BITS = 13, + MIX_FRAC_LEN = (1 << MIX_FRAC_BITS), + MIX_FRAC_MASK = MIX_FRAC_LEN - 1, + }; + + struct IMA_ADPCM_State { + int16_t step_index = 0; + int32_t predictor = 0; + /* values at loop point */ + int16_t loop_step_index = 0; + int32_t loop_predictor = 0; + int32_t last_nibble = 0; + int32_t loop_pos = 0; + int32_t window_ofs = 0; + } ima_adpcm[2]; + + int64_t offset = 0; + int sign = 1; + bool active = false; + friend class AudioStreamWAV; + Ref base; + + template + void do_resample(const Depth *p_src, AudioFrame *p_dst, int64_t &offset, int32_t &increment, uint32_t amount, IMA_ADPCM_State *ima_adpcm); + +public: + virtual void start(float p_from_pos = 0.0) override; + virtual void stop() override; + virtual bool is_playing() const override; + + virtual int get_loop_count() const override; //times it looped + + virtual float get_playback_position() const override; + virtual void seek(float p_time) override; + + virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override; + + virtual void tag_used_streams() override; + + AudioStreamPlaybackWAV(); +}; + +class AudioStreamWAV : public AudioStream { + GDCLASS(AudioStreamWAV, AudioStream); + RES_BASE_EXTENSION("sample") + +public: + enum Format { + FORMAT_8_BITS, + FORMAT_16_BITS, + FORMAT_IMA_ADPCM + }; + + // Keep the ResourceImporterWAV `edit/loop_mode` enum hint in sync with these options. + enum LoopMode { + LOOP_DISABLED, + LOOP_FORWARD, + LOOP_PINGPONG, + LOOP_BACKWARD + }; + +private: + friend class AudioStreamPlaybackWAV; + + enum { + DATA_PAD = 16 //padding for interpolation + }; + + Format format = FORMAT_8_BITS; + LoopMode loop_mode = LOOP_DISABLED; + bool stereo = false; + int loop_begin = 0; + int loop_end = 0; + int mix_rate = 44100; + void *data = nullptr; + uint32_t data_bytes = 0; + +protected: + static void _bind_methods(); + +public: + void set_format(Format p_format); + Format get_format() const; + + void set_loop_mode(LoopMode p_loop_mode); + LoopMode get_loop_mode() const; + + void set_loop_begin(int p_frame); + int get_loop_begin() const; + + void set_loop_end(int p_frame); + int get_loop_end() const; + + void set_mix_rate(int p_hz); + int get_mix_rate() const; + + void set_stereo(bool p_enable); + bool is_stereo() const; + + virtual float get_length() const override; //if supported, otherwise return 0 + + virtual bool is_monophonic() const override; + + void set_data(const Vector &p_data); + Vector get_data() const; + + Error save_to_wav(const String &p_path); + + virtual Ref instantiate_playback() override; + virtual String get_stream_name() const override; + + AudioStreamWAV(); + ~AudioStreamWAV(); +}; + +VARIANT_ENUM_CAST(AudioStreamWAV::Format) +VARIANT_ENUM_CAST(AudioStreamWAV::LoopMode) + +#endif // AUDIO_STREAM_WAV_H diff --git a/servers/audio/effects/audio_effect_record.cpp b/servers/audio/effects/audio_effect_record.cpp index a6553e1431..fff6dbc32a 100644 --- a/servers/audio/effects/audio_effect_record.cpp +++ b/servers/audio/effects/audio_effect_record.cpp @@ -199,16 +199,16 @@ bool AudioEffectRecord::is_recording_active() const { return recording_active; } -void AudioEffectRecord::set_format(AudioStreamSample::Format p_format) { +void AudioEffectRecord::set_format(AudioStreamWAV::Format p_format) { format = p_format; } -AudioStreamSample::Format AudioEffectRecord::get_format() const { +AudioStreamWAV::Format AudioEffectRecord::get_format() const { return format; } -Ref AudioEffectRecord::get_recording() const { - AudioStreamSample::Format dst_format = format; +Ref AudioEffectRecord::get_recording() const { + AudioStreamWAV::Format dst_format = format; bool stereo = true; //forcing mono is not implemented Vector dst_data; @@ -216,7 +216,7 @@ Ref AudioEffectRecord::get_recording() const { ERR_FAIL_COND_V(current_instance.is_null(), nullptr); ERR_FAIL_COND_V(current_instance->recording_data.size() == 0, nullptr); - if (dst_format == AudioStreamSample::FORMAT_8_BITS) { + if (dst_format == AudioStreamWAV::FORMAT_8_BITS) { int data_size = current_instance->recording_data.size(); dst_data.resize(data_size); uint8_t *w = dst_data.ptrw(); @@ -225,7 +225,7 @@ Ref AudioEffectRecord::get_recording() const { int8_t v = CLAMP(current_instance->recording_data[i] * 128, -128, 127); w[i] = v; } - } else if (dst_format == AudioStreamSample::FORMAT_16_BITS) { + } else if (dst_format == AudioStreamWAV::FORMAT_16_BITS) { int data_size = current_instance->recording_data.size(); dst_data.resize(data_size * 2); uint8_t *w = dst_data.ptrw(); @@ -234,7 +234,7 @@ Ref AudioEffectRecord::get_recording() const { int16_t v = CLAMP(current_instance->recording_data[i] * 32768, -32768, 32767); encode_uint16(v, &w[i * 2]); } - } else if (dst_format == AudioStreamSample::FORMAT_IMA_ADPCM) { + } else if (dst_format == AudioStreamWAV::FORMAT_IMA_ADPCM) { //byte interleave Vector left; Vector right; @@ -273,12 +273,12 @@ Ref AudioEffectRecord::get_recording() const { ERR_PRINT("Format not implemented."); } - Ref sample; + Ref sample; sample.instantiate(); sample->set_data(dst_data); sample->set_format(dst_format); sample->set_mix_rate(AudioServer::get_singleton()->get_mix_rate()); - sample->set_loop_mode(AudioStreamSample::LOOP_DISABLED); + sample->set_loop_mode(AudioStreamWAV::LOOP_DISABLED); sample->set_loop_begin(0); sample->set_loop_end(0); sample->set_stereo(stereo); @@ -297,6 +297,6 @@ void AudioEffectRecord::_bind_methods() { } AudioEffectRecord::AudioEffectRecord() { - format = AudioStreamSample::FORMAT_16_BITS; + format = AudioStreamWAV::FORMAT_16_BITS; recording_active = false; } diff --git a/servers/audio/effects/audio_effect_record.h b/servers/audio/effects/audio_effect_record.h index 8a6247e27a..8cdf5f6c2d 100644 --- a/servers/audio/effects/audio_effect_record.h +++ b/servers/audio/effects/audio_effect_record.h @@ -35,7 +35,7 @@ #include "core/io/marshalls.h" #include "core/os/os.h" #include "core/os/thread.h" -#include "scene/resources/audio_stream_sample.h" +#include "scene/resources/audio_stream_wav.h" #include "servers/audio/audio_effect.h" #include "servers/audio_server.h" @@ -85,7 +85,7 @@ class AudioEffectRecord : public AudioEffect { bool recording_active; Ref current_instance; - AudioStreamSample::Format format; + AudioStreamWAV::Format format; void ensure_thread_stopped(); @@ -96,9 +96,9 @@ public: Ref instantiate() override; void set_recording_active(bool p_record); bool is_recording_active() const; - void set_format(AudioStreamSample::Format p_format); - AudioStreamSample::Format get_format() const; - Ref get_recording() const; + void set_format(AudioStreamWAV::Format p_format); + AudioStreamWAV::Format get_format() const; + Ref get_recording() const; AudioEffectRecord(); }; diff --git a/servers/audio_server.cpp b/servers/audio_server.cpp index 1054073377..9052f8e05e 100644 --- a/servers/audio_server.cpp +++ b/servers/audio_server.cpp @@ -39,7 +39,7 @@ #include "core/os/os.h" #include "core/string/string_name.h" #include "core/templates/pair.h" -#include "scene/resources/audio_stream_sample.h" +#include "scene/resources/audio_stream_wav.h" #include "servers/audio/audio_driver_dummy.h" #include "servers/audio/effects/audio_effect_compressor.h" diff --git a/servers/audio_server.h b/servers/audio_server.h index 287a18ecde..5613267909 100644 --- a/servers/audio_server.h +++ b/servers/audio_server.h @@ -43,7 +43,7 @@ class AudioDriverDummy; class AudioStream; -class AudioStreamSample; +class AudioStreamWAV; class AudioStreamPlayback; class AudioDriver { -- cgit v1.2.3