May 21, 2008 AT 11:41 am

Wave shield: Bending the playback sample-rate


Arduino wave shield – speed up – slow down demo from adafruit industries on Vimeo.

One of the reasons I made the Wave Shield kit is that I wanted to be able to play with audio clips and make strange effects. When every sample passes thru the Arduino, it becomes quite easy. For example, the Arduino has an interrupt that goes off 22,050 times a second (for 22kHz audio)…by simply changing the interrupt timing now you’ve changed the playback rate!

Here is the new code for the procedure that plays back the audio. Basically, it checks the analog pin and uses that value to generate a new sample rate scaled off of the original. Then it sets the new sample rate. There’s a little hysteresis code in there to make sure it only updates it when the pot is turned. As collin mentions, you can use any sort of analog sensor, such as a photo cell, bend sensor, etc!

int16_t lastpotval = 0;
#define HYSTERESIS 3

void playcomplete(char *name) {
  int16_t potval;
  uint32_t newsamplerate;

  playfile(name);
  while (wave.isplaying) {
     potval = analogRead(0);
     if ( ((potval - lastpotval) > HYSTERESIS) || ((lastpotval - potval) > HYSTERESIS)) {
        newsamplerate = wave.dwSamplesPerSec;  // get the original sample rate
        newsamplerate *= potval;                       // scale it by the analog value
        newsamplerate /= 512;   // we want to 'split' between 2x sped up and slowed down.
        wave.setSampleRate(newsamplerate);  // set it immediately!
        Serial.println(newsamplerate, DEC);  // for debugging
        lastpotval = potval;
     }
     delay(100);
   }
  card.close_file(f);
}

“D is for Diode” – Circuit Playground Episode 4 is out now! CLICK HERE TO WATCH!

Have an amazing project to share? Join the SHOW-AND-TELL every Wednesday night at 7:30pm ET on Google+ Hangouts.

Join us every Wednesday night at 8pm ET for Ask an Engineer!

Learn resistor values with Mho’s Resistance or get the best electronics calculator for engineers “Circuit Playground”Adafruit’s Apps!



10 Comments

  1. I’m a bit confused about int16_t and uint32_t. I gather that they are integer data types and that u indicates unsigned and the digits 8,16,32 represent the number of bits. But how are they different from byte,int, unsigned int, long and unsigned long?

  2. they are completely identical. i use uint8_t, etc because that way it is extraordinarily clear what type it is. “int” varies on platforms, from 2 to 4 bytes, which makes code less portable. if you say uint32_t then there is no confusion! :)

  3. There may be no confusion with the other platforms. But I’ll bet its confusing to some of other Arduino users, who, like myself, aren’t adept C programmers and have only studied the Arduino programming reference (and your excellent tutorials, of course).

  4. Can it play backwards too? (I assume it’s technically possible, but I mean do the the current libraries easily support that.)

  5. it cant play backwards, but thats due to FAT16 format…each cluster points only to the -next- cluster. so to play backwards you have to fastforward thru the file to the point you want, each time. computers have tons of ram so they just buffer it all!

  6. So would that also mean you can only loop the entire file, and not set loop points in the middle?

  7. This might be a dumb question, since I’m a beginner with the arduino, but what is the resistance of the pot and which pins is it connected to.

  8. When I run this scetch, I get this error:

    In function ‘void playcomplete(char*)':
    error: ‘playfile’ was not declared in this scope

    What is wrong?

  9. This is so cool. It works great.

  10. tryin to speed uo play back what resistance is the pot and witch pins is it connected to please

Sorry, the comment form is closed at this time.