How to send waveform to separate mono DAC channels?

27 views
Skip to first unread message

justin jools

unread,
Jul 23, 2024, 2:38:24 PM7/23/24
to Mozzi-users
Hi, I have two daisy chained MC4822 DACs with separate chip select CS. I have separate instances for each DAC and 4 mono out channels: DAC1 L/R, DAC2 L/R.

I am trying to figure out send separate waveform to each channel.

I've managed to send two wave signals with StereoOutput method by splitting L/R. But I need to also send to DAC2 but this doesn't seems the correct way to do this. I would think there should a way to send separate waves to L/R A/B channels with MonoOutput method.

Does anyone know how to do this?
This is the StereoOutput method to split L/R signals:

  return StereoOutput::from16Bit(aSaw1.next() * env1, aCos2.next() * env2);

  uint16_t outL = (f.l() >> 4) + 2048;
  uint16_t outR = (f.r() >> 4) + 2048;

  dac.outputA(outL);
  dac.outputB(outR);


Complete code

#include "MozziConfigValues.h"  // for named option values
#define MOZZI_AUDIO_MODE MOZZI_OUTPUT_EXTERNAL_TIMED
#define MOZZI_AUDIO_BITS 12  // the default value of 16 for external audio is thus used, instead
#define MOZZI_AUDIO_CHANNELS MOZZI_STEREO
#define MOZZI_CONTROL_RATE 256 // Hz, powers of 2 are most reliable

#include <SPI.h>
#include <Mozzi.h>
#include <Oscil.h>
#include <tables/cos2048_int8.h> // table for Oscils to play
#include <tables/saw256_int8.h>
#include <mozzi_rand.h>
#include <mozzi_midi.h>
#include <mozzi_fixmath.h>
#include <EventDelay.h>
#include <Smooth.h>
#include <DAC_MCP49xx.h>  // https://github.com/tomcombriat/DAC_MCP49XX


// Synthesis part
Oscil<SAW256_NUM_CELLS, MOZZI_AUDIO_RATE> aSaw1(SAW256_DATA);
Oscil<COS2048_NUM_CELLS, MOZZI_AUDIO_RATE> aCos1(COS2048_DATA);
Oscil<COS2048_NUM_CELLS, MOZZI_AUDIO_RATE> aCos2(COS2048_DATA);
Oscil<COS2048_NUM_CELLS, MOZZI_CONTROL_RATE> kEnv1(COS2048_DATA);
Oscil<COS2048_NUM_CELLS, MOZZI_CONTROL_RATE> kEnv2(COS2048_DATA);

// External audio output parameters and DAC declaration
#define SS_PIN PB0 // 10 Arduino
#define SS_PIN2 PB1

DAC_MCP49xx dac(DAC_MCP49xx::MCP4922, SS_PIN);
DAC_MCP49xx dac2(DAC_MCP49xx::MCP4922, SS_PIN2);

void setup() {

  aSaw1.setFreq(440.f);
  aCos1.setFreq(440.f);
  aCos2.setFreq(220.f);
  kEnv1.setFreq(0.25f);
  kEnv2.setFreq(0.30f);

  dac.init();
  dac2.init();
  startMozzi();
}


void audioOutput(const AudioOutput f)  // f is a structure containing both channels
{
  // signal is passed as 16 bit. This DAC expects 12 bits so shift back four bits, and add a bias of 2^(12-1)=2048
  uint16_t outL = (f.l() >> 4) + 2048;
  uint16_t outR = (f.r() >> 4) + 2048;

  dac.outputA(outL);
  dac.outputB(outR);
  dac2.outputA(outL);
  dac2.outputB(outR);
}


// Carry enveloppes
int env1, env2;



void updateControl() {
  env1 = kEnv1.next();
  env2 = kEnv2.next();
}



AudioOutput updateAudio() {  
  return StereoOutput::from16Bit(aSaw1.next() * env1, aCos2.next() * env2);
//  return MonoOutput::fromNBit(12, (int32_t)aSaw1.next() ) ;
}


void loop() {
  audioHook();
}  
Reply all
Reply to author
Forward
0 new messages