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

May 21st, 2008 at 2:22 pm
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?
May 21st, 2008 at 2:39 pm
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!
May 21st, 2008 at 3:28 pm
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).
May 23rd, 2008 at 2:01 pm
Can it play backwards too? (I assume it’s technically possible, but I mean do the the current libraries easily support that.)
May 23rd, 2008 at 2:04 pm
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!
May 24th, 2008 at 12:47 pm
So would that also mean you can only loop the entire file, and not set loop points in the middle?
October 5th, 2008 at 4:43 am
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.