annotate Syncopation models/readmidi.py @ 10:a3ed7d2b57d8

updating main py files to point at new file names
author christopherh <christopher.harte@eecs.qmul.ac.uk>
date Fri, 03 Apr 2015 16:02:10 +0100
parents ac882f5e6a11
children 08c298f47917
rev   line source
christopher@6 1 # -*- coding: utf-8 -*-
christopher@6 2 """
christopher@6 3 Created on Sat Mar 21 22:19:52 2015
christopher@6 4
christopher@6 5 @author: christopherh
christopher@6 6 """
christopher@6 7
christopher@6 8 from midiparser import MidiFile, MidiTrack, DeltaTime, MidiEvent
christopher@6 9 #from RhythmParser import Bar
christopher@6 10
christopher@10 11 from music_objects import *
christopher@6 12
christopher@6 13
christopher@6 14
christopher@6 15
christopher@6 16
christopher@10 17 def read_midi_file(filename):
christopher@6 18 """ open and read a MIDI file, return a MidiFile object """
christopher@6 19
christopher@6 20 #create a midifile object, open and read a midi file
christopher@6 21 midiFile = MidiFile()
christopher@6 22 midiFile.open(filename, 'rb')
christopher@6 23 midiFile.read()
christopher@6 24 midiFile.close()
christopher@6 25
christopher@6 26 return midiFile
christopher@6 27
christopher@10 28 # def get_bars(midiFile, trackindex=1):
christopher@10 29 # """ returns a list of bar objects from a MidiFile object """
christopher@6 30
christopher@10 31 # # select a track to extract (default = 1, ignoring dummy track 0)
christopher@10 32 # track = midiFile.tracks[trackindex]
christopher@10 33 # eventIndex = 0
christopher@10 34 # numNotes = 0
christopher@6 35
christopher@10 36 # noteonlist = []
christopher@10 37 # noteOnFound==True
christopher@6 38
christopher@10 39 # while noteOnFound==True:
christopher@10 40 # (noteOnIndex, noteOnDelta, noteOnFound) = self.find_event(track, eventIndex, lambda e: e.type == 'NOTE_ON')
christopher@10 41 # noteEvent = track.events[noteOnIndex]
christopher@10 42 # eventIndex = noteOnIndex + 1
christopher@6 43
christopher@6 44
christopher@6 45
christopher@6 46
christopher@6 47
christopher@6 48
christopher@10 49 def find_event(track, eventStartIndex, lambdaExpr):
christopher@6 50 '''
christopher@6 51 From code by Csaba Sulyok:
christopher@6 52 Finds MIDI event based on lambda expression, starting from a given index.
christopher@6 53 Returns a tuple of the following 3 elements:
christopher@6 54 1. event index where the lambda expression is true
christopher@6 55 2. aggregate delta time from event start index until the found event
christopher@6 56 3. flag whether or not any value was found, or we've reached the end of the event queue
christopher@6 57 '''
christopher@6 58
christopher@10 59 eventIndex = eventStartIndex
christopher@6 60 deltaTime = 0
christopher@10 61 while eventIndex < len(track.events) and not lambdaExpr(track.events[eventIndex]):
christopher@10 62 if track.events[eventIndex].type == 'DeltaTime':
christopher@10 63 deltaTime += track.events[eventIndex].time
christopher@10 64 eventIndex += 1
christopher@6 65
christopher@10 66 success = eventIndex < len(track.events)
christopher@10 67 return (eventIndex, deltaTime, success)
christopher@6 68
christopher@6 69