From e54318126a660c40f76130288a9d180f0e10469d Mon Sep 17 00:00:00 2001 From: Jan-Henrik Bruhn Date: Thu, 16 Jan 2020 00:07:12 +0100 Subject: [PATCH] Fix Offset DAC gain being set to 1x instead of 2x. Improve performance of stereo_mix. --- stereo_mix/stereo_mix.cc | 52 ++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/stereo_mix/stereo_mix.cc b/stereo_mix/stereo_mix.cc index 73dd8f1..10cfdd5 100644 --- a/stereo_mix/stereo_mix.cc +++ b/stereo_mix/stereo_mix.cc @@ -26,19 +26,20 @@ typedef SpiMaster, MSB_FIRST, 2> dac4Spi; typedef Dac Dac4; typedef SpiMaster, MSB_FIRST, 2> oDac1Spi; -typedef Dac oDac1; +typedef Dac oDac1; typedef SpiMaster, MSB_FIRST, 2> oDac2Spi; -typedef Dac oDac2; +typedef Dac oDac2; typedef SpiMaster, MSB_FIRST, 2> oDac3Spi; -typedef Dac oDac3; +typedef Dac oDac3; typedef SpiMaster, MSB_FIRST, 2> oDac4Spi; -typedef Dac oDac4; +typedef Dac oDac4; + typedef AdcInputScanner AnalogInputs; #define NUM_CHANNELS 4 uint32_t volume[NUM_CHANNELS]; -uint16_t pan[NUM_CHANNELS]; +uint16_t pan[NUM_CHANNELS * 2]; int main(void) { @@ -56,26 +57,37 @@ int main(void) AnalogInputs::Init(); AnalogInputs::set_num_inputs(8); + +#define WRITE(DAC, ODAC, N) \ + DAC::Write((volume[N] * pan[N * NUM_CHANNELS]) >> 8, 0); \ + ODAC::Write((volume[N] * pan[N * NUM_CHANNELS]) >> 8, 0); \ + DAC::Write((volume[N] * pan[N * NUM_CHANNELS + 1]) >> 8, 1); \ + ODAC::Write((volume[N] * pan[N * NUM_CHANNELS + 1]) >> 8, 1); + while (true) { ResetWatchdog(); for (int i = 0; i < NUM_CHANNELS; i++) { - volume[i] = pgm_read_word_near(lut_res_linear_to_exp + (AnalogInputs::Read(i))) >> 1; - pan[i] = AnalogInputs::Read(i + NUM_CHANNELS); + volume[i] = pgm_read_word_near(lut_res_linear_to_exp + (AnalogInputs::Read(i))); + pan[i * NUM_CHANNELS] = pgm_read_word(lut_res_left_sin_pan + AnalogInputs::Read(i + NUM_CHANNELS)); + pan[i * NUM_CHANNELS + 1] = pgm_read_word(lut_res_right_cos_pan + AnalogInputs::Read(i + NUM_CHANNELS)); + + switch (i) { + case 0: + WRITE(Dac1, oDac1, i); + break; + case 1: + WRITE(Dac2, oDac2, i); + break; + case 2: + WRITE(Dac3, oDac3, i); + break; + case 3: + WRITE(Dac4, oDac4, i); + break; + } + AnalogInputs::Scan(); } - - AnalogInputs::Scan(); - -#define WRITE(DAC, ODAC, N) \ - DAC::Write((volume[N] * (pgm_read_word(lut_res_left_sin_pan + pan[N]))) >> 8, 0); \ - ODAC::Write((volume[N] * (pgm_read_word(lut_res_left_sin_pan + pan[N]))) >> 8, 0); \ - DAC::Write((volume[N] * (pgm_read_word(lut_res_right_cos_pan + pan[N]))) >> 8, 1); \ - ODAC::Write((volume[N] * (pgm_read_word(lut_res_right_cos_pan + pan[N]))) >> 8, 1); - - WRITE(Dac1, oDac1, 0); - WRITE(Dac2, oDac2, 1); - WRITE(Dac3, oDac3, 2); - WRITE(Dac4, oDac4, 3); } }