Skip to content

Commit

Permalink
Add more alignment tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberrumor committed May 3, 2024
1 parent b27660f commit 94409d1
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 62 deletions.
8 changes: 6 additions & 2 deletions bin/midilint
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ def main(source: Path, dest: Path, args) -> None:
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog=f"{Path(__file__).name}",
description="Read SOURCE midi file and save processed version to DEST",
description="The midi linter.",
)

parser.add_argument("SOURCE", type=Path, help="the midi file to lint")

parser.add_argument("DEST", type=Path, help="the name of the output file")
parser.add_argument(
"DEST",
type=Path,
help="the name of the output file",
)

parser.add_argument("--velocity", type=int, help="the velocity to set all notes to")

Expand Down
16 changes: 3 additions & 13 deletions midilint/midilint.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,6 @@ def align(source: mido.MidiFile, precision: int = 1) -> mido.MidiFile:
return source


def shift_nearest(track: mido.MidiTrack, notes: list[int]) -> None:
"""
Change pitch into the key by shifting notes to the nearest note in key.
"""
# Get the absolute value of the difference between each item in
# the list and message.note, and pick the smallest amongst them.
for message in track:
if message.type in ("note_on", "note_off"):
message.note = min(notes, key=lambda x: abs(x - message.note))


def correct_pitch(
source: mido.MidiFile,
key: midi_abstraction.Key,
Expand All @@ -78,6 +67,7 @@ def correct_pitch(
notes.extend(midi_abstraction.notes(n))

for track in source.tracks:
shift_nearest(track, notes)

for message in track:
if message.type in ("note_on", "note_off"):
message.note = min(notes, key=lambda x: abs(x - message.note))
return source
Binary file added test/files/bad_timing_overlap.mid
Binary file not shown.
152 changes: 105 additions & 47 deletions test/test_align.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,68 @@
#!/usr/bin/env python3
from pathlib import Path
import pytest

import mido

import midilint

SOURCE = Path(__file__).parent / "files/bad_timing.mid"
BAD_TIMING = Path(__file__).parent / "files/bad_timing.mid"
BAD_TIMING_OVERLAP = Path(__file__).parent / "files/bad_timing_overlap.mid"


def test_align_quarter_note():
mid = mido.MidiFile(SOURCE, clip=True)
@pytest.mark.parametrize(
"source,expected",
[
(
BAD_TIMING,
[
0,
128,
0,
128,
0,
128,
128,
128,
0,
128,
0,
128,
128,
128,
0,
128,
0,
128,
],
),
(
BAD_TIMING_OVERLAP,
[
0,
0,
0,
128,
0,
0,
384,
0,
0,
128,
0,
0,
384,
0,
128,
0,
],
),
],
)
def test_align_quarter_note(source, expected):
mid = mido.MidiFile(source, clip=True)
mid = midilint.align(mid, 1)

expected = [
0,
128,
0,
128,
0,
128,
128,
128,
0,
128,
0,
128,
128,
128,
0,
128,
0,
128,
]

i = 0
for track in mid.tracks:
for message in track:
Expand All @@ -42,31 +72,59 @@ def test_align_quarter_note():
i += 1


def test_align_eighth():
mid = mido.MidiFile(SOURCE, clip=True)
@pytest.mark.parametrize(
"source,expected",
[
(
BAD_TIMING,
[
0,
128,
0,
128,
64,
64,
128,
128,
0,
128,
0,
128,
128,
128,
0,
128,
0,
128,
],
),
(
BAD_TIMING_OVERLAP,
[
0,
0,
0,
128,
64,
0,
256,
64,
0,
128,
0,
0,
320,
0,
192,
0,
],
),
],
)
def test_align_eighth(source, expected):
mid = mido.MidiFile(source, clip=True)
mid = midilint.align(mid, 2)

expected = [
0,
128,
0,
128,
64,
64,
128,
128,
0,
128,
0,
128,
128,
128,
0,
128,
0,
128,
]

i = 0
for track in mid.tracks:
for message in track:
Expand Down

0 comments on commit 94409d1

Please sign in to comment.