Midi Sort (I went a little crazy one night and starting sorting notes in songs)

from midi.MidiOutFile import MidiOutFile
import os

def num2let(n):
    if n==c: return 'c'
    if n==d: return 'd'
    if n==e: return 'e'
    if n==f: return 'f'
    if n==g: return 'g'
    if n==a: return 'a'
    if n==b: return 'b'
    if n==c1: return 'c1'
    if n==d1: return 'd1'
    if n==e1: return 'e1'
    if n==f1: return 'f1'
    if n==g1: return 'g1'
    if n==a1: return 'a1'
    if n==b1: return 'b1'
    if n==c2: return 'c2'

c = 60
d = 62
e = 64
f = 65
g = 67
a = 69
b = 71
c1 = c+12
d1 = d+12
e1 = e+12
f1 = f+12
g1 = g+12
a1 = a+12
b1 = b+12
c2 = c1+12

class M:
    def __init__(self):
        out_file = 'out.mid'
        self.midi = MidiOutFile(out_file)
        self.curtime = 0
        # non optional midi framework
        self.midi.header()
        self.midi.start_of_track()

    def p(self, n):
        # musical events
        self.midi.update_time(self.curtime)
        self.midi.note_on(channel=0, note=n)
        self.curtime = 30
        self.midi.update_time(self.curtime)
        self.midi.note_off(channel=0, note=n)
        print num2let(n),

    def pause(self,n):
        self.midi.update_time(n)
        self.midi.note_on(channel=0, note=255)
        self.midi.note_off(channel=0, note=255)
        print ""

    def close(self):
        # non optional midi framework
        self.midi.update_time(0)
        self.midi.end_of_track()
        self.midi.eof()

    def play(self):
        mpcexe = '/Users/owner/Desktop/apps/media/mplayerc.exe'
        cmd = mpcexe + ' out.mid'
        os.system(cmd)
        
m = M()


def swap(lst,a,b):
    lst[a], lst[b] = lst[b], lst[a]
    m.p(lst[b])
    m.p(lst[a])
#    print lst[a],lst[b]
#    m.pause(30)

def playsong(lst):
    for y in lst:
        m.p(y)
    m.pause(30)

def bubblesort(lst):
    done = False
    while not done:
#        playsong(lst)
        
        done = True
        for x in range(len(lst) - 1):
            if lst[x] > lst[x + 1]:
                swap(lst, x, x + 1)
                done = False

def quicksort(lst, first=0, last=None):
    if last == None:
        last = len(lst) - 1
    if first >= last:
        return
    left = first
    right = last
    mid = lst[left]
    while left < right:
        while left < right and mid < lst[right]:
            right -= 1
        if left != right:
            m.p(lst[right])
        lst[left] = lst[right]        
        while left < right and lst[left] <= mid:
            left += 1
        if right != left:
            m.p(lst[left])
        lst[right] = lst[left]
    lst[left] = mid
    m.p(mid)
    quicksort(lst, first, left - 1)
    quicksort(lst, left + 1, last)

def mergesort(lst, first=0, last=None):
    if last == None:
        last = len(lst)
    #split the list in half
    if last - first <= 1:
        return
    mid = first + (last - first) / 2
    #run mergesort on each half
    mergesort(lst, first, mid)
    mergesort(lst, mid, last)
    #create a temp array with mid - first number of elements
    temp = range(mid - first)
    #and copy left half into temp
    for x in range(mid - first):
        temp[x] = lst[first + x]
    sp = first
    right = mid
    left = 0
    #compare smallest elements from left half and right half
    #put the smallest element in the next spot in the list
    while left < len(temp) and right < last:
        if lst[right] < temp[left]:
            lst[sp] = lst[right]
            right += 1
        else:
            lst[sp] = temp[left]
            left += 1
        m.p(lst[sp])
        sp += 1
    #copy any remaining elements from temp into the end of the array
    while left < len(temp):
        lst[sp] = temp[left]
        left += 1
        sp += 1


song1 = [c,g,e,f,d,a,b,c1]
song2 = [c1,g,a,b,d1,e1,f1,g1]
maryhad = [e,d,c,d,e,e,e,d,d,d,e,g,g,e,d,c,d,e,e,e,e,d,d,e,d,c]
song3 = [c1,a,b,d1,e1,g,f1,g1] #for bubblesort
song4 = [g, e,f, a,b, d,c1,d1] #for bubblesort
backw = [c1,b,a,g,f,e,d,c]
#song5 = [c1,a,b,e1,f1,e1,g,f1,g1] #for bubblesort
song6 = [c1,b,d1,e1,f1,g,f1,g1,c2]
song7 = [c1,d1,e1,f1,g1,e,f,g,a,b,c]
song8 = [c1,d1,e1,f1,g1,e,f,g,a,b,c1]

play=song8

#bubblesort(song2)
#mergesort(song1)
#playsong(song1)
#bubblesort(maryhad)
#mergesort(maryhad)
#quicksort(maryhad)
#quicksort(song1)
playsong(play)
m.pause(200)
bubblesort(play)
m.pause(200)
mergesort(play)
m.pause(200)
quicksort(play)
m.pause(200)
play.sort()
playsong(play)
m.pause(200)

m.close()

m.play()
Advertisements