Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PWM based audio playback #4167

Merged
merged 2 commits into from
Feb 11, 2021
Merged

Conversation

tannewt
Copy link
Member

@tannewt tannewt commented Feb 9, 2021

@tannewt tannewt added audio rp2040 Raspberry Pi RP2040 labels Feb 9, 2021
@tannewt tannewt added this to the 6.2.0 milestone Feb 9, 2021
@tannewt tannewt requested a review from jepler February 9, 2021 23:43
@ladyada
Copy link
Member

ladyada commented Feb 9, 2021

tested, works pretty well!

@kevinjwalters
Copy link

Will stereo playback of samples work ok?

@tannewt
Copy link
Member Author

tannewt commented Feb 10, 2021

Will stereo playback of samples work ok?

Yes, it should work. The pins need to share a PWM slice under the hood.

@kevinjwalters
Copy link

kevinjwalters commented Feb 10, 2021

I saw #4143 but haven't yet worked out exactly what's going on there, is that related to this? Is there anything simple that documents "the slices" or is in the monster datasheet?

A cytron maker board is on the way to me which uses GP18 and GP19 for stereo audio.

@tannewt
Copy link
Member Author

tannewt commented Feb 10, 2021

I saw #4143 but haven't yet worked out exactly what's going on there, is that related to this?

This bug is fixed in main. (I'll close shortly.)

Is there anything simple that documents "the slices" or is in the monster datasheet?

It's in the pin function section. It's simple though. The slice is pin number / 2 % 8 and the channel is pin number %2. In words, starting at zero the next two pins share a slice, then the next two do and so forth until pin 16 which starts the pattern again.

A cytron maker board is on the way to me which uses GP18 and GP19 for stereo audio.

That should work. It will block PWM on pins GP2 and GP3 though.

Copy link
Member

@jepler jepler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problems spotted, but I didn't do any testing. (@ladyada did but didn't formally review the PR)

It's a bummer that the audio sample converter APIs I added didn't work out to use here, but I see why it wasn't used.

@tannewt
Copy link
Member Author

tannewt commented Feb 11, 2021

What converter APIs? Please open a long term issue for us to unify buffer conversion. This DMA stuff is too tied to buffer conversion at the moment.

@tannewt tannewt merged commit aae10fb into adafruit:main Feb 11, 2021
@jepler
Copy link
Member

jepler commented Feb 11, 2021

void audiosample_convert_u8m_s16s(int16_t *buffer_out, const uint8_t *buffer_in, size_t nframes);
void audiosample_convert_u8s_s16s(int16_t *buffer_out, const uint8_t *buffer_in, size_t nframes);
void audiosample_convert_s8m_s16s(int16_t *buffer_out, const int8_t *buffer_in, size_t nframes);
void audiosample_convert_s8s_s16s(int16_t *buffer_out, const int8_t *buffer_in, size_t nframes);
void audiosample_convert_u16m_s16s(int16_t *buffer_out, const uint16_t *buffer_in, size_t nframes);
void audiosample_convert_u16s_s16s(int16_t *buffer_out, const uint16_t *buffer_in, size_t nframes);
void audiosample_convert_s16m_s16s(int16_t *buffer_out, const int16_t *buffer_in, size_t nframes);

there are a bunch of these, for converting bit count, signedness, and mono-to-stereo. Declared in shared-module/audiocore/init.h.

@kevinjwalters
Copy link

kevinjwalters commented Feb 15, 2021

I just ran this

Adafruit CircuitPython 6.2.0-beta.2 on 2021-02-11; Raspberry Pi Pico with rp2040
>>> import pwmio
>>> import board
>>> pins = (board.GP0, board.GP1, board.GP2, board.GP3,
... board.GP4, board.GP5, board.GP6, board.GP7,
... board.GP8, board.GP9, board.GP10, board.GP11,
... board.GP12, board.GP13, board.GP14, board.GP15)
>>> pwms = [pwmio.PWMOut(p, frequency=2000, duty_cycle=3456) for p in pins]

On Cytron Maker Pi Pico which has LEDs on the pins. Even numbered ones light up, odd numbered ones stay off.

>>> pwms[1].duty_cycle = 65534
>>> pwms[1].duty_cycle = 65535

That doesn't help. Is this a bug?

[Added later]: I can get odd ones to light up but not evens with this, it must be the first one in each pair to be instantiated works:

>>> pwms_o = [pwmio.PWMOut(pins[p_idx], frequency=2000, duty_cycle=3456) for p_idx in range(1, len(pins), 2)]
>>> pwms_e = [pwmio.PWMOut(pins[p_idx], frequency=2000, duty_cycle=3456) for p_idx in range(0, len(pins), 2)]

Same issue exists on 6.2.0-beta.2-18-g2a467f137 on 2021-02-15.

@kevinjwalters
Copy link

I can't find libraries which hold PWMAudioOut and WaveFile on 6.2.0-beta.2:

Adafruit CircuitPython 6.2.0-beta.2 on 2021-02-11; Raspberry Pi Pico with rp2040
>>> import audiocore
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: no module named 'audiocore'
>>> import audiopwmio
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: no module named 'audiopwmio'

@tannewt
Copy link
Member Author

tannewt commented Feb 16, 2021

@kevinjwalters Please file new issues that refer to a PR. Discussion on a merged PR is hard to track.

(For those following along, the library finding issue was due to this PR not being in beta.2. It was merged after and is available in "absolute newest" builds.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
audio rp2040 Raspberry Pi RP2040
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement audiopwmio and enable audiocore
4 participants