Playing sound on Arduino MEGA 2560 through SPI card

I have connected to an Arduino MEGA 2560 R3 an SD card, through an SPI breakout, and a Sparkfun MPC4725 DAC on the I2C. For the DAC I use this Adafruit library:

DAC website

And I found this tutorial/library for playing audio, from Adafruit, for playing sound from .wav files stored on SDcard:

Adafruit SDcard sound

but it is for this specific shield. I use a different DAC. What changes should I do to the code above (the 2nd link) in order to make it output the signal to the DAC I have connected to the I2C?

I assume something I have to change to this code (taken from Adafruit’s link) ??:

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps for debugging
  
  putstring_nl("\nWave test!");  // say we woke up!
  
  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(freeRam());  
 
  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  
  //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
  if (!card.init()) {         //play with 8 MHz spi (default faster!)  
    putstring_nl("Card init. failed!");  // Something went wrong, lets print out why
    sdErrorCheck();
    while(1);                            // then 'halt' - do nothing!
  }
  
  // enable optimize read - some cards may timeout. Disable if you're having problems
  card.partialBlockRead(true);
  
  // Now we will look for a FAT partition!
  uint8_t part;
  for (part = 0; part < 5; part++) {     // we have up to 5 slots to look in
    if (vol.init(card, part)) 
      break;                             // we found one, lets bail
  }
  if (part == 5) {                       // if we ended up not finding one  :(
    putstring_nl("No valid FAT partition!");
    sdErrorCheck();      // Something went wrong, lets print out why
    while(1);                            // then 'halt' - do nothing!
  }
  
  // Lets tell the user about what we found
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(),DEC);     // FAT16 or FAT32?
  
  // Try to open the root directory
  if (!root.openRoot(vol)) {
    putstring_nl("Can't open root dir!"); // Something went wrong,
    while(1);                             // then 'halt' - do nothing!
  }
  
  // Whew! We got past the tough parts.
  putstring_nl("Files found:");
  dirLevel = 0;
  // Print out all of the files in all the directories.
  lsR(root);
}

Just buy the shield, you’ll never get reasonable audio out of a MCP4725 DAC as it uses I2C and it will be way too slow.

PS: It would have been a good idea to make this a continuation of your previous thread.

I still want to use the DAC and if the quality is too bad, I see what I will do. Also CrossRoads did an amazing work here: http://forum.arduino.cc/index.php?topic=180769.0

The Adafruit article says they can get reasonable audio out of their shield that uses an SPI DAC. Do you think they used an SPI interface for arbitrary reasons?

Consider this: When using a 16MHz AVR, an SPI DAC can be updated 80 times faster than an equivalent I2C DAC.

Ok, I didn't know that! And where do they connect the SDcard? Isn't it an SPI?

If I use 2-2R ladder as a DAC, is faster?

avr_fred:
Consider this: When using a 16MHz AVR, an SPI DAC can be updated 80 times faster than an equivalent I2C DAC.

Make that 20 times.
400kHz (fastmode) vs. 8Mhz.

Don't think Arduino supports the chip's high speed I2C mode (3.4Mhz).
Leo..

Wawa:
Make that 20 times.
400kHz (fastmode) vs. 8Mhz.

Don't think Arduino supports the chip's high speed I2C mode (3.4Mhz).
Leo..

If I choose a DUE, will it be better?

So, is there any way to play a .wav file from SDcard and send/listen it from MPC4725 DAC ?

After a lot of searching I managed to play a 22kHz 16-bit audio track stored to an SPI SDcard, from the MPC4725 DAC, but it is too noisy and too loud...! I mean I can understand the volume variations, but not the song. Is there any solution to this?

The code I use is this: