Playing audio and reading/writing file from SD at same time, possible?

Status
Not open for further replies.
Hi all

I'm working on a project that requires the use of logging and reading of data while playing sounds at same time (from the same SD card). I've been for the last 5 days trying to achieve this witout any luck and as soon as I'm reading or writing the SD card, the playing soundg (wav) turns into noise and sometimes justs hangs.
I don't know if this issue has been posted already but I could not find any similar issue in the forum.

Maybe it's just not supose to work at all and I'mt trying to do something stupid.

What I've seen working is reading from a 2nd SD card (audio board and builtin sd) wich is my future aproach if I can not get this working. But would be nice to try and have it working from the same SD card.

My setup is a Teensy 3.5 with the built in SD card. I have an wav file that plays perfectly when not using the SD card and also I can successfully log/read data from the SD card when not playing sounds. The sd card is a Sandisk ultra of 32G.
I've looked also into using SdFat library witout any luck. Does anyone knows if this could be done using audio library and sd library?

I'm attachig the example code I'm using for testing.

any help or direction woild be very much apreciated, thanks alot!

Code:
/*
  SD card read/write
 
 This example shows how to read and write data to and from an SD card file  
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11, pin 7 on Teensy with audio board
 ** MISO - pin 12
 ** CLK - pin 13, pin 14 on Teensy with audio board
 ** CS - pin 4, pin 10 on Teensy with audio board
 
 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe
 
 This example code is in the public domain.
   
 */
 
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
// #include <SerialFlash.h>


// GUItool: begin automatically generated code
AudioPlaySdWav           playSdWav4;     //xy=531,567
AudioPlaySdWav           playSdWav1;     //xy=534,277
AudioPlaySdWav           playSdWav2;     //xy=534,372
AudioPlaySdWav           playSdWav3;     //xy=534,473
AudioAmplifier           AMP_L1;         //xy=703,257
AudioAmplifier           AMP_L2;         //xy=703,350
AudioAmplifier           AMP_L3;         //xy=703,449
AudioAmplifier           AMP_L4;         //xy=703,545
AudioAmplifier           AMP_R1;         //xy=705,297
AudioAmplifier           AMP_R2;         //xy=705,390
AudioAmplifier           AMP_R3;         //xy=705,489
AudioAmplifier           AMP_R4;         //xy=705,585
AudioMixer4              mixer2;         //xy=914,460
AudioMixer4              mixer1;         //xy=917,378
AudioMixer4              mixerL;         //xy=1083,585
AudioMixer4              mixerR;         //xy=1083,654
AudioAmplifier           amp1;           //xy=1249,585
AudioAmplifier           amp2;           //xy=1251,655
AudioOutputAnalogStereo  dacs1;          //xy=1402,623
AudioConnection          patchCord1(playSdWav4, 0, AMP_L4, 0);
AudioConnection          patchCord2(playSdWav4, 1, AMP_R4, 0);
AudioConnection          patchCord3(playSdWav1, 0, AMP_L1, 0);
AudioConnection          patchCord4(playSdWav1, 1, AMP_R1, 0);
AudioConnection          patchCord5(playSdWav2, 0, AMP_L2, 0);
AudioConnection          patchCord6(playSdWav2, 1, AMP_R2, 0);
AudioConnection          patchCord7(playSdWav3, 0, AMP_L3, 0);
AudioConnection          patchCord8(playSdWav3, 1, AMP_R3, 0);
AudioConnection          patchCord9(AMP_L1, 0, mixer1, 0);
AudioConnection          patchCord10(AMP_L2, 0, mixer1, 1);
AudioConnection          patchCord11(AMP_L3, 0, mixer1, 2);
AudioConnection          patchCord12(AMP_L4, 0, mixer1, 3);
AudioConnection          patchCord13(AMP_R1, 0, mixer2, 0);
AudioConnection          patchCord14(AMP_R2, 0, mixer2, 1);
AudioConnection          patchCord15(AMP_R3, 0, mixer2, 2);
AudioConnection          patchCord16(AMP_R4, 0, mixer2, 3);
AudioConnection          patchCord17(mixer2, 0, mixerR, 0);
AudioConnection          patchCord18(mixer1, 0, mixerL, 0);
AudioConnection          patchCord19(mixerL, amp1);
AudioConnection          patchCord20(mixerR, amp2);
AudioConnection          patchCord21(amp1, 0, dacs1, 0);
AudioConnection          patchCord22(amp2, 0, dacs1, 1);
// GUItool: end automatically generated code

/////////////////////////////////////////////////////////////////

const int chipSelect = BUILTIN_SDCARD;
// const int chipSelect = 10;  

// SD card
File datafile;    // for storing into a file

/////////////////////////////////////////////////////////////////


void setup()
{

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


  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  

  Serial.println(F("\n..................................\n# Setting up: Audio"));
  AudioMemory(40);                   //Definimos la memoria de trabajo para las conexiones de audio

  dacs1.analogReference(INTERNAL);

  // regulate volume of the mix
    amp1.gain (0.7);
    amp2.gain (0.7);

    // Regulate players volume
    AMP_L1.gain (1.0);
    AMP_R1.gain (1.0);
    AMP_L2.gain (0.8);
    AMP_R2.gain (0.8);
    AMP_L3.gain (0.8);
    AMP_R3.gain (0.8);
    AMP_L4.gain (0.8);
    AMP_R4.gain (0.8);

    playFile();

}



void playFile() {
  Serial.print ("Max blocks used till now: ");
  Serial.println (AudioMemoryUsageMax());
  unsigned long WAVduration = 0;
  playSdWav1.play("soback.wav");
  while (WAVduration == 0) {
    WAVduration = playSdWav1.lengthMillis();
  }

  Serial.print ("Audio duration: ");
  Serial.println (WAVduration);
}

void loop()
{
  dumpFile ("stream2.kll");
  delay (1500);
  Serial.print ("FreeRam: ");
  Serial.println (FreeRam());
}



void dumpFile (char *_fileName) {
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.

  datafile = SD.open(_fileName);
  Serial.print(F("Dumping File "));
  Serial.println(_fileName);


  if (datafile) {
    while (datafile.available()) {
      char a = datafile.read();
      Serial.print(a);
    }

    Serial.println (F("####### END DATA."));
    datafile.close();
  }
}


uint32_t FreeRam(){ // for Teensy 3.0
    uint32_t stackTop;
    uint32_t heapTop;

    // current position of the stack.
    stackTop = (uint32_t) &stackTop;

    // current position of heap.
    void* hTop = malloc(1);
    heapTop = (uint32_t) hTop;
    free(hTop);

    // The difference is the free, available ram.
    return stackTop - heapTop;
}
 
Status
Not open for further replies.
Back
Top