Mercurial > hg > syncopation-dataset
view Syncopation models/readmidi.py @ 8:2c5df6a4a22f
updated music objects, removed old bar definition from rhythm
parser
author | christopherh <christopher.harte@eecs.qmul.ac.uk> |
---|---|
date | Thu, 02 Apr 2015 00:06:57 +0100 |
parents | ac882f5e6a11 |
children | a3ed7d2b57d8 |
line wrap: on
line source
# -*- coding: utf-8 -*- """ Created on Sat Mar 21 22:19:52 2015 @author: christopherh """ from midiparser import MidiFile, MidiTrack, DeltaTime, MidiEvent #from RhythmParser import Bar from MusicObjects import * def readMidiFile(filename): """ open and read a MIDI file, return a MidiFile object """ #create a midifile object, open and read a midi file midiFile = MidiFile() midiFile.open(filename, 'rb') midiFile.read() midiFile.close() return midiFile def getBars(midiFile, trackindex=1): track = midiFile.tracks[trackindex] # ignore dummy track 0 eventIdx = 0 numNotes = 0 noteonlist = [] noteOnFound==True while noteOnFound==True: (noteOnIdx, noteOnDelta, noteOnFound) = self.findEvent(track, eventIdx, lambda e: e.type == 'NOTE_ON') noteEvent = track.events[noteOnIdx] eventIdx = noteOnIdx + 1 def findEvent(track, eventStartIdx, lambdaExpr): ''' From code by Csaba Sulyok: Finds MIDI event based on lambda expression, starting from a given index. Returns a tuple of the following 3 elements: 1. event index where the lambda expression is true 2. aggregate delta time from event start index until the found event 3. flag whether or not any value was found, or we've reached the end of the event queue ''' eventIdx = eventStartIdx deltaTime = 0 while eventIdx < len(track.events) and not lambdaExpr(track.events[eventIdx]): if track.events[eventIdx].type == 'DeltaTime': deltaTime += track.events[eventIdx].time eventIdx += 1 success = eventIdx < len(track.events) return (eventIdx, deltaTime, success)