Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Fmod basics  (Read 7964 times)

0 Members and 1 Guest are viewing this topic.
Fmod basics
« on: February 05, 2010, 05:06:36 am »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
This is gonna be a quickie because well..Fmod is as easy as taking a !@#$% after not pissing for 2 days straight.

Alright, first of all, go download the Fmod library for Windows.  That's usually a good start.  Fire up ye olde compiler, and add the proper dlls and such to the solution (the same way we did with openGL, if you've read my tutorial.).  Got it?  Good, let's get some code going.

First thing you'll want to do is include the hpp file so..

Code: [Select]
#include "fmod.hpp"
If you don't know what this means, then go learn your damn C++ first!

Next you'll need to create a pointer to the System class, which we'll do by..

Code: [Select]
FMOD::System * system;
This is not going to be initialized like a normal pointer, I.E we won't be calling new on it.  Instead, we'll pass it to a function..

Code: [Select]
FMOD::System_Create(&system);
system->init(100, FMOD_INIT_NORMAL, 0);

easy enough, right?  This sets it up for you.  Honestly, I have no idea what the hell these parameters do, but they work.

Alright, no you need a few things.  You need a sound pointer and a channel pointer.  We'll do that like so..


Code: [Select]
FMOD::Sound *sound;
FMOD::Channel *channel;

The sound is where we'll be storing the sound file, and the channel is what will be what is essentially playing the sound.

now depending on whether or not your sound is going to stream, you're going to use one of two functions.  Streaming is typically
more CPU intensive (check me on that someone? I may be confusing streams with sounds.)

Should you choose to use a stream...

Code: [Select]
system->createStream("sound.ogg", FMOD_DEFAULT,0,&sound);
and if a sound..

Code: [Select]
system->createSound("sound.wav", FMOD_DEFAULT,0,&sound);
from that, I hope you see the difference between the two.  Take note of the first parameter.  The stream uses an ogg, while the sound
uses a wav.  Things like background music are good for streams, while quick sound effects are good for sounds.

and now for actually playing the sound you've loaded..

Code: [Select]
system->playSound(FMOD_CHANNEL_FREE, sound, 0, &channel);
The sound is played on the specified channel, so if you want the sound to shut the hell up..

Code: [Select]
channel->stop();
and that's it!  When you're done with your program, clean up your mess, and you're done.

Code: [Select]
sound->release();
system->release();

This was pretty simple, Fmod is straight forward.  See if you can make this modular or even class based on your own.  It makes it
really easy to manage.
« Last Edit: February 05, 2010, 05:08:52 am by MG-Zero »
Logged



i love big weenies and i cannot lie
Re: Fmod basics
« Reply #1 on: February 05, 2010, 08:10:17 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Nice tutorial MG. And is that really all to FMOD. I was kind of hoping there were some awesome features for panning and volume control.

Whenever I try to set up an audio system with FMOD, I run into the problem that I can only play one sample or stream at the time. When a sample or stream is already playing any other samples and streams started are ignored. FMOD does have audio mixing capabilities. Do I set up something wrong in the compiler or linker. I use MSVC9 with Visual Studio C++ 2008 Express.

Code: [Select]
FMOD::System_Create(&system);
system->init(100, FMOD_INIT_NORMAL, 0);

easy enough, right?  This sets it up for you.  Honestly, I have no idea what the hell these parameters do, but they work.
If I remember correct the first parameter '100' is the max number of channels you want to use, thus the max number audiofiles you can play at the same time.

The second parameter is FMOD's internal configuration, about how it deals with sounds and streams. But beside FMOD_INIT_NORMAL, I have no !@#$% clue what else you could use.

The third I have no idea, but in most circumstances not important. So if I have to guess, it probably is additional info to be used with some flags for the second parameter. But don't quote me on that.

now depending on whether or not your sound is going to stream, you're going to use one of two functions.  Streaming is typically more CPU intensive (check me on that someone? I may be confusing streams with sounds.)
Streams are more CPU intensive and Sounds are more memory intensive. With Streams the music is loaded partially into memory and played at the moment the sound is played, thus not when the Stream is created and initialized. Sounds are completely loaded into the memory, when you create the sound. When playing the sound there is nothing to be loaded anymore.

With both options you can use compressed (ogg,mp3) and raw (wav) soundfiles. With compressed however FMOD decompresses them into memory when it is loaded. Thus with Sounds, the decompression happens during initialization and with Streams during playback.
Logged
Re: Fmod basics
« Reply #2 on: February 05, 2010, 05:31:48 pm »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
I'm not quite sure why you can't play more than 1 thing at once.  I haven't had any problems with this yet.  Can you show some code?
Logged



i love big weenies and i cannot lie
Re: Fmod basics
« Reply #3 on: February 05, 2010, 06:16:00 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Okay, it isn't exactly my code. I have been learning from a book called "Advanced 2D Game Development" typed in the code they gave it there. Which is exact (to the letter) the same code as the demo on the CD. But with me I can't get multiple audio to play, but the demo on the CD can. The only difference is that they use Dev C++ and I use Visual Studio 2008 Express.

Audio.h:
Code: [Select]
#include "Advanced2D.h"
#pragma once

namespace Advanced2D{

class Sample{
private:
std::string name;
public:
FMOD_SOUND *sample;
FMOD_CHANNEL *channel;

Sample(void);
~Sample(void);
std::string getName(){ return name; }
void setName(std::string value){ name = value; }
};//class Sample

class Audio{
private:
FMOD_SYSTEM *system;
typedef std::vector<Sample*> Samples;
typedef std::vector<Sample*>::iterator Iterator;
Samples samples;
public:
Audio();
~Audio();
FMOD_SYSTEM* getSystem(){ return system; }
bool Init();
void Update(); //must be called once per frame
bool Load(std::string filename, std::string name);
Sample* Load(std::string filename);
bool Play(std::string name);
bool Play(Sample *sample);
void Stop(std::string name);
void StopAll();
void StopAllExcept(std::string name);
bool IsPlaying(std::string name);
bool SampleExists(std::string name);
Sample *FindSample(std::string name);
};//class Audio

}; //namespace

Audio.cpp:
Code: [Select]
#include "Advanced2D.h"

namespace Advanced2D{

Audio::Audio(){
system = NULL;
}

Audio::~Audio(){
//release all samples
for(Iterator i = samples.begin(); i != samples.end(); ++i){
(*i) = NULL;
}
FMOD_System_Release(system);
}

bool Audio::Init(){
if(FMOD_System_Create(&system) != FMOD_OK){
return false;
}
if(FMOD_System_Init(system, 100, FMOD_INIT_NORMAL, NULL) != FMOD_OK){
return false;
}
return true;
}

void Audio::Update(){
FMOD_System_Update(system);
}

Sample* Audio::Load(std::string filename){
if(filename.length() == 0) return false;

Sample* sample = new Sample();
FMOD_RESULT res;
res = FMOD_System_CreateSound(
system, //FMOD system
filename.c_str(), //filename
FMOD_DEFAULT, //default audio
NULL, //n/a
&sample->sample); //pointer to sample

if(res != FMOD_OK){
sample = NULL;
}
return sample;
}

bool Audio::Load(std::string filename, std::string name){
if(filename.length() == 0 || name.length() == 0) return false;
Sample *sample = new Sample();
sample->setName(name);
FMOD_RESULT res;
res = FMOD_System_CreateSound(
system,
filename.c_str(),
FMOD_DEFAULT,
NULL,
&sample->sample);
if(res != FMOD_OK){
return false;
}
samples.push_back(sample);
return true;
}

bool Audio::SampleExists(std::string name){
for(Iterator i = samples.begin(); i != samples.end(); ++i){
if((*i)->getName() == name){
return true;
}
}
return false;
}

bool Audio::IsPlaying(std::string name){
Sample *samp = FindSample(name);
if(samp == NULL) return false;

int index;
FMOD_Channel_GetIndex(samp->channel, &index);
//FMOD returns 99 if sample is playing, 0 if not
return(index > 0);
}

Sample* Audio::FindSample(std::string name){
Sample *sample = NULL;
for(Iterator i = samples.begin(); i != samples.end(); ++i){
if((*i)->getName() == name){
sample = (*i);
break;
}
}
return sample;
}

bool Audio::Play(std::string name){
FMOD_RESULT res;
Sample *sample = FindSample(name);
if(sample->sample != NULL){
//sample found, play it
res = FMOD_System_PlaySound(
system,
FMOD_CHANNEL_FREE,
sample->sample,
true,
&sample->channel);
if(res != FMOD_OK) return false;
FMOD_Channel_SetLoopCount(sample->channel, -1);
FMOD_Channel_SetPaused(sample->channel, false);
}
return true;
}

bool Audio::Play(Sample* sample){
FMOD_RESULT res;
if(sample == NULL) return false;
if(sample->sample == NULL) return false;
res = FMOD_System_PlaySound(
system,
FMOD_CHANNEL_FREE,
sample->sample,
true,
&sample->channel);
if(res != FMOD_OK) return false;
FMOD_Channel_SetLoopCount(sample->channel, -1);
FMOD_Channel_SetPaused(sample->channel, false);
return true;
}

void Audio::Stop(std::string name){
if(!IsPlaying(name))return;
Sample *sample = FindSample(name);
if(sample == NULL) return;
FMOD_Channel_Stop(sample->channel);
}

void Audio::StopAll(){
for(Iterator i = samples.begin(); i != samples.end(); ++i){
FMOD_Channel_Stop((*i)->channel);
}
}

void Audio::StopAllExcept(std::string name){
for(Iterator i = samples.begin(); i != samples.end(); ++i){
if((*i)->getName() != name){
FMOD_Channel_Stop((*i)->channel);
}
}
}



Sample::Sample(){
sample = NULL;
channel = NULL;
}

Sample::~Sample(){
if(sample != NULL){
FMOD_Sound_Release(sample);
sample = NULL;
}
}

}//namespace

EDIT: Hmm, I just noticed that in this code they use C functions instead of C++ class capabilities.
« Last Edit: February 05, 2010, 06:29:45 pm by Niek »
Logged
Re: Fmod basics
« Reply #4 on: March 27, 2010, 12:37:49 pm »
  • 笑い男
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 2124
Sorry if this topic's a bit too old?
But I believe:
"Sound": loads audio file into memory (good for small sfx that you need to use often and fast (pointless if a "hit" sfx plays slightly after the "hit" graphic)).
Stream: streams audio file from hdd (so no need to load it all into memory first, which would not only possibly take too long but also easily eat up far too much memory. So good for music.)
Channel: Used to play an instance of the "loaded" audio file (via sound or stream I guess). Use this to play your sounds (Whether its one or a million of the same audio file)

Can't say if I am 100% correct, but I'm glad if it helps.

Just skimmed through your code, Niek and it seemed fine with how you're using it all though.
« Last Edit: March 27, 2010, 12:40:38 pm by hawthorneluke »
Logged

この世に悪があるとすれば、それは人の心だ
  • .hack//The World

Antidote

>.>
Re: Fmod basics
« Reply #5 on: March 28, 2010, 03:39:23 am »
  • In all seriousness who's serious?
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1485
Use Audiere 'nuf said.
Logged
  • Axiomatic Data Laboratories
Re: Fmod basics
« Reply #6 on: March 28, 2010, 05:39:30 am »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
Use Audiere 'nuf said.
!@#$% audiere. >.>
Logged
Quote
There's such a double standard about religion in the modern world. Catholics can gather, wear white robes, and say "In nomine Patris, et Filii, et Spiritus Sancti" and be considered normal.

But if my friends and I gather, wear black robes, and say  "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", we're considered cultists.
  • Development Blog

Antidote

>.>
Re: Fmod basics
« Reply #7 on: April 04, 2010, 03:41:07 am »
  • In all seriousness who's serious?
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 1485
Audiere is cross platform and I for one am a huge supporter of cross platform.
Logged
  • Axiomatic Data Laboratories
Re: Fmod basics
« Reply #8 on: April 04, 2010, 07:02:14 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.054 seconds with 52 queries.