audio_effect_pitch_shift.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. SMBPitchShift shift_l;
  69. SMBPitchShift shift_r;
  70. public:
  71. virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count);
  72. };
  73. class AudioEffectPitchShift : public AudioEffect {
  74. GDCLASS(AudioEffectPitchShift, AudioEffect)
  75. friend class AudioEffectPitchShiftInstance;
  76. float pitch_scale;
  77. int window_size;
  78. float wet;
  79. float dry;
  80. bool filter;
  81. protected:
  82. static void _bind_methods();
  83. public:
  84. Ref<AudioEffectInstance> instance();
  85. void set_pitch_scale(float p_pitch_scale);
  86. float get_pitch_scale() const;
  87. AudioEffectPitchShift();
  88. };
  89. #endif // AUDIO_EFFECT_PITCH_SHIFT_H