Filter LFO issue

Status
Not open for further replies.

ulonix

Member
Hey guys I'm building a basic synth and i already have 2 osc, a filter controlled by a waveform osc to act as LFO. The 12 bit dac is used as audio output. I'm just starting to understand audio programming so everything is quite new to me.

On very specific values , like Freq : 19000 and Res 0.9 it makes a terrible sound (especially when i added the LFO), like white noise from a tv or sth.
Please let me know what i have been doing wrong.

Update: I'm also using 100K pots, can this be the issue?... connected to + and ground and to the analog pins

Code:
#include <MIDI.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>



// GUItool: begin automatically generated code
AudioSynthWaveform       waveform1;      //xy=190,258
AudioSynthWaveform       waveform2;      //xy=221,327
AudioSynthWaveform       wave_lfo;            //xy=357,444
AudioMixer4              mixer1;         //xy=424,284
AudioMixer4              lfo_mixer_int;         //xy=557,431
AudioEffectEnvelope      envelope1;      //xy=577,268
AudioFilterStateVariable filter1;        //xy=756,285
AudioOutputAnalog        dac1;           //xy=936,272
AudioConnection          patchCord1(waveform1, 0, mixer1, 0);
AudioConnection          patchCord2(waveform2, 0, mixer1, 1);
AudioConnection          patchCord3(wave_lfo, 0, lfo_mixer_int, 0);
AudioConnection          patchCord4(mixer1, envelope1);
AudioConnection          patchCord5(lfo_mixer_int, 0, filter1, 1);
AudioConnection          patchCord6(envelope1, 0, filter1, 0);
AudioConnection          patchCord7(filter1, 0, dac1, 0);
// GUItool: end automatically generated code





  float freq;
  float res;
  float mixer_osc1_vol;
  float mixer_osc2_vol;

  float lfo_rate = 0.0;
  float lfo_int = 0.0;
    
  bool note_on ;

  byte current_midi_note;
  
void setup() {
  AudioMemory(12);
Serial.begin(57600);
  //Envelopes
  // VCA envelopes
  envelope1.attack(1.5);
  envelope1.hold(0.0); // not used
  envelope1.decay(15);
  envelope1.sustain(1.0);
  envelope1.release(5.0);

  //filtro
  freq = 0.0;
  res = 0.0;
  //filter1.octaveControl(1.0);

  //Lfo
  lfo_rate = 0.0;
  lfo_int = 0.0;

  mixer1.gain(0,0.0); //Iniciar SAW
  mixer1.gain(1,0.5); //Iniciar SQUARE
  lfo_mixer_int.gain(0,0.0); //Iniciar Intensidad LFO Filtro
  
  //MIDI STUFF
  Serial.begin(57600);
  Serial.println("MIDI Input Test");

  // put your setup code here, to run once:
  waveform1.begin(0.1,220,WAVEFORM_SAWTOOTH);
  waveform2.begin(0.1,220,WAVEFORM_SQUARE);
  wave_lfo.begin(0.1, 0, WAVEFORM_SINE);


  MIDI.setHandleNoteOn(handleNoteOn);
  MIDI.setHandleNoteOff(handleNoteOff);
  MIDI.begin();
}

void loop() {

  
  
  int type, note, velocity, channel, d1, d2;
  // put your main code here, to run repeatedly:

  int knob1 = analogRead(A1);
  int knob2 = analogRead(A0);
  int knob3 = analogRead(A2);
  int knob4 = analogRead(A3);


  freq = map(knob1,0,1023,0,19000);
  res = map(knob2,0,1023,90,500);

  lfo_rate = map(knob3,0,1023,0,20);
  lfo_int = map(knob4,0,1023,0,500);



  res = float(res / 100);

  lfo_int = float(lfo_int / 100);
  

      wave_lfo.frequency(lfo_rate);

      lfo_mixer_int.gain(0,lfo_int);

      Serial.println(String("Cut: ") + freq + ", Res=" + res + ", LFO INT=" + lfo_int);
      
      filter1.frequency(freq);
      filter1.resonance(res); 


  MIDI.read();
}


void handleNoteOn(byte inChannel, byte inNote, byte inVelocity)
{


            //Tocamos nota          
              current_midi_note = inNote;
              note_on = true;
              waveform1.frequency(mtof(inNote));
              waveform2.frequency(mtof(inNote));
              envelope1.noteOn(); //Tocar nota
              
              if(inVelocity == 0 && inNote == current_midi_note)
              {

                envelope1.noteOff();
                }

    
}

void handleNoteOff(byte inChannel, byte inNote, byte inVelocity)
{

      
              if(inNote == current_midi_note){
                envelope1.noteOff();
                note_on = false;
              }

}

float mtof(float midival)
{
   
  // code from AF_precision_synthesis sketch, copyright 2009, Adrian Freed.
  float f = 0.0;
  if(midival) f = 8.1757989156 * pow(2.0, midival/12.0);
  return f;
}
 
Last edited:
I have heard the filter hiss at me, but I never figured out what caused it. I had some scaling values wrong in the math that figures out things like freq and ADSR settings. All the funny sounds went away when I fixed the math. I wonder why you are using such a high frequency setting, 19 KHz? That might be too high. I limit the freq to 10,000 (10 KHz) and I will probably lower it some more since the action is all at the bottom end of the freq knob. The freq knob sets the starting frequency, which can be pushed up or down with the ADSR and LFO. I also use a higher number for filter.octaveControl. I use 4 or 5. Haven't tried other settings, just stuck a number in there and it worked, so I never messed with it. I also have a pot on the resonance input. The scaling value was messed up on that too.

I started out with 100K pots on my first breadboard because I had some. This will cause small errors in the analogRead function because of the way the A/D converter works. It's explained somewhere in the zillion page manual for the processor chip used in the Teensys. I noticed some minor interaction between different pots, and since my synth is voltage controlled, including VCO pitch, that's a bad thing. I switched to 10K and the interaction went away.
 
Hey, thanks for your answer, I will try with other pots....About the frequencies I was just using 19khz because 20khz is the human limit for hearing right?, maybe i'm just confused. Could you explain to me why it has to be lower??
Thanks again!
 
It doesn't have to be lower, it just works better in my synth with an upper limit around 10Khz. The filter changes the sound by removing the harmonic energy above it's cutoff frequency. The VCO's have little harmonic energy above about 10KHz, so the filter has minimal effect above 10 KHz or so. My ears are 64 years old and have been blasted with heavy metal far too many times. Maybe I don't hear anything above 10 KHz. Try a different frequency to see if it makes a difference for you.
 
I've also realised about this, that at some freq/res values I could have problems. I did some things to fix them like using IF conditions, if it reaches a specific freq value i'll just limit it, but i thought it was a problem on my side so I tried to make it work with that value(19khz). I will try to lower the freq value and to use 10k pots, is this the only value for a pot to avoid reading problems on teensy?.
Cheers!
 
Last edited:
The analog inputs are multiplexed through a switch matrix into two A/D converters. All of this takes place inside the chip. The converter will connect the selected input to an internal capacitor. The cap is charged for a short time, then disconnected and the charge is read by the converter. If the external resistance is large the cap will not be fully charged when the reading is taken, causing lower than expected readings. Ideally you would want the lowest possible resistance path from the knob to the converter, but the switching stuff inside the chip has some built in resistance which can not be eliminated. So extremely low values offer no benefit.

Low resistance values will draw more current from your power supply, so you need to choose a value that doesn't waste too much power. This is not an issue with a few pots, but I currently have 48 pots and keep adding more. I tried 25K which worked, and couldn't hear a difference between 5K and 25 K. I settled on 10K for an optimum balance between speed and current drain when I ordered 50 matching pots. They work fine and I just ordered more. 5K may offer a small benefit, but I doubt it would be noticed except for places where fast pitch changes are needed, like the pitch bend wheel. The ear will notice small errors in pitch, but not amplitude or timbre.
 
Status
Not open for further replies.
Back
Top