0.2.0-dev Download ZIP
111 lines ·4.1 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* audio_effect_pitch_shift.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include "servers/audio/audio_effect.h" |
| 34 | |
| 35 | class SMBPitchShift { |
| 36 | enum { |
| 37 | MAX_FRAME_LENGTH = 8192 |
| 38 | }; |
| 39 | |
| 40 | float gInFIFO[MAX_FRAME_LENGTH] = {}; |
| 41 | float gOutFIFO[MAX_FRAME_LENGTH] = {}; |
| 42 | float gFFTworksp[2 * MAX_FRAME_LENGTH] = {}; |
| 43 | float gLastPhase[MAX_FRAME_LENGTH / 2 + 1] = {}; |
| 44 | float gSumPhase[MAX_FRAME_LENGTH / 2 + 1] = {}; |
| 45 | float gOutputAccum[2 * MAX_FRAME_LENGTH] = {}; |
| 46 | float gAnaFreq[MAX_FRAME_LENGTH] = {}; |
| 47 | float gAnaMagn[MAX_FRAME_LENGTH] = {}; |
| 48 | float gSynFreq[MAX_FRAME_LENGTH] = {}; |
| 49 | float gSynMagn[MAX_FRAME_LENGTH] = {}; |
| 50 | long gRover = 0; |
| 51 | |
| 52 | void smbFft(float *fftBuffer, long fftFrameSize, long sign); |
| 53 | |
| 54 | public: |
| 55 | void PitchShift(float pitchShift, long numSampsToProcess, long fftFrameSize, long osamp, float sampleRate, float *indata, float *outdata, int stride); |
| 56 | }; |
| 57 | |
| 58 | class AudioEffectPitchShift; |
| 59 | |
| 60 | class AudioEffectPitchShiftInstance : public AudioEffectInstance { |
| 61 | GDCLASS(AudioEffectPitchShiftInstance, AudioEffectInstance); |
| 62 | friend class AudioEffectPitchShift; |
| 63 | Ref<AudioEffectPitchShift> base; |
| 64 | |
| 65 | int fft_size = 0; |
| 66 | SMBPitchShift shift_l; |
| 67 | SMBPitchShift shift_r; |
| 68 | |
| 69 | public: |
| 70 | virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) override; |
| 71 | }; |
| 72 | |
| 73 | class AudioEffectPitchShift : public AudioEffect { |
| 74 | GDCLASS(AudioEffectPitchShift, AudioEffect); |
| 75 | |
| 76 | public: |
| 77 | friend class AudioEffectPitchShiftInstance; |
| 78 | |
| 79 | enum FFTSize : unsigned int { |
| 80 | FFT_SIZE_256, |
| 81 | FFT_SIZE_512, |
| 82 | FFT_SIZE_1024, |
| 83 | FFT_SIZE_2048, |
| 84 | FFT_SIZE_4096, |
| 85 | FFT_SIZE_MAX |
| 86 | }; |
| 87 | |
| 88 | float pitch_scale = 1.0; |
| 89 | int oversampling = 4; |
| 90 | FFTSize fft_size = FFT_SIZE_2048; |
| 91 | float wet = 0.0; |
| 92 | float dry = 0.0; |
| 93 | bool filter = false; |
| 94 | |
| 95 | protected: |
| 96 | static void _bind_methods(); |
| 97 | |
| 98 | public: |
| 99 | Ref<AudioEffectInstance> instantiate() override; |
| 100 | |
| 101 | void set_pitch_scale(float p_pitch_scale); |
| 102 | float get_pitch_scale() const; |
| 103 | |
| 104 | void set_oversampling(int p_oversampling); |
| 105 | int get_oversampling() const; |
| 106 | |
| 107 | void set_fft_size(FFTSize); |
| 108 | FFTSize get_fft_size() const; |
| 109 | }; |
| 110 | |
| 111 | VARIANT_ENUM_CAST(AudioEffectPitchShift::FFTSize); |