Using the FMOD API with MASM – Part 1/3

To succeed in the world it is not enough to be stupid, you must also be well-mannered. — Voltaire



FMOD (available at http://www.fmod.org) is an API which allows easy code implementation of playing back different formats of sound files. I stumbled upon it after a sudden flash of nostalgia send me searching for some simple routines to playback some of my old module files (no need to reinvent the wheel, now is there?) The main problem with using APIs with MASM is that oftentimes there are no libraries available for those APIs. Even though apparently the FMOD devs used to supply MASM header and libraries with their distro, they stopped doing so. But fear not, not all is lost – there are still ways to make FMOD, and every other API for that matter, work from within MASM code.

Using LoadLibraryA and GetProcAddress

Achieving our goal is actually pretty simple. The Microsoft Windows API gives us two functions which help us to do that: LoadLibrary and GetProcAddress. LoadLibrary returns a handle to the specified DLL and GetProcAddress uses that handle to return the address of the specified exported DLL-function.

To get a handle to „fmod.dll“ we would need to use the following code (note that the data will certainly have to get defined in its proper locations):

.data
DllName db "fmod.dll",0
.data?
DllHandle dd ?
.code
invoke LoadLibraryA, addr DllName
mov DllHandle, eax

Now that we have the handle to „fmod.dll“ we can use GetProcAddress to call the function we wanted to call in the first place:

.data
FSInit db "_FSOUND_Init@12",0  ; name of the function
.code
; === get the procedure adress of FMUSIC_LoadSong ===
invoke GetProcAddress, DllHandle, addr FSInit
push 0                         ; third parameter
push 32                        ; second parameter
push 44100                     ; first parameter
call eax                       ; call the function FSOUND_Init

In the above pseudo-code snippet we first define the name of the function. The @12 behind it tells MASM how many parameters that function accepts. For each parameter you add a 4 to that number (4 Bytes = DWORD = size of one parameter). As we know the function FSOUND_Init has 3 parameters this number is 3*4=12. After we got the address of the function by using GetProcAddress we need to supply the parameters the function needs in reverse order as the last one will obviously bet he first to be popped from the stack and vice versa. By calling eax (which holds the return value of GetProcAddress which again should be the address of our function) we call FSOUND_Init.

That is pretty much all there is to know about how to get your MOD/XM/S3M or even MP3s working by using the FMOD API with MASM. If you still have trouble implementing it yourself have a look at the example I have coded. It does display a simple messagebox and loops a MOD file as long as this message box displays. it also features how one would be using the GetLastError function provided by Microsoft.

Download the first part of the FMOD API programming in MASM tutorial:

FMOD-MASM Part 1/3 - Part one of the "Programming FMOD in MASM" tutorial series.
VN:F [1.9.18_1163]
Rating: 0.0/5 (0 votes cast)
Thursday, January 29th, 2009 at 14:16
61 visits
  • Feb 8th, 2009 at 19:15 | #1

    People tell me that the executable aint working – they get the error-message “The file could not be found!” Note: this is on purpose! As I do mention in the readme.txt there is no “*.mod” file included in the archive (for copyright reasons). So you will need to download or create your own .mod file, rename it “modname.mod” if you dont want to recompile the source and copy it into the folder where the executable is located.

    VN:F [1.9.18_1163]
    Rating: 0.0/5 (0 votes cast)

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>