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#ifndef AUDIO_EFFECT_PITCH_SHIFT_H
32#define AUDIO_EFFECT_PITCH_SHIFT_H
33
34#include "servers/audio/audio_effect.h"
35
36class SMBPitchShift {
37 enum {
38 MAX_FRAME_LENGTH = 8192
39 };
40
41 float gInFIFO[MAX_FRAME_LENGTH];
42 float gOutFIFO[MAX_FRAME_LENGTH];
43 float gFFTworksp[2 * MAX_FRAME_LENGTH];
44 float gLastPhase[MAX_FRAME_LENGTH / 2 + 1];
45 float gSumPhase[MAX_FRAME_LENGTH / 2 + 1];
46 float gOutputAccum[2 * MAX_FRAME_LENGTH];
47 float gAnaFreq[MAX_FRAME_LENGTH];
48 float gAnaMagn[MAX_FRAME_LENGTH];
49 float gSynFreq[MAX_FRAME_LENGTH];
50 float gSynMagn[MAX_FRAME_LENGTH];
51 long gRover;
52
53 void smbFft(float *fftBuffer, long fftFrameSize, long sign);
54
55public:
56 void PitchShift(float pitchShift, long numSampsToProcess, long fftFrameSize, long osamp, float sampleRate, float *indata, float *outdata, int stride);
57
58 SMBPitchShift() {
59 gRover = 0;
60 memset(gInFIFO, 0, MAX_FRAME_LENGTH * sizeof(float));
61 memset(gOutFIFO, 0, MAX_FRAME_LENGTH * sizeof(float));
62 memset(gFFTworksp, 0, 2 * MAX_FRAME_LENGTH * sizeof(float));
63 memset(gLastPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(float));
64 memset(gSumPhase, 0, (MAX_FRAME_LENGTH / 2 + 1) * sizeof(float));
65 memset(gOutputAccum, 0, 2 * MAX_FRAME_LENGTH * sizeof(float));
66 memset(gAnaFreq, 0, MAX_FRAME_LENGTH * sizeof(float));
67 memset(gAnaMagn, 0, MAX_FRAME_LENGTH * sizeof(float));
68 }
69};
70
71class AudioEffectPitchShift;
72
73class AudioEffectPitchShiftInstance : public AudioEffectInstance {
74 GDCLASS(AudioEffectPitchShiftInstance, AudioEffectInstance);
75 friend class AudioEffectPitchShift;
76 Ref<AudioEffectPitchShift> base;
77
78 int fft_size;
79 SMBPitchShift shift_l;
80 SMBPitchShift shift_r;
81
82public:
83 virtual void process(const AudioFrame *p_src_frames, AudioFrame *p_dst_frames, int p_frame_count) override;
84};
85
86class AudioEffectPitchShift : public AudioEffect {
87 GDCLASS(AudioEffectPitchShift, AudioEffect);
88
89public:
90 friend class AudioEffectPitchShiftInstance;
91
92 enum FFTSize {
93 FFT_SIZE_256,
94 FFT_SIZE_512,
95 FFT_SIZE_1024,
96 FFT_SIZE_2048,
97 FFT_SIZE_4096,
98 FFT_SIZE_MAX
99 };
100
101 float pitch_scale;
102 int oversampling;
103 FFTSize fft_size;
104 float wet;
105 float dry;
106 bool filter;
107
108protected:
109 static void _bind_methods();
110
111public:
112 Ref<AudioEffectInstance> instantiate() override;
113
114 void set_pitch_scale(float p_pitch_scale);
115 float get_pitch_scale() const;
116
117 void set_oversampling(int p_oversampling);
118 int get_oversampling() const;
119
120 void set_fft_size(FFTSize);
121 FFTSize get_fft_size() const;
122
123 AudioEffectPitchShift();
124};
125
126VARIANT_ENUM_CAST(AudioEffectPitchShift::FFTSize);
127
128#endif // AUDIO_EFFECT_PITCH_SHIFT_H
129