Coding a musical game using MIDI
Posted: Sun Jan 15, 2023 12:45 pm
Hello,
I'm an beginner musician who tinkers with code in his spare time and I would like to write a little video game to help me make progress in music. I'm a bit of a jack of all trades, I have a guitar, a keyboard and a dualo.
I would like to write a simple video game: the game plays a midi melody, I listen to it, and then I have to reproduce it, by ear, with my instrument.
At first, I would like to be able to play the game using my dualo connected as a MIDI keyboard.
The game plays a MIDI melody then pauses and waits for me to play the notes. Then I use my dualo to play the melody back to the game, and the game simply checks that I am playing the right notes in the right order, ignoring the rhythm.
In other words, if the midi file contains the melody for "frère jacques", the game simply checks that I am indeed playing "do re mi do, do re mi do, mi fa sol, mi fa sol..."
(Later on, I would like to be able to play it with my guitar or piano, but that means capturing audio, transforming the captured sounds into midi notes... and uh... for now, writing the code to play directly in midi is complicated enough for me!)
I have a little bit of experience in programming, but I'm a beginner in coding video games... and then MIDI is a strange beast... although it looks quite simple at first glance, but in fact i find it... a bit confusing. And the documentation I found is rather cryptic.
Here is precisely what I am trying to do
Step 1: I just want to write the code that opens the midi file, plays it in audio, and gets the sequence of notes of the melody
Step 2: write the code that plays the sound when I press a key on my dualo and compares my note sequence with the one from the file
In my mind, that sounded easy... and then... well...
If I understand correctly, MIDI is only text information that describes sound events, but a MIDI file, in itself, does not include any sound. If the MIDI file contains an event 0x92 0x3D 0x78... , that means "note on", note 61 (meaning C# 4), velocity 78, right?
And my code must forward the event to some virtual synthetizer library that will synthesize sound, right?
Same thing if it's my dualo that sends the event, I guess? My kb will send a "note on" when I press a key, and a "note off" when I release the key?
From what I understand, to have sound, I need a virtual synthesizer, I guess it's a library?
In my research, I found fluidsynth.lua, a lua library that interfaces with fluidsynth Does this sound like a good idea? Should I keep digging there? Or do you have something better to recommend? It looks like the people at dualo are developping their new app around SurgeXT, would that be a good option for my game? If so, how can I make surgeXT calls from Lua/Löve2d?
A lot of things I find mention ALSA... but that stands for "Advanced Linux Sound Architecture", I guess that means it won't run on windows?
Then the sounds themselves are organized in a sound font, right?
The midi file also contains the info telling the synth which instrument to use, right? So, I would need to create some kind of mapping to say that this instrument corresponds to this sound font?
Has any of you ever coded musical games using MIDI? Can anyone here orient me to the appropriate libraries/tools?
Any pointers would be very appreciated. I bought the book "the midi manual" by David Miles Huber, but it's a difficult read... do you have other resources to recommend?
Thanks
Bruno
In case you're wondering, a dualo is a small synth that looks like that (https://dualo.com/dualo/)
I'm an beginner musician who tinkers with code in his spare time and I would like to write a little video game to help me make progress in music. I'm a bit of a jack of all trades, I have a guitar, a keyboard and a dualo.
I would like to write a simple video game: the game plays a midi melody, I listen to it, and then I have to reproduce it, by ear, with my instrument.
At first, I would like to be able to play the game using my dualo connected as a MIDI keyboard.
The game plays a MIDI melody then pauses and waits for me to play the notes. Then I use my dualo to play the melody back to the game, and the game simply checks that I am playing the right notes in the right order, ignoring the rhythm.
In other words, if the midi file contains the melody for "frère jacques", the game simply checks that I am indeed playing "do re mi do, do re mi do, mi fa sol, mi fa sol..."
(Later on, I would like to be able to play it with my guitar or piano, but that means capturing audio, transforming the captured sounds into midi notes... and uh... for now, writing the code to play directly in midi is complicated enough for me!)
I have a little bit of experience in programming, but I'm a beginner in coding video games... and then MIDI is a strange beast... although it looks quite simple at first glance, but in fact i find it... a bit confusing. And the documentation I found is rather cryptic.
Here is precisely what I am trying to do
Step 1: I just want to write the code that opens the midi file, plays it in audio, and gets the sequence of notes of the melody
Step 2: write the code that plays the sound when I press a key on my dualo and compares my note sequence with the one from the file
In my mind, that sounded easy... and then... well...
If I understand correctly, MIDI is only text information that describes sound events, but a MIDI file, in itself, does not include any sound. If the MIDI file contains an event 0x92 0x3D 0x78... , that means "note on", note 61 (meaning C# 4), velocity 78, right?
And my code must forward the event to some virtual synthetizer library that will synthesize sound, right?
Same thing if it's my dualo that sends the event, I guess? My kb will send a "note on" when I press a key, and a "note off" when I release the key?
From what I understand, to have sound, I need a virtual synthesizer, I guess it's a library?
In my research, I found fluidsynth.lua, a lua library that interfaces with fluidsynth Does this sound like a good idea? Should I keep digging there? Or do you have something better to recommend? It looks like the people at dualo are developping their new app around SurgeXT, would that be a good option for my game? If so, how can I make surgeXT calls from Lua/Löve2d?
A lot of things I find mention ALSA... but that stands for "Advanced Linux Sound Architecture", I guess that means it won't run on windows?
Then the sounds themselves are organized in a sound font, right?
The midi file also contains the info telling the synth which instrument to use, right? So, I would need to create some kind of mapping to say that this instrument corresponds to this sound font?
Has any of you ever coded musical games using MIDI? Can anyone here orient me to the appropriate libraries/tools?
Any pointers would be very appreciated. I bought the book "the midi manual" by David Miles Huber, but it's a difficult read... do you have other resources to recommend?
Thanks
Bruno
In case you're wondering, a dualo is a small synth that looks like that (https://dualo.com/dualo/)