FMod:Tutorials:Playing Tracker Modules

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

Loading and playing tracker modules with FMod

FMod makes it very simple to play tracker modules:

The Fmod system is initalized with a call to FSOUND_Init()

#include <stdio.h>
#include <conio.h>
#include <time.h>
#include "fmod.h"
 
void Pause(clock_t Delay);
 
int main(int argc, char* argv[])
 {
  int Index;
 
  FMUSIC_MODULE *Song;
 
  // Initalize the Fmod System at 44Khz and 32 channels
   if(FSOUND_Init(44100, 32, 0))
    {
     printf("FMOD Init OK\n");
    }
   else
    {
     printf("FMOD Init Failed!\n");
     return 0;
    }

We load our tracker mod with FMUSIC_LoadSong()

// Load our tracker module
  Song=FMUSIC_LoadSong("distant.mod");
 
   if(Song==NULL)
    {
     printf("Load song failed!\n");
     return 0;
    }
   else
    {
     printf("Load song OK, Press any key to stop playback\n");
    }

FMUSIC_PlaySong() and FMUSIC_StopSong() control the playback, here I've used FMUSIC_SetMasterVolume to fade out the track.

// Play the tune
  FMUSIC_PlaySong(Song);
 
  // Wait for a keypress
  getch();
 
  printf("Fading...\n");
 
  // Fade the song out nicely
   for(Index=256;Index>0;Index--)
    {
     FMUSIC_SetMasterVolume(Song,Index);
     Pause(1);
    }
  // Stop playback
  FMUSIC_StopSong(Song);

Finally a call to FSOUND_Close() cleans up Fmod.

// Shut down the FMod system
  FSOUND_Close();
 
  return 0;
 }

This function provides a short delay to allow the music to fade slowly

// Delay execution for a few milliseconds
void Pause(clock_t Delay)
{
 clock_t ExitTime;
 
 ExitTime=Delay + clock();
 
  while( ExitTime > clock() ) ;
}

Note: You'll need to link in the fmod library, "fmodvc.lib" or whatever is appropriate for your compiler.

Source Code

External Links