[sdiy] OBJECT ORIENTED C in synth programming

Jerry Gray-Eskue jerryge at cableone.net
Fri Apr 8 20:41:10 CEST 2011


The structure below is C, it looks a bit off normal because of the byte
type. When writing C to port over to other target platforms it is not
uncommon to use a define like typedef char BYTE; - this is done because some
of the primitive types may have different sizes on other platforms. This is
not normally true of an unsigned char, but is common with int and other
larger types. When using bit masks and compares it is important to have the
bit mask size match the data type or you may have some unintended behavior.

The o1.phase = 0; is just accessing the structure value directly, you can
also use a pointer to the structure 
-> look at structures and pointers for the details.

some examples from the FreeDOS Kernel:

/*                                                              */
/* Common byte, 16 bit and 32 bit types                         */
/*                                                              */
typedef char BYTE;
typedef short WORD;
typedef long DWORD;

typedef unsigned char UBYTE;
typedef unsigned short UWORD;

typedef short SHORT;

typedef unsigned int BITS;      /* for use in bit fields(!)     */

typedef int COUNT;
typedef unsigned int UCOUNT;
typedef unsigned long ULONG;

#ifdef UNIX
typedef char FAR *ADDRESS;
#else
typedef void FAR *ADDRESS;
#endif

#ifdef STRICT
typedef signed long LONG;
#else
#define LONG long
#endif


-----Original Message-----
From: synth-diy-bounces at dropmix.xs4all.nl
[mailto:synth-diy-bounces at dropmix.xs4all.nl] On Behalf Of dan snazelle
Sent: Friday, April 08, 2011 12:21 PM
To: Synth-diy at synth-diy.org DIY
Subject: [sdiy] OBJECT ORIENTED C in synth programming

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;
  }
}


_______________________________________________
Synth-diy mailing list
Synth-diy at synth-diy.org
http://synth-diy.org/mailman/listinfo/synth-diy



More information about the Synth-diy mailing list