Simple audio with DAC

riklaunim
Posts: 12
Joined: Wed Jul 19, 2017 1:20 pm

Simple audio with DAC

Postby riklaunim » Wed Jul 19, 2017 1:34 pm

I'm trying to use the built in DAC to play a simple/short audio message. As there seems to be no high level libraries for it I'm trying to do something similar to what http://highlowtech.org/?p=1963 showcase - get audio data as numeric array and then write to the DAC at a rate equal to the sample rate.

Code: Select all

int j;
for (j = 0; j < 26000; j++ ) {
      dacWrite(25, sample[j]);
}
The question is - how to time it correctly? delaying for 0.125 (1/8000) doesn't do the trick.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Simple audio with DAC

Postby kolban » Wed Jul 19, 2017 4:29 pm

How is your audio data encoded?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

riklaunim
Posts: 12
Joined: Wed Jul 19, 2017 1:20 pm

Re: Simple audio with DAC

Postby riklaunim » Thu Jul 20, 2017 8:38 am

Numeric values in an array from a 8-bit 8kHz wave file.

I looked at the timers example and the timer does make it work and ESP32 can say something :)

very crude code for this:

Code: Select all

const unsigned char sample[] = {
// here a lot of numbers - audio data
};

hw_timer_t * timer = NULL;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;

volatile uint32_t isrCounter = 0;
volatile uint32_t lastIsrAt = 0;

void IRAM_ATTR onTimer(){
  // Increment the counter and set the time of ISR
  portENTER_CRITICAL_ISR(&timerMux);
  isrCounter++;
  lastIsrAt = millis();
  portEXIT_CRITICAL_ISR(&timerMux);
  // It is safe to use digitalRead/Write here if you want to toggle an output
  if (isrCounter > 25000) { // hardcoded end...
      if (timer) {
      // Stop and free timer
      timerEnd(timer);
      timer = NULL;
      }
    }
  dacWrite(25, sample[isrCounter]);
}

void setup() {
  // Use 1st timer of 4 (counted from zero).
  // Set 80 divider for prescaler (see ESP32 Technical Reference Manual for more
  // info).
  timer = timerBegin(0, 80, true);

  // Attach onTimer function to our timer.
  timerAttachInterrupt(timer, &onTimer, true);

  // Set alarm to call onTimer function every second (value in microseconds).
  // Repeat the alarm (third parameter)
  timerAlarmWrite(timer, 125, true);

  // Start an alarm
  timerAlarmEnable(timer);
}

void loop() {
  
}


tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Simple audio with DAC

Postby tele_player » Fri Jul 21, 2017 2:45 pm

Going back to your first attempt, exactly how were you doing delay of .125? That should work, if you use delayMicroseconds(125).

If you use delay(0.125), the float 0.125 will be silently converted to int 0 , and it won't work.

riklaunim
Posts: 12
Joined: Wed Jul 19, 2017 1:20 pm

Re: Simple audio with DAC

Postby riklaunim » Fri Jul 21, 2017 2:49 pm

Didn't used delayMicroseconds - will have to check that out.

riklaunim
Posts: 12
Joined: Wed Jul 19, 2017 1:20 pm

Re: Simple audio with DAC

Postby riklaunim » Mon Jul 24, 2017 7:55 am

delayMicroseconds works too :)

riklaunim
Posts: 12
Joined: Wed Jul 19, 2017 1:20 pm

Re: Simple audio with DAC

Postby riklaunim » Tue Aug 01, 2017 9:15 am


XTronical
Posts: 2
Joined: Mon Aug 20, 2018 3:51 pm

Re: Simple audio with DAC

Postby XTronical » Mon Aug 20, 2018 5:17 pm

Bit of a late reply but I've written a high level library for producing sound via the DAC. Key points :

Samples can be any rate up to 44100Bps and the library will auto find this information from the sample supplied.

Multiple samples can be played at once (the library will mix the waves together to form one for output) and the samples to be mixed can be different Bps, the library will mix them all together correctly.

There are additional classes for playing sequences of samples, musical notes, different musical instrument sounds (similar to a synth).

It's interrupt driven so your main code can just get on with its task as your sound(s) play independently. I've used it on an Arcade game project where multiple sounds often play together.

See this link on my blog for a starter :

http://www.xtronical.com/introduction-to-dac-audio/

and these videos from my youtube channel will give you a feel for what it can do (note some will be out of date as the library has developed)

https://www.youtube.com/playlist?list=P ... KfXyjGdttZ

Unfortunately I've not had time yet to produce a manual/function/property list for the library yet, but the videos/ write ups and examples bundled with the library should help. If you want to look at how I've used it in my arcade project see :

https://youtu.be/J9mV2IcEvP0

Who is online

Users browsing this forum: Google [Bot] and 19 guests