issue #11

mod 1

By Bill McDonald

Wanna know how to do a MOD player?

SECTION 1
1.1 Ramblings
Hey everyone! some of you might know me some of you might not. In any case my name is Bill McDonald head of PyroElectric Software. I have about 6 years of programming experience behind me so i consider myself pretty advanced, but not a god like Angelo Mottola, Toshiriro Horie, and all those other guys who can do 3d. I envy you!

This is my first tutorial so i thought an intro would be nice. Please excuse any spelling mistakes and so on.

 
 

1.2 Thanks
special thanks to
Brett Paterson of Firelight for his Tutorial
Angelo Mottola of Enhanced Creations for DQB and many other things
ZkMan for publishing this tut.
Jcv (where ever you are) for giving my mod player some competition
and all others who have helped me.

1.3 Blah Blah Blah get on with it..Disclaimer
I hate to do this but

Disclaimer:
Any use of this material or ideas/code presented in it is your own responsibility and the author takes no responsibility for what you do to your computer/data with this information. And all this other legal crap everyone doesn't read.

SECTION 2
2.1 What is a mod file?
What is a mod file you say? well a mod file is a music format originally started on the amiga computers. It consists of patterns and orders in which samples (wav files) are played a varying frequency's and intervals to produce a unique sounding music. Mod files have been around a long time so there are many formats it have evolved into but in this tutorial we will cover 4 channel, 31 sample mod files.

2.2 Why Bother?

  • - Why not?
  • - Its a programming challenge to do it in QB and it has been done only a few times by Me, JCV, and Rich GeidReich.
  • - The music is great for a background in your games/demos.
  • - It shows off your programming talent :)
  • - It is good practise for all kinds of programmers

2.3 Memory Requirements and Programs needed
The memory requirement of a .mod player are steep unfortunately, 4 channel mods with 30 patterns will take just under 32k of conventional memory but you could always use EMS/XMS to hold the patterns. You always need to think of new memory saving techniques and ways to store that patterns that will use the least memory. The way I set up my first player was a array dimensioned to the size I needed to store the patterns and if there are too many patterns load as many as you can and then skip the rest and change the orders so that they don't point to non-existent patterns.

The programs you will need to run the examples in this tut are

  • QB v4.5 - for library support and speed (haha, speed). You can find this at www.quickbasic.com
  • DQB v1.61 - for the terrific sound engine among other things. you can get this at ec.quickbasic.com
  • A working .mod player (Mod4win.com, or winamp.com)
  • Some .mod files to play. I suggest getting axelf.mod - it has no effects, nor.mod - only a few effects and then impsible.mod - Mission Impossible, to test pattern jump effect
  • Lots of Free Time :)

     

 
 

SECTION 3
3.1 Notes/Periods Explained (Terminology)
Ok, here goes..

  • ROWS - go from 0 to 63 and each row in a 4 channel holds 4 notes
  • NOTEs - are actually the sample number you play
  • PERIODS - are the frequency you play the sample at
  • ORDERS - orders are how the mod plays from 0 to length of song.
  • PATTERNS - patterns are played in any ORDER, and are the physical information.

TYPE  LENGTH Bits RANGE           Basic
byte  1      8    0-255           String * 1
word  2      16   0-65,535        Integer
dword 4      32   0-4,294,967,295 Long

* This is as close to the actual ones that I could think of.. (anyone that can do better email me).

3.2 Setting up the arrays/variables
You will need to make a couple type array to hold the song info.

TYPE Song
Handle as integer
MK as string * 4
Length as integer
Channels as integer
END TYPE

3.3 Checking to see if its a valid .mod
For the header of the mod file it contains the name which is a 20 character string sometimes terminated by a null (0) character.

Before we load a mod we have to check that it is a mod. Every mod has a signature this is in the form of a 4 letter string that equals "M.K." The signature is stored at offset 1080 (438h) in the file, so it should be checked first.

At offset 1081 there should be "M.K." identifies it as a 4 channel 31 sample mod file. So the check should look something like this..

Open the mod file for binary
SEEK Modfile, 1080
GET modfile, , Song.MK


IF Song.Mk = "M.K." THEN
Song.Channels =4
ELSEIF
PRINT "Not a 4 channel mod file!"
END
END IF

SECTION 4
4.1 Loading the rest of the header
The rest of the header contains the sample info and the order/pattern info.

Sample information is stored at the start of a MOD file, and contains all the relevant information for each of the 31 samples. This includes its name, length, loop points, finetune and what not. 4.2 Data types for the sample headers
I think the best way to store information on the 31 instruments, is to store its information in a structure, then have an array of 31 of these instrument structures.

type SAMPLE
NAME as string * 22
LENGTH as integer
FINETUNE as integer
VOLUME as integer
LOOPSTART as integer
LOOPLENGTH as integer
end type
DIM SHARED Sample as Sample

4.3 Loading the Sample headers
So from here we loop 31 times and read in a block of information about the sample according to the loop counter. Load all the info into temporary strings first so you can manipulate the bytes and then store them in the proper array element.

for i = 1 to 31
- read in 22 bytes, store as NAME$
- read in 2 bytes, store as LENGTH$
- read in 1 byte, store as FINETUNE$
- read in 1 byte, store as VOLUME$
- read in 2 bytes, store as LOOPSTART$
- read in 2 bytes, store as LOOPLENGTH$
next I

The Name, Length, Loopstart and Looplength need to be caclulated with this formula (byte1*100h + byte2) * 2 I will provide my alternate formula in the next issue.

Byte1$ = LEFT$(Length$, 1)
Byte2$ = RIGHT$(Length$, 1)
Sample.Length = (ASC(Byte1$) * 100h + ASC(Byte2$))

For FINETUNE, if the value is > 7, subtract 16 from it to get the signed value

Conclusion
Well this is the end of the first part of my 5 or 6 (depending if I cover effects) part MOD player tutorial.

Thanks for reading
So SpaceWar is your favorite game? Lemme know what a mistake at this address.

 

Back to Top