Seamless Looping

Status
Not open for further replies.

Amandwato

Member
Hi, I'm having a little trouble trying to seamlessly loop an audio file, with the current code I have, which I've pasted below, there's a very brief pause once the track is done playing, is there a way to eliminate any pause between the end of a track and the start of the same track? My code is just a modification of the SDTest example code

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

AudioPlaySdWav           playWav1;
// Use one of these 3 output types: Digital I2S, Digital S/PDIF, or Analog DAC
AudioOutputI2S           audioOutput;
AudioConnection          patchCord1(playWav1, 0, audioOutput, 0);
AudioConnection          patchCord2(playWav1, 1, audioOutput, 1);
AudioControlSGTL5000     sgtl5000_1;

// Use these with the audio adaptor board
#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7
#define SDCARD_SCK_PIN   14

void setup() {
  Serial.begin(9600);

  // Audio connections require memory to work.  For more
  // detailed information, see the MemoryAndCpuUsage example
  AudioMemory(8);
  
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  sgtl5000_1.lineOutLevel(13);

  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    // stop here, but print a message repetitively
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }
}

void playFile(const char *filename)
{
  Serial.print("Playing file: ");
  Serial.println(filename);

  // Start playing the file.  This sketch continues to
  // run while the file plays.
  playWav1.play(filename);

  // A brief delay for the library read WAV info
  //delay(5);
}


void loop() {
  if (!playWav1.isPlaying()) playFile("042317.WAV");
  }
 
Hello,

From audio system design tool:
A brief delay after calling play() will usually occur before isPlaying() returns true and positionMillis() returns valid time offset. WAV files have a header at the beginning of the file, which the audio library must read and parse before playing can begin.

So I'm assuming delay is always there.

I just created another playSdWav object with mixer:
Capture.PNG

Then added some code to play this second object 1 second before the first one finishes playing.
Both playSdWav object are using the same file:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioPlaySdWav           playSdWav2;     //xy=176,193
AudioPlaySdWav           playSdWav1;     //xy=187,98
AudioMixer4              mixer2;         //xy=442,198
AudioMixer4              mixer1;         //xy=444,113
AudioOutputI2S           i2s2;           //xy=790,181
AudioConnection          patchCord1(playSdWav2, 0, mixer1, 1);
AudioConnection          patchCord2(playSdWav2, 1, mixer2, 1);
AudioConnection          patchCord3(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord4(playSdWav1, 1, mixer2, 0);
AudioConnection          patchCord5(mixer2, 0, i2s2, 1);
AudioConnection          patchCord6(mixer1, 0, i2s2, 0);
AudioControlSGTL5000     sgtl5000_1;     //xy=430,363
// GUItool: end automatically generated code

// Use these with the audio adaptor board
#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7
#define SDCARD_SCK_PIN   14

void setup() {
  Serial.begin(9600);

  // Audio connections require memory to work.  For more
  // detailed information, see the MemoryAndCpuUsage example
  AudioMemory(8);
  
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  sgtl5000_1.lineOutLevel(13);

  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    // stop here, but print a message repetitively
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }
}

void playFile1(const char *filename)
{
  Serial.print("Playing file: ");
  Serial.println(filename);

  // Start playing the file.  This sketch continues to
  // run while the file plays.
  playSdWav1.play(filename);

  // A brief delay for the library read WAV info
  delay(10);
}
void playFile2(const char *filename)
{
  Serial.print("Playing file: ");
  Serial.println(filename);

  // Start playing the file.  This sketch continues to
  // run while the file plays.
  playSdWav2.play(filename);

  // A brief delay for the library read WAV info
  delay(10);
}

void loop() {
  if (!playSdWav1.isPlaying()&!playSdWav2.isPlaying()) //if neither playing play 1 (first loop)
  {
    Serial.println("first play");
    playFile1("SDTEST4.WAV");
  }

 if(playSdWav1.isPlaying()&(playSdWav1.lengthMillis()-playSdWav1.positionMillis())<1000&!playSdWav2.isPlaying()) //if play1 is nearly finsiched and play2 isn't already playing then play2
 {
  Serial.println("play2");
  playFile2("SDTEST4.WAV");
  
 }
 
 if(playSdWav2.isPlaying()&(playSdWav2.lengthMillis()-playSdWav2.positionMillis())<1000&!playSdWav1.isPlaying()) //if play2 is nearly finsiched and play1 isn't already playing then play1
 {
  Serial.println("play1");
  playFile1("SDTEST4.WAV");
 }        
}

Twerk the delays to see if that does what you want.
 
Last edited:
Code:
if (!playSdWav1.isPlaying()&!playSdWav2.isPlaying())

& = bit-wise AND
&& = logical AND

I think you want the latter.
 
Status
Not open for further replies.
Back
Top