[sdiy] OBJECT ORIENTED C in synth programming

dan snazelle subjectivity at hotmail.com
Fri Apr 8 19:21:02 CEST 2011


there has been a lot of talk about C for Synth Programming on the list lately. I have been trying to learn it for a few months now and applying what I learn to the Arduino format.

however, even after reading up a bunch on C, something I am seeing a lot in code, but really dont have any way of understanding is stuff like this (below)(which I am pretty sure is OBJECT ORIENTED programming. using structures,  classes, public and private etc. 


In the example below there are all these
o1._parameters, etc. 



So I am wondering....can anyone recommend a good book On Object oriented C? Or on OOP in general? Am i correct that object oriented is using structures and classes?  

The more of this I can understand, the more I will be able to read and hence learn from the code I look at. Even though I dont undestand it yet, intuitively it looks like it makes sense. And I guess this is what Java and C++ are all about?

thanks





example (taken from FM TOY)


typedef struct {
    // Patch structure
    // parameters stored as bytes, containing values from 0-127
    // exactly as received over MIDI

    // Operator 1
    byte op1_ratio;   // 0-127, only 0-7 useful
    byte op1_detune;  // -64 to 63, u64 = 0
    byte op1_lfo;     // 0-127, scaled
    byte op1_gain;    // 0-127, scaled
    byte op1_env;     // -64 to 63, u64 = 0
    byte op1_a, op1_d, op1_s, op1_r;

    // Operator 2
    byte op2_ratio;
    byte op2_detune;
    byte op2_lfo;
    byte op2_gain;
    byte op2_env;
    byte op2_a, op2_d, op2_s, op2_r;
    
    // lfo
    byte lfo_rate;
    byte lfo_shape;
    byte lfo_delay;
   
    // instrument
    byte portamento;
    byte fb; 
} patch;




another example ( Adrian Freed)


void setup()
{
    o1.phase = 0;
   o1.phase_increment = 0 ;
   o1.amplitude_increment = 0;
   o1.frequency_increment = 0;
   o1.framecounter =0;
     o1.amplitude = 0; // full amplitude
 
 initializeTimer();
 
 }

void loop() {

// Examples
  o1.amplitude = 255*256; // full amplitude
 
  // All the MIDI note numbers
   for(int i=0;i<128;++i)
   {
     o1.phase_increment = midinotetophaseinc[i];
     delay(100);
   }
 delay(1000); 
 
   // linear frequency steps from fractional frequency
   unsigned long l;
   for(l=100;l<15000;l+=200)
   {
     o1.phase_increment = phaseinc_from_fractional_frequency(l*256);
    delay(200);
   }

 o1.phase_increment = phaseinc(440.0);
  delay(1000); 
  o1.phase_increment = phaseinc(220.0);
  delay(1000); 
  o1.phase_increment = phaseinc(600.0);


 delay(1000); 
   //sweep up
 o1.phase_increment = phaseinc_from_fractional_frequency(100UL*256);
  o1.frequency_increment = phaseinc(0.02);
  o1.framecounter = 200000;
  delay(10000);
  //sweep down
  o1.phase_increment = phaseinc_from_fractional_frequency(10000UL*256);
  o1.frequency_increment = -phaseinc(0.02);
  o1.framecounter = 200000;
  delay(10000);
}

// this is the heart of the wavetable synthesis. A phasor looks up a sine table
int8_t outputvalue  =0;
SIGNAL(PWM_INTERRUPT)
{
   PWM_VALUE_DESTINATION = outputvalue; //output first to minimize jitter
   outputvalue = (((uint8_t)(o1.amplitude>>8)) * pgm_read_byte(sintable+((o1.phase>>16)%LUTsize)))>>8;
 
  o1.phase += (uint32_t)o1.phase_increment;
  
  // ramp amplitude and frequency
  if(o1.framecounter>0)
  {
     --o1.framecounter;
     o1.amplitude += o1.amplitude_increment;
     o1.phase_increment += o1.frequency_increment;
  }
}





More information about the Synth-diy mailing list