Pages: [1]   Go Down
Author Topic: Sound FX Library/Code Snippets  (Read 916 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 267
dinosaur cork
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good stuff. Sine waves would be a good challenge!
Logged

Underhill Center, Vermont, USA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 71
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: