audio file continuous loop

Status
Not open for further replies.

j_dunavin

Well-known member
I really did try searching and looking through the example files, but couldn't find any examples on how to loop an audio file.
For example in the Sample Player sketch, I wold like to switch a toggle and have the snare play over and over.
 
Look at the Audio -> Tutorial -> Part_1_03_Playing_Music example for how to loop a sample. If you want to trigger it to start by a button one way would be to add a flag variable and have the button press set the flag, and only play if it's not currently playing and the flag is set.
 
I have edited out all the "unecessary" lines from the AudioSnareSample sketch to leave minimum to get the snare play over and over at the touch or toggle of a button........you can add to it to suit.......this is on a teensy3.2 only ....no attached audio board.

Code:
#include <Audio.h>

// WAV files converted to code by wav2sketch
#include "AudioSampleSnare.h"        // http://www.freesound.org/people/KEVOY/sounds/82583/

// Create the Audio components.  These should be created in the
// order data flows, inputs/sources -> processing -> outputs
//
AudioPlayMemory    sound0;

AudioOutputAnalog  dac;     // play to both I2S audio board and on-chip DAC

// Create Audio connections between the components
//

AudioConnection c1(sound0, 0, dac, 0);

void setup() {

  // Audio connections require memory to work.  For more
  // detailed information, see the MemoryAndCpuUsage example
  AudioMemory(10);

}

void loop() {

  // Snare plays continuously while the pin is touched.
 
    while(touchRead(1) < 2000);
    
    sound0.play(AudioSampleSnare);
    
    delay(100);    //change the delay ms to suit
 
}
 
Status
Not open for further replies.
Back
Top