def annotate(path): midi = representation.MidiFile(path) (name, version, track, singletrack) = midi.parsename(midi.name) key = '{0}-{1}-{2}'.format(name, version, track) parts = ['percussion', 'bass', 'accompaniment', 'melody', None] part = None choice = -1 index = rbsearch.load_file('data/realbooks/index.csv') hits = rbsearch.find(index, name.replace('_', ' ')) score = 'No score found' if len(hits) > 0: score = 'Scores found' header = '>>> [{0}]\t[#{1}: {2}]\t[{3}]\t({4})'.format(name, track, midi['1'].name.strip(), midi['1'][1].instrument(), score) while choice not in [5, 6]: # Show options: play file,don't add to corpus choice = commandline.menu(header, ['Play file', 'Annotate', 'View info', 'Drop to shell', 'Search score', 'Save and next', 'Skip', 'Abort']) # Play file if choice == 0: midi['1'].play(gui=True) # Annotate elif choice == 1: part = parts[commandline.menu('Choose value for \'part\' attribute.', parts)] # View info elif choice == 2: print 'Name:\t\t{0}\nPath:\t\t{1}\nInstrument:\t{2}\n'.format(\ midi.name, path, midi['1'][0].instrument()) # Drop to shell elif choice == 3: code.interact(local=locals()) elif choice == 4: if len(hits) == 0: print 'Sorry! No score found' if raw_input('Would you like to search manually? (y/n) ') == 'y': query = raw_input('Search for: ') hits2 = rbsearch.find(index, query) if len(hits2) == 0: print 'No results' continue (song, book) = rbsearch.choose_book(index, hits2) rbsearch.view(song, book, 'data/realbooks/songs/') continue (song, book) = rbsearch.choose_book(index, hits) rbsearch.view(song, book, 'data/realbooks/songs/') # Abort elif choice == 7: if raw_input('Are you sure? (y/n) ') is 'y': exit(0) if choice == 5: return {'path':path, 'part':part} if choice == 6: return None
def choose_book(index, results, stdscr=None): if stdscr: song = results[cgui.menu(stdscr, "Select a song", results)] else: song = results[commandline.menu("Select a song", results)] locations = zip(books, index[song]) bookhits = [] for book in books: if index[song][book]: bookhits.append(book) if stdscr: book = bookhits[cgui.menu(stdscr, "Select a book", bookhits)] else: book = bookhits[commandline.menu("Select a book", bookhits)] return (song, book)
def selectfile(collection=None, song=None, version=None, track=None): level = 1 midifile = None while level > 0 and not midifile: if level == 1 and not collection: choice = commandline.menu("Choose collection", collections()) if choice == -1: level -= 1 continue else: level += 1 collection = collections()[choice] elif level == 2 and not song: choice = commandline.menu("Choose song", songs(collection=collection)) if choice == -1: level -= 1 collection = None continue else: level += 1 song = songs(collection=collection)[choice] elif level == 3 and not version: choice = commandline.menu("Choose version", versions(song, collection=collection)) if choice == -1: level -= 1 song = None continue else: level += 1 version = versions(song, collection=collection)[choice] elif level == 4 and not track: singletrack = False track = 0 if len(tracks(song, version, collection=collection)) > 0: singletrack = True choice = commandline.menu("Choose track", tracks(song, version, collection=collection)) if choice == -1: level -= 1 version = None continue track = tracks(song, version, collection=collection)[choice] else: level += 1 elif level < 4: level += 1 else: return load(song, version, track, singletrack, collection=collection)
def commandline(self): seq = self.seq inp = '' print 'Command line midi player. Type help for a list of commands' while inp is not 0 and inp is not 'exit': try: inp = raw_input(':') except(EOFError, KeyboardInterrupt): print break if inp == 'stop': seq.control(seq.STOP, None) elif inp == 'play' or inp == 'p': seq.control(seq.PLAY, None) elif inp == 'metricality' or inp == 'm': seq.midifile[str(seq.currenttrack)].isMetrical() elif inp == 'pause': seq.control(seq.PAUSE, None) elif inp == 'status' or inp == 's': print seq.status elif inp == 'h': seq.control(seq.SETTIME, seq.time-1) elif inp == 'l': seq.control(seq.SETTIME, seq.time+1) elif inp == 'track' or inp == 't': if seq.midifile: choice = commandline.menu('Choose a track',\ ['Track #{0}.\tName: {1}\tNumber of notes: {2}'.format(\ seq.midifile[t].n, seq.midifile[t].name, len(seq.midifile[t]))\ for t in seq.tracklist()]) if choice < 0: continue seq.control(seq.LOADTRACK, seq.tracklist()[choice]) elif inp == 'load' or inp == 'f': choice = commandline.menu('Choose a file', sorted(midi.names())) if choice < 0: continue print 'Loading file' seq.control(seq.LOADFILE, midi.loadname(sorted(midi.names())[choice])) elif inp == 'output' or inp == 'device' or inp == 'd' or inp == 'o': choice = commandline.menu('Choose a midi device', seq.devicelist()) if choice < 0: continue seq.control(seq.SETOUTPUT, choice)
def run(): cross_validate(5) directory = os.listdir('.')[commandline.menu('', os.listdir('.'))] files = os.listdir(directory) standard = re.compile('([a-z_]+-[0-9]+-[0-9]+)-[0-9]+') standards = {} for f in files: m = standard.match(f) if m: name = m.group(0) if not name in standards: standards[name] = m.group(1) for s in standards.keys(): parses = [] exp = re.compile('{0}-parse_([0-9])+$'.format(s)) print s for f in files: m = exp.match(f) if m: parses.append((int(m.group(1)), m.group(0))) parses = sorted(parses, key=lambda x: x[0]) print '{0} parses found.'.format(len(parses)) symbols = [] results = [] for (number, parse) in parses: S = pickle.load(open('{0}/{1}'.format(directory, parse))) p = probability(S, model) results.append((p, number)) print 'Parse {0}: {1}'.format(number, p) if len(results) > 0: print 'Best parse: {0}'.format(max(results, key=lambda x: x[0])) while True: choice = -1 if len(results) > 1: choice = commandline.menu('View tree?', [str(result[0]) for result in results]) if choice == -1: break os.system('evince "{0}/{1}"'.format(directory, '{0}-parse_{1}.pdf'.format(s, results[choice][1])))
def interactive(path=path, songspath=songspath): index = load_file(path) while True: query = raw_input('Say a name! ').lower() results = find(index, query) if len(results) > 0: (song, book) = choose_book(index, results) print "What would you like to do?" while True: choice = commandline.menu("Please select", ["View", "Continue", "Quit"]) if choice is 0: view(song, book, songspath) if choice is 1: break if choice is 2: exit(0)
def filtermanually(): choice = commandline.menu('Choose an input collection', files.collections()) if choice != -1: inp = files.collections()[choice] else: return outp = raw_input('Specify output collection: ') if outp in files.collections(): if raw_input('Warning: collection exists, continue? (y/n)') != 'y': return else: os.mkdir('{0}{1}'.format(files.corpuspath, outp)) songs = files.songs(collection=inp) midifiles = {} counter = 1 for song in songs: print 'Song {0} of {1}'.format(counter, len(songs)) counter += 1 versions = files.versions(song, collection=inp) for version in versions: tracks = files.tracks(song, version, collection=inp) for track in tracks: decide(song, version, track, inp, outp)
def transcribe(): corpus = annotations.loadAll() choice = commandline.menu('Choose item', [x.name for x in corpus]) annotation = corpus[choice] score = annotation.transcribe() score.show()
d = data.Data(datafile) for item, attribs in d.data.iteritems(): print '{0}'.format(item) for key, value in attribs.iteritems(): print '\t{0}: {1}'.format(key, value) def shell(): code.interact(local=locals()) def quit(): exit(0) options = [\ ('Run quick \'n dirty tests', quickndirty),\ ('Transcribe standard', transcribe),\ ('Check corpustool', check_corpus),\ ('Cross validate temperley cross entropy', cross_validate),\ ('Test annotation tool', testtool),\ ('Check corpus', check_corpus),\ ('Search realbooks', search),\ ('Drop to shell', shell),\ ('Quit', quit)] #('Run quick \'n dirty tests', quickndirty),\ #('Parse realbook index', parse_index),\ #('Parse realbook index (NEW)', parse_newindex),\ #('Search realbooks', search),\ #('Drop to shell', shell),\ #('Split realbooks', split),\ #('Review datafile', review_datafile),\ while commandline.menu('What shall we do?', options, executableOptions=True) != -1: pass
def select(results): name = sorted(results.keys())[commandline.menu('', sorted(results.keys()))] return results[name]
#!/usr/bin/python from jazzr.rhythm import groupingparser as gp from jazzr.corpus import annotations from jazzr.tools import commandline, rbsearch import math, os, pickle, re directory = os.listdir('.')[commandline.menu('', os.listdir('.'))] files = os.listdir(directory) standard = re.compile('([a-z_]+-[0-9]+-[0-9]+)-[0-9]+') standards = {} for f in files: m = standard.match(f) if m: name = m.group(0) if not name in standards: standards[name] = m.group(1) for s in standards.keys(): parses = [] exp = re.compile('{0}-parse_([0-9])+.pdf'.format(s)) pdffiles = [] print s for f in files: m = exp.match(f) if m: pdffiles.append((int(m.group(1)), m.group(0))) pdffiles = sorted(pdffiles, key=lambda x: x[0]) print '{0} parses found.'.format(len(pdffiles)) for (part, pdf) in pdffiles:
def load(): files = sorted(os.listdir('results')) choice = commandline.menu('Choose', files) if choice != -1: f = open('results/{0}'.format(files[choice]), 'rb') return pickle.load(f)