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!
Embed and Play Midi Music in your Code (Python)
# 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
SQABRmQjRgABRGQjRAABQmMjQgABRmQjRgABSWQjSQABTGMjTAABU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CMeACUjZDUjABMnZDUnABMqZFkqADclZFklADceY1keADclZEclAEkeY1keADclZEclAEkeYzUe
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
diliupgabadamudalige
TrustyTony 888 pyMod Team Colleague Featured Poster
diliupgabadamudalige
TrustyTony 888 pyMod Team Colleague Featured Poster
RufusVS 0 Newbie Poster
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
William_27 0 Newbie Poster
Rufus_1 14 Newbie Poster
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.