Go Down

Topic: Sound FX Library/Code Snippets (Read 996 times) previous topic - next topic

zvsmedia

Does anyone know of a PWM Sound FX library or code snippets? (Sorry if this is the wrong forum!)

paulb

This might be a start.
It accepts frequency in hz, time t in ms

It's just square waves. It would be a nice to have a function that generated PWM sine waves.



void freqout(int freq, int t)
{
   int hperiod;     //calculate 1/2 period in us
   long cycles, i;
   hperiod = (500000 / freq) - 7;             // subtract 7 us to make up for digitalWrite overhead - determined empirically
   // calculate cycles
   cycles = ((long)freq * (long)t) / 1000;    // calculate cycles

/*   Serial.print(freq);
  Serial.print((char)9);                // ascii 9 is tab - you have to coerce it to a char to work
   Serial.print(hperiod);
   Serial.print((char)9);
   Serial.println(cycles);  */

   for (i=0; i<= cycles; i++){              // play note for t ms
       digitalWrite(outpin, HIGH);
       delayMicroseconds(hperiod);
       digitalWrite(outpin, LOW);
       delayMicroseconds(hperiod - 1);     // - 1 to make up for fractional microsecond in digitaWrite overhead
   }
}

zvsmedia

Good stuff. Sine waves would be a good challenge!

brianbr

Quote
This might be a start.
It accepts frequency in hz, time t in ms

It's just square waves. It would be a nice to have a function that generated PWM sine waves.



Take a look at this brief schematic for simple RC low pass filter. I lifted it right form the Parallax manual for use with PBasic's  FREQOUT command. I have looked at input and output  on a scope and the digitally synthesized square waves round off very nicely.



cheers ... BBR

Go Up