Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

play MIDI files in python?

Tags:

I'm looking for a method to play midi files in python. It seems python does not support MIDI in its standard library. After I searched, I found some python midi librarys such as pythonmidi. However, most of them can only create and read MIDI file without playing function. I would like to find a python midi library including playing method. Any recommendations? Thanks!

like image 990
YeJiabin Avatar asked May 17 '11 11:05

YeJiabin


People also ask

How do I play a MIDI file?

MIDI files can be opened with Windows Media Player, VLC, WildMidi, TiMidity++, NoteWorthy Composer, WildMIDI, Synthesia, MuseScore, Amarok, Apple's Logic Pro, and very likely some other popular media players. To play one online, try Online Sequencer.

How do I view MIDI files?

How to open a MIDI file. You can open a MIDI file and play its contents in many audio players and editors. For example, you can open a MIDI file in VideoLAN VLC media player (multiplatform), Microsoft Windows Media Player (Windows), and Apple GarageBand (macOS).

Where can I play MIDI files?

If you want to just play MIDI files on your PC, you can use media player software like VLC. Thankfully, Windows Media Player also supports MIDI as input format. So, you don't need third-party software to play MIDI files. Just import a MIDI file into it and play it.


2 Answers

The pygame module can be used to play midi files.

http://www.pygame.org/docs/ref/music.html

See the example here:

http://www.daniweb.com/software-development/python/code/216979

a whole bunch of options available at:

http://wiki.python.org/moin/PythonInMusic

and also here which you can modify to suit your purpose: http://xenon.stanford.edu/~geksiong/code/playmus/playmus.py

like image 168
Vijay Avatar answered Sep 28 '22 00:09

Vijay


Just to add a minimal example (via DaniWeb):

# conda install -c cogsci pygame import pygame  def play_music(midi_filename):   '''Stream music_file in a blocking manner'''   clock = pygame.time.Clock()   pygame.mixer.music.load(midi_filename)   pygame.mixer.music.play()   while pygame.mixer.music.get_busy():     clock.tick(30) # check if playback has finished      midi_filename = 'FishPolka.mid'  # mixer config freq = 44100  # audio CD quality bitsize = -16   # unsigned 16 bit channels = 2  # 1 is mono, 2 is stereo buffer = 1024   # number of samples pygame.mixer.init(freq, bitsize, channels, buffer)  # optional volume 0 to 1.0 pygame.mixer.music.set_volume(0.8)  # listen for interruptions try:   # use the midi file you just saved   play_music(midi_filename) except KeyboardInterrupt:   # if user hits Ctrl/C then exit   # (works only in console mode)   pygame.mixer.music.fadeout(1000)   pygame.mixer.music.stop()   raise SystemExit 
like image 26
duhaime Avatar answered Sep 28 '22 00:09

duhaime