From f4866a140ce35fcc5ce5f52b130d7db2cc852238 Mon Sep 17 00:00:00 2001 From: Samuel Janas Date: Tue, 7 Nov 2023 16:56:32 +0100 Subject: [PATCH] test to_midi, update trimming test. --- .gitignore | 1 + fortepyan/midi/structures.py | 2 +- tests/midi/test_structures.py | 27 +++++++++++++++++++-------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 1ce91b4..35c94c7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ tmp/ *.egg-info/ dist/ *.pyc +*.ipynb diff --git a/fortepyan/midi/structures.py b/fortepyan/midi/structures.py index 5675509..9af11ea 100644 --- a/fortepyan/midi/structures.py +++ b/fortepyan/midi/structures.py @@ -56,7 +56,7 @@ def trim(self, start: float, finish: float) -> "MidiPiece": """Trim the MidiPiece object between the specified start and finish time. This function takes two parameters, `start` and `finish`, which represent the start and end time in seconds, - and returns a new MidiPiece object that contains only the notes within the specified time range. + and returns a new MidiPiece object that contains only the notes that start within the specified time range. Args: - start (float): start time in seconds diff --git a/tests/midi/test_structures.py b/tests/midi/test_structures.py index e794b2c..1c6bffe 100644 --- a/tests/midi/test_structures.py +++ b/tests/midi/test_structures.py @@ -10,8 +10,8 @@ def sample_df(): df = pd.DataFrame( { "start": [0, 1, 2, 3, 4], - "end": [1, 2, 3, 4, 5], - "duration": [1, 1, 1, 1, 1], + "end": [1, 2, 3, 4, 5.5], + "duration": [1, 1, 1, 1, 1.5], "pitch": [60, 62, 64, 65, 67], "velocity": [80, 80, 80, 80, 80], } @@ -24,8 +24,8 @@ def sample_midi_piece(): df = pd.DataFrame( { "start": [0, 1, 2, 3, 4], - "end": [1, 2, 3, 4, 5], - "duration": [1, 1, 1, 1, 1], + "end": [1, 2, 3, 4, 5.5], + "duration": [1, 1, 1, 1, 1.5], "pitch": [60, 62, 64, 65, 67], "velocity": [80, 80, 80, 80, 80], } @@ -70,7 +70,7 @@ def test_missing_pitch(sample_df): def test_midi_piece_duration_calculation(sample_df): piece = MidiPiece(df=sample_df) - assert piece.duration == 5 + assert piece.duration == 5.5 def test_trim_within_bounds(sample_midi_piece): @@ -80,18 +80,18 @@ def test_trim_within_bounds(sample_midi_piece): trimmed_piece = sample_midi_piece.trim(2, 3) assert len(trimmed_piece.df) == 2, "Trimmed MidiPiece should contain 2 notes." assert trimmed_piece.df["start"].iloc[0] == 0, "New first note should start at 0 seconds." + assert trimmed_piece.df["pitch"].iloc[0] == 64, "New first note should have pitch 64." assert trimmed_piece.df["end"].iloc[-1] == 2, "New last note should end at 2 seconds." def test_trim_at_boundaries(sample_midi_piece): - trimmed_piece = sample_midi_piece.trim(0, 5) + trimmed_piece = sample_midi_piece.trim(0, 5.5) assert trimmed_piece.size == sample_midi_piece.size, "Trimming at boundaries should not change the size." def test_trim_out_of_bounds(sample_midi_piece): - # Assuming the behavior is to return an empty MidiPiece or raise an error with pytest.raises(IndexError): - _ = sample_midi_piece.trim(6, 7) # Out of bounds, should raise an error + _ = sample_midi_piece.trim(5.5, 8) # Out of bounds, should raise an error def test_trim_with_invalid_range(sample_midi_piece): @@ -103,3 +103,14 @@ def test_trim_with_invalid_range(sample_midi_piece): def test_source_update_after_trimming(sample_midi_piece): trimmed_piece = sample_midi_piece.trim(1, 3) assert trimmed_piece.source["start_time"] == 1, "Source start_time should be updated to reflect trimming." + + +def test_to_midi(sample_midi_piece): + # Create the MIDI track + midi_track = sample_midi_piece.to_midi() + # Set the expected end time according to the sample MIDI piece + expected_end_time = 5.5 + # Get the end time of the MIDI track + midi_end_time = midi_track.get_end_time() + + assert midi_end_time == expected_end_time, f"MIDI end time {midi_end_time} does not match expected {expected_end_time}"