audio_effect_pitch_shift.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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;
  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. SMBPitchShift() {
  52. gRover = 0;
  53. memset(gInFIFO, 0, MAX_FRAME_LENGTH * sizeof(float));
  54. memset(gOutFIFO, 0, MAX_FRAME_LENGTH * sizeof(float));
  55. memset(gFFTworksp, 0, 2 * MAX_FRAME_LENGTH * sizeof(float));
  56. memset(gLastPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(float));
  57. memset(gSumPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(float));
  58. memset(gOutputAccum, 0, 2 * MAX_FRAME_LENGTH * sizeof(float));
  59. memset(gAnaFreq, 0, MAX_FRAME_LENGTH * sizeof(float));
  60. memset(gAnaMagn, 0, MAX_FRAME_LENGTH * sizeof(float));
  61. }
  62. };
  63. class AudioEffectPitchShift;
  64. class AudioEffectPitchShiftInstance : public AudioEffectInstance {
  65. GDCLASS(AudioEffectPitchShiftInstance, AudioEffectInstance);
  66. friend class AudioEffectPitchShift;
  67. Ref<AudioEffectPitchShift> base;
  68. int fft_size;
  69. SMBPitchShift shift_l;
  70. SMBPitchShift shift_r;
  71. public:
  72. virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count);
  73. };
  74. class AudioEffectPitchShift : public AudioEffect {
  75. GDCLASS(AudioEffectPitchShift, AudioEffect);
  76. public:
  77. friend class AudioEffectPitchShiftInstance;
  78. enum FFT_Size {
  79. FFT_SIZE_256,
  80. FFT_SIZE_512,
  81. FFT_SIZE_1024,
  82. FFT_SIZE_2048,
  83. FFT_SIZE_4096,
  84. FFT_SIZE_MAX
  85. };
  86. float pitch_scale;
  87. int oversampling;
  88. FFT_Size fft_size;
  89. float wet;
  90. float dry;
  91. bool filter;
  92. protected:
  93. static void _bind_methods();
  94. public:
  95. Ref<AudioEffectInstance> instance();
  96. void set_pitch_scale(float p_pitch_scale);
  97. float get_pitch_scale() const;
  98. void set_oversampling(int p_oversampling);
  99. int get_oversampling() const;
  100. void set_fft_size(FFT_Size);
  101. FFT_Size get_fft_size() const;
  102. AudioEffectPitchShift();
  103. };
  104. VARIANT_ENUM_CAST(AudioEffectPitchShift::FFT_Size);
  105. #endif // AUDIO_EFFECT_PITCH_SHIFT_H