Generating quality sound from the speaker port of Macropad

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
RufusVS2020
 
Posts: 62
Joined: Thu Nov 19, 2020 6:31 pm

Generating quality sound from the speaker port of Macropad

Post by RufusVS2020 »

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.

User avatar
adafruit_support_mike
 
Posts: 67391
Joined: Thu Feb 11, 2010 2:51 pm

Re: Generating quality sound from the speaker port of Macrop

Post by adafruit_support_mike »

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

User avatar
RufusVS2020
 
Posts: 62
Joined: Thu Nov 19, 2020 6:31 pm

Re: Generating quality sound from the speaker port of Macrop

Post by RufusVS2020 »

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.

User avatar
adafruit_support_mike
 
Posts: 67391
Joined: Thu Feb 11, 2010 2:51 pm

Re: Generating quality sound from the speaker port of Macrop

Post by adafruit_support_mike »

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.

User avatar
RufusVS2020
 
Posts: 62
Joined: Thu Nov 19, 2020 6:31 pm

Re: Generating quality sound from the speaker port of Macrop

Post by RufusVS2020 »

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.

User avatar
adafruit_support_mike
 
Posts: 67391
Joined: Thu Feb 11, 2010 2:51 pm

Re: Generating quality sound from the speaker port of Macrop

Post by adafruit_support_mike »

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.

Locked
Please be positive and constructive with your questions and comments.

Return to “AdaBox! Show us what you made!”