Frequently Asked Questions
Here are some issues you may encounter when using musicpy with solutions, some of them are existing bugs of the python libraries that musicpy has dependencies with, or just not giving you the desired result when the function parameters are set as default.
-
I get
On Linux, the pygame library uses the freepats sound libraries to play MIDI files, so you need to install freepats.pygame.error: Couldn't open /etc/timidity/freepats.cfg
error on Linux
Solution: open the terminal and run sudo apt-get install freepats
and it will fix the errors. This solution works mostly for Ubuntu, for other Linux distributions you need to search the internet to find the install command to install freepats for them, on some Linux distributions you may also need to install timidity++.
-
When I run the code in the IDE, the play function does not make any sound
When you use python IDE like Pycharm or VS Code, this issue will occur, since these IDE won't wait till the pygame's function that plays MIDI file ends, they will stops the whole process after all of the code are executed without waiting for the playback of the MIDI file. You won't encounter this issue with more interactive python IDE, such as Jupyter Notebook, Wing IDE, or directly use the interactive shell of python in terminal.
Solution: You can add wait=True
in the parameter of the play
function, which will block the function till the playback ends, so you can hear the sounds.
-
I notice that every time I play a musicpy note/chord/piece/track instance, there is a MIDI file generated in current directory, can musicpy just play what I write without writing a MIDI file?
Yes, the default mechanism of theplay
function of musicpy is firstly write the musicpy data structure to a MIDI file, and then use pygame's mixer module to play the MIDI file, but you can change to another mechanism to play internally without any MIDI file generated by setting the parametersave_as_file
of theplay
function to False. By doing this, the MIDI file data stream is generated and held internally, and pygame is able to play the MIDI file stream directly, there are no MIDI files will be generated.
Solution: Set the parameter save_as_file
of the play
function to False.