How to play XM Music From your own code
=======================================
Over View
=========
Heya all,
the perefect way to play XM music is by using the MiniFmod.
since it is free to use, we can produce really cool keygens.
i'v choosed keygens as the perfect taregt to play music on,
as we all know its cool in the end.
allright, lets begine.
first we will deal with the music.
The best way to find our XM music is the mod archive located at: http://www.modarchive.com/
it is a huge archive, and allot of cool music can be found there,
so just before coding, select ur file (recomended size : 2k-30k)
i especially like the "Hybrid Song.XM", (i first heard it in a installer
of Worms :-) ) or "trainer.XM", but i am sure there are millions of them out there.
once we choose our music, we need to dump its content!!
now, sicne this article is for Visual C++ coders, our dump
is apparently C++ style hex.
for the dumping rutine we will use Thigo's excellent Table Extractor,
located at protools/anticrack..or just google for it.
Duming Table
============
/* Dumping Hybrid Song.XM (51k) */
Load your XM file in Thigo's Table extractor,
now we need to supply few info to the program:
Address (HEX): 0
Items: Bytes
Size (Bytes) of Table(Hex): CCD4 /* File Size */
Number Of Items on a Line: 20 /* put how many you like personally i put 20 */
C/C++ /* Dump Style Language */
Dump to file: checked /* [v] */
ok, we dont need anymore options filled,
Press the "Go !" button and we see our thread make an effort to dump
the Image as fast as possible ;-),
0....100%, great Table dumped in result.txt (same dir as the XM file).
Coder Point of View
===================
look at the dumped results.txt..geesh, it is very big..(300k)
yes, thats the coast of files that have 20++k and above!,
so keep in mind that since your making a keygen, the file should be
as minimal as possible, but consider the best sound u can get :-)
save this file somewhere, we need to use some source code from now on.
The MiniFmod
============
information about MiniFmod 1.6:
This small XM replay system only adds 5k to your exe! Now including FULL SOURCE CODE!!!
FEXP tool to export a header based on your song, which will be compiled into MiniFMOD and exclude whole portions of code!
Pre-buffered output for 0 latency, and high output stability
100% click free.
XM sample callbacks for user generated or compressed XM samples!
File system callbacks so you can specify whatever loading system you like! (disk/wad/memory)
Copyright 2001-2002 Firelight Technologies Pty.
inside the MiniFmod package, there is a small tool called Fexp.exe,
this will be our XM effects optimizer for the dumped result.txt!!
other that, we have the *.c/*.h files that we will incldue in our program.
i have included a keygen template which will suite us for including the sound code.
that way, we will code together step by step.
ok, get the template and load the DSW into VC++ IDE.
it is a very basic keygen with 3 buttons,
Generate, Play, About.
our main goal is to make the sound be playble via the Play button,
it is later to be loaded automatiaclly when keygen starts.
ok, so we have our keygen.cpp with everything ready.
Starting to Code
================
the Sound must have an Handle, of course, that way
we can determine whenever we try to stop, reload, and even sharing
with other sound players (i.e: Winamp)
as a global variable, we start with:
FMUSIC_MODULE *mod; /* fmod music handler */
now, before we add more code, lets add to our project the minifmod
functions and main code.
make a new folder in the keygen's main folder, call it "Lib"
you can see it is also in the minifmod16.zip
copy the files from the minifmod16.zip\lib -> our lib folder
note: in the zip\lib there is 2 folders named "debug/final" don'not
copy its content!
alright, now back to our project,
in the project dir viewer, make a folder named "MiniFmod.c" (under the Source Files folder)
add all *.c files into it from our lib folder.
these files are:
now we need to add the header files (*.h),
make a new folder named "MiniFmod.h" (under the Header Files folder)
add all *.h files into the it from our lib folder + the lib file.
these files are:
1. minifmod.h
2. Mixer.h
3. mixer_clipcopy.h
4. mixer_fpu_ramp.h
5. Music.h
6. music_formatxm.h
7. Sound.h
8. system_file.h
9. system_memory
10. xmeffects.h
11. winmm.lib ; not an *.h file, but dont forget to add it too!!!
now, since its a keygen and using the standard minifmod source is not enough,
we will make some modifications into its code, that means some files from the original
has been slighly modifyed to suite our needs!
note: the modified files has been added to the source code, so i took of it already.
another thing we need to do is to create a new folder named "loadmusic.h"
(under the Header Files folder).
now listen carefully, remember the result.txt we have dumped ?
we will use it's hex dump as the file it self!,
rename the result.txt -> music.h (as specified in the modified files :-))
open it (be carefull, editing big files can cause a crash :-)),
now we need to edit the file a bit.
original Dump:
==============
static unsigned char table[52436] = {};
note: the diffrences, always do this when trying to load new music into ur keygen!!!
once we finished editing and saving, we need to make a new *.h file:
open notepad and write/paste this code:
// Memory functions
unsigned int memopen(char *name);
void memclose(unsigned int handle);
int memread(void *buffer, int size, unsigned int handle);
void memseek(unsigned int handle, int pos, signed char mode);
int memtell(unsigned int handle);
its a few addons(add on), basically we are making it easier for us to load the dumped xm
file into memory and make it playble.
save this file as loadmusic.h
finally, we add those 2 files (music.h / loadmusic.h) into the loadmusic folder.
last and not least, we need to add the loadmusic.cpp, which is of course not a part
of minifmod, as i said it is our modified code.
make a folder named "loadmusic.c" (under the Source Files fodler).
add this file (loadmusic.cpp)
allright, this fun the setup part of minifmod,
we now need to add those as #include in our main keygen.cpp
note: if you take a look at loadmusic.cpp you can see our music.h declared there,
along with our new modified functions.
The Last Touch
==============
we have declared the handle of our fmod music rememebr?
FMUSIC_MODULE *mod;
right, all we need to do is to set the Fmod up and load the dumped xm file (music.h)
into memory and last call the play function!
at our play button id code (case IDC_PLAY:)
we add this code:
if (mod == NULL) // mod handle is free? (though it will work fine with other loaded audio devices)
{
// We defined our music file to be loaded in loadmusic.cpp //
//=============================================================//
loadmusic(); // Call & set ready memory to load the music
if(!FSOUND_Init(44100, 0)) // intialize memory for sound and format
{
break;
}
mod = FMUSIC_LoadSong(NULL, NULL); // handle = LoadSong()
FMUSIC_PlaySong(mod); // Play it (from memory), way cool!!
}
note: if you put this code in the WM_INITDIALOG, you will get autoplay effect when
loading the keygen!
if we want to Stop the music only thing we need to do is add new button called Stop,
choose an ID to it and use this code:
if (mod != NULL) // handle is loaded (playing)?
{
FMUSIC_FreeSong(mod); // Free memory (handle)
FSOUND_Close(); // Close it (stop it from playing)
mod=NULL; // make handle to be Free again
}
note: add same code at WM_CLOSE msg!!
Important Tips
==============
this is the part where if u dont read it, u will get a funny results
when playing your XM file!!
/* from Firelight's readme */
Making your exe even smaller with XMEFFECTS.H and FEXP.EXE:
- Note all the effects in the xm replay code are wrapped with #ifdef's. They are defined in
xmeffects.h.
- Have a look in the zip/lib directory .. There is an executable called FEXP.EXE
- Use FEXP.EXE on an xm file and it will generate xmeffects.h for you!
- Recompile the minifmod library with the new xmeffects.h
- See your exe size go down AGAIN!
- (only play the song you fexp'ed or it will screw up on other songs probably :) ..)
yup, this mean, we make a new xmeffects.h for each music file we dump!!
else as said, we have a screwed up music :-)
-> fexp Hybrid_Song.XM -> xmeffects.h
overwrite the old xmeffects.h in our keygen\lib\ folder, and recompile our source code,
see your music plays fine.
some little note is that the minifmod abit looses the quality of the music,
well this is understandble since minifmod is not a part of the fmod library, so
it does miss some important stuff, but hey, nothing is perfect!!
for me, it rox to have cool sound in keygens, and now you guys too.
Ending
======
well,this complete the essay,
the best way to learn from it is by using the files included in the compiled keygen,
that way u can always have this as a base keygen.
MiniFmod is coded by FireLight (r)
Keygen source code + music coded by Ben & [Goofy] of FreeStylerS 2002
用Thigo的Table extractor加载你的XM文件,现在我们需要提供一点信息给程序:
地址(十六进制):0 (Address(hex): 0)
项目:Bytes (Items:Bytes)
表(十六进制)的大小(Bytes): CCD4 /文件大小/ Size (Bytes) of Table(Hex): CCD4 /* File Size */
每条线上项目数:20 /输入多少由你喜欢,我个人是20/
Number Of Items on a Line: 20 /* put how many you like personally i put 20 */
C/C++ /抓取所用的语言风格/ C/C++ /* Dump Style Language */
抓取到文件:勾选 Dump to file: checked /* [v] */
注意:当试图加载新的音乐到你的注册机里时,这些区别总是要做的(the diffrences, always do this when trying to load new music into ur keygen!!!)。一旦完成编辑并保存,我们需要新建一个*.h文件:
打开记事本,写或粘贴如下代码:
//===============================================================
#ifndef __LOADMUSIC_H__
#define __LOADMUSIC_H__
// Memory functions
unsigned int memopen(char *name);
void memclose(unsigned int handle);
int memread(void *buffer, int size, unsigned int handle);
void memseek(unsigned int handle, int pos, signed char mode);
int memtell(unsigned int handle);
if (mod == NULL) // mod handle is free? (though it will work fine with other loaded audio devices)
{
// We defined our music file to be loaded in loadmusic.cpp //
//=============================================================//
loadmusic(); // Call & set ready memory to load the music
if(!FSOUND_Init(44100, 0)) // intialize memory for sound and format
{
break;
}
mod = FMUSIC_LoadSong(NULL, NULL); // handle = LoadSong()
FMUSIC_PlaySong(mod); // Play it (from memory), way cool!!
}
注意:如果你将这段代码放在WM_INITDIALOG,在加载注册机时会有自动播放效果。
如果要停止音乐,我们只要新增一个Stop按钮,对它选个ID,并使用下列代码:
if (mod != NULL) // handle is loaded (playing)?
{
FMUSIC_FreeSong(mod); // Free memory (handle)
FSOUND_Close(); // Close it (stop it from playing)
mod=NULL; // make handle to be Free again
}