def testExtractPianorollSequences(self): music_testing_lib.add_track_to_sequence(self.note_sequence, 0, [(60, 100, 0.0, 4.0)]) quantized_sequence = sequences_lib.quantize_note_sequence( self.note_sequence, steps_per_quarter=1) seqs, _ = pianoroll_pipeline.extract_pianoroll_sequences( quantized_sequence) self.assertLen(seqs, 1) seqs, _ = pianoroll_pipeline.extract_pianoroll_sequences( quantized_sequence, min_steps_discard=2, max_steps_discard=5) self.assertLen(seqs, 1) self.note_sequence.notes[0].end_time = 1.0 self.note_sequence.total_time = 1.0 quantized_sequence = sequences_lib.quantize_note_sequence( self.note_sequence, steps_per_quarter=1) seqs, _ = pianoroll_pipeline.extract_pianoroll_sequences( quantized_sequence, min_steps_discard=3, max_steps_discard=5) self.assertEmpty(seqs) self.note_sequence.notes[0].end_time = 10.0 self.note_sequence.total_time = 10.0 quantized_sequence = sequences_lib.quantize_note_sequence( self.note_sequence, steps_per_quarter=1) seqs, _ = pianoroll_pipeline.extract_pianoroll_sequences( quantized_sequence, min_steps_discard=3, max_steps_discard=5) self.assertEmpty(seqs)
def testExtractNonZeroStart(self): music_testing_lib.add_track_to_sequence(self.note_sequence, 0, [(60, 100, 0.0, 4.0)]) quantized_sequence = sequences_lib.quantize_note_sequence( self.note_sequence, steps_per_quarter=1) seqs, _ = pianoroll_pipeline.extract_pianoroll_sequences( quantized_sequence, start_step=4, min_steps_discard=1) self.assertEmpty(seqs) seqs, _ = pianoroll_pipeline.extract_pianoroll_sequences( quantized_sequence, start_step=0, min_steps_discard=1) self.assertLen(seqs, 1)
def testExtractPianorollMultiProgram(self): music_testing_lib.add_track_to_sequence( self.note_sequence, 0, [(60, 100, 0.0, 4.0), (64, 100, 0.0, 3.0), (67, 100, 1.0, 2.0)]) self.note_sequence.notes[0].program = 2 quantized_sequence = sequences_lib.quantize_note_sequence( self.note_sequence, steps_per_quarter=1) seqs, _ = pianoroll_pipeline.extract_pianoroll_sequences( quantized_sequence) self.assertEmpty(seqs)
def _generate(self, input_sequence, generator_options): if len(generator_options.input_sections) > 1: raise mm.SequenceGeneratorError( 'This model supports at most one input_sections message, but got %s' % len(generator_options.input_sections)) if len(generator_options.generate_sections) != 1: raise mm.SequenceGeneratorError( 'This model supports only 1 generate_sections message, but got %s' % len(generator_options.generate_sections)) # This sequence will be quantized later, so it is guaranteed to have only 1 # tempo. qpm = mm.DEFAULT_QUARTERS_PER_MINUTE if input_sequence.tempos: qpm = input_sequence.tempos[0].qpm steps_per_second = mm.steps_per_quarter_to_steps_per_second( self.steps_per_quarter, qpm) generate_section = generator_options.generate_sections[0] if generator_options.input_sections: input_section = generator_options.input_sections[0] primer_sequence = mm.trim_note_sequence(input_sequence, input_section.start_time, input_section.end_time) input_start_step = mm.quantize_to_step(input_section.start_time, steps_per_second, quantize_cutoff=0) else: primer_sequence = input_sequence input_start_step = 0 if primer_sequence.notes: last_end_time = max(n.end_time for n in primer_sequence.notes) else: last_end_time = 0 if last_end_time > generate_section.start_time: raise mm.SequenceGeneratorError( 'Got GenerateSection request for section that is before or equal to ' 'the end of the NoteSequence. This model can only extend sequences. ' 'Requested start time: %s, Final note end time: %s' % (generate_section.start_time, last_end_time)) # Quantize the priming sequence. quantized_primer_sequence = mm.quantize_note_sequence( primer_sequence, self.steps_per_quarter) extracted_seqs, _ = pianoroll_pipeline.extract_pianoroll_sequences( quantized_primer_sequence, start_step=input_start_step) assert len(extracted_seqs) <= 1 generate_start_step = mm.quantize_to_step(generate_section.start_time, steps_per_second, quantize_cutoff=0) # Note that when quantizing end_step, we set quantize_cutoff to 1.0 so it # always rounds down. This avoids generating a sequence that ends at 5.0 # seconds when the requested end time is 4.99. generate_end_step = mm.quantize_to_step(generate_section.end_time, steps_per_second, quantize_cutoff=1.0) if extracted_seqs and extracted_seqs[0]: pianoroll_seq = extracted_seqs[0] else: raise ValueError('No priming pianoroll could be extracted.') # Ensure that the track extends up to the step we want to start generating. pianoroll_seq.set_length(generate_start_step - pianoroll_seq.start_step) # Extract generation arguments from generator options. arg_types = { 'beam_size': lambda arg: arg.int_value, 'branch_factor': lambda arg: arg.int_value, } args = dict((name, value_fn(generator_options.args[name])) for name, value_fn in arg_types.items() if name in generator_options.args) total_steps = pianoroll_seq.num_steps + (generate_end_step - generate_start_step) pianoroll_seq = self._model.generate_pianoroll_sequence( total_steps, pianoroll_seq, **args) pianoroll_seq.set_length(total_steps) generated_sequence = pianoroll_seq.to_sequence(qpm=qpm) assert (generated_sequence.total_time - generate_section.end_time) <= 1e-5 return generated_sequence