Here's how to make Löve play MIDIs, on GNU/Linux and Android.
Still not completely solved for Windows, could use some help here!
This solution works only when the game is unpacked. Some additional steps are needed for packed
.love files to work. (Described in the next post.)
1) Prepare instruments by converting a soundfont to GUS patches
It seems there is no way to make use of system's native way of playing MIDI files, so you should include the instruments yourself. As oposed to Timidity++, the Timidity included in libmodplug uses instruments in GUS patch format only. As these are hard to find, you can use
unsf utility to convert
sf2 to them.
You can find some soundfonts
here. (The ideal soundfont should be small, should include all instruments your song uses, and should have no copyright issues.)
After executing
unsf somefile.sf2, you will end up with
somefile.cfg and several subfolders with instruments in PAT format.
2) Create directory structure
In the folder of your love game, create folder
instruments - it must be called like that. Move all subfolders with instruments (and any PATs not in subfolder) there.
Move
somefile.cfg to the folder above
instruments (your love game folder in this case) and rename it to
timidity.cfg (also important).
3) Create minimal main.lua to play the song and set environment variable
Code: Select all
local ffi = require('ffi')
ffi.cdef[[
int setenv(const char*, const char*, int); // Unixes
int _putenv_s(const char*, const char*); // Windows
]]
function love.load()
local path = love.filesystem.getRealDirectory('timidity.cfg')
if love.system.getOS() == 'Windows' then
ffi.C._putenv_s('MMPAT_PATH_TO_CFG', path)
else
ffi.C.setenv('MMPAT_PATH_TO_CFG', path, 1)
end
love.audio.newSource('ame002.mid', 'static'):play()
end
This should set environment variable
MMPAT_PATH_TO_CFG pointing to the folder with
timidity.cfg (in this example, your love game folder).
Don't forget to place
somesong.mid in the same folder...
... and you should hear the song correctly -
if the game is unpacked!
The working proof-of-concept standalone
.love file is in the next post.
EDIT: Figured out how to set an environment variable using FFI
EDIT: Updated info for standalone files
EDIT: Updated info for Windows