simple sound player: PWM audio output, with DMA, without timer interrupt

Post your cool example code here.
stevestrong
Posts: 1832
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

simple sound player: PWM audio output, with DMA, without timer interrupt

Post by stevestrong » Sun Oct 29, 2017 9:20 am

I have a project in which I need some short sound effects, without reading WAV files from SD card.

To output PWM audio, current solutions use timer interrupt to update the new value in the compare output register.
This version uses the DMA (channel 7) triggered by timer (4) update event to do the same, so the processor has now plenty of time to do other tasks.
Speaker out on PB6 (timer 4 ch 1).
No interrupts are used, although one could use the DMA transfer end IRQ to do some stuff.

EDIT: - I added the possibility to use DMA interrupt. For this just enable the define in line 10.

This example uses a half second long WAV file (converted to C code) with 8 bit resolution, 16 kHz sampling rate, stored in the flash (~8kB data).
The timer prescaler and preload registers are optimized in order to have a maximum peek to peek output range for 0-255 PWM values.

Have fun.

Needed patches:
https://github.com/rogerclarkmelbourne/ ... 2/pull/366
https://github.com/rogerclarkmelbourne/ ... 2/pull/367

[quiz: what does the sound say?]
Attachments
STM32_SoundPlayer.ino
(5.88 KiB) Downloaded 35 times
sound_data.h
(52.62 KiB) Downloaded 24 times

User avatar
RogerClark
Posts: 7494
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: simple sound player: PWM audio output, with DMA, without timer interrupt

Post by RogerClark » Sun Oct 29, 2017 9:57 pm

Thanks Steve...

Does this run on the F103?

stevestrong
Posts: 1832
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: simple sound player: PWM audio output, with DMA, without timer interrupt

Post by stevestrong » Sun Oct 29, 2017 10:21 pm

Yes, it runs on blue pill.
It can be adapted for F4 as well, the DMA part needs some change for that.

User avatar
RogerClark
Posts: 7494
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: simple sound player: PWM audio output, with DMA, without timer interrupt

Post by RogerClark » Mon Oct 30, 2017 12:33 am

Thanks

I'll try it when I get from free time

ChrisMicro
Posts: 308
Joined: Fri Mar 24, 2017 4:51 pm
Location: Germany

Re: simple sound player: PWM audio output, with DMA, without timer interrupt

Post by ChrisMicro » Mon Oct 30, 2017 3:04 pm

Some time ago I also made some sound player. for the STM32F103 in the GENERIC repo.
It has no DMA but the sound quality was OK.
It is only a sine wave but could easily be extended for playing a wav.

stevestrong
Posts: 1832
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: simple sound player: PWM audio output, with DMA, without timer interrupt

Post by stevestrong » Mon Oct 30, 2017 3:11 pm

Yes, I saw that, and also the bluepill delta-sigma examples.
By those examples, however, I still don't understand how do you convert the sampling frequency to bit output frequency.
It would help a lot if you could make at least a hand-written scheme of the working principle (not for the pure delta-sigma, but of the whole concept).

Anyway, the main point here is that my example does NOT use the timer ISR, just the DMA. ;)

ChrisMicro
Posts: 308
Joined: Fri Mar 24, 2017 4:51 pm
Location: Germany

Re: simple sound player: PWM audio output, with DMA, without timer interrupt

Post by ChrisMicro » Mon Oct 30, 2017 3:53 pm

Anyway, the main point here is that my example does NOT use the timer ISR, just the DMA.
Yes, it is a good idea to use the DMA to avoid processing load.
If it would have been earlier available, I would have used your example ;-)

stevestrong
Posts: 1832
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: simple sound player: PWM audio output, with DMA, without timer interrupt

Post by stevestrong » Mon Oct 30, 2017 4:17 pm

I had some more thoughts on this.

The audio signal level, given by the WAV sample value, is actually the duty-cycle of the PWM.
Now, it is desirable to move the PWM base frequency from the sampling one to an upper frequency region in order to avoid PWM frequency disturbance on the audio output.
That is why good PWM DACs are using frequencies of 140kHz and above.

I think we could do the same trick, at least for 8 bit resolution signals, using SPI.
Outputting an array of 8 x 32bit words (= 256 bits) with high clock frequency we could simulate the same duty cycle of the audio signal.
On each audio sample frequency tick we just have to update the array values correspondingly to the required duty cycle.
Using SPI1 with 36MHz clock we could achieve 36MHz / 256 = 140.625 kHz PWM base frequency for 8 bit resolution.

I will try this.

ChrisMicro
Posts: 308
Joined: Fri Mar 24, 2017 4:51 pm
Location: Germany

Re: simple sound player: PWM audio output, with DMA, without timer interrupt

Post by ChrisMicro » Mon Oct 30, 2017 5:57 pm

I think the ear can't hear frequencies above 20 kHz. Therefore PWM frequencies above 20kHz are always enough.
It might have other reasons when they use 140kHz in the PWM Audio-DACs.
But what really matters: the resolution. The more bits, the better.

There are various tricks in the mozzi library to increase the DAC resolution.
Can you record the results of your various DAC implementations and place it somewhere?

stevestrong
Posts: 1832
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: simple sound player: PWM audio output, with DMA, without timer interrupt

Post by stevestrong » Mon Oct 30, 2017 6:25 pm

The reason to increase PWM frequency is to reduce distortions, see: http://www.openmusiclabs.com/learning/d ... dac.1.html, chapter 4.
So, if you have a 30kHz carrier, and you are creating a 5kHz tone, you will have signals at 30kHz +/- 5kHz, 10kHz, 15kHz, etc.

Post Reply