Package jazzparser :: Package taggers :: Package segmidi :: Module midi
[hide private]
[frames] | no frames]

Source Code for Module jazzparser.taggers.segmidi.midi

 1  from __future__ import absolute_import 
 2  """Midi processing for segmented midi taggers. 
 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, metric=True, 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 metric: bool 41 @param metric: include a metrical model. Each observation has a metrical 42 value associated with it. If this is False, the value will always be 0 43 @type remove_empty: bool 44 @param remove_empty: remove any chunks that have no observations in them 45 (default True) 46 47 """ 48 chunks = [] 49 start_times = [] 50 tick_unit = segmidi.tick_unit 51 52 for segment in segmidi: 53 segment_start = segment.segment_start 54 start_times.append(segment_start) 55 note_ons = [ev for ev in segment.trackpool if isinstance(ev, NoteOnEvent)] 56 57 # Produce an observation for every event 58 chunk = [] 59 for ev in note_ons: 60 if metric: 61 # Compute the metrical prominence value 62 bar_time = ev.tick - segment_start 63 if bar_time == 0: 64 rhythm = 0 65 elif bar_time == tick_unit/2: 66 rhythm = 1 67 elif bar_time == tick_unit/4 or bar_time == tick_unit*3/4: 68 rhythm = 2 69 else: 70 rhythm = 3 71 else: 72 # No metrical values: always 0 73 rhythm = 0 74 pc = ev.pitch % 12 75 chunk.append((pc, rhythm)) 76 chunks.append(chunk) 77 78 # Get rid of duplicate values in the chunks (octaves) 79 if unique_notes: 80 chunks = [list(set(c)) for c in chunks] 81 if remove_empty: 82 # Remove chunks that have no observations in them 83 chunks = [ems for ems in chunks if len(ems) > 0] 84 chunks_times = zip(chunks, start_times) 85 86 # Return a tuple of the chunks and the start times 87 return zip(*chunks_times)
88