Posts: 1
Joined: Sun Jul 26, 2015 8:54 pm

No sound from pygame.midi

Sun Jul 26, 2015 9:17 pm

I am new to Raspberry Pi and am having trouble with some Python code.

Code: Select all

import pygame
import pygame.midi
from time import sleep

instrument = 0
note = 74
volume = 127


for id in range(pygame.midi.get_count()):
  print pygame.midi.get_device_info(id)

port = 0
midiOutput = pygame.midi.Output(port, 1)
for note in range(0,127):
del midiOutput
The code runs without errors but there is no sound is produced. I get the output:
  • ('ALSA', 'Midi Through Port-0', 0, 1, 0)
    ('ALSA', 'Midi Through Port-0', 1, 0, 0)
I can play .wav files from Python and other programs can make sounds (e.g. Sonic Pi).

Can anyone help?

Posts: 23
Joined: Mon Jul 09, 2012 7:31 pm
Location: Market Harborough

Re: No sound from pygame.midi

Tue Oct 27, 2015 11:28 pm

I'm having exactly the same problems - did you, by any chance, get to the bottom of this? It's proving very frustrating, just trying to get notes playing from Python.

Posts: 6
Joined: Sat Feb 07, 2015 10:55 pm
Location: New Paltz, NY

Re: No sound from pygame.midi

Sat Dec 05, 2015 10:56 pm

Same here. Any solution? Possible insight but I have no idea how to connect it to a software synthesizer.

User avatar
Posts: 4759
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: No sound from pygame.midi

Sun Dec 06, 2015 12:04 am

Look into fluidsynth. It's a good soft synth for the Pi, and it's in the repository.

Posts: 1284
Joined: Mon Oct 29, 2012 8:12 pm
Location: Vancouver Island
Contact: Website

Re: No sound from pygame.midi

Sun Dec 06, 2015 12:12 am

Consider timidity; it's already installed.

I use it for the Scratch MIDI instrument output but you will also need the requisite sound font files, which appear to take up about 150Mb of space - which is why they don't get installed by default. You might find the install script saved at download/file.php?id=12333 useful, though it is potentially Scratch specific. At least it should give you an idea about loading the sound font files.
Making Smalltalk on ARM since 1986; making your Scratch better since 2012

Posts: 6
Joined: Sat Feb 07, 2015 10:55 pm
Location: New Paltz, NY

Re: No sound from pygame.midi

Sun Dec 06, 2015 12:40 am

Thanks a ton for the timidity answer. I kind of assumed I wasn't going to get fast answers here because the thread had been quiet for a while, but I discovered timidity just now myself, then came back to post my findings and found yours :)

Posts: 764
Joined: Tue Aug 13, 2013 6:55 pm
Location: Dublin, Ireland

Re: No sound from pygame.midi

Sun Dec 06, 2015 8:04 pm

I'm not 100% about this, but I believe when using Pygame mixer, the mixer must be initialized before Pygame itself. So, I would try something like:

Code: Select all

pygame.mixer.pre_init(44100, -16, 2, 2048) # setup mixer to avoid sound lag
I'm assuming that other (non Pygame) sounds play normally.
Hope this helps,

Posts: 1
Joined: Sat Dec 17, 2016 12:20 pm

Re: No sound from pygame.midi

Sat Dec 17, 2016 12:32 pm

Hi Guys,

I was facing the same problem. I got to solve it as per below.

First check if you are facing the same problem as me.
Type in a new shell timidity <ValidPathOfAMidiFile>

If you see a message like
No instrument mapped to tone bank 0, program 49 - this instrument will not be heard

Then this means that you dont have the proper soundfonts installed. This is the reason why you cannot hear any midi output. To fix this I did
  • 1) sudo apt-get install fluid-soundfont-gm
    2) Now modify your timidity configuration to use the new fonts
    3) sudo nano /etc/timidity/timidity.cfg
    4) comment out the line "source /etc/timidity/freepats.cfg" and add a line "source /etc/timidity/fluidr3_gm.cfg"
    5) open a shell and run timidity -iA (this runs timidity as a server for midi note_on, note-off events)
    6) Note that timidity shows you the ports it is running on.
    6) open another shell and run aplaymidi -l (this should show you some ports with name timidity)
    7) run the command timidity <ValidMidiFileHere> again. You should hear the music now.
    8) In you python pygame program. You have to use the port for timidity server to run the music. (For my case timidity opened port=2)
    9) So modify your program something like below to hear a tone from r pi's speakers

Code: Select all


def initPlayer():
    global player
    player = pygame.midi.Output(TIMIDITYPORT) #this should be the correct port (like timidity)
    player.note_on(64, 127)
    player.note_off(64, 127)


Return to “Troubleshooting”