-
-
Notifications
You must be signed in to change notification settings - Fork 80
[v2] [EV3] Minimum-viable speaker driver #342
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
Conversation
lib/pbio/drv/sound/sound_ev3.c
Outdated
| // The division factor of 40 gives decent coverage of the frequency range | ||
| // 64 Hz - 24 kHz that the other drivers support. | ||
| // (This is limited by the 16-bit period counter.) | ||
| #define PWM_TIMEBASE_FREQ (SOC_EHRPWM_0_MODULE_FREQ / 40) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessarily for this pull request, but we need to have a look at the clock tree.
In the official LEGO firmware and in ev3dev, we have the ASYNC3 clock mux selecting SYSCLK2 from PLL1 rather than PLL2. It is 132 MHz instead of 150MHz.
lib/pbio/drv/sound/sound_ev3.c
Outdated
| } | ||
|
|
||
| void pbdrv_beep_start(uint32_t frequency, uint16_t sample_attenuator) { | ||
| // This hardware doesn't implement volume control |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how we handle the volume control in ev3dev: https://github.com/ev3dev/lego-linux-drivers/blob/1b387f3bacb4ab8f623494d29a855de6163c8dec/ev3/legoev3_sound.c#L138-L151
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite understand what this is doing. Is this just changing the duty cycle? Shouldn't that only affect the timbre and not the volume?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With a sufficiently narrow PWM pulse, I think the low pass filter starts to kick in and thus attenuates the signal and decreases the volume.
|
Made some changes, but I don't quite understand how the volume control works. Could you please explain it? |
|
Apparently you're right, and this somewhat actually works. You can notice a timbre change around maximum volume, but it generally does get quieter once the duty cycle gets very short. |
This now allows sound drivers to support only beeping, only playing samples, or both.
Add a default volume configuration option for each platform that supports sound. The EV3 speaker can be quite loud, so we don't want it to default to 100%.
|
I rebased and added a commit to set the default volume to 75% since the beeps can be quite loud (also, I think this is what the official EV3 software does by default, so users could be expecting it). |
|
Once again, thanks for the excellent work! |
|
running The "REST" sound is a nasty BRRRR. Also tested with: # from pybricks.hubs import EV3Brick
from pybricks.hubs import ThisHub
from pybricks.tools import wait
# hub = EV3Brick()
hub = ThisHub()
hub.speaker.volume(100)
notes = []
for octave in range(2, 9): # 2 to 8
for note in ["A", "B", "C", "D", "E", "F", "G"]:
notes.append(note + str(octave) +"/8")
notes.append("R/1")
for n in range(len(notes) / 8):
print(notes[n*8:n*8+8])
hub.speaker.play_notes(notes[n*8:n*8+8])
wait(500)Could not upload the m3 files, so zipped them: Converted to mp4: EV3.REST.250719_112219.mp4octaves: EV3.octaves.250719_112431.mp4It may be my hearing but it seems on the primehub that on each octave from the A to the B note the pitch increases and to C it drops a bit and then the pitch goes up again. |
|
Oh that's fun. Apparently a rest plays a frequency of 0 Hz expecting that to turn the sound off, but that function wasn't implemented. As for the pitch jumping around, that's just because the API is excessively music-theory-brained:
So A3 and B3 come after G3 and are far above C3. |
|
Thanks, for the music theory, I am such a noob in that. So I should use C D E F G A B per octave. Sounds better! |
This is a basic speaker driver for the EV3. The API surface exposed to user code is the same Speaker class as other hubs.
Unlike v1, this driver directly generates a square wave of the desired frequency rather than playing back samples.
As a consequence, it does not support volume control. However, it plays the full note range better than v1.