The Art of Demomaking - Issue 15 - Music And Synchronization by (29 November 1999) | Return to The Archives

Introduction

Well, we've pretty much covered all the topics on graphics I had intended to write about. So pretty much the only aspect of demomaking that is left to cover is music. Now since I'm not qualified enough to tell you about the process of actually creating the music in great depth, I'll just barely scratch the surface. On the other hand, I'll tell you all you need to know to integrate that music into your demo, and how to make sure everything is perfectly synchronised.

Synchronisation

I bet you've always wondered how synchronised swimmers can swim in sync with the music, while their ears are inside a tight swimming hat under water. My theory is they practice a lot! You could also use that method for a demo. Your musician can write the music, you will use an independent module player and start play when the demo starts, and run all your effects like if there was no sound, hoping that everything matches. With a bit of fine tuning and a lot of spare time, you can get acceptable results, but the sync will never be perfect. Depending on which file format you use, there are different tricks for synchronising your demo so that you don't have to go through that tedious process of fine tuning it by hand.

MP3 File Format

There are in my mind two good file formats you could use to store the music for your demo. First there's the famous MP3 format, which enables you to get impressive amouts of music into one file. The quality is much worse than a wave, since all the high and low frequency waves are trimmed out during the compression. I'll spare you the details. But this is not too obvious on normal PC speakers, and you can get some pretty good results by setting the bit rate to 128kbits. So all you need to start with is a huge WAV file. I suppose you could use one of the many sampling tools out there, you could record a live band, or just dump the content of your favourite CD. Then compress it with your favourite MP3 encoder. Now all you need is a MP3 player you can include into your demo without much hassle. I personally like XAudio, which you can find at www.xaudio.com. It's a bit of a hassle to get it working with DJGPP, since you might have to rename a few files to actually manage to compile stuff.
There are a few drawbacks with using MP3s, especially when it comes to synchronising your demo. The only easy way of doing it is to split your demo into small parts, and play an MP3 for each part. I know that's only basic synchronisation, but you can't really get much better unless you program your own MP3 decoder, and insert sync marks into the compressed data. Needless to say that's a lot of trouble to work out, especially when you can do a lot simpler...

Modules

The concept was invented by demomakers, specifically for demos, but the advantages of this file format are so obvious that some games use the same principle. The whole module is basically a list of tracks, that can be played in virtually any order. A track is a list of channels, in which samples are played with various different effects. This is very similar to midi, except that the samples are actually different for each module, and therefore have to be stored. That's the main reason why modules are bigger than MIDI files, since a fair bit of wave data needs to be appended to the file. The instructions for each channel of each track also need to be stored, but usually take up much less space.
Synchronisation is extremely easy with modules. Usually, module players have a synchronisation feature, so that when the module player encounters a special instruction in the pattern, it calls a user defined callback procedure. This procedure can in turn do whatever you want, like set a fading out coefficient, swap a texture, print some text. This week's demo will show you how to do that.

Using MIDAS Sound System

If we use MIDAS as our module player, synchronisation is really easy. The first thing to do is to get the latest version of Midas Sound System from http://www.s2.org/midas/ and extract it. You will find a file named midasdll.h in the MIDAS\INCLUDE subdirectory. Copy that file to your DJGPP\INCLUDE directory. Then copy the file libmidas.a from MIDAS\LIB\DOS\GCRETAIL into your DJGPP\LIB directory. As usual, this will make things much easier to compile in the future.
The MIDAS API being extremely simple, we can initialise the system, and start playing some music in about 12 lines of code:

        MIDASmodule module;
        MIDASmodulePlayHandle playHandle;

        MIDASstartup();
        MIDASinit();

        module = MIDASloadModule( "lord.xm" );

        playHandle = MIDASplayModule( module, true );

        MIDASstartBackgroundPlay( 0 );

        while (!kbhit())
        {

        }

        MIDASstopModule(playHandle);
        MIDASstopBackgroundPlay();

        MIDASfreeModule(module);
        MIDASclose();

Now that just runs a main loop with absolutely no synchronisation. To add synchronisation, we need to define a callback procedure that will be called everytime we get a special instruction in the pattern.

       void MIDAS_CALL MusicCallback( unsigned syncInfo, unsigned position, unsigned row )
       {
       	switch (syncInfo)
    	{
    	    case 1: break;
    		...
            }
       }

Then just after the initialization, we tell MIDAS what it has to do when it finds synchronisation instructions:

        MIDASsetMusicSyncCallback( playHandle, &MusicCallback );

And that's pretty much all we need. The MusicCallBack procedure will modify some global variables which we will take into account when drawing our effect.

Composing Modules

This process is also known as tracking. All you need is a tracker, and a musical touch... then again, given the fact that some of you out there are fans of techno, you probably don't need the latter :) Anyway, there are various trackers available: Scream Tracker, Impulse Tracker, Fast Tracker, Modplug Tracker and so on. The most famous and my personal favourite being Fast Tracker II, which you can get here. The best way to go if you're a complete beginner is to download some sample modules from demos, and take a look inside. FT2 has all the inline help you need to understand how they work. As for everything, all you need is practice.

Random Information

You will notice this week's demo just focuses on the sound. It's a good base to build all the effects upon, except for the timer routine, which needs to be completely replaced. The main disadvantage with the DJGPP version of MIDAS is that it uses Allegro. Allegro is an excellent games library, well programmed, but extremely heavy, and overkill for what we want to do. It's a bit of a hassle to compile, so i've zipped up the library and the header to make things easier for you. Get it here.
This module in the demo was tracked by Steve Carter (aka Juggler), the only other member of Base Sixteen. It was lying around on my hard disk for a while, and since I've always wanted to put it to music, I figured this was the right opportunity.
Another thing you may have noticed about the demo is the size of the executable. It's about 300k, and even compressed that's still pretty big. So if you wanted to code a small 64k intro, you'd have to write your own sound library. Otherwise, I strongly recommend using MIDAS, since it's fully portable and simple to use. MIDAS can do quite a few other things appart from play modules, like play sound effects, and wave post processing. You will have no trouble finding out about all this.

Final words

Music adds a whole new dimension to a demo, and it's definitely worth putting just as much effort into the music than into the graphics. Also make sure all that effort is not put to shame by forgetting the synchronisation... Well, next week should be the last issue of this tutorial. Hopefully, I will have enough time to put all the effects to the music, and finish with a big bang ;)
Feel free to download this week's example with source code right here (688k)
Have fun,
Alex

Article Series:

> - The Art of Demomaking - Issue 01 - Prologue
> - The Art of Demomaking - Issue 02 - Introduction To Computer Graphics
> - The Art of Demomaking - Issue 03 - Timer Related Issues
> - The Art of Demomaking - Issue 04 - Per Pixel Control
> - The Art of Demomaking - Issue 05 - Filters
> - The Art of Demomaking - Issue 06 - Bitmap Distortion
> - The Art of Demomaking - Issue 07 - Bump Mapping
> - The Art of Demomaking - Issue 08 - Fractal Zooming
> - The Art of Demomaking - Issue 09 - Static Texture Mapping
> - The Art of Demomaking - Issue 10 - Roto-Zooming
> - The Art of Demomaking - Issue 11 - Particle Systems
> - The Art of Demomaking - Issue 12 - Span Based Rendering
> - The Art of Demomaking - Issue 13 - Polygon Engines
> - The Art of Demomaking - Issue 14 - Perspective Correct Texture Mapping
> - The Art of Demomaking - Issue 15 - Music And Synchronization
> - The Art of Demomaking - Issue 16 - The Final Product

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.