Skip to content

Conversation

@ArcaneNibble
Copy link
Collaborator

This is a basic speaker driver for the EV3. The API surface exposed to user code is the same Speaker class as other hubs.

This runs the hardware PWM module at a fixed frequency using asymmetrical PWM and fetches samples using a FIQ handler on the ARM core.

This implementation struggles to play higher-frequency notes due to interactions with the way the Speaker class is implemented. Specifically, the Speaker class always generates a waveform of the same number of samples (128 samples) but keeps increasing the sampling rate. This driver always outputs samples to the hardware at a fixed rate and does not implement a proper resampler, so it ends up badly distorting the waveform. However, beeps and notes around octaves 2-4 work.

@coveralls
Copy link

Coverage Status

coverage: 57.239%. remained the same
when pulling db1119a on ArcaneNibble:audio
into 49f716f on pybricks:master.

@dlech
Copy link
Member

dlech commented Jul 16, 2025

For beeps on the EV3, we want to just set the PWM period equal to 1 / frequency of the beep (TBPRD) with a duty cycle proportional to the volume (CMPB) to get a square wave. This is how the official LEGO firmware and ev3dev does it. It will have a better frequency range.

I'm not sure why we put the waveform generator in the MicroPython class. It is specific to SPIKE Prime because it has an actual audio peripheral rather than a PWM. So that need to get moved to the specific driver for that platform and we need to introduce a new pbdrv_sound_beep() method that sets the beep frequency. Then use that in the MicroPython Speaker class.

So we shouldn't need the FIQ quite yet.

@ArcaneNibble
Copy link
Collaborator Author

Ah, but doing that will make it even harder to implement custom wave shapes, which looks to at least be a vaguely-planned feature?

@dlech
Copy link
Member

dlech commented Jul 16, 2025

which looks to at least be a vaguely-planned feature?

It's not something on our roadmap. The person that wrote the sound driver for SPIKE Prime was having fun with ideas. 😄

@ArcaneNibble
Copy link
Collaborator Author

Okay, I'll close this and reimplement it as you've suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants