From 89cd9cf06924887297787d102c380ec98dbc8bc6 Mon Sep 17 00:00:00 2001 From: Holger Wirtz Date: Mon, 29 Nov 2021 11:11:02 +0100 Subject: [PATCH] Fixed stereo panning. --- MicroDexed.ino | 2 +- effect_stereo_panorama.cpp | 29 ++++++++++++++++++++--------- effect_stereo_panorama.h | 7 +++++-- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/MicroDexed.ino b/MicroDexed.ino index 750fa1c..5c22454 100644 --- a/MicroDexed.ino +++ b/MicroDexed.ino @@ -5,7 +5,7 @@ Dexed ist heavily based on https://github.com/google/music-synthesizer-for-android (c)2018-2021 H. Wirtz - M. Koslowski + (c)2021 M. Koslowski This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/effect_stereo_panorama.cpp b/effect_stereo_panorama.cpp index 11c3841..846142f 100644 --- a/effect_stereo_panorama.cpp +++ b/effect_stereo_panorama.cpp @@ -68,7 +68,19 @@ inline float mapfloat(float val, float in_min, float in_max, float out_min, floa void AudioEffectStereoPanorama::panorama(float p) { - pan = mapfloat(p, -1.0, 1.0, 1.0, 0.0); + //pan = mapfloat(p, -1.0, 1.0, 1.0, 0.0); + if (p == 0.5) + pan_l = pan_r = 1.0; + else if (p > 0.5) + { + pan_r = 0.5 - abs(0.5 - p); + pan_l = 1.0 - pan_r; + } + else + { + pan_l = 0.5 - abs(0.5 - p); + pan_r = 1.0 - p; + } } void AudioEffectStereoPanorama::update(void) @@ -92,17 +104,16 @@ void AudioEffectStereoPanorama::update(void) arm_q15_to_float(in[0]->data, in_f[0], AUDIO_BLOCK_SAMPLES); arm_q15_to_float(in[1]->data, in_f[1], AUDIO_BLOCK_SAMPLES); - float fsin = arm_sin_f32(pan * PI / 2.0); - float fcos = arm_cos_f32(pan * PI / 2.0); - int16_t* out_p[2] = {&out[0]->data[0], &out[1]->data[0]}; - for (uint16_t n = 0; n < AUDIO_BLOCK_SAMPLES; n++) { - out_p[0][n] = int16_t(in_f[0][n] * _pseudo_log * fsin * SHRT_MAX); - out_p[1][n] = int16_t(in_f[1][n] * _pseudo_log * fcos * SHRT_MAX); - out_p[0][n] = int16_t(in_f[0][n] * _pseudo_log * fcos * SHRT_MAX); - out_p[1][n] = int16_t(in_f[1][n] * _pseudo_log * fsin * SHRT_MAX); + out_f[0][n] = in_f[0][n] * pan_r; + out_f[0][n] += in_f[1][n] * pan_l; + out_f[1][n] = in_f[1][n] * pan_r; + out_f[1][n] += in_f[0][n] * pan_l; } + + arm_float_to_q15(out_f[0], out[0]->data, AUDIO_BLOCK_SAMPLES); + arm_float_to_q15(out_f[1], out[1]->data, AUDIO_BLOCK_SAMPLES); } if (in[0] != (audio_block_t*)&zeroblock) diff --git a/effect_stereo_panorama.h b/effect_stereo_panorama.h index 2c7d16d..cacbbc1 100644 --- a/effect_stereo_panorama.h +++ b/effect_stereo_panorama.h @@ -35,7 +35,8 @@ class AudioEffectStereoPanorama : public AudioStream AudioEffectStereoPanorama(void): AudioStream(1, inputQueueArray) { - pan = 0.5; + pan_l = 1.0; + pan_r = 1.0; } virtual void update(void); @@ -45,7 +46,9 @@ class AudioEffectStereoPanorama : public AudioStream audio_block_t *inputQueueArray[2]; audio_block_t *out[2]; float in_f[2][AUDIO_BLOCK_SAMPLES]; - float pan; + float out_f[2][AUDIO_BLOCK_SAMPLES]; + float pan_l; + float pan_r; const float _pseudo_log = 1048575 / (float)(1 << 20); };