Go Down

Topic: Arduino Due Audio Library and Performing Other Tasks During Playback (Read 1 time) previous topic - next topic

electrichotmatt

Has anyone been able to modify the Simple Audio Player sketch so that playback can happen without pausing other operations in the loop code for the Due board?  I'm not sure whether it's the buffer that's causing the pause or the Audio.write command, but I added a blinking light line of code in and when I press the button I've assigned to play my audio file, the light completely stops until the audio is done playing.  I'm hoping there is a way to perform an audio playback on both DACs as well as run the regular loop code simultaneously.  Shouldn't be too hard since it's pushing analog signals from a buffer, but I'm lost... Here's my test code.

Code: [Select]

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

unsigned long previousMillis = 0;
boolean ledstate = true;

void setup()
{
pinMode(5,OUTPUT); //this is the LED output
digitalWrite(5,ledstate);
pinMode(7,INPUT); //this is the button input, I just run a 10k resistor to GND to activate the sound
digitalWrite(7,HIGH); //internal pullup to apply a high voltage to the pin so we can pull it low to activate

  SPI.setClockDivider(4);
  Audio.begin(12800, 100); //my wav file was recorded at a slower bit rate
}

void loop()
{
  //flashing the LED every 250 milliseconds
  unsigned long currentMillis = millis();
   if(currentMillis-previousMillis > 250){
   previousMillis = currentMillis;
   if (ledstate == LOW){
     ledstate = HIGH;
   }
   else
   {
ledstate = LOW;
   }
   digitalWrite(5,ledstate);
}

if(digitalRead(7) == LOW){
  // open wave file from sdcard
  File myFile = SD.open("talking.wav");

  const int S=1024; // Number of samples to read in block
  short buffer[S];

  // until the file is not finished
  while (myFile.available()) {
    // read from the file into buffer
    myFile.read(buffer, sizeof(buffer));

    // Prepare samples
    int volume = 1024;
    Audio.prepare(buffer, S, volume);
    // Feed samples to audio
    Audio.write(buffer, S);

  }
  myFile.close();
}
}

electrichotmatt

Another thing I will note is that the first playback of the audio sounds good, but playing the audio after that sounds very scratchy for some reason... I have absolutely no clue why unless i should clear my buffer or something...

himijendrix

You have to put your code into the  while loop. But I'm also looking for a better way to play audio in the background while doing other things ...

Code: [Select]
  while (myFile.available()) {
...
  }


Does anybody know if it is possible to play two files parallel (by using both DACs and reading from the SD card alternating)?
The Audio library says that it writes the audio signal to DAC0 and DAC1, but actually only DAC0 is used, right?

Paul Stoffregen

This might be a bit off topic, but you may be interested in an alternate audio library I've been writing for Teensy.  It's still at a fairly early stage of development.  A major feature (which does work well) is playback in the background.  In fact, this new library lets you create many audio objects, interconnected in arbitrary ways, with automatic audio data flow between them, all updated in the background.  For example, you can create 3 wav file players, connect them to the inputs of a 4 channel mixer, and then connect the mixer to the DAC output, or an audio shield output, or both.

Unfortunately, this new library requires different hardware.  While the audio processing objects are relatively pure software, all the input & output objects are coded for the DMA hardware on Freescale's chip.  The library also makes heavy use of the Cortex-M4 special DSP accelerating instructions, which dramatically speed up some objects like the FFT, BiQuad filters, and waveform synthesis.

https://github.com/PaulStoffregen/Audio

himijendrix

@Paul
This sounds absolutely perfect. I think this is something many people could use for projects. Sometimes I think of something like an "operating system" which combines many tasks in one library, where you can add buttons which are already debounced, 7 Segments, Displays, Audio ^^ and so on ... like a simple LabView for Arduino

but I bought a DUE, because I thought there would be more libraries to handle this. But actually there are very little tutorials and libraries for the DUE available compared to the UNO.

But I think I have an idea to write my own program for playing two WAV files from the SD card.
The idea is to use this timer library https://github.com/ivanseidel/DueTimer and create two timers that read the WAV (starting from byte 44) at the given sample rate and then write it to the DAC0 and DAC1. That should do everything I need.

But I'm still thinking about what happens if two timers are used. Will they called both one after the other with the given frequency? What if other Input interrupts (Buttons and Hall Sensors will also be used) are coming in?

Paul Stoffregen

I'm curious to hear how using those timers work out.  I hope you'll post updates here, whether it works or does not work out.

Because this forum is for Arduino products, I'm reluctant to talk more about other hardware.  If you'd like to chat about the other library, there's a link on that github page to another forum.

Go Up