There are example Python scripts to produce musical tones and even to play a music file (presumably MP3 - although I’ve also tried WAV) and these scripts don’t work for me. I know the speaker on the Picar-X is working because the TTS scripts all work fine.
An example: this piece of script…
from robot_hat import Music, Buzzer
import time
m = Music() # create a music object
buzzer = Buzzer(“P0”)
m.tempo(120) # set current tempo to 120 beat per minute
print("Music object: ",m)
print("Note Middle C: ",m.note(“Middle C”))
play middle C, D, E, F ,G, A, B every 1 beat.
buzzer.play(m.note(“Middle C”), m.beat(1))
Produces this output…
pygame 2.1.2 (SDL 2.0.9, Python 3.7.3)
Hello from the pygame community. Contribute - pygame wiki
Music object: <robot_hat.music.Music object at 0xb5b4d170>
Note Middle C: 523.25
Traceback (most recent call last):
File “/home/pi/picar-x/myExamples/tryMusic.py”, line 12, in
buzzer.play(m.note(“Middle C”), m.beat(1))
File “/usr/local/lib/python3.7/dist-packages/robot_hat-1.0.2-py3.7.egg/robot_hat/modules.py”, line 166, in play
self.freq(freq)
File “/usr/local/lib/python3.7/dist-packages/robot_hat-1.0.2-py3.7.egg/robot_hat/modules.py”, line 159, in freq
self.pwm.freq(freq)
AttributeError: ‘str’ object has no attribute ‘freq’
The code in question is all copied from supplied code apart from some extra print() commands that I added at the top.
I also tried this (based on modifying an example)…
song = ‘/home/pi/Music/peace.mp3’
m.music_set_volume(80)
print(‘Music duration’,m.sound_length(song))
m.background_music(song, loops=1, volume=80)
time.sleep(5)
m.sound_play(song)
This runs to completion with no errors reported but does not make any sound.
Anybody got this kind of thing working on the Picar-X?
P.S. I couldn’t find a control on this forum to enable code to be pasted so I’ve bracketed my code and results with bold lines before and after.