Audio Sample Player

Hi all

Having some trouble with this art project called “The macePod”. Audio samples should play on demand (cf. sample machine). As test I tried the sketch below where the samples are played for 2 secs.
It works more or less: I have some strange vibrations in my playback. After a while the files start playing like in tremolo.

Another thing is a lot of noise…

/*
  DUE - SD card WAV player 3

 The circuit: use 6-pin SPI connector

  Uno 10  CS   SS     Due  10
      11  DI   MOSI        ICSP-4
      12  DO   MISO        ICSP-1
      13  CLK  SCK         ICSP-3

 */
// include the SD library:

#include <SPI.h>
#include <SD.h>
#include <Audio.h>
#include <Timer.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

Timer t;

File myFile;

const int chipSelect = 10;

void setup() {

  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("\nInitializing SD card...");


  pinMode(SS, OUTPUT);


  while (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed....");

  }



  if (!SD.begin(chipSelect)) {
    Serial.println(" Failed!");
    return;
  }

  Serial.println(" OK.");


  // 44100kHz stereo => 88200 sample rate
  // 100 mSec of prebuffering.

  SPI.setClockDivider(4);

  Audio.begin(88200, 100);

}


void loop() {

  int sPRF = 1;
  int sBNK = 1;
  int sTRK = 1;
  int OK;

  OK = macePod(sPRF, sBNK, sTRK, 2000, 0, 512);
  Serial.print("\n*** macePod "); Serial.println(OK);

}

int macePod(int PRF, int BNK, int TRK, int duration, int pitch, int vol) {

  char sampleFile[20];
  const int S = 1024; // Number of samples to read in block
  short buffer[S];
  int volume = vol;

  for (int track = 1; track <= 4; track++) {

    sprintf(sampleFile, "%d-%d-%d.wav", PRF, BNK, track);

    Serial.print("["); Serial.print(track); Serial.print("] ");

    // open wave file from sdcard
    myFile = SD.open(sampleFile, FILE_READ);
    if (!myFile) {
      return 1;
    }


    int count = 0;
    while (count <= duration/10) {
  
      // read from the file into buffer
      myFile.read(buffer, sizeof(buffer));
      // Prepare samples
      Audio.prepare(buffer, S, volume);
      // Feed samples to audio
      Audio.write(buffer, S);

     count++;
   }

    myFile.close();

    //Serial.println("End of file. Thank you for listening!");
    // while (true) ;

  }

  return 0;
}

Changed the sketch so it skips to the next track with a button. Same problem: I get “tremolo” (vibrating) sound after some tracks…

/*
DUE - SD card WAV player BUTTON

The circuit: use 6-pin SPI connector

Uno 10 CS SS Due 10
11 DI MOSI ICSP-4
12 DO MISO ICSP-1
13 CLK SCK ICSP-3

*/
// include the SD library:

#include <SPI.h>
#include <SD.h>
#include <Audio.h>

Sd2Card card;

File myFile;

const int chipSelect = 10;

const int btnPin = A2;
const int ledPin = A3;

int trkSel = 1;

int buttonState = LOW;

void setup() {

pinMode(btnPin, INPUT);
pinMode(ledPin, OUTPUT);

pinMode(SS, OUTPUT);

digitalWrite(ledPin, LOW);

// Open serial communications and wait for port to open:
Serial.begin(9600);

Serial.print("\nInitializing SD card…");

// we’ll use the initialization code from the utility libraries
// since we’re just testing if the card is working!
while (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println(“initialization failed…”);
}

if (!SD.begin(chipSelect)) {
Serial.println(" Failed!");
return;
}

Serial.println(" OK.");

// 44100kHz stereo => 88200 sample rate
// 100 mSec of prebuffering.
// SPI.setClockDivider(4);
Audio.begin(88200, 100);

}

void loop() {

  int sPRF = 1;
  int sBNK = 1;
  int sTRK = 1;
  int OK;

  buttonState = digitalRead(btnPin);
  if (buttonState == HIGH)
  {

    OK = macePod(sPRF, sBNK, trkSel, 1500, 0, 512);
    Serial.print("\n*** macePod "); Serial.println(OK);

    trkSel++;

    if (trkSel > 4 ) {
      trkSel = 1;
    }

  }

}

int macePod(int PRF, int BNK, int TRK, int duration, int pitch, int vol) {

  char sampleFile[20];
  const int S = 1024; // Number of samples to read in block
  short buffer[S] = {};

  int volume = vol;

  //  for (int track = 1; track <= 4; track++) {

  sprintf(sampleFile, "%d-%d-%d.wav", PRF, BNK, TRK);

  Serial.print("[Sample "); Serial.print(TRK); Serial.print("] ");

  // open wave file from sdcard
  myFile = SD.open(sampleFile);
  if (!myFile) {
    return 1;
  }

    int count = 0;
     while (count <= duration/10) {
        
  // read from the file into buffer
  myFile.read(buffer, sizeof(buffer));
  // Prepare samples
  Audio.prepare(buffer, S, volume);
  // Feed samples to audio
  Audio.write(buffer, S);

    count++;

}
  myFile.close();
  
  return 0;
}

The format of the filenames is: 1-1-1.wav, 1-1-2.wav etc ...