def next(self, length): self.note.reset() self.duration.reset() self.velocity.reset() try: self.rest.reset() except Exception as e: print e print 'no rest to reset' seq = OSequence() total_length = 0 while total_length < length: next = self.note.next() next_duration = self.duration.next() next_rest = self.rest.next() if total_length+next_duration+next_rest > length: #if there isn't time for this note, fill the rest of the time with silence #print "fill"+str(total_length) duration_left = length - total_length next.update({DURATION_64: duration_left, OFFSET_64: total_length, "velocity": 0}) else: #print "add"+str(total_length) next.update({DURATION_64: next_duration, OFFSET_64: total_length, "velocity": self.velocity.next()}) total_length += next_duration+next_rest seq.append(next) #print 'made it' self.last = seq return self.last
class SebastianHandler(BaseHandler): def header(self, format, num_tracks, division): self.division = division self.tracks = [None] * num_tracks def track_start(self, track_num): self.current_sequence = OSequence() self.tracks[track_num] = self.current_sequence def note(self, offset, channel, midi_pitch, duration): offset_64 = 16 * offset // self.division duration_64 = 16 * duration // self.division point = Point({OFFSET_64: offset_64, MIDI_PITCH: midi_pitch, DURATION_64: duration_64}) self.current_sequence.append(point)
class SebastianHandler(BaseHandler): def header(self, format, num_tracks, division): self.division = division self.tracks = [None] * num_tracks def track_start(self, track_num): self.current_sequence = OSequence() self.tracks[track_num] = self.current_sequence def note(self, offset, channel, midi_pitch, duration): offset_64 = 16 * offset // self.division duration_64 = 16 * duration // self.division point = Point({ OFFSET_64: offset_64, MIDI_PITCH: midi_pitch, DURATION_64: duration_64 }) self.current_sequence.append(point)
down_degrees = [6, 4, 3, 2, 1, 2, 3, 4] final_degree = [1] sections = [ (up_degrees, 4, range(14)), (down_degrees, 4, range(13, -2, -1)), (final_degree, 32, range(1)), ] hanon_1 = OSequence() for section in sections: pattern, duration_64, offset = section for o in offset: for note in pattern: hanon_1.append({"degree": note + o, DURATION_64: duration_64}) hanon_1 = hanon_1 | degree_in_key_with_octave(Key("C", major_scale), 4) | midi_pitch() hanon_rh_1 = hanon_1 hanon_lh_1 = hanon_1 | transpose(-12) seq = hanon_lh_1 // hanon_rh_1 if __name__ == "__main__": f = open("hanon.mid", "w") s = SMF([seq]) s.write(f) f.close()