#!/usr/bin/python ##### modules needed: from sys import * from Tkinter import * from time import * from random import * import string ####### my modules: from midi_functions import * from scales import * ###### midi setup: if len(argv)<2: print "Usage: \n fractionmusic \n" exit() device_arg=str(argv[1]) open_port(device_arg) ###### init some variables: virgin=1 which=1 autocounter=0 new_note=60 old_note=60 ##### init some GUI-related variables: root=Tk() root.title("fractionmusic") runstate="START" tempo=IntVar(root) tempo.set(120) note_denominator=IntVar(root) note_denominator.set(11) note_numerator=1 rhythm_numerator=1 note_range=IntVar(root) note_range.set(3) center=IntVar(root) center.set(1) rhythm_denominator=IntVar(root) rhythm_denominator.set(7) rhythm_range=IntVar(root) rhythm_range.set(2) scale_string=StringVar(root) scale_string.set("modal") ####### scales: pentatonic_scale=create_scale(24,90,pentatonic) modal_scale=create_scale(24,90,modal) gypsy_scale=create_scale(24,90,gypsy) octatonic_scale=create_scale(24,90,octatonic) chromatic_scale=create_scale(24,90,chromatic) whole_tone_scale=create_scale(24,90,whole_tone) scale_choice=eval("%s_scale" % scale_string.get()) scale_size=len(scale_choice)-1 index=22 ##### evoked on start/stop button def reverse(): global runstate, old_note, which if runstate=="START": runstate="STOP!" buttons.RUN.config(text=runstate) main() else: runstate="START" which=1 buttons.RUN.config(text=runstate) note_off(0, old_note) ####### to config sliders after scale choice: def slider_config(): global scale_choice, scale_string, scale_size scale_choice=eval("%s_scale" % scale_string.get()) scale_size=len(scale_choice)-1 ###### Frames: menu=Menu(root) buttons=Frame(root) buttons.pack(side=BOTTOM, pady=4) sliders=Frame(root) sliders.pack(side=TOP, padx=5) ######### Widgets: scale_menu=Menu(menu) menu.add_cascade(label="Scales", menu=scale_menu) for scale in ["modal","pentatonic", "octatonic", "whole_tone", "gypsy", "chromatic", "tabla"]: exec("""scale_menu.add_radiobutton(label=\"%s\", variable=scale_string, value=\"%s\", command=slider_config)""" % (scale,scale)) root.config(menu=menu) buttons.RUN = Button(buttons, text=runstate, command=reverse) buttons.QUIT = Button(buttons, text='QUIT', foreground='red', command=root.quit) sliders.note_denominator=Scale(sliders, from_=3, to=101, orient=HORIZONTAL, length=250, width=16, variable=note_denominator, label="note denominator", showvalue=1) sliders.note_range=Scale(sliders, from_=1, to=12, orient=HORIZONTAL, length=250, width=16, variable=note_range, label="note_range", showvalue=1) sliders.center=Scale(sliders, from_=1, to=36, orient=HORIZONTAL, length=250, width=16, variable=center, label="center", showvalue=1) sliders.rhythm_denominator=Scale(sliders, from_=1, to=101, orient=HORIZONTAL, length=250, width=16, variable=rhythm_denominator, label="rhythm denominator", showvalue=1) sliders.rhythm_range=Scale(sliders, from_=1, to=12, orient=HORIZONTAL, length=250, width=16, variable=rhythm_range, label="rhythm range", showvalue=1) sliders.tempo=Scale(sliders, from_=30, to=220, orient=HORIZONTAL, length=250, width=16, variable=tempo, label="tempo", showvalue=1) ####### Packing: buttons.RUN.pack(side=LEFT, padx=8) buttons.QUIT.pack(side=LEFT, padx=8) sliders.note_denominator.pack(side=TOP, pady=2) sliders.note_range.pack(side=TOP, pady=2) sliders.center.pack(side=TOP, pady=2) sliders.rhythm_denominator.pack(side=TOP, pady=2) sliders.rhythm_range.pack(side=TOP, pady=2) sliders.tempo.pack(side=TOP, pady=2) ##################################################################### def main(): global runstate,new_note, old_note,virgin,note_numerator,note_denominator,scale_size, index, scale_choice,note_range, \ rhythm_denominator, rhythm_range, rhythm_numerator, center while runstate=="STOP!": if virgin==0: note_off(0, old_note) sleep(.01) ############ get new note; note_answer,note_remainder=divmod(note_numerator,note_denominator.get()) if note_remainder==0: note_remainder=1 index=bounce(note_answer+center.get(),scale_size) new_note=scale_choice[index] ########### get rhythm; rhythm_answer, rhythm_remainder=divmod(rhythm_numerator, rhythm_denominator.get()) if rhythm_remainder==0: rhythm_remainder=1 rhythm_answer=rhythm_answer+1 ############## send data; note_on(0,new_note,64) old_note=new_note sleep(15.0/tempo.get()*rhythm_answer) ########### update variables, etc. root.update() note_numerator=note_remainder*note_range.get() rhythm_numerator=rhythm_remainder*rhythm_range.get() virgin=0 ####### run the thing!!!! root.mainloop()