def extract_performances( quantized_sequence, start_step=0, min_events_discard=None, max_events_truncate=None, max_steps_truncate=None, num_velocity_bins=0, split_instruments=False, note_performance=False): """Extracts one or more performances from the given quantized NoteSequence. Args: quantized_sequence: A quantized NoteSequence. start_step: Start extracting a sequence at this time step. min_events_discard: Minimum length of tracks in events. Shorter tracks are discarded. max_events_truncate: Maximum length of tracks in events. Longer tracks are truncated. max_steps_truncate: Maximum length of tracks in quantized time steps. Longer tracks are truncated. num_velocity_bins: Number of velocity bins to use. If 0, velocity events will not be included at all. split_instruments: If True, will extract a performance for each instrument. Otherwise, will extract a single performance. note_performance: If True, will create a NotePerformance object. If False, will create either a MetricPerformance or Performance based on how the sequence was quantized. Returns: performances: A python list of Performance or MetricPerformance (if `quantized_sequence` is quantized relative to meter) instances. stats: A dictionary mapping string names to `statistics.Statistic` objects. """ sequences_lib.assert_is_quantized_sequence(quantized_sequence) stats = dict([(stat_name, statistics.Counter(stat_name)) for stat_name in ['performances_discarded_too_short', 'performances_truncated', 'performances_truncated_timewise', 'performances_discarded_more_than_1_program', 'performance_discarded_too_many_time_shift_steps', 'performance_discarded_too_many_duration_steps']]) if sequences_lib.is_absolute_quantized_sequence(quantized_sequence): steps_per_second = quantized_sequence.quantization_info.steps_per_second # Create a histogram measuring lengths in seconds. stats['performance_lengths_in_seconds'] = statistics.Histogram( 'performance_lengths_in_seconds', [5, 10, 20, 30, 40, 60, 120]) else: steps_per_bar = sequences_lib.steps_per_bar_in_quantized_sequence( quantized_sequence) # Create a histogram measuring lengths in bars. stats['performance_lengths_in_bars'] = statistics.Histogram( 'performance_lengths_in_bars', [1, 10, 20, 30, 40, 50, 100, 200, 500]) if split_instruments: instruments = set(note.instrument for note in quantized_sequence.notes) else: instruments = set([None]) # Allow only 1 program. programs = set() for note in quantized_sequence.notes: programs.add(note.program) if len(programs) > 1: stats['performances_discarded_more_than_1_program'].increment() return [], stats.values() performances = [] for instrument in instruments: # Translate the quantized sequence into a Performance. if note_performance: try: performance = NotePerformance( quantized_sequence, start_step=start_step, num_velocity_bins=num_velocity_bins, instrument=instrument) except NotePerformanceTooManyTimeShiftSteps: stats['performance_discarded_too_many_time_shift_steps'].increment() continue except NotePerformanceTooManyDurationSteps: stats['performance_discarded_too_many_duration_steps'].increment() continue elif sequences_lib.is_absolute_quantized_sequence(quantized_sequence): performance = Performance(quantized_sequence, start_step=start_step, num_velocity_bins=num_velocity_bins, instrument=instrument) else: performance = MetricPerformance(quantized_sequence, start_step=start_step, num_velocity_bins=num_velocity_bins, instrument=instrument) if (max_steps_truncate is not None and performance.num_steps > max_steps_truncate): performance.set_length(max_steps_truncate) stats['performances_truncated_timewise'].increment() if (max_events_truncate is not None and len(performance) > max_events_truncate): performance.truncate(max_events_truncate) stats['performances_truncated'].increment() if min_events_discard is not None and len(performance) < min_events_discard: stats['performances_discarded_too_short'].increment() else: performances.append(performance) if sequences_lib.is_absolute_quantized_sequence(quantized_sequence): stats['performance_lengths_in_seconds'].increment( performance.num_steps // steps_per_second) else: stats['performance_lengths_in_bars'].increment( performance.num_steps // steps_per_bar) return performances, stats.values()
def extract_performances(quantized_sequence, start_step=0, min_events_discard=None, max_events_truncate=None, num_velocity_bins=0): """Extracts a performance from the given quantized NoteSequence. Currently, this extracts only one performance from a given track. Args: quantized_sequence: A quantized NoteSequence. start_step: Start extracting a sequence at this time step. min_events_discard: Minimum length of tracks in events. Shorter tracks are discarded. max_events_truncate: Maximum length of tracks in events. Longer tracks are truncated. num_velocity_bins: Number of velocity bins to use. If 0, velocity events will not be included at all. Returns: performances: A python list of Performance instances. stats: A dictionary mapping string names to `statistics.Statistic` objects. """ sequences_lib.assert_is_quantized_sequence(quantized_sequence) stats = dict([(stat_name, statistics.Counter(stat_name)) for stat_name in [ 'performances_discarded_too_short', 'performances_truncated', 'performances_discarded_more_than_1_program' ]]) if sequences_lib.is_absolute_quantized_sequence(quantized_sequence): steps_per_second = quantized_sequence.quantization_info.steps_per_second # Create a histogram measuring lengths in seconds. stats['performance_lengths_in_seconds'] = statistics.Histogram( 'performance_lengths_in_seconds', [5, 10, 20, 30, 40, 60, 120]) else: steps_per_bar = sequences_lib.steps_per_bar_in_quantized_sequence( quantized_sequence) # Create a histogram measuring lengths in bars. stats['performance_lengths_in_bars'] = statistics.Histogram( 'performance_lengths_in_bars', [1, 10, 20, 30, 40, 50, 100, 200, 500]) # Allow only 1 program. programs = set() for note in quantized_sequence.notes: programs.add(note.program) if len(programs) > 1: stats['performances_discarded_more_than_1_program'].increment() return [], stats.values() performances = [] # Translate the quantized sequence into a Performance. if sequences_lib.is_absolute_quantized_sequence(quantized_sequence): performance = Performance(quantized_sequence, start_step=start_step, num_velocity_bins=num_velocity_bins) else: performance = MetricPerformance(quantized_sequence, start_step=start_step, num_velocity_bins=num_velocity_bins) if (max_events_truncate is not None and len(performance) > max_events_truncate): performance.truncate(max_events_truncate) stats['performances_truncated'].increment() if min_events_discard is not None and len( performance) < min_events_discard: stats['performances_discarded_too_short'].increment() else: performances.append(performance) if sequences_lib.is_absolute_quantized_sequence(quantized_sequence): stats['performance_lengths_in_seconds'].increment( performance.num_steps // steps_per_second) else: stats['performance_lengths_in_bars'].increment( performance.num_steps // steps_per_bar) return performances, stats.values()