Coder Social home page Coder Social logo

midi2audio's Introduction

midi2audio

PyPI version Supported Python versions License

Easily synthesize MIDI to audio or just play it.

It provides a Python and command-line interface to the FluidSynth synthesizer to make it easy to use and suitable for scripting and batch processing. In contrast, most MIDI processing software is GUI-based.

Why?

First, FluidSynth has a CLI which is not so straightforward to use. The goal was to make it easy to use as possible by making some parameters implicit.

fluidsynth -ni sound_font.sf2 input.mid -F output.wav -r 44100

vs.

midiplay input.mid
midi2audio input.mid output.wav

Second, we can have as easy interface scriptable in Python.

FluidSynth().midi_to_audio('input.mid', 'output.wav')

What it is not?

Note that is it not a Python binding to all the FluidSynth commands. If needed check out these packages instead:

Requirements

You need at least one sound font. Normally you'd install them manually or via a package manager. midi2audio looks for its default sound font in ~/.fluidsynth/default_sound_font.sf2. It can be a symlink or an actual file.

Installation

pip

You can install this package via pip.

pip install midi2audio

Or for development (changes in code take effect without reinstalling):

git clone https://github.com/bzamecnik/midi2audio
pip install -e midi2audio

OS X

I'd recommend adding the non-default libsndfile which supports output to FLAC and wide variety of audio formats. Otherwise only WAV, raw and a few other will be supported.

For Mac OS X we provide a script install_fluidsynth_with_soundfonts_osx.sh to automatically install FluidSynth with libsndfile and a basic sound font (Fluid R3 GM) and symlink it so that it's recognized as a default sound font for this module. Note it it install installed via pip as another entry point.

install_fluidsynth_with_soundfonts_osx.sh

Or install it manually:

brew install fluidsynth --with-libsndfile
mkdir -p ~/.fluidsynth
ln -s /path/to/my/sound_font.sf2 ~/.fluidsynth/default_sound_font.sf2

Check the script how to install a few additional (nice but bigger) sound fonts which are not installed by default.

Other OSs

Check you package manager and link your default sound font as descibed above.

Usage

Basically you can either play a MIDI file or synthesize it to audio. FluidSynth allows non-realtime synthesis which is faster than the playback.

Note that the audio format is determined from the audio file extension. The libsoundfile supports a lot of formats.

Python

from midi2audio import FluidSynth

Play MIDI:

FluidSynth().play_midi('input.mid')

Synthesize MIDI to audio:

# using the default sound font in 44100 Hz sample rate
fs = FluidSynth()
fs.midi_to_audio('input.mid', 'output.wav')

# optional third argument to control gain (defaults to 0.2)
fs.midi_to_audio('input.mid', 'output.wav', gain=0.75)

# FLAC, a lossless codec, is supported as well (and recommended to be used)
fs.midi_to_audio('input.mid', 'output.flac')

Change the defaults:

# use a custom sound font
FluidSynth('sound_font.sf2')

# use a custom sample rate
FluidSynth(sample_rate=22050)

Command line interface

A shell sugars midi2audio and midiplay are provided instead of more verbose python -m midi2audio.

# play MIDI
$ midiplay input.mid

# synthesize MIDI to audio
$ midi2audio input.mid output.wav

# also to FLAC
$ midi2audio input.mid output.flac

# custom sound font
$ midi2audio -s sound_font.sf2 input.mid output.flac

# custom sample rate
$ midi2audio -r 22050 input.mid output.flac

About

Shoulders of giants

Thanks to the authors of FluidSynth for a nice command-line MIDI synthesizer!

Support the project

Need some consulting or coding work regarding audio processing, machine learning or big data? Drop me a message via email or LinkedIn. Or just say hello :).

midi2audio's People

Contributors

bzamecnik avatar daniel-chin avatar dredly avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

midi2audio's Issues

Problems converting MIDI to WAV using midi2audio with the described instructions.

I am having trouble doing the conversion from midi to wav using your tool.

I installed fluidsynth using the sh file inside the directory of midi2audio.
Then, I installed the midi audio with python3 with the following command: python3 setup.py install.
Finally, I installed the fluidsynth separately using the source with the cmake build.

What should I do? Here below is the output of the tool after installing fluid synth, sound fonts, and the tool itself -- midi2audio:

Shyamals-iMac:midi2audio testuser$ midi2audio ~/Downloads/chpn_op7_1.mid ~/Downloads/chpn_op7_1.wav
FluidSynth version 1.1.8
Copyright (C) 2000-2017 Peter Hanappe and others.
Distributed under the LGPL license.
SoundFont(R) is a registered trademark of E-mu Systems, Inc.

Parameter '/Users/testuser/.fluidsynth/default_sound_font.sf2' not a SoundFont or MIDI file or error occurred identifying it.
fluidsynth: error: Unable to open file "share/soundfonts/default.sf2"
fluidsynth: error: Couldn't load soundfont file
fluidsynth: error: Failed to load SoundFont "share/soundfonts/default.sf2"
Rendering audio to file '/Users/testuser/Downloads/chpn_op7_1.wav'..
fluidsynth: warning: No preset found on channel 0 [bank=0 prog=0]
fluidsynth: warning: No preset found on channel 0 [bank=0 prog=0]
Shyamals-iMac:midi2audio testuser$ 

Thanks in advance!

Warning: 'fluidsynth: panic' Error Message Despite Code Execution

I have been working with the midi2audio Python library and noticed a consistent warning message when using the library. The warning message reads: "fluidsynth: panic: An error occurred while reading from stdin." This occurs even though the code appears to work as expected and generates the desired audio output.

FileNotFoundError: [WinError 2] The system cannot find the file specified

Hi, when running the following script in order to convert a directory of midi files to wav I get the following error:

from midi2audio import FluidSynth
import os
import sys

files = [file for file in os.listdir('./') if '.mid' in file]
target_dir = 'C:\\Users\\Arham\\Desktop\\GuitarWavs'

if not os.path.exists(target_dir):
	print("Creating directory: " + target_dir)
	os.mkdir(target_dir)

print("Converting files...")
for file in files:
	fs = FluidSynth()
	fs.midi_to_audio(file, target_dir+'\\'+file[0:file.index('.mid')]+'.wav')

print('Convertion complete!')

Error:

Traceback (most recent call last):
  File "miditowav.py", line 27, in <module>
    fs.midi_to_audio(os.path.join(os.getcwd(),file), target_dir+'\\'+file[0:file.index('.mid')]+'.wav')
  File "C:\Users\Arham\AppData\Local\Programs\Python\Python37\lib\site-packages\midi2audio.py", line 46, in midi_to_audio
    subprocess.call(['fluidsynth', '-ni', self.sound_font, midi_file, '-F', audio_file, '-r', str(self.sample_rate)])
  File "C:\Users\Arham\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 323, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Users\Arham\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 775, in __init__
    restore_signals, start_new_session)
  File "C:\Users\Arham\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1178, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

I believe this has something to do with the fact that midi2audio runs fluidsynth as a subprocess but I'm unsure of the problem. I've installed fluidsynth on my system (Windows 10) via vcpkg with visual studio and I've added the fluidsynth_x86-windows/bin path to my system PATH variable.

I thought that the above would solve the problem but it hasn't. I'd appreciate any help in solving this.

Can't get fluidsynth to work

Hello,
I installed everything according to the instructions (installed fluidsynth via homebrew) and added a custom sound font. Playback via terminal/console is working. But when I am trying to use the midi2audio module in python I always get the following error (using python 3.5 on macOS 10.13) :

from midi2audio import FluidSynth
fs = FluidSynth()
fs.play_midi('ahead_on_our_way_piano.mid')
Traceback (most recent call last):

  File "<ipython-input-3-6ff900f0d0db>", line 4, in <module>
    fs.play_midi('ahead_on_our_way_piano.mid')

  File "/Users/user/anaconda3/envs/deeplearning/lib/python3.5/site-packages/midi2audio.py", line 49, in play_midi
    subprocess.call(['fluidsynth', '-i', self.sound_font, midi_file, '-r', str(self.sample_rate)])

  File "/Users/user/anaconda3/envs/deeplearning/lib/python3.5/subprocess.py", line 247, in call
    with Popen(*popenargs, **kwargs) as p:

  File "/Users/user/anaconda3/envs/deeplearning/lib/python3.5/subprocess.py", line 676, in __init__
    restore_signals, start_new_session)

  File "/Users/user/anaconda3/envs/deeplearning/lib/python3.5/subprocess.py", line 1289, in _execute_child
    raise child_exception_type(errno_num, err_msg)

FileNotFoundError: [Errno 2] No such file or directory: 'fluidsynth'

any help would be appreciated!

Small fix to the docs

The docs state that gain=0.75 is an optional third parameter to midi_to_audio(), but in reality it is an optional third parameter to the FluidSynth() constructor. A small tweak to the docs should fix this.

Thank you very much :)

Hey Bohumir,

Thank you very much for finally doing something about that nasty FluidSynth problem. Your efforts are much needed and appreciated.

I am already using it for my Music AI/SuperPiano repo implementation, although I prefer a simpler solution which you can find below:

!pip install pyFluidSynth
!apt install fluidsynth #Pip does not work for some reason. Only apt works
!pip install midi2audio

!cp /usr/share/sounds/sf2/FluidR3_GM.sf2 /content/font.sf2

from midi2audio import FluidSynth
from google.colab import output
from IPython.display import display, Javascript, HTML, Audio

FluidSynth("/content/font.sf2").midi_to_audio('output_midi.mid','output_wav.wav')
# set the src and play
Audio("output_wav.wav")

This way, it is a much more straightforward IMHO. But I still need midi2audio to summon FluidSynth properly. Our solutions are just a hack...I hope that FluidSynth team will do it right and properly eventually so that we do not have to go through all this pain 👍

Thank you again.

Alex

Problems converting from midi to wav

Hi, I am having issues converting from midi to wav using your package with your suggested code
fs.midi_to_audio(midi_file_path, wav_file_path).

I have been looking into it and have been able to successfully convert using the following line (directly with fluidsynth)
subprocess.call(['fluidsynth', '-T', 'wav', '-F', wav_file_path, '-ni', soundfont, midi_file_path])
but not this other line
subprocess.call(['fluidsynth', '-F', wav_file_path, '-ni', soundfont, midi_file_path])
so for some reason it seems like fluidsynth on my computer does not like the default value for the file type -T option and I need to specify it.

Would it be an interesting addition to parse the file type from the file path on (second parameter) on your midi2audio and pass it to the subprocess.call(...). It could probably help avoid these kinds of issues.
I am relatively new with Github and python but I would be happy to contribute if you are ok with it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.