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;

  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;

Join Adafruit on Mastodon

Adafruit is on Mastodon, join in! adafruit.com/mastodon

Stop breadboarding and soldering – start making immediately! Adafruit’s Circuit Playground is jam-packed with LEDs, sensors, buttons, alligator clip pads and more. Build projects with Circuit Playground in a few minutes with the drag-and-drop MakeCode programming site, learn computer science using the CS Discoveries class on code.org, jump into CircuitPython to learn Python and hardware together, TinyGO, or even use the Arduino IDE. Circuit Playground Express is the newest and best Circuit Playground board, with support for CircuitPython, MakeCode, and Arduino. It has a powerful processor, 10 NeoPixels, mini speaker, InfraRed receive and transmit, two buttons, a switch, 14 alligator clip pads, and lots of sensors: capacitive touch, IR proximity, temperature, light, motion and sound. A whole wide world of electronics and coding is waiting for you, and it fits in the palm of your hand.

Have an amazing project to share? The Electronics Show and Tell is every Wednesday at 7pm ET! To join, head over to YouTube and check out the show’s live chat – we’ll post the link there.

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

Join over 36,000+ makers on Adafruit’s Discord channels and be part of the community! http://adafru.it/discord

CircuitPython – The easiest way to program microcontrollers – CircuitPython.org

Maker Business — Chip inventories rise as demand falls

Wearables — Go insane with a membrane (button or keypad)

Electronics — Shift away from basic arithmetic

Python for Microcontrollers — Python on Microcontrollers Newsletter: CircuitPython 8.0 RC 1 Out, Board Guides, and Much More! #CircuitPython #Python @ThePSF @micropython @Raspberry_Pi

Adafruit IoT Monthly — Cat Doorbell, Deep Algae Bloom Detector, and more!

Microsoft MakeCode — MakeCode Thank You!

EYE on NPI — Maxim’s Himalaya uSLIC Step-Down Power Module #EyeOnNPI @maximintegrated @digikey

New Products – Adafruit Industries – Makers, hackers, artists, designers and engineers! — New Products 02/01/23 Featuring Adafruit Floppy FeatherWing with 34-Pin IDC Connector!

Get the only spam-free daily newsletter about wearables, running a "maker business", electronic tips and more! Subscribe at AdafruitDaily.com !


  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.