Print Page | Close Window

FMod Music Support

Printed From: Mirage Source
Category: Tutorials
Forum Name: Submitted Tutorials
Forum Discription: Tutorial submissions for MSE are posted here, waiting for approval
URL: http://ms.shannaracorp.com/backup-forums/forum_posts.asp?TID=33
Printed Date: 20 December 2006 at 6:02pm
Software Version: Web Wiz Forums 8.01 - http://www.webwizforums.com


Topic: FMod Music Support
Posted By: Sync
Subject: FMod Music Support
Date Posted: 07 February 2006 at 5:37pm
Originally posted by Dave

This tutorial assumes you're using MSE Build 1, and you have the FMod 3 package.

First go into the FMod package and get the FMod VB module, add that to your /src/ folder, then go into the editor and add it to the project.  Next, copy the FMod DLL file into the client folder, I made a seperate folder in my client called dlls.  You  will need to distribute this DLL with the client.  Next, put that FMod.dll file into your system32 directory, and regsister it.

Now you're ready for the code!

Open up modSound and comment everything in it.  That's a different sound system, of cource, we don't need it.  You can also go into modDeclares and comment the declarations to those functions for sound.  (dont delete it, you may want to try using that stuff later)

Add this code to sub Main(), right after the TCP init stuff. This initializes FMod, and if it doenst work, it displays an error.  You need to do this before you do (almost) anything with FMod.
Call SetStatus("Initializing FMod...")
    If FSOUND_Init(44100, 32, 0) = 0 Then
        'Error
        MsgBox "An error occured initializing fmod!  Sound will not play!" & vbCrLf & _
        FSOUND_GetErrorString(FSOUND_GetError), vbOKOnly
    End If


At the top of modSound, declare:
Dim songHandle As Long
Public CurrentSong As Byte


Ok go into PlayMIDI(), lets start playing those songs!

Put this into your little modifications box, come on, you know you want to...
09/03/2005  Dave       Converted to FMod


Declare FilePath as String, and then set FilePath equal to App.Path & "\music\" & Song

Check and make sure it exists:
If Not FileExist(FilePath, True) Then
        MsgBox "Music file " & Song & " doesn't exist"
        Exit Sub
    End If


Now I get to explain a little about FMod.  To play a song, you need to load it, then play, when it's done you need to stop it if necesary, and then unload it again.  For this code here, we need to load the song now... it's easy!

songHandle = FMUSIC_LoadSong(FilePath)


Now we use that number, SongHandle, to play the song... but first we do a little check.
If songHandle <> 0 Then
        'Loading was successful
        ------------------
    Else
        'Something went wrong
        MsgBox "An error occured opening the song!" & vbCrLf & _
             FSOUND_GetErrorString(FSOUND_GetError), vbOKOnly
    End If


ERROR HANDELING!  WOO!

Here's the code to play the song, with error handeling, of cource :)
If FMUSIC_PlaySong(songHandle) = 0 Then
             'Something went wrong
             MsgBox "An error occured playing the song!" & vbCrLf & _
                 FSOUND_GetErrorString(FSOUND_GetError), vbOKOnly
        End If


Here's the code to put in StopMIDI:
FMUSIC_FreeSong songHandle
    songHandle = 0
    CurrentSong = 0

Easy, eh.

Ctrl-F, type in there PlayMIDI( and find the one instance of it in the program, it's in HandleData.

After the play music comment, ADD THIS LINE IF AND ONLY IF YOU WANT THE MUSIC TO CONTINUE PLAYING WHEN YOU CHANGE TO ANOTHER MAP WITH THE SAME MUSIC.  IF YOU LEAVE THIS OUT, THE MUSIC WILL RESTART WITH EVERY MAP CHANGE.
If CurrentSong = Map.Music Then Exit Sub


After PlayMIDI( is called, add this line.
CurrentSong = Map.Music


That's all, you've got FMod now.  Hoo-rah.  No options or anything, you get to do them yourself :)  Good luck, if anyone expands on this code, such as making the songs fade when switching and so forth, please do share it! 

Enjoy your FMod sound stuffs,
DAve
[edit] Forgot to tell you to close FMod before closing your program, you get errors and crashses and sometimes a little guy jumps out of your screen and eats you.  Don't forget this!

Put this after in GameDestroy:  FSOUND_Close

I'd like to point out the one and only bug I've seen with this code, I have no idea what's wrong, and it's a very minor bug.  If any of you know/find a fix to this problem, let me know.

Music Play Fast bug: Music randomly plays at an accelerated pace... and only seems to happen on outset island O.o
Cause: Happens when playing one song, and warping to a map with outset island on it, it does not matter which number the two songs are in, the relationship between them stands strong.

Happens when warping from map 1 when music is 1 to the map with outset island, does not speed up other songs.
Happens from map 2 when map music is 1, only speeds up with outset island.
Does not happen when music <> 1

Definantly a relationship between that first song and ... outset island


But... Dave... we want MP3 support!

Okay!

Dim StreamChannel As Long
Dim streamHandle As Long

Put those at the top of your modSound

Public Sub StopMP3()

FSOUND_Stream_Close streamHandle
streamHandle = 0
CurrentSong = 0
End Sub


Add that sub in there...

and add this sub in there too!

Public Sub PlayMP3(Song As String)
Dim FilePath As String

    FilePath = App.Path & "\music\" & Song
   
    If Not FileExist(FilePath, True) Then
        MsgBox "Music file " & Song & " doesn't exist"
        Exit Sub
    End If

    streamHandle = FSOUND_Stream_Open(FilePath, FSOUND_LOOP_NORMAL, 0, 0)
    If streamHandle <> 0 Then
        StreamChannel = FSOUND_Stream_Play(FSOUND_FREE, streamHandle)
        If StreamChannel = 0 Then
             'Error occured
             MsgBox "An error occured playing the stream!" & vbCrLf & _
             FSOUND_GetErrorString(FSOUND_GetError), vbOKOnly
        End If
    Else
        MsgBox "An error occured opening the stream!" & vbCrLf & _
        FSOUND_GetErrorString(FSOUND_GetError), vbOKOnly
        Exit Sub
    End If
End Sub



After this, I suggest you write up a little bit of code on your own to check if Fmod initialized correctly, and store it in a boolean variable.  If you try to play a song without fmod initialized, your client will crash... I think.  Something like If FmodInit = False then exitsub does nicely.  set FmodInit = True when you start up fmod.

Use those two subs to your liking to play MP3 files.

~Enjoy



Replies:
Posted By: Sync
Date Posted: 07 February 2006 at 5:38pm
If someone can figure out the problem with the fast sound playing, that would rule, otherwise this tutorial would of been approved!




Print Page | Close Window

Bulletin Board Software by Web Wiz Forums version 8.01 - http://www.webwizforums.com
Copyright ©2001-2006 Web Wiz Guide - http://www.webwizguide.info