Skip to content

Commit

Permalink
mem bug fixes, phaser bypass
Browse files Browse the repository at this point in the history
  • Loading branch information
pio committed Dec 3, 2024
1 parent c1f748b commit 9d3b0c0
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 35 deletions.
7 changes: 3 additions & 4 deletions src/basic_allpass.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ template <int N>
class AudioFilterAllpass
{
public:
~AudioFilterAllpass()
{
if (bf) free(bf);
}
AudioFilterAllpass() { bf = NULL; }
~AudioFilterAllpass() { free(bf); }
/**
* @brief Allocate the filter buffer in RAM
* set the pointer to the allpass coeff
Expand All @@ -28,6 +26,7 @@ class AudioFilterAllpass
*/
bool init(float* coeffPtr)
{
free(bf);
bf = (float *)malloc(N*sizeof(float)); // allocate buffer
if (!bf) return false;
kPtr = coeffPtr;
Expand Down
10 changes: 4 additions & 6 deletions src/basic_delay.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,19 @@


/**
* @brief Basic delay line with buffer placed in PSRAM
* @brief Basic delay line with buffer placed in PSRAM or DMARAM
*
* @tparam N delay length in samples (float)
*/
//template <int N>
class AudioBasicDelay
{
public:
~AudioBasicDelay()
{
if(bf) free(bf);
}
AudioBasicDelay() { bf = NULL; }
~AudioBasicDelay() { free(bf); }
bool init(uint32_t size_samples, bool psram=false)
{
if(bf) free(bf);
free(bf);
use_psram = psram;
size = size_samples;
if (use_psram) bf = (float *)extmem_malloc(size * sizeof(float)); // allocate buffer in PSRAM
Expand Down
3 changes: 2 additions & 1 deletion src/basic_pitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ extern const float music_intevals[]; // semitone intervals -1oct to +2oct
class AudioBasicPitch
{
public:
AudioBasicPitch() { bf = NULL; }
~AudioBasicPitch() { free(bf); }
bool init()
{
outFilter.init(hp_f, (float *)&hp_gain, lp_f, &lp_gain);
Expand All @@ -43,7 +45,6 @@ class AudioBasicPitch
}
void setTone(float t)
{
//lp_f = constrain(t, 0.01f, 1.0f);
lp_gain = constrain(t, 0.0f, 1.0f);
}

Expand Down
12 changes: 6 additions & 6 deletions src/effect_delaystereo_i16.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AudioEffectDelayStereo_i16 : public AudioStream
{
t = constrain(t, 0.0f, 1.0f);
t = t * t;
t = map(t, 0.0f, 1.0f, (float32_t)(dly_length-dly_time_min), 0.0f);
t = map_sat(t, 0.0f, 1.0f, (float32_t)(dly_length-dly_time_min), 0.0f);
__disable_irq();
if (force) dly_time = t;
dly_time_set = t;
Expand Down Expand Up @@ -77,8 +77,8 @@ class AudioEffectDelayStereo_i16 : public AudioStream
if (infinite) return;
float32_t fb, attn;
n = constrain(n, 0.0f, 1.0f);
fb = map(n, 0.0f, 1.0f, 0.0f, feedb_max) * hp_feedb_limit;
attn = map(n*n*n, 0.0f, 1.0f, 1.0f, 0.4f);
fb = map_sat(n, 0.0f, 1.0f, 0.0f, feedb_max) * hp_feedb_limit;
attn = map_sat(n*n*n, 0.0f, 1.0f, 1.0f, 0.4f);
inputGain_tmp = attn;
__disable_irq();
feedb = fb;
Expand All @@ -95,7 +95,7 @@ class AudioEffectDelayStereo_i16 : public AudioStream
{
n = constrain(n, 0.0f, 1.0f);
n = 2.0f * n - (n*n);
n = map (n, 0.0f, 1.0f, 10.0f, 0.3f);
n = map_sat(n, 0.0f, 1.0f, 10.0f, 0.3f);
__disable_irq();
dly_time_step = n;
__enable_irq();
Expand Down Expand Up @@ -190,7 +190,7 @@ class AudioEffectDelayStereo_i16 : public AudioStream
void mod_rate(float32_t r)
{
r = constrain(r*r*r, 0.0f, 1.0f);
r = map(r, 0.0f, 1.0f, 0.0f, lfo_fmax);
r = map_sat(r, 0.0f, 1.0f, 0.0f, lfo_fmax);
__disable_irq();
lfo.setRate(r);
__enable_irq();
Expand All @@ -203,7 +203,7 @@ class AudioEffectDelayStereo_i16 : public AudioStream
void mod_depth(float32_t d)
{
d = constrain(d, 0.0f, 1.0f);
d = map(d, 0.0f, 1.0f, 0.0f, lfo_ampl_max);
d = map_sat(d, 0.0f, 1.0f, 0.0f, lfo_ampl_max);
__disable_irq();
lfo.setDepth(d);
__enable_irq();
Expand Down
16 changes: 10 additions & 6 deletions src/effect_phaserStereo_i16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,14 @@ void AudioEffectPhaserStereo_i16::update()

if (!bypass_process(&blockL, &blockR, BYPASS_MODE_PASS, bp))
return;

if (bp)
{
transmit(blockL, 0);
transmit(blockR, 1);
release(blockL);
release(blockR);
return;
}
blockMod = receiveReadOnly(2); // bipolar/int16_t control input
if (!blockMod) internalLFO = true; // no modulation input provided -> use internal LFO

Expand Down Expand Up @@ -117,9 +124,6 @@ void AudioEffectPhaserStereo_i16::update()
modSigL = modSigL * _lfo_scaler + _lfo_bias;
modSigR = modSigR * _lfo_scaler + _lfo_bias;




drySigL = ((float32_t)blockL->data[i] / 32768.0f) * (1.0f - abs(fdb)*0.25f); // attenuate the input if using feedback
inSigL = drySigL + last_sampleL * fdb;
drySigR = ((float32_t)blockR->data[i] / 32768.0f) * (1.0f - abs(fdb)*0.25f);
Expand Down Expand Up @@ -207,7 +211,7 @@ bool AudioEffectPhaserStereo_i16::bypass_process(audio_block_t** p_blockL, audio
*p_blockL = allocate(); // try to allocate a new block
if( !*p_blockL)
{
if (*p_blockR) release(*p_blockR); // if the Rch is present, release/discard it
if (*p_blockR) release(*p_blockR); // if the Rch is present, release/discard it
result = false;
break; // operation failed due to no audio memory left
}
Expand All @@ -218,7 +222,7 @@ bool AudioEffectPhaserStereo_i16::bypass_process(audio_block_t** p_blockL, audio
*p_blockR = allocate();
if( !*p_blockR) // no memory for a new block, but we might have L channel zeroed
{
if (*p_blockL) release(*p_blockL); // blockL is available and contains audio. Discard it
if (*p_blockL) release(*p_blockL); // blockL is available and contains audio. Discard it
result = false;
break; // and sigbnal failed operation
}
Expand Down
21 changes: 19 additions & 2 deletions src/effect_phaserStereo_i16.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,22 @@ class AudioEffectPhaserStereo_i16 : public AudioStream
/**
* @brief Controls the feedback parameter
*
* @param fdb ffedback value in range 0.0f to 1.0f
* @param fdb ffedback value in range -1.0f to 1.0f
*/
void feedback(float32_t fdb)
{
feedb = constrain(fdb, -1.0f, 1.0f);
float t = constrain(fdb, -1.0f, 1.0f);
t = fabs(t);
t = 2*t - (t*t);

if (fdb < 0.0f)
{
t *= -1.0;
}
t = t * feedb_max;
__disable_irq()
feedb = t;
__enable_irq();
}
/**
* @brief Dry / Wet mixer ratio. Classic Phaser sound uses 0.5f for 50% dry and 50%Wet
Expand All @@ -161,7 +172,9 @@ class AudioEffectPhaserStereo_i16 : public AudioStream
*/
void mix(float32_t ratio)
{
__disable_irq()
mix_ratio = constrain(ratio, 0.0f, 1.0f);
__enable_irq();
}
/**
* @brief Sets the number of stages used in the phaser
Expand All @@ -173,7 +186,9 @@ class AudioEffectPhaserStereo_i16 : public AudioStream
{
if (st && st == ((st >> 1) << 1) && st <= PHASER_STEREO_STAGES) // only 2, 4, 6, 8, 12 allowed
{
__disable_irq()
stg = st;
__enable_irq();
}
}
/**
Expand All @@ -183,6 +198,7 @@ class AudioEffectPhaserStereo_i16 : public AudioStream
*/
void bypass_set(bool state) {bp = state;}
bool bypass_tgl(void) {bp ^= 1; return bp;}
bool bypass_get() { return bp;}

private:
uint8_t stg; // number of stages
Expand All @@ -192,6 +208,7 @@ class AudioEffectPhaserStereo_i16 : public AudioStream
float32_t allpass_y[2][PHASER_STEREO_STAGES]; // allpass outputs
float32_t mix_ratio; // 0 = dry. 1.0 = wet
float32_t feedb; // feedback
static constexpr float32_t feedb_max = 0.95f;
float32_t last_sampleL;
float32_t last_sampleR;
uint32_t lfo_phase_acc; // interfnal lfo
Expand Down
3 changes: 1 addition & 2 deletions src/effect_platereverb_i16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,13 @@ bool AudioEffectPlateReverb_i16::begin()

flags.bypass = 1;
flags.freeze = 0;
initialised = true;
return true;
}

void AudioEffectPlateReverb_i16::update()
{
#if defined(__IMXRT1062__)
if (!initialised) return;
if (!initialized) return;
audio_block_t *blockL, *blockR;
float sampleL, sampleR;
int16_t i;
Expand Down
6 changes: 3 additions & 3 deletions src/effect_platereverb_i16.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
class AudioEffectPlateReverb_i16 : public AudioStream
{
public:
AudioEffectPlateReverb_i16() : AudioStream(2, inputQueueArray) { begin();}
AudioEffectPlateReverb_i16() : AudioStream(2, inputQueueArray) { initialized = begin();}
~AudioEffectPlateReverb_i16(){};
virtual void update();

Expand Down Expand Up @@ -395,7 +395,7 @@ class AudioEffectPlateReverb_i16 : public AudioStream
pitchRatio = s;
__enable_irq();
}
friend bool bypass_process(AudioStream* stream, audio_block_t** p_blockL, audio_block_t** p_blockR, bypass_mode_t mode, bool state);
bool isInitialized() { return initialized;}
private:
struct flags_t
{
Expand Down Expand Up @@ -507,7 +507,7 @@ class AudioEffectPlateReverb_i16 : public AudioStream
const float freeze_lodamp_k = 0.0f;
const float freeze_hidamp_k = 1.0f;

bool initialised = false;
bool initialized = false;
uint16_t block_size = AUDIO_BLOCK_SAMPLES;

bool bypass_process(audio_block_t** p_blockL, audio_block_t** p_blockR, bypass_mode_t mode, bool state);
Expand Down
8 changes: 4 additions & 4 deletions src/effect_reverbsc_i16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ AudioEffectReverbSC_i16::AudioEffectReverbSC_i16(bool use_psram) : AudioStream(2
if (external_psram_size == 0)
{
flags.mem_fail = 1;
initialised = true;
initialized = true;
return;
}
aux_ = (float32_t *) extmem_malloc(aux_size_bytes);
#else
flags.mem_fail = 1;
initialised = true;
initialized = true;
return;
#endif
}
Expand All @@ -82,7 +82,7 @@ AudioEffectReverbSC_i16::AudioEffectReverbSC_i16(bool use_psram) : AudioStream(2
}
mix(0.5f);

initialised = true;
initialized = true;
}

static int DelayLineMaxSamples(float32_t sr, float32_t i_pitch_mod, int n)
Expand Down Expand Up @@ -168,7 +168,7 @@ void AudioEffectReverbSC_i16::update()
int buffer_size; /* Local copy */
float32_t damp_fact = damp_fact_;

if (!initialised) return;
if (!initialized) return;
// special case if memory allocation failed, pass the input signal directly to the output
if (flags.mem_fail) bp_mode = BYPASS_MODE_PASS;

Expand Down
2 changes: 1 addition & 1 deletion src/effect_reverbsc_i16.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ class AudioEffectReverbSC_i16 : public AudioStream
float32_t i_pitch_mod_;
float32_t sample_rate_;
float32_t damp_fact_, damp_fact_tmp;
bool initialised = false;
bool initialized = false;
ReverbScDl_t delay_lines_[8];
float32_t *aux_; // main delay line storage buffer, placed either in RAM2 or PSRAM
const uint32_t aux_size_bytes = REVERBSC_I16_DLYBUF_SIZE*sizeof(float32_t);
Expand Down

0 comments on commit 9d3b0c0

Please sign in to comment.