Embed and Play Midi Music in your Code (Python)

vegaseat 5 Tallied Votes 12K Views Share

Another application of Python module base64, this time to embed midi music data in your program. You also need the module pygame to execute this Python code. Enjoy the music!

# play an embedded midi music file on your computer's sound card
# experiments with module pygame from: http://www.pygame.org/
# tested with Python25 and PyGame171      vegaseat      04sep2007

"""
# use this short program to create the base64 encoded midi music string
# (base64 encoding simply produces a readable string from binary data)
# then copy and paste the result into your pygame program ...
import base64
mid_file = "FishPolka.mid"
print "mid64='''\\\n" + base64.encodestring(open(mid_file, 'rb').read()) + "'''"
"""

import pygame
import base64

def play_music(music_file):
    """
    stream music with mixer.music module in blocking manner
    this will stream the sound from disk while playing
    """
    clock = pygame.time.Clock()
    try:
        pygame.mixer.music.load(music_file)
        print "Music file %s loaded!" % music_file
    except pygame.error:
        print "File %s not found! (%s)" % (music_file, pygame.get_error())
        return
    pygame.mixer.music.play()
    while pygame.mixer.music.get_busy():
        # check if playback has finished
        clock.tick(30)

mid64='''\
TVRoZAAAAAYAAQAGAJBNVHJrAAAAJwD/WAQEAhgIAP9ZAgAAAP9RAwfCOwD/BgpGaXNoLVBvbGth
AP8vAE1UcmsAAANoAP8hAQAA/wMOQWNjb3JkaW9uIEhpZ2gAwRUAsQd/ALEKQAD/AQpGaXNoIFBv
bGthAJFOZGhOAAFQYyNQAAFSZBdSADFQZCNQACVOYxdOADFMZCNMACVLYxdLADFJYyNJACVOZCNO
AAFNYyNNAAFOYyNOAAFQZCNQAAFOZCNOAAFMZCNMAAFLZCNLAAFHYyNHAAFLYyNLAAFKYyNKAAFL
YyNLAAFMZCNMAAFLZCNLAAFJZCNJAAFHYyNHAAFCYyNCAAE/YyM/AAFCYyNCAAFHZCNHAAFLZCNL
AAFOYxFOADdQYyNQACVQYyNQACVJYyNJACVJZIEPSQABTGQjTAABS2QjSwABTGMjTAABTmQjTgAB
TGMjTAABS2QjSwABSWMjSQABSGMjSAABSWQjSQABQmMjQgABRmQjRgABSWQjSQABTGQjTAABSWMj
SQABRmQjRgAB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EkeZFkeADcjZEcjAEkeZFkeADcjZEcjAAEe
ZCMeACUjZDUjABMnZDUnABMqZFkqADclZFklADceY1keADclZEclAEkeY1keADclZEclAEkeYzUe
ABMfZEcfAAEgZCMgACUiZEciAAEjZCMjACUiYyMiACUgZCMgACUeY0ceAAEjZCMjAG0eY1keADcj
Y0cjAEkeZFkeADcjYyMjACUeY0ceAAEjZCMjACUnYyMnACUoZEcoAEkjZEcjAEkcY1kcADcdY1kd
ADceZFkeADcgY1kgADclY1klADceY1keADcjYy8jABkiZC8iABkgZC8gABkeYy8eABkcY1kcADcd
ZFkdADceZFkeADcgY1kgADclY1klADceZFkeADcjZC8jABkeYy8eABkjYy8jAAD/LwBNVHJrAAAD
PQD/IQEAAP8DBURydW1zALkHcQC5CkAAmTluACZ3ACRuASYAAyQAADkADCZYASYADyZVASYADyZU
ASYADyZSASYADiZPASYADiZNASYADSZ6BCYAICZ5ACRtBCQAACYAQyZ6BCYARSZ5ACRsBCQAACYA
QiZ+BCYAFyZ8BCYADSZ/BCYAGCZ6AiRnAiYAAiQAQiZ/BCYARiRvAjluAiQAAjkAQip3BCoAQyRl
BCQARSpoBCoAQyRmBCQARSpqBCoAQyRoBCQARSpsBCoAQyRoBCQARSprBCoAQyRkBCQARSpuBCoA
QyRjBCQARSpkBCoAQyRmBCQARSppBCoAQyRmBCQARSpxBCoARCRmBCQARCp6BCoARCRoBCQARCpy
BCoARCRlBCQARCp/BCoARSRpBCQAQyp6BCoARCRqBCQAQyp6BCoARSRqBCQARCp6BCoARCRpBCQA
RCp/BCoARSRrBCQAQyV6ACp1BCoAQyUAASRnBCQARCV7ACp7BCoAQyUAASRlBCQARCV7ACp9BCoA
QyUAASRoBCQARCV7ACp1BCoAQyUAASRrBCQARCV6ACp+BCoAQyUAASRpBCQAQyp3ASV7ACRoAyoA
ASQAQyUAASRnBCQARCV7ACp6BCoAICV6IyUAAiRpBCQAHiUAJSV6ACp5BCoAQyUAASRrBCQARCV6
ACp4BCoAQyUAASRwBCQARCV6ACp8BCoAQyUAASRrBCQAQyp6ASV6AyoARCUAASRmBCQARCV6ACp9
BCoAQyUAASRpBCQARCV7ACp3BCoAQyUAASRqBCQARCV7ACp0BCoAQyUAASRqBCQARCV6ACp6BCoA
ICV6IyUAASRlBCQAHyUAJCp6ASV7AyoARCUAADlhAiR/AjkAAiQAH7kHdBG5B3ETmSV6ACp4BCoA
QyUAASRuBCQARCV6ACpuBCoAQyUAACRwBCQARCp3ASV7AyoARCUAASRnBCQAQyp2ASV7AyoARCUA
ASRuBCQARCV7ACp6BCoAQyUAASRtBCQAQyp6ASV7AyoARCUAASV6ACRsBCQAQiZ/ASUAAyYAFyZ8
BCYADSZ/BCYAGCZ5Ajl6ACRoAiYAAiQAgQs5AAD/LwA=
'''

music_file = "FishPolka.mid"

# convert back to a binary midi and save to a file in the working directory
fish = base64.b64decode(mid64)
fout = open(music_file,"wb")
fout.write(fish)
fout.close()

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)

try:
    # use the midi file you just saved
    play_music(music_file)
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
Member Avatar for diliupgabadamudalige
diliupgabadamudalige

What type of MIDI files do Python recognize?
This programme does not work in Python 2.7
I changed the fole name to one which is in the directory. Still it says file not found

Editor's note ...
Works just fine with Python 2.7.3, also if you just want to play existing MIDI files see
http://www.daniweb.com/software-development/python/code/454835/let-pygame-play-your-midi-or-mp3-files

TrustyTony 888 pyMod Team Colleague Featured Poster

Are you sure that you initialize the midi system like in this snippet before playing the file?

Member Avatar for diliupgabadamudalige
diliupgabadamudalige

Thak you for your kind response. I am not a very good coder. I am still learning so I don't understand your question. How do I do initialize the MIDI system? Also my MIDI playback is on Edirol SOund Canvas software synth which is configured to play ALL MIDI file on my computer. How do I initialize the MIDI system? What do I have to do? What is actually happening is that the MIDI files are not being recognized. None of them. I keep getting the error message "file not found'. Please help if you can.

TrustyTony 888 pyMod Team Colleague Featured Poster

lines 114 to 121 in vegaseat's code

RufusVS 0 Newbie Poster

It isn't necessary to write the file out to the disk and reread it. Just create a StringIO with the binary contents and pass that to play_music(). First, if you copied the program source file from the internet, you might want to do this:

mid64 = mid64.replace(" ","").replace("\n","")

to remove spaces and end-of-lines from the music data string. Then after this line:

fish = base64.b64decode(mid64)

add:

music_file = StringIO.StringIO(fish)

and pass that to

play_music(music_file)

(of course, don't forget to "import StringIO" at the top of the file!)

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Actually
base64.b64decode()
takes care of trailing newline characters

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Thanks RufusVS, you gave me the push to finally update this little gem to work with both versions of Python ...

""" PG_MidiBase64_py23.py
experiments with module pygame from: http://www.pygame.org/
play an embedded midi music file (base64 encoded)

use this short program to create the base64 encoded midi music string
(base64 encoding simply produces a readable string from binary data)
then copy and paste the result into your pygame program

Python2 code ...
import base64
mid_file = "FishPolka.mid"
b64_str = base64.encodestring(open(mid_file, 'rb').read())
print("mid64='''\\\n" + b64_str + "'''")

Python3 code ...
import base64
mid_file = "FishPolka.mid"
b64_str = base64.encodestring(open(mid_file, 'rb').read()).decode("utf8")
print("mid64='''\\\n" + b64_str + "'''")

updated to work with Python2 and Python3 by  vegaseat  16may2013
"""

import pygame
import base64
import io

def play_music(music_file):
    """
    stream music with mixer.music module in blocking manner
    this will stream the sound from disk while playing
    """
    pygame.mixer.music.load(music_file)
    clock = pygame.time.Clock()
    pygame.mixer.music.play()
    # check if playback has finished
    while pygame.mixer.music.get_busy():
        clock.tick(30)


mid64='''\
TVRoZAAAAAYAAQAGAJBNVHJrAAAAJwD/WAQEAhgIAP9ZAgAAAP9RAwfCOwD/BgpGaXNoLVBvbGth
AP8vAE1UcmsAAAN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BCoAQyRmBCQARSpqBCoAQyRoBCQARSpsBCoAQyRoBCQARSprBCoAQyRkBCQARSpuBCoA
QyRjBCQARSpkBCoAQyRmBCQARSppBCoAQyRmBCQARSpxBCoARCRmBCQARCp6BCoARCRoBCQARCpy
BCoARCRlBCQARCp/BCoARSRpBCQAQyp6BCoARCRqBCQAQyp6BCoARSRqBCQARCp6BCoARCRpBCQA
RCp/BCoARSRrBCQAQyV6ACp1BCoAQyUAASRnBCQARCV7ACp7BCoAQyUAASRlBCQARCV7ACp9BCoA
QyUAASRoBCQARCV7ACp1BCoAQyUAASRrBCQARCV6ACp+BCoAQyUAASRpBCQAQyp3ASV7ACRoAyoA
ASQAQyUAASRnBCQARCV7ACp6BCoAICV6IyUAAiRpBCQAHiUAJSV6ACp5BCoAQyUAASRrBCQARCV6
ACp4BCoAQyUAASRwBCQARCV6ACp8BCoAQyUAASRrBCQAQyp6ASV6AyoARCUAASRmBCQARCV6ACp9
BCoAQyUAASRpBCQARCV7ACp3BCoAQyUAASRqBCQARCV7ACp0BCoAQyUAASRqBCQARCV6ACp6BCoA
ICV6IyUAASRlBCQAHyUAJCp6ASV7AyoARCUAADlhAiR/AjkAAiQAH7kHdBG5B3ETmSV6ACp4BCoA
QyUAASRuBCQARCV6ACpuBCoAQyUAACRwBCQARCp3ASV7AyoARCUAASRnBCQAQyp2ASV7AyoARCUA
ASRuBCQARCV7ACp6BCoAQyUAASRtBCQAQyp6ASV7AyoARCUAASV6ACRsBCQAQiZ/ASUAAyYAFyZ8
BCYADSZ/BCYAGCZ5Ajl6ACRoAiYAAiQAgQs5AAD/LwA=
'''

# create a memory file object
try:
    # Python27
    midi_str = base64.b64decode(mid64)
    music_file = io.BytesIO(midi_str)
except TypeError:
    # Python3
    midi_bytes = base64.b64decode(mid64.encode())
    music_file = io.BytesIO(midi_bytes)

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)

try:
    # use the midi file object from memory
    play_music(music_file)
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
William_27 0 Newbie Poster

Does the short program have to be separate? I want to do this for multiple midi files in an iteration but it doesn't work because it just prints out '\' instead of all the numbers.

Please help?

Rufus_1 14 Newbie Poster

William_27, I think you misinterpret what the "short program" is used for.

The short program is run separately to create a python source code snippet which is the base64
encoding (in this case, stored in the variable mid64). This code snipped is pasted into the source
code of the playing program (so you don't have to read the .mid file from disk).

If you are doing this for multiple midi files in an iteration, you could put the mid64 strings into an
array, for example.

Your posting is 11 months old. Have you solve this by now, or dropped it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.