Reply To: Polyphonic Nanosynth v1.0

Home Forums Products NS1nanosynth Polyphonic Nanosynth v1.0 Reply To: Polyphonic Nanosynth v1.0

#3659

Niklas
Participant

Good work! But I need to ask something on a code specific manner.

You have:

Oscil <2048, AUDIO_RATE> oscil1; 
...
Oscil <2048, AUDIO_RATE> oscil6;
...
ADSR <CONTROL_RATE, CONTROL_RATE> envelope1;
...
ADSR <CONTROL_RATE, CONTROL_RATE> envelope6;

// notes
byte note=0;
....
byte note6=0;

That’s a lot of VARIABLE_1_of_1 to VARIABLE_1_of_6, VARIABLE_2_of_1 to VARIABLE_2_of_6, etc.

If one would declare a struct and array:

struct Voice{
  Oscil <2048, AUDIO_RATE> oscil;
  ADSR <CONTROL_RATE, CONTROL_RATE> envelope;
  byte note = 0;
};

Voice voices[6];

You could easily reduce the code size with for loops, for instance:

// NOTE OFF ROUTING
void  USBNoteoff(byte note)
{
  for(uint8_t i = 0 ; i < 5 ; ++i )
  {
    Voice v = voices[i];
    if(note==v.note){
        v.envelope1.noteOff();
        v.note=0;
        break;
      }
  }
  if (notecounter == 0) {  
    digitalWrite(LED,LOW); // no notes playing
    allnotesoff();
  } 
}

And that makes also the code more flexible.

I have seen it in some other arduino code also and I ask myself why?
It is just one more reference/indexing, comparing and increment per voice.
I don’t think that could cause any big overhead or…?