10#ifndef DSY_PITCHSHIFTER_H
11#define DSY_PITCHSHIFTER_H
17#include "Utility/dsp.h"
18#include "Utility/delayline.h"
19#include "Control/phasor.h"
24#define SHIFT_BUFFER_SIZE 16384
31static inline uint32_t hash_xs32(uint32_t x)
39inline uint32_t myrand()
41 static uint32_t seed = 1;
42 seed = hash_xs32(seed);
73 force_recalc_ =
false;
77 for(uint8_t i = 0; i < 2; i++)
81 phs_[i].
Init(sr, 50, i == 0 ? 0 : PI_F);
84 del_size_ = SHIFT_BUFFER_SIZE;
93 float val, fade1, fade2;
97 if(prev_phs_a_ > fade1)
99 mod_a_amt_ = fun_ * ((float)(myrand() % 255) / 255.0f)
100 * (del_size_ * 0.5f);
102 = 0.0002f + (((float)(myrand() % 255) / 255.0f) * 0.001f);
104 if(prev_phs_b_ > fade2)
106 mod_b_amt_ = fun_ * ((float)(myrand() % 255) / 255.0f)
107 * (del_size_ * 0.5f);
109 = 0.0002f + (((float)(myrand() % 255) / 255.0f) * 0.001f);
111 slewed_mod_[0] += mod_coeff_[0] * (mod_a_amt_ - slewed_mod_[0]);
112 slewed_mod_[1] += mod_coeff_[1] * (mod_b_amt_ - slewed_mod_[1]);
117 fade1 = 1.0f - fade1;
118 fade2 = 1.0f - fade2;
120 mod_[0] = fade1 * (del_size_ - 1);
121 mod_[1] = fade2 * (del_size_ - 1);
123 gain_[0] = arm_sin_f32(fade1 * (
float)M_PI);
124 gain_[1] = arm_sin_f32(fade2 * (
float)M_PI);
126 gain_[0] = sinf(fade1 * PI_F);
127 gain_[1] = sinf(fade2 * PI_F);
135 d_[0].
SetDelay(mod_[0] + mod_a_amt_);
136 d_[1].
SetDelay(mod_[1] + mod_b_amt_);
137 d_[0].
SetDelay(mod_[0] + slewed_mod_[0]);
138 d_[1].
SetDelay(mod_[1] + slewed_mod_[1]);
140 val += (d_[0].
Read() * gain_[0]);
141 val += (d_[1].
Read() * gain_[1]);
151 if(transpose_ != transpose || force_recalc_)
153 transpose_ = transpose;
154 idx = (uint8_t)fabsf(transpose);
155 ratio = semitone_ratios_[idx % 12];
156 ratio *= (uint8_t)(fabsf(transpose) / 12) + 1;
165 mod_freq_ = ((ratio - 1.0f) * sr_) / del_size_;
174 force_recalc_ =
false;
183 del_size_ = size < SHIFT_BUFFER_SIZE ? size : SHIFT_BUFFER_SIZE;
184 force_recalc_ =
true;
190 inline void SetFun(
float f) { fun_ = f; }
193 inline void SetSemitones()
195 for(
size_t i = 0; i < 12; i++)
197 semitone_ratios_[i] = powf(2.0f, (
float)i / 12);
200 typedef DelayLine<float, SHIFT_BUFFER_SIZE> ShiftDelay;
202 float pitch_shift_, mod_freq_;
210 float gain_[2], mod_[2], transpose_;
211 float fun_, mod_a_amt_, mod_b_amt_, prev_phs_a_, prev_phs_b_;
212 float slewed_mod_[2], mod_coeff_[2];
215 float semitone_ratios_[12];
void SetDelay(size_t delay)
Definition delayline.h:51
const T Read() const
Definition delayline.h:78
void Write(const T sample)
Definition delayline.h:70
void Init()
Definition delayline.h:35
float Process()
Definition phasor.cpp:13
void SetFreq(float freq)
Definition phasor.cpp:7
void Init(float sample_rate, float freq, float initial_phase)
Definition phasor.h:34
Definition pitchshifter.h:65
void Init(float sr)
Definition pitchshifter.h:71
void SetTransposition(const float &transpose)
Definition pitchshifter.h:147
float Process(float &in)
Definition pitchshifter.h:91
void SetDelSize(uint32_t size)
Definition pitchshifter.h:181
void SetFun(float f)
Definition pitchshifter.h:190
FIR Filter implementation, generic and ARM CMSIS DSP based.
Definition adenv.h:16