Author Topic: Ran out of DACs - need ideas producing audio output from MCU  (Read 1829 times)

0 Members and 1 Guest are viewing this topic.

Offline bitmanTopic starter

  • Supporter
  • ****
  • Posts: 299
  • Country: us
  • Open Source Guy jabbing with Electronics
Ran out of DACs - need ideas producing audio output from MCU
« on: October 18, 2021, 08:57:01 pm »
I have a small ESP32 project which is connected to a HUB75 LED display and that takes care of about every interesting pin at least from a DAC/ADC perspective. Problem is, I would like to generate a bit of sound (for a kids game). The "old" obvious way is to use the build in DACs which the ESP32 has two of, but they're both taken by the default HUB75 connectors. I have a bit of a task to figure out if they're taken because they're DACs or because they're pins, but beside that - I am looking for a way to generate audio sound waves in the 1-5kHz range.

I'm looking for ideas to do this. I tried an external DAC that uses i2c but boy, that's slow and not even close to fun (at 9 bit resolution I don't even get 10Hz!). And for some reason it looks like my MCP4725 "turns off" the output when new data is sent - perhaps my "off the shelf" converter just isn't up to this task.  I was thinking of using PWM to generate a sound wave but that seems to require yet another MCU and that sorta kills the idea. So I need ideas.

My next experiment is setting up a timer and use a digital pin out, even though that's a square wave that may be "good enough for 4 years old".  But in the mean time I was thinking I may be overlooking the obvious here, so I wanted to ask.

At this point I may simply go with 555 and a fixed frequency I can turn on and off :D  But it would really be nice if I could have a small tune or speech from a WAV played.
 

Offline james_s

  • Super Contributor
  • ***
  • Posts: 21611
  • Country: us
Re: Ran out of DACs - need ideas producing audio output from MCU
« Reply #1 on: October 18, 2021, 10:36:15 pm »
Seems a bit silly to use a 555 if you've got a microcontroller. A digital pin on the micro can do pretty much anything a 555 can. On FPGAs I typically use a delta-sigma DAC but I don't know if that's feasible on a microcontroller.
 

Offline ve7xen

  • Super Contributor
  • ***
  • Posts: 1193
  • Country: ca
    • VE7XEN Blog
Re: Ran out of DACs - need ideas producing audio output from MCU
« Reply #2 on: October 18, 2021, 10:49:04 pm »
ESP32 has hardware PWM? I would use that.

HUB75 also seems to be a purely digital device so it likely wouldn't be hard to move it to other pins.
73 de VE7XEN
He/Him
 

Online langwadt

  • Super Contributor
  • ***
  • Posts: 4424
  • Country: dk
Re: Ran out of DACs - need ideas producing audio output from MCU
« Reply #3 on: October 18, 2021, 10:52:42 pm »
ESP32 has hardware PWM? I would use that.

HUB75 also seems to be a purely digital device so it likely wouldn't be hard to move it to other pins.

google to the rescue: https://deepbluembedded.com/esp32-pwm-tutorial-examples-analogwrite-arduino/
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4034
  • Country: nz
Re: Ran out of DACs - need ideas producing audio output from MCU
« Reply #4 on: October 18, 2021, 11:47:10 pm »
A few years ago now I did an experiment using a single GPIO to output sound. I even used the awful Arduino digitalWrite() to do it rather than manipulating the port directly.

It worked pretty well right off, and could probably be improved with a bit of work.



That's using the CPU to turn the GPIO on and off every single time, but if you have a PWM you'd only have to change the PWM setting at 22 kHz i.e. every 45 us.
« Last Edit: October 18, 2021, 11:54:01 pm by brucehoult »
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3036
  • Country: us
Re: Ran out of DACs - need ideas producing audio output from MCU
« Reply #5 on: October 19, 2021, 12:38:20 am »
For that vintage arcade game sound you could use an old sound synthesizer chip:

SAA1099 & Arduino - How to DIY -- Jacob Field
https://youtu.be/0UxQgYc3ELU
 

Offline augustol

  • Contributor
  • Posts: 41
  • Country: br
Re: Ran out of DACs - need ideas producing audio output from MCU
« Reply #6 on: October 19, 2021, 02:29:22 am »
Have you seen the ESP8266Audio github? I'm actually researching to see if I can get the ESP32 to output a PWM for each channel of a bluetooth received audio for a project. I've seen this, but haven't had the time to go in deeper. I want to check if I can transform the OutputWAV option in a PWM output.

Don't know if it will be helpful, but might be interesting to take a look.

https://github.com/earlephilhower/ESP8266Audio
 
The following users thanked this post: james_s

Online radiolistener

  • Super Contributor
  • ***
  • Posts: 3352
  • Country: ua
Re: Ran out of DACs - need ideas producing audio output from MCU
« Reply #7 on: October 19, 2021, 07:24:36 am »
You can connect external audio codec to STM32. On the other hand, you can connect some programmable sound generator which was very popular in 80-90 for the music and sound effects in computer games. Such as AY8910 or Yamaha YM2149. It has 3 channels which you can mix into stereo. Each channel allows to setup it's own frequency or noise, or both together, also each channel has 16 bit DA converter for volume, also it allows to switch volume to be controlled with one of 16 predefined waveform with programmable period. PSG requires much less resources than using PCM audio to generate sound effects and music in the game.

The player for PSG chip just needs to execute hundred asm opcodes every 20 ms to get a music like this:



typical music size in the memory is about 2-4 kB, and there are a tons of music for PSG chips, you can download it from internet.


Here is how it's channels output (before stereo mixer) looks on oscilloscope:
« Last Edit: October 19, 2021, 07:49:34 am by radiolistener »
 

Offline bitmanTopic starter

  • Supporter
  • ****
  • Posts: 299
  • Country: us
  • Open Source Guy jabbing with Electronics
Re: Ran out of DACs - need ideas producing audio output from MCU
« Reply #8 on: October 19, 2021, 04:40:20 pm »
Have you seen the ESP8266Audio github? I'm actually researching to see if I can get the ESP32 to output a PWM for each channel of a bluetooth received audio for a project. I've seen this, but haven't had the time to go in deeper. I want to check if I can transform the OutputWAV option in a PWM output.

Don't know if it will be helpful, but might be interesting to take a look.

https://github.com/earlephilhower/ESP8266Audio

I'm trying to move to the ESP32 to gain the performance boost and memory it provides; that said I do like my old Wemo D1 minis.  For ESP32 I have seen this https://diyi0t.com/i2s-sound-tutorial-for-esp32/ which I may end up with since it bypassed the DAC and takes care of the low resolution of the DAC too).  While I'm definitely not a fan of break-out boards, it will do for a prototype right now.

PWM never really seemed like a real solution. It too is just 8 bit and I don't think it's precision is good enough for sound generation even if 8 bit would work. 
 

Offline bitmanTopic starter

  • Supporter
  • ****
  • Posts: 299
  • Country: us
  • Open Source Guy jabbing with Electronics
Re: Ran out of DACs - need ideas producing audio output from MCU
« Reply #9 on: October 19, 2021, 04:46:44 pm »
ESP32 has hardware PWM? I would use that.

HUB75 also seems to be a purely digital device so it likely wouldn't be hard to move it to other pins.

That's what I'm coming to too - the only drawback is that I would need to customize a "standard library" but that's not a biggie. I'll update here once I've done some testing.  That said, the onboard DACs may not really be an option if they are 8 bit only (according to specs they are).
 

Offline bitmanTopic starter

  • Supporter
  • ****
  • Posts: 299
  • Country: us
  • Open Source Guy jabbing with Electronics
Re: Ran out of DACs - need ideas producing audio output from MCU
« Reply #10 on: October 19, 2021, 04:48:17 pm »
Seems a bit silly to use a 555 if you've got a microcontroller. A digital pin on the micro can do pretty much anything a 555 can. On FPGAs I typically use a delta-sigma DAC but I don't know if that's feasible on a microcontroller.

Yes and no. It would free up the MCU from having to loop/interrupt while updating a screen at 10-20FPS, and instead I would just have an "enable" digital pin that would turn on/off sound.  But yeah, it's a bit simple and not what I really want.
 

Offline ve7xen

  • Super Contributor
  • ***
  • Posts: 1193
  • Country: ca
    • VE7XEN Blog
Re: Ran out of DACs - need ideas producing audio output from MCU
« Reply #11 on: October 19, 2021, 06:19:31 pm »
ESP32 has hardware PWM? I would use that.

HUB75 also seems to be a purely digital device so it likely wouldn't be hard to move it to other pins.

That's what I'm coming to too - the only drawback is that I would need to customize a "standard library" but that's not a biggie. I'll update here once I've done some testing.  That said, the onboard DACs may not really be an option if they are 8 bit only (according to specs they are).

8-bit is fine for basic sound effects; at least considerably better than the square wave that you were considering.

I believe the PWM peripheral offers up to 16-bit, but resolution will trade off against PWM frequency (which is typically the peripheral clock / <2^BIT_DEPTH>), and you probably don't have a fast enough clock available to run it at 20KHz. But maybe you can get 12-bit @ ~20KHz (80MHz / 2^12 = 19531) which should give you something like 8KHz of audio bandwidth depending on your filters etc.

If you want 'high fidelity' you will need a dedicated audio DAC. Either of the above options is probably fine for the use case you're describing though.
73 de VE7XEN
He/Him
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3036
  • Country: us
Re: Ran out of DACs - need ideas producing audio output from MCU
« Reply #12 on: October 20, 2021, 02:07:04 am »
I've read that the ESP32 supports I2S (with DMA), so maybe you could use that and a I2S amp, e.g.:

https://www.adafruit.com/product/3006
 

Offline james_s

  • Super Contributor
  • ***
  • Posts: 21611
  • Country: us
Re: Ran out of DACs - need ideas producing audio output from MCU
« Reply #13 on: October 22, 2021, 12:20:27 am »
Another option if you want something simple and versatile is one of those ICs that can play back digitized sounds. I think some of them will play MP3 or WAV files right off a SD card and they are controllable via a simple interface. Then you can use whatever sounds you want, and update them easily at any time.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf