def test_duration(): duration = Duration.from_num_beats(2, Tempo(120)) assert duration.seconds == 1 tempo = Tempo(613) duration = Duration.from_num_beats(0.5, tempo) assert duration.beats(tempo) == 0.5 assert Duration.from_num_beats(1, Tempo(120)).samples(44100) == 22050
def test_measure_note_generate(): measure = Measure(Tempo(120), Signature(4, 4)) measure.add_note(Note('C#', 3), 0, 1) measure.add_note(Note('D', 3), 1, 1) measure.add_note(Note('E', 3), 2, 1) measure.add_note(Note('C#', 3), 3, 1) result = measure.generate_notes(Duration(1.5)) assert result[0].offset.seconds == (measure.notes[0].offset + Duration(1.5)).seconds pass
def test_duration_operators(): duration = Duration.from_num_beats(2, Tempo(120)) assert duration.seconds == 1 assert (duration * 2).seconds == 2 assert duration.seconds == 1 assert (2 * duration).seconds == 2 assert duration.seconds == 1 assert (duration + Duration(1)).seconds == 2 assert duration.seconds == 1 pass
def generate_note(self, note: Note, duration: Duration = Duration(1), velocity: float = 1): return self.generator.generate_note(note, duration.seconds, amplitude=velocity, phase=0)
def generate_notes(self): notes = [] offset = Duration(0) for measure in self.measures: notes += measure.generate_notes(offset) offset += measure.total_time() return notes
def test_max_duration(): measure = Measure(Tempo(120), Signature(4, 4)) measure.add_note(Note('C#', 3), 0, 1) measure.add_note(Note('F', 3), 2, 5) measure.add_note(Note('D', 3), 1, 1) measure.add_note(Note('E', 3), 2, 1) measure.add_note(Note('C#', 3), 3, 1) max_duration = get_max_duration(measure.generate_notes(Duration(1.5))) assert max_duration.seconds == 5.0
def add_note(self, note: Note, position: float, duration: float, velocity=1.0): """Add a note to a measure Args: note: notes to play position: position w.r.t. beats in the bar duration: duration w.r.t. beats in the bar (whole notes is 4 for 4/4) velocity: relative velocity of the note Returns: Measure: self """ position = Duration.from_num_beats(position, self.tempo) duration = Duration.from_num_beats(duration, self.tempo) self.notes.append(PositionedNote(note, position, duration, velocity)) return self
def generate_chord(self, chord: Chord, time: Duration): tempo = Tempo(120) measure = Measure(tempo, Signature(4, 4)) for n in chord.notes: measure.add_note(n, 0, duration=time.beats(tempo)) track = Track([measure]) return self.generate_track(track)