Skip to content

Commit

Permalink
test to_midi, update trimming test.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelJanas committed Nov 7, 2023
1 parent 933749c commit f4866a1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ tmp/
*.egg-info/
dist/
*.pyc
*.ipynb
2 changes: 1 addition & 1 deletion fortepyan/midi/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 19 additions & 8 deletions tests/midi/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
}
Expand All @@ -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],
}
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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}"

0 comments on commit f4866a1

Please sign in to comment.