Skip to main content

User login

What is OpenID?
  • Log in using OpenID
  • Cancel OpenID login
  • Create new account
  • Request new password
Register
  • Home
  • Browse
    • 2D Art
    • 3D Art
    • Concept Art
    • Textures
    • Music
    • Sound Effects
    • Documents
    • Featured Tutorials
  • Submit Art
  • Collect
    • My Collections
    • Art Collections
  • Forums
  • FAQ
  • Leaderboards
    • All Time
      • Total Points
      • Comments
      • Favorites (All)
      • Favorites (2D)
      • Favorites (3D)
      • Favorites (Concept Art)
      • Favorites (Music)
      • Favorites (Sound)
      • Favorites (Textures)
    • Weekly
      • Total Points
      • Comments
      • Favorites (All)
      • Favorites (2D)
      • Favorites (3D)
      • Favorites (Concept Art)
      • Favorites (Music)
      • Favorites (Sound)
      • Favorites (Textures)
  • ❤ Donate
Programming

MIDI in AS3 (Adobe Flash)

William.Thompsonj
Tuesday, July 23, 2013 - 13:25
William.Thompsonj's picture

For some time I've been looking at making a MIDI synthesizer in AS3 (Adobe Flash). Since AS3 doesn't natively support playing MIDI files (or reading them for that matter) this is a bit of a challenge. Through research I've learned of some methods that allow a binary file to be embedded and loaded as a byte array. From that point it's whatever handy work you can muster to interpret a byte array.

Has anyone worked with the MIDI file format or know of a good starting point for me to roll a MIDI interpreter? I understand the file structure a bit and I've read a little about generating different tones and sounds with saw waves, sine waves, square waves, triangle waves, et al, but I'm looking to accomplish this in AS3. I saw the tutorial here on OGA (opengameart.org/forumtopic/kickin-it-old-school-setting-up-nes-style-chiptunes) and read that too by the way.

There's a project that can play a MIDI file from Flash Player called SiON (wonderfl.net/c/4IH3). I've looked at that but haven't tried to implement it. I don't know if the sound quality it produces is good enough or it'll just annoy players. Particularly, it makes violins sound like synthetic pipe-organs and murders a few grand piano sounds without regard. It hits the right key and pitch but sometimes that's about all it can do.

Due to technical constraints, I'm forced to make an AS3 based game of less than 10MB total finished SWF size, only a single SWF, and it can't make remote calls for resources. So far the graphics I've chosen and had fabricated (by Sharm and the other awesome artists here on OGA) are well under that 10MB cap once compiled. I'm still struggling with the sound portion (pretty important component of any finished game). Since using 2-3 good MP3 files eats most of my size restriction I have to go with an ultri slim file type like MIDI.

Please OGA community, in your vast and collective wisdoms and interweb crawling mastery, how do you think I should proceed with making a MIDI music player for my Flash game?

  • Log in or register to post comments
bart
joined 14 years 1 week ago
Tuesday, July 23, 2013 - 13:54
bart's picture

AS3 is a bit out of my area of expertise, since I've never worked with it, but here's how I would go about it:

First off, apparently there's an alpha quality midi loader on google code under the MIT license:

http://code.google.com/p/as3midilib/

Grab the code to that.

Then, write a few simple synths (white noise, square, triangle, saw, etc) and associate them with whatever midi channels you want (obviously you're not going to want to write synths for every single standard midi instrument, because that would take forever).

I've never written a music synth before, but I'd try something like this:

  • At the beginning of the main loop, check the current time and compare it to the previous time.
  • If a note should have played in that timeframe, synthesize that note as raw sound and begin playing it. 
  • If this makes things too choppy, you might want to make a separate thread that you can call to synthesize your notes a second or so before it's time to play them.
  • If memory usage isn't an issue, you could synthesize the entire song before the actual game stars playing it, and then just play the entire raw sound in the background.  This may be 10 to 50 megs per song, though, since it's uncompressed sound data.

That's about it.  If you need anything clarified, let me know.

Bart

  • Log in or register to post comments
Syrsly
joined 12 years 9 months ago
Wednesday, July 24, 2013 - 17:46
Syrsly's picture

There is a mod player for Flash that works fairly well, but there is not a MIDI player for Flash at all, at least not one that actually works out-of-box.  You will need to make synths and all that jazz.  I have used the mod player inside of Flixel as a replacement library for music to keep the filesize low, and the CPU hit is minimal.

Syrsly
Twitch Streamer, Web/Game Developer, & Artist

syrsly.com - contact me for commissions, thanks!

  • Log in or register to post comments