Skip to content

Commit

Permalink
Reimplement MidiFile with pretty_midi (#7)
Browse files Browse the repository at this point in the history
* reimplement MidiFile with pretty_midi, update tests

* remove previous implementation

* deal with warnings in tests 👍

* add method to merge MidiFiles and a test

* improve midi file merge logic

* cleanup

- remove copies of pretty_midi internals
- improve tests

* Add MidiFile docstring

* add streamlit for development flow
  • Loading branch information
roszcz authored Sep 27, 2024
1 parent 1cfd102 commit 98b7f57
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 1,039 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.pymon
.plan
.env
tmp/
Expand Down
40 changes: 40 additions & 0 deletions dashboards/merge_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import streamlit as st
from streamlit_pianoroll import from_fortepyan

from fortepyan import MidiFile


def main():
st.write("# Test MidiFile merging")
uploaded_files = st.file_uploader(
label="Upload one or many MIDI files",
accept_multiple_files=True,
)

if not uploaded_files:
st.write("Waiting for files")
return

midi_files = []
for uploaded_file in uploaded_files:
midi_file = MidiFile.from_file(uploaded_file)
midi_files.append(midi_file)

merge_spacing = st.number_input(
label="merge spacing [s] (time interval inserted between files)",
min_value=0.0,
max_value=30.0,
value=5.0,
)
merged_midi_file = MidiFile.merge_files(
midi_files=midi_files,
space=merge_spacing,
)
st.write("Duration after merge:", merged_midi_file.duration)
st.write("Number of notes after merge:", merged_midi_file.piece.size)

from_fortepyan(merged_midi_file.piece)


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion fortepyan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
# Pretty MIDI will throw an unwanted error for large files with high PPQ
# This is a workaround
# https://github.com/craffel/pretty-midi/issues/112
pretty_midi.pretty_midi.MAX_TICK = 1e10
pretty_midi.MAX_TICK = 1e10
4 changes: 2 additions & 2 deletions fortepyan/audio/render.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import tempfile
from typing import Union

import pretty_midi
from pydub import AudioSegment
from midi2audio import FluidSynth

from fortepyan.audio import soundfont
from fortepyan.midi import structures
from fortepyan.midi import containers as midi_containers


def midi_to_wav(midi: Union[structures.MidiFile, structures.MidiPiece], wavpath: str):
Expand Down Expand Up @@ -41,7 +41,7 @@ def midi_to_wav(midi: Union[structures.MidiFile, structures.MidiPiece], wavpath:
# Add an silent event to make sure the final notes
# have time to ring out
end_time = midi.get_end_time() + 0.2
pedal_off = midi_containers.ControlChange(64, 0, end_time)
pedal_off = pretty_midi.ControlChange(64, 0, end_time)
midi.instruments[0].control_changes.append(pedal_off)

midi.write(tmp_midi_path)
Expand Down
324 changes: 0 additions & 324 deletions fortepyan/midi/containers.py

This file was deleted.

Loading

0 comments on commit 98b7f57

Please sign in to comment.