Some pepople like to play music in their applications. Generally, maybe because of size, people prefer .xm files and play them. Most cases are MFC and Win32. un4seen.com had coded BASSMOD.dll that can play them. This project uses this DLL without link to bassmod.lib.
There are some code samples in the internet to do this, but this project has three good points that others don't:
- Their code uses bassmod.lib for linking the application to bassmod.dll. So, the coder cannot extract bassmod.dll from its resource and use it.
- Their code is so confusing and people cannot use it easily.
- bassmod.dll is packed using PEtite 2.2. This may casue some anti virus softwares; detect it as virus or suspicious file. Or maybe users get suspicious about it. I unpacked bassmod.dll successfully and you can use unpacked bassmod.dll in your project. Unpacked bassmod.dll is in package.
Using the Code
You just need bassmod.dll to use this code (included in the package). For your convenience, copy the bassmod.dll to Windows directory.
When playing music reaches the end of music, it replays it. So, playing music is an infinite process that will never return. Since, I created I thread to play music.
HANDLE hthread = CreateThread(NULL, NULL, MusicThread, NULL, NULL, NULL);
If your project has a window (MFC or using CreateWindow), this code will work. But if your project is Win32, you should create another thread for your job and wait for it:
As this project doesn't link bassmod.dll to EXE, we should define function pointers that we need in run-time. The needed function pointers in bassmod.dll are:
typedef int (__stdcall *func_BASSMOD_Init)(int device, int frequency, int flags);
typedef int (__stdcall *func_BASSMOD_MusicLoad)
(int mem, char *XMfile, int offset, int length, int flags);
typedef int (__stdcall *func_BASSMOD_MusicSetSync)
(int ptype, int param, void *function, int user);
typedef int (__stdcall *func_BASSMOD_MusicPlay)();
typedef int (__stdcall *func_BASSMOD_Free)();
Needed function pointer in winmm.dll:
typedef DWORD (__stdcall *func_timeGetTime)();
Now, we should get the address of needed functions in run-time (using
GetProcAddress API) and set them to the defined pointers:
HMODULE bassmod_module = LoadLibrary("bassmod.dll");
pBASSMOD_Init = (func_BASSMOD_Init)GetProcAddress
pBASSMOD_MusicLoad = (func_BASSMOD_MusicLoad)GetProcAddress
pBASSMOD_MusicSetSync = (func_BASSMOD_MusicSetSync)GetProcAddress
pBASSMOD_MusicPlay = (func_BASSMOD_MusicPlay)GetProcAddress
pBASSMOD_Free = (func_BASSMOD_Free)GetProcAddress
HMODULE winmm_module = LoadLibrary("winmm.dll");
ptimeGetTime = (func_timeGetTime)GetProcAddress(winmm_module, "timeGetTime");
The only thing to do is initialize bassmod (
BASSMOD_Init) and play the music.
Play from File
You can play xm file from file:
pBASSMOD_MusicLoad(FALSE, xmFileName, 0, 0,
Play from Memory
You can play xm file from memory. My example reads file, loads all of it in memory and then plays it from memory, but you can load in memory from your project's resource. You should load your xm file from resource and play it. Playing from memory is like this:
HANDLE hfile = CreateFile(xmFileName, GENERIC_READ,0,0,
DWORD filesize = GetFileSize(hfile, 0);
BYTE *data = new BYTE[filesize];
ReadFile(hfile, data, filesize, &read, NULL);
pBASSMOD_MusicLoad(TRUE, data, 0, filesize,
I coded the project to replay the music but you can change
BASSMOD_MusicSetSync's parameters to play once.
I defined two global variables in my project:
You can notice their use by their names. In my project's
WinMain fucntion, you'll see these lines:
playFromMemory = true;
strcpy(xmFileName, "f:\\4EverBlue Morninsky.xm");
If you set
true, the application will play from memory and if you set it to
false, the application will play it from file.
xmFileName directly, but you can use it as you want.
I included bassmod.h in the package that doesn't need indeed. I included it for special uses, because maybe someone wants to change function paramters, etc. There are a lot of value definitions in bassmod.h that you can use on your own (project doesn't need it).
I included a sample .xm file in the package: 4EverBlue Morninsky.xm.
You can play it using my project and players like
KMplayer. It's cool.
I included bassmod.lib in the package too. It's unnecessary for my code, as you see in this picture:
I included bassmod.lib, because I think maybe some people want to link bassmod.dll to their projects. I mention again that bassmod.lib is absolutely unnecessary.