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


Halloween season is here!
Halloween season is here! Check out all the posts, gift guides, and more!

Adafruit publishes a wide range of writing and video content, including interviews and reporting on the maker market and the wider technology world. Our standards page is intended as a guide to best practices that Adafruit uses, as well as an outline of the ethical standards Adafruit aspires to. While Adafruit is not an independent journalistic institution, Adafruit strives to be a fair, informative, and positive voice within the community – check it out here: adafruit.com/editorialstandards

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 7:30pm ET! To join, head over to YouTube and check out the show’s live chat and our Discord!

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

Join over 38,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


New Products – Adafruit Industries – Makers, hackers, artists, designers and engineers! — New Products 10/11/2024 Featuring Snap-on Enclosure for Adafruit Feather RP2040 USB Host! (Video)

Python for Microcontrollers – Adafruit Daily — Python on Microcontrollers Newsletter: New Python Releases, an ESP32+MicroPython IDE and Much More! #CircuitPython #Python #micropython @ThePSF @Raspberry_Pi

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

Adafruit IoT Monthly — Garden Lights, Bluetooth 6.0, and more!

Maker Business – Adafruit Daily — First Solar’s $1.1 billion development of vertically integrated factory in the U.S.

Electronics – Adafruit Daily — My signal isn’t THAT noisy, is it?

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



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.