Demo Coding with Pure Basic #02
Written By Fable Fox
Playing MIDI File Part #1
Why part 1? Simply, for now I'm teaching you how to play a MIDI file using MCI and NOT DirectX. It's still a good way to add a music to your demos. If you think that MIDI is dead, think again. The MIDI file format is alive, well, and NOT updated means something - it can stand the test of time. If your idea of music is techno, electronic, etc - this file format might be useless, but having access to a lot of real world musical intruments, and writing tunes & melody using notation software, or MIDI hardware - for a small size file, IT IS GREAT!
NOTE: I stray away from Polaris' tutorial for a reason. 1 - graphics programming with Pure Basic is easy. 2 - code to play music is shorter. So before I went into some long code, I want to cover music first.
The Easy Way.
First, I will give credits where it's due. There is a midi.pbi written by a very helpful person, but I don't know his name. It is supplied with jaPBe, an even more powerfull editor (it's free and open source, written with Pure Basic too ;-P) for Pure Basic. Open the file to see how to communicate with MCI using Pure Basic, and other available functions. There's an commented example too. Just copy/paste to see how other functions works.
NOTE: For this tutorial, I'm not sure if you can run it using demo version of Pure Basic, this is because it uses Win32 calls.
But as far as simplicity goes, this is all what you need to play a MIDI file inside Pure Basic, MCI way.
Most of the function is self explanatory. However, the 0 / zero is the MIDI number. Each MIDI loaded must be given a number, and you use that number to play that particular MIDI file.
Include MIDI inside EXE.
Go use pecompiler. Heh.. heh.. Sorry Polaris, just kidding ;-P
Since Polaris already taught you how to use pecompiler, I'll guess I'll skip that one. Someone has written an example on including a file and to dump it to disk, but I can't remember who it was. Anyway, it was just a simple binary data writing, the trick is embedding the file and get its memory address. Pure Basic makes it easy to embed data into your .exe, just look at the code below.
#IncludeFile = 1
Procedure CreateIncludeFile(Name.s, *StartFile, *EndFile)
If CreateFile(#IncludeFile, Name)
WriteData(*StartFile, *EndFile - *StartFile)
CreateIncludeFile("canon.mid", ?StartFile_frunlog, ?EndFile_frunlog)
I think that the code speaks for itself, nothing too complex there, except for one. WriteData(). This is a Pure Basic function. It writes data into a file. But you must first create a file using CreateFile(), after you finished writing the data, then close it using CloseFile(). The code speaks for itself, no explaination is needed. It makes your job easy as you doesn't have to loop byte per byte. This funtion is makes data dumping easy. If you give it the size of the whole file, then it will write the whole file, easy.
Syntax: WriteData(*MemoryBuffer, LengthToWrite)
Description: Write the contents of the specified memory buffer in current file.
This application here doesn't read the MIDI file straight from memory, it allows you to include a binary file into the .exe and dump it into the file when the user runs the application. Good enough for a beginner 64K, don't you think? You might notice that I didn't delete the file later on, I think that it is quite easy and you should be able to learn this on your own. Not to mention that while this is easy, it might break some competition rules - which is why I don't plan to elaborating too much. If you plan to play a MIDI file quickly and easily, this is it.
- Fable Fox, http://www.fablefox.com