Distortion when adding multiple signals

Hi,

I’m making a simple sequencer, using various examples while learning daisyduino. Things go wrong when i add more than 2 signals. For example: snaredrum plus osc is fine, but adding snare leads to a nasty distorted osc sound. Also, when this happens, the upload never completes, and serial doesn’t work, take out the snare, and the distortion is gone and serial works again. I’m using a breadboard with stiff cables (the sort you strip and bend, not jumper cables)

Code below. I’m a bit stuck where to debug. any tips appreciated!

#include "DaisyDuino.h"

DaisyHardware hw;
AnalogBassDrum bd;
AnalogSnareDrum sd;
HiHat<> hh;
Oscillator osc;

Metro tick;
int seq[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
int step = 0;
int curstep = 0;
Switch button_bd;
Switch button_sd;
Switch button_hh;
Switch button_osc;

static AdEnv adenv;
float amp =0;

void AudioCallback(float **in, float **out, size_t size) {
 
  
  float signal = 0;

  button_bd.Debounce();
  button_osc.Debounce();
  button_sd.Debounce();
  button_hh.Debounce();

  if (button_bd.RisingEdge()) {
    bd.Trig();
    Serial.println("bd pressed");
    seq[curstep] = 1;
  }

  if (button_sd.RisingEdge()) {
    sd.Trig();
    Serial.println("sd pressed");
  }

  if (button_hh.RisingEdge()) {
    hh.Trig();
    Serial.println("hh pressed");
  }

  if (button_osc.RisingEdge()) {
      adenv.Trigger();
      Serial.println("osc pressed");
  }
  

  for (size_t i = 0; i < size; i++) {
    
    signal = 0;

    bool t = tick.Process();

    // on the metro beat
    if (t) {
      bd.SetTone(analogRead(A0) / 1024.0);
      step++;
      curstep = step % 8;

      if (seq[curstep] == 1) {
        bd.Trig();    
      }
    }

    amp = adenv.Process();
    osc.SetAmp(0.1f * amp);

    signal += bd.Process();
    signal += sd.Process();
    signal += hh.Process();
    signal += osc.Process();

    out[0][i] = out[1][i] = signal;
  }
}

void setup() {
  
   Serial.begin(9600);
  
  //potmeter
  pinMode(A0, INPUT);
  //switches
  button_bd.Init(1000, true, 14, INPUT_PULLUP);
  button_sd.Init(1000, true, 12, INPUT_PULLUP);
  button_hh.Init(1000, true, 13, INPUT_PULLUP);
  button_osc.Init(1000, true, 10, INPUT_PULLUP);

  


  hw = DAISY.init(DAISY_SEED, AUDIO_SR_48K);
  float sample_rate = DAISY.get_samplerate();

  //drumkit
  bd.Init(sample_rate);
  bd.SetFreq(50.f);
  sd.Init(sample_rate);
  hh.Init(sample_rate);

  //synth
  osc.Init(sample_rate);
  osc.SetWaveform(osc.WAVE_SIN);
  osc.SetFreq(440);
  osc.SetAmp(0.2f);

  //synth env
  adenv.Init(sample_rate);
  adenv.SetTime(ADENV_SEG_ATTACK,0.2);
  adenv.SetTime(ADENV_SEG_DECAY,1.0);
  adenv.SetMin(0);
  adenv.SetMax(1.0);
  adenv.SetCurve(0);


  tick.Init(2.f, sample_rate);

  DAISY.begin(AudioCallback);
}

void loop() {

}

I would try first

 signal += bd.Process() / 4.0f;
    signal += sd.Process()  / 4.0f;
    signal += hh.Process()  / 4.0f;
    signal += osc.Process()  / 4.0f;
1 Like

Thank you for responding! I have tried multiplying by 0.25 before adding up and that didn’t make a difference.

[edit] just to be on the safe side I tried with / 4.0f as well. Volume down (as expected) but identical sound issue. It sounds a bit like buzzing interference, like the osc is picking up some other signal.

I would remove any printing in the AudioCallback

done. no improvement unfortunately.

It sounds like you are trying to do too much stuff at once as opposed to an amplitude issue. You can try adding some cpu busyness monitoring code and experiment with that. Sorry I don’t know specifics but hopefully you can find other instances in this forum where it has come up,

Have you tried to increase the block size?

hw.SetAudioBlockSize(96); // or 128 ?

(and as mentioned in another answer move the serial printing in the main loop())
You can also try to strip-off the buttons handling (and generate a fixed rhythm) to find where exactly the issue is.

blocksize - tried, no effect. BTW for future readers: in daisyduino this gets called like DAISY.SetAudioBlockSize(128);

Replaced button triggers by fixed rhythm. Done, no effect.

I’m wondering if the kick, snare and hihat presets have built-in envelopes that are colliding/interfering/overwriting with the osc envelope.

Just try with a bunch of sine waves :slight_smile:

Made some progress - by adding thicker wires on the line-out and dramatically lowering the volume on the osc.