0

Generating quality sound from the speaker port of Macropad
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Generating quality sound from the speaker port of Macropad

by RufusVS2020 on Sun Aug 01, 2021 3:06 am

The speaker interface from the rp2040 is a single pin to an amplifier chip. The standard sound is simply a square wave sent to PIN_SPEAKER of the board. But the RP2040 has state machines and/or PWM output that should be able to give a much higher quality audio signal to the pin. Before I invest a lot of time and/or research into this, can I get some feedback as to if this is even possible? If not, why not? Are resources used up by MicroPython/CircuitPython? I did notice I had some time getting sound out with the Arduino library code, though I was able to get some sound with CircuitPython audio demo.

RufusVS2020
 
Posts: 35
Joined: Thu Nov 19, 2020 6:31 pm

Re: Generating quality sound from the speaker port of Macrop

by adafruit_support_mike on Tue Aug 03, 2021 1:31 am

How far do you want to go down the rabbit hole? Any nontrivial audio generation involves a *lot* of calculation.

adafruit_support_mike
 
Posts: 63701
Joined: Thu Feb 11, 2010 2:51 pm

Re: Generating quality sound from the speaker port of Macrop

by RufusVS2020 on Tue Aug 03, 2021 12:37 pm

adafruit_support_mike wrote:How far do you want to go down the rabbit hole? Any nontrivial audio generation involves a *lot* of calculation.


Even to play a .wav file? there is a method play_file in adafruit_macropad.py which doesn't seem to work well for me.

RufusVS2020
 
Posts: 35
Joined: Thu Nov 19, 2020 6:31 pm

Re: Generating quality sound from the speaker port of Macrop

by adafruit_support_mike on Tue Aug 03, 2021 5:12 pm

That’s much easier. Instead of trying to generate the audio data you only have to generate PWM duty cycles to match the samples in the WAV file.

Timer-based PWM would probably be more nuisance than it’s worth. You’d have to change the target value every counting cycle.

A state machine could be useful though.. create states that set the pins high or low for a fixed amount of time, then build a series of them where each delay is twice as long as the previous one. Then the machine could read samples one bit at a time, with the output signal and timing as side effects of stepping through the sequence.

I don’t know if the programmable logic in the RP2040 allows that kind of side effect, but it’s worth finding out. If so, the idea is feasible and would be a good starting point for playing with state machines.

adafruit_support_mike
 
Posts: 63701
Joined: Thu Feb 11, 2010 2:51 pm

Re: Generating quality sound from the speaker port of Macrop

by RufusVS2020 on Tue Aug 03, 2021 10:05 pm

adafruit_support_mike wrote:That’s much easier. Instead of trying to generate the audio data you only have to generate PWM duty cycles to match the samples in the WAV file.

Timer-baseyd PWM would probably be more nuisance than it’s worth. You’d have to change the target value every counting cycle.

A state machine could be useful though.. create states that set the pins high or low for a fixed amount of time, then build a series of them where each delay is twice as long as the previous one. Then the machine could read samples one bit at a time, with the output signal and timing as side effects of stepping through the sequence.

I don’t know if the programmable logic in the RP2040 allows that kind of side effect, but it’s worth finding out. If so, the idea is feasible and would be a good starting point for playing with state machines.


This is what I was thinking. I was hoping an RP2040 state machine could be programmed to output an arbitrary series of pulses at a high speed, and therefore could generate a PWM pulse train. I envisioned the slower, prone-to-jitter python program would feed one side of a fifo, and the deterministic state machine would stream out the other end.

RufusVS2020
 
Posts: 35
Joined: Thu Nov 19, 2020 6:31 pm

Re: Generating quality sound from the speaker port of Macrop

by adafruit_support_mike on Wed Aug 04, 2021 2:39 pm

That sounds reasonable.

Please post whatever you learn if you have time. The RP2040’s programmable logic is still new territory, and any information helps.

adafruit_support_mike
 
Posts: 63701
Joined: Thu Feb 11, 2010 2:51 pm

Please be positive and constructive with your questions and comments.