Rendering
These functions provide a simple interface to render audio files from MIDI representations.
midi_to_mp3(midi, mp3_path=None)
Converts a MIDI file to an MP3 file.
This function takes a MIDI object, either as a MidiFile object or a MidiPiece object,
and first converts it to a WAV file. It then converts this WAV file to an MP3 file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
midi
|
Union[MidiFile, MidiPiece]
|
The MIDI file to convert. Can be either a |
required |
mp3_path
|
str
|
The path where the converted MP3 file will be saved. If not specified, a temporary file is created. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
mp3_path |
str
|
The path to the created MP3 file. |
Note
If a MidiPiece object is provided, it is first converted to a MidiFile object
before proceeding with the WAV and then MP3 conversion.
Examples:
>>> some_midi = ff.MidiPiece(midi_df)
>>> mp3_path = midi_to_mp3(some_midi, "test.mp3")
>>> print("MP3 file created at:", mp3_path)
Source code in fortepyan/audio/render.py
midi_to_wav(midi, wavpath)
Converts a MIDI file to a WAV file.
This function takes a MIDI object, either as a ff.MidiFile object or a MidiPiece object,
and converts it to a WAV file. The conversion uses the FluidSynth synthesizer with a downloaded sound font.
The function also adds a silent event at the end of the MIDI sequence to ensure that the final notes have time
to ring out properly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
midi
|
Union[MidiFile, MidiPiece]
|
The MIDI file to convert. Can be either a |
required |
wavpath
|
str
|
The path where the converted WAV file will be saved. |
required |
Note
If a MidiPiece object is provided, it is first converted to a MidiFile object
before proceeding with the WAV conversion.
Examples:
Source code in fortepyan/audio/render.py
download_if_needed()
Ensures the SoundFont file is downloaded in the user's data directory.
The function checks for 'soundfont.sf2' in the user's data directory, downloading it if absent. The download URL is sourced from the application's configuration.
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
The path to the SoundFont file. |