Skip to content

Instantly share code, notes, and snippets.

@deladriere
Created December 14, 2015 06:24
Show Gist options
  • Save deladriere/50433f1dad90ba4fddb7 to your computer and use it in GitHub Desktop.
Save deladriere/50433f1dad90ba4fddb7 to your computer and use it in GitHub Desktop.
Code Text file into Midi
#! /usr/bin/env PYTHON
# -*- coding: utf-8 -*-
# Dectalk Express from Midi
from midiutil.MidiFile import MIDIFile #https://code.google.com/p/midiutil/
text="[:nh][:dv ap 90 pr 0] The 3 laws of Robotics. By Isaac Asimov. " \
"1. A robot may not injure a human being or, through inaction, allow a human being to come to harm. [:pp 100]" \
"2. A robot must obey the orders given to it by human beings, except where such orders would conflict with the First Law. [:pp 100]" \
"3. A robot must protect its own existence as long as such protection does not conflict with the First or Second Law.[:pp 100]" \
"[:pp 100] "
# Create the MIDIFile Object with 1 track
MyMIDI = MIDIFile(1)
# Tracks are numbered from zero. Times are measured in beats.
track = 0
time = 0
# Add track name and tempo.
MyMIDI.addTrackName(track,time,"The 3 Laws")
MyMIDI.addTempo(track,time,120)
# Add a note. addNote expects the following information:
track = 0
channel = 0
pitch = 0
time =0
duration = 0.1
volume = 100
# loop to the text and write ascii code into midi
for i in text:
pitch=ord(i)
MyMIDI.addNote(track,channel,pitch,time,duration,volume)
time=time+0.15 # 0.2 is too slow
# And write it to disk.
binfile = open("output.mid", 'wb')
MyMIDI.writeFile(binfile)
binfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment