audio_effect_pitch_shift.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifndef AUDIO_EFFECT_PITCH_SHIFT_H
  31. #define AUDIO_EFFECT_PITCH_SHIFT_H
  32. #include "servers/audio/audio_effect.h"
  33. class SMBPitchShift {
  34. enum {
  35. MAX_FRAME_LENGTH = 8192
  36. };
  37. float gInFIFO[MAX_FRAME_LENGTH] = {};
  38. float gOutFIFO[MAX_FRAME_LENGTH] = {};
  39. float gFFTworksp[2 * MAX_FRAME_LENGTH] = {};
  40. float gLastPhase[MAX_FRAME_LENGTH / 2 + 1] = {};
  41. float gSumPhase[MAX_FRAME_LENGTH / 2 + 1] = {};
  42. float gOutputAccum[2 * MAX_FRAME_LENGTH] = {};
  43. float gAnaFreq[MAX_FRAME_LENGTH] = {};
  44. float gAnaMagn[MAX_FRAME_LENGTH] = {};
  45. float gSynFreq[MAX_FRAME_LENGTH] = {};
  46. float gSynMagn[MAX_FRAME_LENGTH] = {};
  47. long gRover = 0;
  48. void smbFft(float *fftBuffer, long fftFrameSize, long sign);
  49. public:
  50. void PitchShift(float pitchShift, long numSampsToProcess, long fftFrameSize, long osamp, float sampleRate, float *indata, float *outdata, int stride);
  51. };
  52. class AudioEffectPitchShift;
  53. class AudioEffectPitchShiftInstance : public AudioEffectInstance {
  54. GDCLASS(AudioEffectPitchShiftInstance, AudioEffectInstance);
  55. friend class AudioEffectPitchShift;
  56. Ref<AudioEffectPitchShift> base;
  57. int fft_size = 0;
  58. SMBPitchShift shift_l;
  59. SMBPitchShift shift_r;
  60. public:
  61. virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) override;
  62. };
  63. class AudioEffectPitchShift : public AudioEffect {
  64. GDCLASS(AudioEffectPitchShift, AudioEffect);
  65. public:
  66. friend class AudioEffectPitchShiftInstance;
  67. enum FFTSize : unsigned int {
  68. FFT_SIZE_256,
  69. FFT_SIZE_512,
  70. FFT_SIZE_1024,
  71. FFT_SIZE_2048,
  72. FFT_SIZE_4096,
  73. FFT_SIZE_MAX
  74. };
  75. float pitch_scale = 1.0;
  76. int oversampling = 4;
  77. FFTSize fft_size = FFT_SIZE_2048;
  78. float wet = 0.0;
  79. float dry = 0.0;
  80. bool filter = false;
  81. protected:
  82. static void _bind_methods();
  83. public:
  84. Ref<AudioEffectInstance> instantiate() override;
  85. void set_pitch_scale(float p_pitch_scale);
  86. float get_pitch_scale() const;
  87. void set_oversampling(int p_oversampling);
  88. int get_oversampling() const;
  89. void set_fft_size(FFTSize);
  90. FFTSize get_fft_size() const;
  91. };
  92. VARIANT_ENUM_CAST(AudioEffectPitchShift::FFTSize);
  93. #endif // AUDIO_EFFECT_PITCH_SHIFT_H