Package jazzparser :: Package misc :: Package chordlabel :: Module midi
[hide private]
[frames] | no frames]

Source Code for Module jazzparser.misc.chordlabel.midi

 1  from __future__ import absolute_import 
 2  """Midi processing for midi chord labeler. 
 3   
 4  """ 
 5  """ 
 6  ============================== License ======================================== 
 7   Copyright (C) 2008, 2010-12 University of Edinburgh, Mark Granroth-Wilding 
 8    
 9   This file is part of The Jazz Parser. 
10    
11   The Jazz Parser is free software: you can redistribute it and/or modify 
12   it under the terms of the GNU General Public License as published by 
13   the Free Software Foundation, either version 3 of the License, or 
14   (at your option) any later version. 
15    
16   The Jazz Parser is distributed in the hope that it will be useful, 
17   but WITHOUT ANY WARRANTY; without even the implied warranty of 
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
19   GNU General Public License for more details. 
20    
21   You should have received a copy of the GNU General Public License 
22   along with The Jazz Parser.  If not, see <http://www.gnu.org/licenses/>. 
23   
24  ============================ End license ====================================== 
25   
26  """ 
27  __author__ = "Mark Granroth-Wilding <mark.granroth-wilding@ed.ac.uk>"  
28   
29  from midi import NoteOnEvent 
30    
31 -def midi_to_emission_stream(segmidi, remove_empty=True, unique_notes=False):
32 """ 33 Get a list of emissions from the midi stream's note on events. 34 35 Returns a 2-tuple of the list of emissions and their 36 corresponding start times in midi ticks. 37 38 @type segmidi: L{jazzparser.data.input.SegmentedMidiInput} 39 @param segmidi: midi input 40 @type remove_empty: bool 41 @param remove_empty: remove any chunks that have no observations in them 42 (default True) 43 44 """ 45 chunks = [] 46 start_times = [] 47 tick_unit = segmidi.tick_unit 48 49 for segment in segmidi: 50 segment_start = segment.segment_start 51 start_times.append(segment_start) 52 note_ons = [ev for ev in segment.trackpool if isinstance(ev, NoteOnEvent)] 53 54 # Produce an observation for every event 55 chunk = [] 56 for ev in note_ons: 57 pc = ev.pitch % 12 58 chunk.append(pc) 59 chunks.append(chunk) 60 61 # Get rid of duplicate values in the chunks (octaves) 62 if unique_notes: 63 chunks = [list(set(c)) for c in chunks] 64 if remove_empty: 65 # Remove chunks that have no observations in them 66 chunks = [ems for ems in chunks if len(ems) > 0] 67 chunks_times = zip(chunks, start_times) 68 69 # Return a tuple of the chunks and the start times 70 return zip(*chunks_times)
71