Picar-X and Music

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
File “/usr/local/lib/python3.7/dist-packages/robot_hat-1.0.2-py3.7.egg/robot_hat/modules.py”, line 159, in 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’

print(‘Music duration’,m.sound_length(song))
m.background_music(song, loops=1, volume=80)

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.

May I ask where you saw the relevant examples, the Buzzer in the robot_hat library is a class that controls the buzzer, you need to connect an external active buzzer, and you need to pass in a PWM object to initialize the buzzer = Buzzer(PWM("P0 "))

The playing music examples can be found here…
Sunfounder Robot Hat

I had made some small adaptions to it. In the light of your reply I have added PWM to the import and initialised the buzzer with Buzzer(PWM(“P0”)) instead of Buzzer(“P0”) and that has made a big difference to the outcome although is still does not play the musical notes.

I can now update my post as follows:-

First, I found out that If I comment out the code for the notes (all the buzzer.play lines) and then run from a PuTTY connection then it does play WAV and MP3 files on the buzzer in the robot hat on the Picar-X. It won’t play them from a desktop nor through a Terminal window opened via VNC.

Then from your msg I changed the initialisation of the Buzzer and now I no longer get any error messages for the code to play notes - but it doesn’t actually produce any sound.

This is the output…

pi@smPicar-X:~/picar-x/myExamples $ sudo python3 tryMusic.py
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 0xb66eebf0>
Note Middle C: 523.25
Playing musical notes
Playing a MP3 or WAV file
Music file /home/pi/Music/power.mp3
ALSA lib pcm.c:8424:(snd_pcm_recover) underrun occurred
Music duration 71.7

  1. There is an error in the API example of robot_hat, it should be modified as shown in the figure
  2. If you are using a buzzer without an amplifier circuit, you need to use it with a triode. You can refer to the following course: https://docs.sunfounder.com/projects/davinci-kit/en/latest/1.2.1_active_buzzer .html
  3. Note that to play music, you need to use sudo to run the program before there will be sound. In VNC, you need to run sudo killall pulseaudio before there will be sound.