Next Broadcast

MIDI playback in a game engine

hakkthazard GameDev DK30 Winter 2020 1 1

Description

I plan to load and play MIDI files in my windows/webassembly-based game engine. The engine can already load and play .wav files, and it also has the beginnings of sound synthesis in the form of a piano demo. So the project comes down to loading and parsing the MIDI file, implementing all of the MIDI commands needed to play back music, and generating sounds with a set of synthetic instruments.

Recent Updates

hakkthazard 5 years ago

I created some more (simple) instruments, and added pitch wheel support. There are over 100 instruments in the spec, but I ended up with 6 instruments that vaguely sound like the ones they’re named after: piano, trumpet, guitar, violin, flute, and snare drum. I did add support for a low pass filter, which helped the instruments sound less buzzy, but there are some slightly more sophisticated things that could be done to better simulate the instruments.

I completed what I set out to do: the culmination of the project can be heard here. (imgur doesn’t allow videos over 60s. Also, BEWARE the high pitched noises in the second tune and at the end). It is a version of onestop.mid (which is on every Windows computer) played back in my MIDI engine.

I am happy with the results; you can really here the pitch bending in some of the guitar parts. If I have time I will go back and add more instruments and make them sound better (add an ADSR envelope to the low pass filter, add a low frequency oscillator, etc.) but for now I am content to call the project complete.

hakkthazard 5 years ago

I tried to find an editor online for generating instrument sounds with only additive synthesis, but I couldn’t find one. So I decided to try to make a simple instrument sound editor myself:

imgur link (turn audio on)

I think I will be able to come up with some passable keyboard and woodwind sounds, but I’m not sure I can get things like brass instruments working without adding more complexity to the synthesizer. So I’m going to stick with just these simple instrument sounds and work on getting the MIDI playback to switch instruments and handle more MIDI events (maybe pitch wheel support?).

hakkthazard 5 years ago

I managed to get simple MIDI playback working. It only can make piano sounds, all at the same volume:

imgur link (audio only)

I found a reference on the MIDI spec here. I implemented code to read the header and track information, and enough of the meta events to set the tempo and detect when tracks end. But I’ve only implemented “note on” and “note off” events, and none of the other voice and control events.

I’ve also reused some piano sound synthesis code from a previous project. So the next step will be implementing some more of the voice events and coming up with more synthesized instruments.

hakkthazard 5 years ago

I won’t get a chance to work on the MIDI project for a little while, but I did implement loading/saving files with a file dialog in windows and in browsers. I implemented it for a hitbox editor, but I think I can reuse it to interactively load MIDI files.

Estimated Timeframe

Jan 24th - Feb 23rd

Week 1 Goal

Will be preparing the engine for the Global Game Jam, so this work may end up being pushed to week 2 or 3

  • Attempt to load MIDI file into engine, in both windows build and web build. The MIDI file should be something basic that’s easy to debug.
  • Try to come up with a data structure to hold the unparsed MIDI file data.

Week 2 Goal

  • Write code to parse out all of the information necessary for playback. Try to determine which of these commands are necessary (maybe note on/off commands, instruments, tracks, etc.) and which can be ignored. Prepare a data structure from all of this info that is ready for playback

Week 3 Goal

  • Work on playback. Try to utilize the piano synthesis that already exists in the engine just to get it working.
  • Add more sounds. MIDI supports many, many instruments, so I plan to map ranges of these instruments to single instruments, i.e., piano, honky-tonk piano, organ, etc, all will sound the same.

Week 4 Goal

  • Test on a real MIDI file that plays an actual song.
  • Stretch goal: Program some kind of visualization that reacts to the music

Tags

  • gamedev
  • music
  • game
  • engine
  • midi
  • sound
  • synthesis
  • webassembly
  • windows
  • c
  • c++
  • javascript
  • audio
  • instrument
  • code
  • piano
  • sample
  • file
  • parse
  • graphics
  • opengl
  • webgl
  • programming