audio_effect_pitch_shift.h 4.1 KB

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