diff options
Diffstat (limited to 'servers/audio')
| -rw-r--r-- | servers/audio/audio_stream.cpp | 370 | ||||
| -rw-r--r-- | servers/audio/audio_stream.h | 77 | ||||
| -rw-r--r-- | servers/audio/effects/audio_effect_delay.cpp | 3 | ||||
| -rw-r--r-- | servers/audio/effects/audio_effect_pitch_shift.cpp | 96 | ||||
| -rw-r--r-- | servers/audio/effects/audio_effect_pitch_shift.h | 41 | ||||
| -rw-r--r-- | servers/audio/effects/audio_effect_stereo_enhance.cpp | 1 |
6 files changed, 463 insertions, 125 deletions
diff --git a/servers/audio/audio_stream.cpp b/servers/audio/audio_stream.cpp index 170a7ef967..39060286a4 100644 --- a/servers/audio/audio_stream.cpp +++ b/servers/audio/audio_stream.cpp @@ -351,100 +351,372 @@ AudioStreamPlaybackMicrophone::AudioStreamPlaybackMicrophone() { //////////////////////////////// -void AudioStreamRandomPitch::set_audio_stream(const Ref<AudioStream> &p_audio_stream) { - audio_stream = p_audio_stream; - if (audio_stream.is_valid()) { - for (Set<AudioStreamPlaybackRandomPitch *>::Element *E = playbacks.front(); E; E = E->next()) { - E->get()->playback = audio_stream->instance_playback(); - } +void AudioStreamRandomizer::add_stream(int p_index) { + if (p_index < 0) { + p_index = audio_stream_pool.size(); + } + ERR_FAIL_COND(p_index > audio_stream_pool.size()); + PoolEntry entry{ nullptr, 1.0f }; + audio_stream_pool.insert(p_index, entry); + emit_signal(SNAME("changed")); + notify_property_list_changed(); +} + +void AudioStreamRandomizer::move_stream(int p_index_from, int p_index_to) { + ERR_FAIL_COND(p_index_from < 0); + ERR_FAIL_COND(p_index_from >= audio_stream_pool.size()); + ERR_FAIL_COND(p_index_to < 0); + ERR_FAIL_COND(p_index_to > audio_stream_pool.size()); + audio_stream_pool.insert(p_index_to, audio_stream_pool[p_index_from]); + // If 'from' is strictly after 'to' we need to increment the index by one because of the insertion. + if (p_index_from > p_index_to) { + p_index_from++; } + audio_stream_pool.remove_at(p_index_from); + emit_signal(SNAME("changed")); + notify_property_list_changed(); +} + +void AudioStreamRandomizer::remove_stream(int p_index) { + ERR_FAIL_COND(p_index < 0); + ERR_FAIL_COND(p_index >= audio_stream_pool.size()); + audio_stream_pool.remove_at(p_index); + emit_signal(SNAME("changed")); + notify_property_list_changed(); +} + +void AudioStreamRandomizer::set_stream(int p_index, Ref<AudioStream> p_stream) { + ERR_FAIL_COND(p_index < 0); + ERR_FAIL_COND(p_index >= audio_stream_pool.size()); + audio_stream_pool.write[p_index].stream = p_stream; + emit_signal(SNAME("changed")); } -Ref<AudioStream> AudioStreamRandomPitch::get_audio_stream() const { - return audio_stream; +Ref<AudioStream> AudioStreamRandomizer::get_stream(int p_index) const { + ERR_FAIL_COND_V(p_index < 0, nullptr); + ERR_FAIL_COND_V(p_index >= audio_stream_pool.size(), nullptr); + return audio_stream_pool[p_index].stream; } -void AudioStreamRandomPitch::set_random_pitch(float p_pitch) { +void AudioStreamRandomizer::set_stream_probability_weight(int p_index, float p_weight) { + ERR_FAIL_COND(p_index < 0); + ERR_FAIL_COND(p_index >= audio_stream_pool.size()); + audio_stream_pool.write[p_index].weight = p_weight; + emit_signal(SNAME("changed")); +} + +float AudioStreamRandomizer::get_stream_probability_weight(int p_index) const { + ERR_FAIL_COND_V(p_index < 0, 0); + ERR_FAIL_COND_V(p_index >= audio_stream_pool.size(), 0); + return audio_stream_pool[p_index].weight; +} + +void AudioStreamRandomizer::set_streams_count(int p_count) { + audio_stream_pool.resize(p_count); +} + +int AudioStreamRandomizer::get_streams_count() const { + return audio_stream_pool.size(); +} + +void AudioStreamRandomizer::set_random_pitch(float p_pitch) { if (p_pitch < 1) { p_pitch = 1; } - random_pitch = p_pitch; + random_pitch_scale = p_pitch; +} + +float AudioStreamRandomizer::get_random_pitch() const { + return random_pitch_scale; +} + +void AudioStreamRandomizer::set_random_volume_offset_db(float p_volume_offset_db) { + if (p_volume_offset_db < 0) { + p_volume_offset_db = 0; + } + random_volume_offset_db = p_volume_offset_db; +} + +float AudioStreamRandomizer::get_random_volume_offset_db() const { + return random_volume_offset_db; } -float AudioStreamRandomPitch::get_random_pitch() const { - return random_pitch; +void AudioStreamRandomizer::set_playback_mode(PlaybackMode p_playback_mode) { + playback_mode = p_playback_mode; } -Ref<AudioStreamPlayback> AudioStreamRandomPitch::instance_playback() { - Ref<AudioStreamPlaybackRandomPitch> playback; +AudioStreamRandomizer::PlaybackMode AudioStreamRandomizer::get_playback_mode() const { + return playback_mode; +} + +Ref<AudioStreamPlayback> AudioStreamRandomizer::instance_playback_random() { + Ref<AudioStreamPlaybackRandomizer> playback; playback.instantiate(); - if (audio_stream.is_valid()) { - playback->playback = audio_stream->instance_playback(); + playbacks.insert(playback.ptr()); + playback->randomizer = Ref<AudioStreamRandomizer>((AudioStreamRandomizer *)this); + + double total_weight = 0; + Vector<PoolEntry> local_pool; + for (const PoolEntry &entry : audio_stream_pool) { + if (entry.stream.is_valid() && entry.weight > 0) { + local_pool.push_back(entry); + total_weight += entry.weight; + } + } + if (local_pool.is_empty()) { + return playback; } + double chosen_cumulative_weight = Math::random(0.0, total_weight); + double cumulative_weight = 0; + for (PoolEntry &entry : local_pool) { + cumulative_weight += entry.weight; + if (cumulative_weight > chosen_cumulative_weight) { + playback->playback = entry.stream->instance_playback(); + last_playback = entry.stream; + break; + } + } + if (playback->playback.is_null()) { + // This indicates a floating point error. Take the last element. + last_playback = local_pool[local_pool.size() - 1].stream; + playback->playback = local_pool.write[local_pool.size() - 1].stream->instance_playback(); + } + return playback; +} + +Ref<AudioStreamPlayback> AudioStreamRandomizer::instance_playback_no_repeats() { + Ref<AudioStreamPlaybackRandomizer> playback; + double total_weight = 0; + Vector<PoolEntry> local_pool; + for (const PoolEntry &entry : audio_stream_pool) { + if (entry.stream == last_playback) { + continue; + } + if (entry.stream.is_valid() && entry.weight > 0) { + local_pool.push_back(entry); + total_weight += entry.weight; + } + } + if (local_pool.is_empty()) { + playback = instance_playback_random(); + WARN_PRINT("Playback stream pool is too small to prevent repeats."); + return playback; + } + + playback.instantiate(); playbacks.insert(playback.ptr()); - playback->random_pitch = Ref<AudioStreamRandomPitch>((AudioStreamRandomPitch *)this); + playback->randomizer = Ref<AudioStreamRandomizer>((AudioStreamRandomizer *)this); + double chosen_cumulative_weight = Math::random(0.0, total_weight); + double cumulative_weight = 0; + for (PoolEntry &entry : local_pool) { + cumulative_weight += entry.weight; + if (cumulative_weight > chosen_cumulative_weight) { + last_playback = entry.stream; + playback->playback = entry.stream->instance_playback(); + break; + } + } + if (playback->playback.is_null()) { + // This indicates a floating point error. Take the last element. + last_playback = local_pool[local_pool.size() - 1].stream; + playback->playback = local_pool.write[local_pool.size() - 1].stream->instance_playback(); + } return playback; } -String AudioStreamRandomPitch::get_stream_name() const { - if (audio_stream.is_valid()) { - return "Random: " + audio_stream->get_name(); +Ref<AudioStreamPlayback> AudioStreamRandomizer::instance_playback_sequential() { + Ref<AudioStreamPlaybackRandomizer> playback; + playback.instantiate(); + playbacks.insert(playback.ptr()); + playback->randomizer = Ref<AudioStreamRandomizer>((AudioStreamRandomizer *)this); + + Vector<Ref<AudioStream>> local_pool; + for (const PoolEntry &entry : audio_stream_pool) { + if (entry.stream.is_null()) { + continue; + } + if (local_pool.find(entry.stream) != -1) { + WARN_PRINT("Duplicate stream in sequential playback pool"); + continue; + } + local_pool.push_back(entry.stream); + } + if (local_pool.is_empty()) { + return playback; } - return "RandomPitch"; + bool found_last_stream = false; + for (Ref<AudioStream> &entry : local_pool) { + if (found_last_stream) { + last_playback = entry; + playback->playback = entry->instance_playback(); + break; + } + if (entry == last_playback) { + found_last_stream = true; + } + } + if (playback->playback.is_null()) { + // Wrap around + last_playback = local_pool[0]; + playback->playback = local_pool.write[0]->instance_playback(); + } + return playback; } -float AudioStreamRandomPitch::get_length() const { - if (audio_stream.is_valid()) { - return audio_stream->get_length(); +Ref<AudioStreamPlayback> AudioStreamRandomizer::instance_playback() { + switch (playback_mode) { + case PLAYBACK_RANDOM: + return instance_playback_random(); + case PLAYBACK_RANDOM_NO_REPEATS: + return instance_playback_no_repeats(); + case PLAYBACK_SEQUENTIAL: + return instance_playback_sequential(); + default: + ERR_FAIL_V_MSG(nullptr, "Unhandled playback mode."); } +} +String AudioStreamRandomizer::get_stream_name() const { + return "Randomizer"; +} + +float AudioStreamRandomizer::get_length() const { return 0; } -bool AudioStreamRandomPitch::is_monophonic() const { - if (audio_stream.is_valid()) { - return audio_stream->is_monophonic(); +bool AudioStreamRandomizer::is_monophonic() const { + for (const PoolEntry &entry : audio_stream_pool) { + if (entry.stream.is_valid() && entry.stream->is_monophonic()) { + return true; + } + } + return false; +} + +bool AudioStreamRandomizer::_get(const StringName &p_name, Variant &r_ret) const { + if (AudioStream::_get(p_name, r_ret)) { + return true; } + Vector<String> components = String(p_name).split("/", true, 2); + if (components.size() == 2 && components[0].begins_with("stream_") && components[0].trim_prefix("stream_").is_valid_int()) { + int index = components[0].trim_prefix("stream_").to_int(); + if (index < 0 || index >= (int)audio_stream_pool.size()) { + return false; + } - return true; // It doesn't really matter what we return here, but no sense instancing a many playbacks of a null stream. + if (components[1] == "stream") { + r_ret = get_stream(index); + return true; + } else if (components[1] == "weight") { + r_ret = get_stream_probability_weight(index); + return true; + } else { + return false; + } + } + return false; +} + +bool AudioStreamRandomizer::_set(const StringName &p_name, const Variant &p_value) { + if (AudioStream::_set(p_name, p_value)) { + return true; + } + Vector<String> components = String(p_name).split("/", true, 2); + if (components.size() == 2 && components[0].begins_with("stream_") && components[0].trim_prefix("stream_").is_valid_int()) { + int index = components[0].trim_prefix("stream_").to_int(); + if (index < 0 || index >= (int)audio_stream_pool.size()) { + return false; + } + + if (components[1] == "stream") { + set_stream(index, p_value); + return true; + } else if (components[1] == "weight") { + set_stream_probability_weight(index, p_value); + return true; + } else { + return false; + } + } + return false; +} + +void AudioStreamRandomizer::_get_property_list(List<PropertyInfo> *p_list) const { + AudioStream::_get_property_list(p_list); // Define the trivial scalar properties. + p_list->push_back(PropertyInfo(Variant::NIL, "Streams", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_GROUP)); + for (int i = 0; i < audio_stream_pool.size(); i++) { + p_list->push_back(PropertyInfo(Variant::OBJECT, vformat("stream_%d/stream", i), PROPERTY_HINT_RESOURCE_TYPE, "AudioStream")); + p_list->push_back(PropertyInfo(Variant::FLOAT, vformat("stream_%d/weight", i), PROPERTY_HINT_RANGE, "0,100,0.001,or_greater")); + } } -void AudioStreamRandomPitch::_bind_methods() { - ClassDB::bind_method(D_METHOD("set_audio_stream", "stream"), &AudioStreamRandomPitch::set_audio_stream); - ClassDB::bind_method(D_METHOD("get_audio_stream"), &AudioStreamRandomPitch::get_audio_stream); +void AudioStreamRandomizer::_bind_methods() { + ClassDB::bind_method(D_METHOD("add_stream", "index"), &AudioStreamRandomizer::add_stream); + ClassDB::bind_method(D_METHOD("move_stream", "index_from", "index_to"), &AudioStreamRandomizer::move_stream); + ClassDB::bind_method(D_METHOD("remove_stream", "index"), &AudioStreamRandomizer::remove_stream); + + ClassDB::bind_method(D_METHOD("set_stream", "index", "stream"), &AudioStreamRandomizer::set_stream); + ClassDB::bind_method(D_METHOD("get_stream", "index"), &AudioStreamRandomizer::get_stream); + ClassDB::bind_method(D_METHOD("set_stream_probability_weight", "index", "weight"), &AudioStreamRandomizer::set_stream_probability_weight); + ClassDB::bind_method(D_METHOD("get_stream_probability_weight", "index"), &AudioStreamRandomizer::get_stream_probability_weight); + + ClassDB::bind_method(D_METHOD("set_streams_count", "count"), &AudioStreamRandomizer::set_streams_count); + ClassDB::bind_method(D_METHOD("get_streams_count"), &AudioStreamRandomizer::get_streams_count); + + ClassDB::bind_method(D_METHOD("set_random_pitch", "scale"), &AudioStreamRandomizer::set_random_pitch); + ClassDB::bind_method(D_METHOD("get_random_pitch"), &AudioStreamRandomizer::get_random_pitch); - ClassDB::bind_method(D_METHOD("set_random_pitch", "scale"), &AudioStreamRandomPitch::set_random_pitch); - ClassDB::bind_method(D_METHOD("get_random_pitch"), &AudioStreamRandomPitch::get_random_pitch); + ClassDB::bind_method(D_METHOD("set_random_volume_offset_db", "db_offset"), &AudioStreamRandomizer::set_random_volume_offset_db); + ClassDB::bind_method(D_METHOD("get_random_volume_offset_db"), &AudioStreamRandomizer::get_random_volume_offset_db); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "audio_stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"), "set_audio_stream", "get_audio_stream"); + ClassDB::bind_method(D_METHOD("set_playback_mode", "mode"), &AudioStreamRandomizer::set_playback_mode); + ClassDB::bind_method(D_METHOD("get_playback_mode"), &AudioStreamRandomizer::get_playback_mode); + + ADD_ARRAY("streams", "stream_"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "streams_count", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_streams_count", "get_streams_count"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_mode", PROPERTY_HINT_ENUM, "Random (Avoid Repeats),Random,Sequential"), "set_playback_mode", "get_playback_mode"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "random_pitch", PROPERTY_HINT_RANGE, "1,16,0.01"), "set_random_pitch", "get_random_pitch"); + ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "random_volume_offset_db", PROPERTY_HINT_RANGE, "0,40,0"), "set_random_volume_offset_db", "get_random_volume_offset_db"); + + BIND_ENUM_CONSTANT(PLAYBACK_RANDOM_NO_REPEATS); + BIND_ENUM_CONSTANT(PLAYBACK_RANDOM); + BIND_ENUM_CONSTANT(PLAYBACK_SEQUENTIAL); } -AudioStreamRandomPitch::AudioStreamRandomPitch() { - random_pitch = 1.1; +AudioStreamRandomizer::AudioStreamRandomizer() { + random_pitch_scale = 1.1; + random_volume_offset_db = 5; } -void AudioStreamPlaybackRandomPitch::start(float p_from_pos) { +void AudioStreamPlaybackRandomizer::start(float p_from_pos) { playing = playback; - float range_from = 1.0 / random_pitch->random_pitch; - float range_to = random_pitch->random_pitch; + { + float range_from = 1.0 / randomizer->random_pitch_scale; + float range_to = randomizer->random_pitch_scale; - pitch_scale = range_from + Math::randf() * (range_to - range_from); + pitch_scale = range_from + Math::randf() * (range_to - range_from); + } + { + float range_from = -randomizer->random_volume_offset_db; + float range_to = randomizer->random_volume_offset_db; + + float volume_offset_db = range_from + Math::randf() * (range_to - range_from); + volume_scale = Math::db2linear(volume_offset_db); + } if (playing.is_valid()) { playing->start(p_from_pos); } } -void AudioStreamPlaybackRandomPitch::stop() { +void AudioStreamPlaybackRandomizer::stop() { if (playing.is_valid()) { playing->stop(); - ; } } -bool AudioStreamPlaybackRandomPitch::is_playing() const { +bool AudioStreamPlaybackRandomizer::is_playing() const { if (playing.is_valid()) { return playing->is_playing(); } @@ -452,7 +724,7 @@ bool AudioStreamPlaybackRandomPitch::is_playing() const { return false; } -int AudioStreamPlaybackRandomPitch::get_loop_count() const { +int AudioStreamPlaybackRandomizer::get_loop_count() const { if (playing.is_valid()) { return playing->get_loop_count(); } @@ -460,7 +732,7 @@ int AudioStreamPlaybackRandomPitch::get_loop_count() const { return 0; } -float AudioStreamPlaybackRandomPitch::get_playback_position() const { +float AudioStreamPlaybackRandomizer::get_playback_position() const { if (playing.is_valid()) { return playing->get_playback_position(); } @@ -468,13 +740,13 @@ float AudioStreamPlaybackRandomPitch::get_playback_position() const { return 0; } -void AudioStreamPlaybackRandomPitch::seek(float p_time) { +void AudioStreamPlaybackRandomizer::seek(float p_time) { if (playing.is_valid()) { playing->seek(p_time); } } -int AudioStreamPlaybackRandomPitch::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) { +int AudioStreamPlaybackRandomizer::mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) { if (playing.is_valid()) { return playing->mix(p_buffer, p_rate_scale * pitch_scale, p_frames); } else { @@ -485,7 +757,7 @@ int AudioStreamPlaybackRandomPitch::mix(AudioFrame *p_buffer, float p_rate_scale } } -AudioStreamPlaybackRandomPitch::~AudioStreamPlaybackRandomPitch() { - random_pitch->playbacks.erase(this); +AudioStreamPlaybackRandomizer::~AudioStreamPlaybackRandomizer() { + randomizer->playbacks.erase(this); } ///////////////////////////////////////////// diff --git a/servers/audio/audio_stream.h b/servers/audio/audio_stream.h index 32159e96ef..ce9bcabb9c 100644 --- a/servers/audio/audio_stream.h +++ b/servers/audio/audio_stream.h @@ -167,43 +167,86 @@ public: // -class AudioStreamPlaybackRandomPitch; +class AudioStreamPlaybackRandomizer; -class AudioStreamRandomPitch : public AudioStream { - GDCLASS(AudioStreamRandomPitch, AudioStream); - friend class AudioStreamPlaybackRandomPitch; +class AudioStreamRandomizer : public AudioStream { + GDCLASS(AudioStreamRandomizer, AudioStream); - Set<AudioStreamPlaybackRandomPitch *> playbacks; - Ref<AudioStream> audio_stream; - float random_pitch; +public: + enum PlaybackMode { + PLAYBACK_RANDOM_NO_REPEATS, + PLAYBACK_RANDOM, + PLAYBACK_SEQUENTIAL, + }; + +private: + friend class AudioStreamPlaybackRandomizer; + + struct PoolEntry { + Ref<AudioStream> stream; + float weight; + }; + + Set<AudioStreamPlaybackRandomizer *> playbacks; + Vector<PoolEntry> audio_stream_pool; + float random_pitch_scale; + float random_volume_offset_db; + + Ref<AudioStreamPlayback> instance_playback_random(); + Ref<AudioStreamPlayback> instance_playback_no_repeats(); + Ref<AudioStreamPlayback> instance_playback_sequential(); + + Ref<AudioStream> last_playback = nullptr; + PlaybackMode playback_mode = PLAYBACK_RANDOM_NO_REPEATS; protected: static void _bind_methods(); + bool _set(const StringName &p_name, const Variant &p_value); + bool _get(const StringName &p_name, Variant &r_ret) const; + void _get_property_list(List<PropertyInfo> *p_list) const; + public: - void set_audio_stream(const Ref<AudioStream> &p_audio_stream); - Ref<AudioStream> get_audio_stream() const; + void add_stream(int p_index); + void move_stream(int p_index_from, int p_index_to); + void remove_stream(int p_index); + + void set_stream(int p_index, Ref<AudioStream> p_stream); + Ref<AudioStream> get_stream(int p_index) const; + void set_stream_probability_weight(int p_index, float p_weight); + float get_stream_probability_weight(int p_index) const; - void set_random_pitch(float p_pitch); + void set_streams_count(int p_count); + int get_streams_count() const; + + void set_random_pitch(float p_pitch_scale); float get_random_pitch() const; + void set_random_volume_offset_db(float p_volume_offset_db); + float get_random_volume_offset_db() const; + + void set_playback_mode(PlaybackMode p_playback_mode); + PlaybackMode get_playback_mode() const; + virtual Ref<AudioStreamPlayback> instance_playback() override; virtual String get_stream_name() const override; virtual float get_length() const override; //if supported, otherwise return 0 virtual bool is_monophonic() const override; - AudioStreamRandomPitch(); + AudioStreamRandomizer(); }; -class AudioStreamPlaybackRandomPitch : public AudioStreamPlayback { - GDCLASS(AudioStreamPlaybackRandomPitch, AudioStreamPlayback); - friend class AudioStreamRandomPitch; +class AudioStreamPlaybackRandomizer : public AudioStreamPlayback { + GDCLASS(AudioStreamPlaybackRandomizer, AudioStreamPlayback); + friend class AudioStreamRandomizer; - Ref<AudioStreamRandomPitch> random_pitch; + Ref<AudioStreamRandomizer> randomizer; Ref<AudioStreamPlayback> playback; Ref<AudioStreamPlayback> playing; + float pitch_scale; + float volume_scale; public: virtual void start(float p_from_pos = 0.0) override; @@ -217,7 +260,9 @@ public: virtual int mix(AudioFrame *p_buffer, float p_rate_scale, int p_frames) override; - ~AudioStreamPlaybackRandomPitch(); + ~AudioStreamPlaybackRandomizer(); }; +VARIANT_ENUM_CAST(AudioStreamRandomizer::PlaybackMode); + #endif // AUDIO_STREAM_H diff --git a/servers/audio/effects/audio_effect_delay.cpp b/servers/audio/effects/audio_effect_delay.cpp index bddd235204..1909ab6eae 100644 --- a/servers/audio/effects/audio_effect_delay.cpp +++ b/servers/audio/effects/audio_effect_delay.cpp @@ -54,15 +54,12 @@ void AudioEffectDelayInstance::_process_chunk(const AudioFrame *p_src_frames, Au float tap_1_level_f = base->tap_1_active ? Math::db2linear(base->tap_1_level) : 0.0; int tap_1_delay_frames = int((base->tap_1_delay_ms / 1000.0) * mix_rate); - ; float tap_2_level_f = base->tap_2_active ? Math::db2linear(base->tap_2_level) : 0.0; int tap_2_delay_frames = int((base->tap_2_delay_ms / 1000.0) * mix_rate); - ; float feedback_level_f = base->feedback_active ? Math::db2linear(base->feedback_level) : 0.0; unsigned int feedback_delay_frames = int((base->feedback_delay_ms / 1000.0) * mix_rate); - ; AudioFrame tap1_vol = AudioFrame(tap_1_level_f, tap_1_level_f); diff --git a/servers/audio/effects/audio_effect_pitch_shift.cpp b/servers/audio/effects/audio_effect_pitch_shift.cpp index 3c53887931..ba2d257c0a 100644 --- a/servers/audio/effects/audio_effect_pitch_shift.cpp +++ b/servers/audio/effects/audio_effect_pitch_shift.cpp @@ -74,7 +74,7 @@ * *****************************************************************************/ -void SMBPitchShift::PitchShift(float pitchShift, long numSampsToProcess, long fftFrameSize, long osamp, float sampleRate, float *indata, float *outdata,int stride) { +void SMBPitchShift::PitchShift(float pitchShift, int64_t numSampsToProcess, int64_t fftFrameSize, int64_t osamp, float sampleRate, float *indata, float *outdata,int stride) { /* @@ -85,19 +85,32 @@ void SMBPitchShift::PitchShift(float pitchShift, long numSampsToProcess, long ff */ double magn, phase, tmp, window, real, imag; - double freqPerBin, expct; - long i,k, qpd, index, inFifoLatency, stepSize, fftFrameSize2; + double freqPerBin, expct, reciprocalFftFrameSize; + int64_t i,k, qpd, index, inFifoLatency, stepSize, fftFrameSize2; /* set up some handy variables */ fftFrameSize2 = fftFrameSize/2; + reciprocalFftFrameSize = 1./fftFrameSize; stepSize = fftFrameSize/osamp; - freqPerBin = sampleRate/(double)fftFrameSize; - expct = 2.*Math_PI*(double)stepSize/(double)fftFrameSize; + freqPerBin = reciprocalFftFrameSize * sampleRate; + expct = Math_TAU * reciprocalFftFrameSize * stepSize; inFifoLatency = fftFrameSize-stepSize; - if (gRover == 0) { gRover = inFifoLatency; -} + if (gRover == 0) { + gRover = inFifoLatency; + } - /* initialize our static arrays */ + // If pitchShift changes clear arrays to prevent some artifacts and quality loss. + if (lastPitchShift != pitchShift) { + lastPitchShift = pitchShift; + memset(gInFIFO, 0, MAX_FRAME_LENGTH * sizeof(float)); + memset(gOutFIFO, 0, MAX_FRAME_LENGTH * sizeof(float)); + memset(gFFTworksp, 0, 2 * MAX_FRAME_LENGTH * sizeof(double)); + memset(gLastPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(double)); + memset(gSumPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(double)); + memset(gOutputAccum, 0, 2 * MAX_FRAME_LENGTH * sizeof(double)); + memset(gAnaFreq, 0, MAX_FRAME_LENGTH * sizeof(double)); + memset(gAnaMagn, 0, MAX_FRAME_LENGTH * sizeof(double)); + } /* main processing loop */ for (i = 0; i < numSampsToProcess; i++){ @@ -112,7 +125,7 @@ void SMBPitchShift::PitchShift(float pitchShift, long numSampsToProcess, long ff /* do windowing and re,im interleave */ for (k = 0; k < fftFrameSize;k++) { - window = -.5*cos(2.*Math_PI*(double)k/(double)fftFrameSize)+.5; + window = -.5*cos(Math_TAU * reciprocalFftFrameSize * k)+.5; gFFTworksp[2*k] = gInFIFO[k] * window; gFFTworksp[2*k+1] = 0.; } @@ -124,6 +137,7 @@ void SMBPitchShift::PitchShift(float pitchShift, long numSampsToProcess, long ff /* this is the analysis step */ for (k = 0; k <= fftFrameSize2; k++) { + /* de-interlace FFT buffer */ real = gFFTworksp[2*k]; imag = gFFTworksp[2*k+1]; @@ -141,13 +155,15 @@ void SMBPitchShift::PitchShift(float pitchShift, long numSampsToProcess, long ff /* map delta phase into +/- Pi interval */ qpd = tmp/Math_PI; - if (qpd >= 0) { qpd += qpd&1; - } else { qpd -= qpd&1; -} + if (qpd >= 0) { + qpd += qpd&1; + } else { + qpd -= qpd&1; + } tmp -= Math_PI*(double)qpd; /* get deviation from bin frequency from the +/- Pi interval */ - tmp = osamp*tmp/(2.*Math_PI); + tmp = osamp*tmp/Math_TAU; /* compute the k-th partials' true frequency */ tmp = (double)k*freqPerBin + tmp*freqPerBin; @@ -160,8 +176,8 @@ void SMBPitchShift::PitchShift(float pitchShift, long numSampsToProcess, long ff /* ***************** PROCESSING ******************* */ /* this does the actual pitch shifting */ - memset(gSynMagn, 0, fftFrameSize*sizeof(float)); - memset(gSynFreq, 0, fftFrameSize*sizeof(float)); + memset(gSynMagn, 0, fftFrameSize*sizeof(double)); + memset(gSynFreq, 0, fftFrameSize*sizeof(double)); for (k = 0; k <= fftFrameSize2; k++) { index = k*pitchShift; if (index <= fftFrameSize2) { @@ -184,7 +200,7 @@ void SMBPitchShift::PitchShift(float pitchShift, long numSampsToProcess, long ff tmp /= freqPerBin; /* take osamp into account */ - tmp = 2.*Math_PI*tmp/osamp; + tmp = Math_TAU*tmp/osamp; /* add the overlap phase advance back in */ tmp += (double)k*expct; @@ -199,33 +215,35 @@ void SMBPitchShift::PitchShift(float pitchShift, long numSampsToProcess, long ff } /* zero negative frequencies */ - for (k = fftFrameSize+2; k < 2*fftFrameSize; k++) { gFFTworksp[k] = 0.; -} + for (k = fftFrameSize+2; k < 2*MAX_FRAME_LENGTH; k++) { + gFFTworksp[k] = 0.; + } /* do inverse transform */ smbFft(gFFTworksp, fftFrameSize, 1); /* do windowing and add to output accumulator */ for(k=0; k < fftFrameSize; k++) { - window = -.5*cos(2.*Math_PI*(double)k/(double)fftFrameSize)+.5; + window = -.5*cos(Math_TAU * reciprocalFftFrameSize * k)+.5; gOutputAccum[k] += 2.*window*gFFTworksp[2*k]/(fftFrameSize2*osamp); } - for (k = 0; k < stepSize; k++) { gOutFIFO[k] = gOutputAccum[k]; -} + for (k = 0; k < stepSize; k++) { + gOutFIFO[k] = gOutputAccum[k]; + } /* shift accumulator */ - memmove(gOutputAccum, gOutputAccum+stepSize, fftFrameSize*sizeof(float)); + memmove(gOutputAccum, gOutputAccum+stepSize, fftFrameSize*sizeof(double)); /* move input FIFO */ - for (k = 0; k < inFifoLatency; k++) { gInFIFO[k] = gInFIFO[k+stepSize]; -} + for (k = 0; k < inFifoLatency; k++) { + gInFIFO[k] = gInFIFO[k+stepSize]; + } } } } - -void SMBPitchShift::smbFft(float *fftBuffer, long fftFrameSize, long sign) +void SMBPitchShift::smbFft(double *fftBuffer, int64_t fftFrameSize, int64_t sign) /* FFT routine, (C)1996 S.M.Bernsee. Sign = -1 is FFT, 1 is iFFT (inverse) Fills fftBuffer[0...2*fftFrameSize-1] with the Fourier transform of the @@ -238,14 +256,16 @@ void SMBPitchShift::smbFft(float *fftBuffer, long fftFrameSize, long sign) of the frequencies of interest is in fftBuffer[0...fftFrameSize]. */ { - float wr, wi, arg, *p1, *p2, temp; - float tr, ti, ur, ui, *p1r, *p1i, *p2r, *p2i; - long i, bitm, j, le, le2, k; + double wr, wi, arg, *p1, *p2, temp; + double tr, ti, ur, ui, *p1r, *p1i, *p2r, *p2i; + int64_t i, bitm, j, le, le2, k, logN; + logN = (int64_t)(log(fftFrameSize) / log(2.) + .5); for (i = 2; i < 2*fftFrameSize-2; i += 2) { for (bitm = 2, j = 0; bitm < 2*fftFrameSize; bitm <<= 1) { - if (i & bitm) { j++; -} + if (i & bitm) { + j++; + } j <<= 1; } if (i < j) { @@ -255,7 +275,8 @@ void SMBPitchShift::smbFft(float *fftBuffer, long fftFrameSize, long sign) *p1 = *p2; *p2 = temp; } } - for (k = 0, le = 2; k < (long)(log((double)fftFrameSize)/log(2.)+.5); k++) { + + for (k = 0, le = 2; k < logN; k++) { le <<= 1; le2 = le>>1; ur = 1.0; @@ -288,6 +309,14 @@ void SMBPitchShift::smbFft(float *fftBuffer, long fftFrameSize, long sign) void AudioEffectPitchShiftInstance::process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) { float sample_rate = AudioServer::get_singleton()->get_mix_rate(); + // For pitch_scale 1.0 it's cheaper to just pass samples without processing them. + if (Math::is_equal_approx(base->pitch_scale, 1.0f)) { + for (int i = 0; i < p_frame_count; i++) { + p_dst_frames[i] = p_src_frames[i]; + } + return; + } + float *in_l = (float *)p_src_frames; float *in_r = in_l + 1; @@ -361,7 +390,4 @@ AudioEffectPitchShift::AudioEffectPitchShift() { pitch_scale = 1.0; oversampling = 4; fft_size = FFT_SIZE_2048; - wet = 0.0; - dry = 0.0; - filter = false; } diff --git a/servers/audio/effects/audio_effect_pitch_shift.h b/servers/audio/effects/audio_effect_pitch_shift.h index 0478d05ceb..23da61bb32 100644 --- a/servers/audio/effects/audio_effect_pitch_shift.h +++ b/servers/audio/effects/audio_effect_pitch_shift.h @@ -40,31 +40,33 @@ class SMBPitchShift { float gInFIFO[MAX_FRAME_LENGTH]; float gOutFIFO[MAX_FRAME_LENGTH]; - float gFFTworksp[2 * MAX_FRAME_LENGTH]; - float gLastPhase[MAX_FRAME_LENGTH / 2 + 1]; - float gSumPhase[MAX_FRAME_LENGTH / 2 + 1]; - float gOutputAccum[2 * MAX_FRAME_LENGTH]; - float gAnaFreq[MAX_FRAME_LENGTH]; - float gAnaMagn[MAX_FRAME_LENGTH]; - float gSynFreq[MAX_FRAME_LENGTH]; - float gSynMagn[MAX_FRAME_LENGTH]; - long gRover; - - void smbFft(float *fftBuffer, long fftFrameSize, long sign); + double gFFTworksp[2 * MAX_FRAME_LENGTH]; + double gLastPhase[MAX_FRAME_LENGTH / 2 + 1]; + double gSumPhase[MAX_FRAME_LENGTH / 2 + 1]; + double gOutputAccum[2 * MAX_FRAME_LENGTH]; + double gAnaFreq[MAX_FRAME_LENGTH]; + double gAnaMagn[MAX_FRAME_LENGTH]; + double gSynFreq[MAX_FRAME_LENGTH]; + double gSynMagn[MAX_FRAME_LENGTH]; + int64_t gRover; + float lastPitchShift; + + void smbFft(double *fftBuffer, int64_t fftFrameSize, int64_t sign); public: - void PitchShift(float pitchShift, long numSampsToProcess, long fftFrameSize, long osamp, float sampleRate, float *indata, float *outdata, int stride); + void PitchShift(float pitchShift, int64_t numSampsToProcess, int64_t fftFrameSize, int64_t osamp, float sampleRate, float *indata, float *outdata, int stride); SMBPitchShift() { gRover = 0; memset(gInFIFO, 0, MAX_FRAME_LENGTH * sizeof(float)); memset(gOutFIFO, 0, MAX_FRAME_LENGTH * sizeof(float)); - memset(gFFTworksp, 0, 2 * MAX_FRAME_LENGTH * sizeof(float)); - memset(gLastPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(float)); - memset(gSumPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(float)); - memset(gOutputAccum, 0, 2 * MAX_FRAME_LENGTH * sizeof(float)); - memset(gAnaFreq, 0, MAX_FRAME_LENGTH * sizeof(float)); - memset(gAnaMagn, 0, MAX_FRAME_LENGTH * sizeof(float)); + memset(gFFTworksp, 0, 2 * MAX_FRAME_LENGTH * sizeof(double)); + memset(gLastPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(double)); + memset(gSumPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(double)); + memset(gOutputAccum, 0, 2 * MAX_FRAME_LENGTH * sizeof(double)); + memset(gAnaFreq, 0, MAX_FRAME_LENGTH * sizeof(double)); + memset(gAnaMagn, 0, MAX_FRAME_LENGTH * sizeof(double)); + lastPitchShift = 1.0; } }; @@ -101,9 +103,6 @@ public: float pitch_scale; int oversampling; FFTSize fft_size; - float wet; - float dry; - bool filter; protected: static void _bind_methods(); diff --git a/servers/audio/effects/audio_effect_stereo_enhance.cpp b/servers/audio/effects/audio_effect_stereo_enhance.cpp index 757edd6d43..c81efc55e2 100644 --- a/servers/audio/effects/audio_effect_stereo_enhance.cpp +++ b/servers/audio/effects/audio_effect_stereo_enhance.cpp @@ -61,7 +61,6 @@ void AudioEffectStereoEnhanceInstance::process(const AudioFrame *p_src_frames, A //r is delayed r = delay_ringbuff[(ringbuff_pos - delay_frames) & ringbuff_mask]; - ; } p_dst_frames[i].l = l; |