0
0

It’s alot so I’ll have to post it all hehe.

Edit: This is for Windows. I forgot to add that the first time.

Sound.h:

[code:1dde6k4q]

pragma once

include "fmod.h"

include "fmod_errors.h"

include <list>

include <windows.h>

class SoundSample
{
public:
SoundSample(void);
~SoundSample(void);

int id;
FSOUND_SAMPLE* sample;
};

typedef std::list<SoundSample *> SoundList;
typedef SoundList::iterator SLIT;
class CSound
{
public:
CSound(void);
~CSound(void);
BOOL LoadSound(char *filename, int id, BOOL loop);
BOOL PlaySound(int id);
void DeleteSounds();
void DeleteSound(int id);

private:
SoundList sounds;

};
[/code:1dde6k4q]

Sound.cpp:

[code:1dde6k4q]

include “sound.h”

pragma comment(lib, “fmodvc.lib”)

CSound::CSound(void)
{

FSOUND_SetBufferSize(100);
if(!FSOUND_Init(44100, 32, FSOUND_INIT_ENABLEOUTPUTFX))
MessageBox(NULL, “Error initializing FMOD”, “Error”, MB_ICONEXCLAMATION);

FSOUND_SetOutput(FSOUND_OUTPUT_DSOUND);

}

CSound::~CSound(void)
{
SLIT slit = sounds.begin();

while (slit != sounds.end())
{
delete *slit;
}

FSOUND_Close();
}

BOOL CSound::LoadSound(char *filename, int id, BOOL loop)
{
SoundSample *samp = new SoundSample();

samp->sample = FSOUND_Sample_Load(FSOUND_FREE, filename, FSOUND_NORMAL, 0);
samp->id = id;

if(!samp->sample)
return FALSE;

if(!loop)
FSOUND_Sample_SetMode(samp->sample, FSOUND_LOOP_OFF);

sounds.push_back(samp);

return TRUE;

}

BOOL CSound::PlaySound(int id)
{
SLIT slit = sounds.begin();

while (slit != sounds.end())
{
if((*slit)->id == id)
{
int result = 0;

     result = FSOUND_PlaySound(FSOUND_FREE, (*slit)-&gt;sample);

     if(result == -1)
        return FALSE;

     return TRUE;

  }

  slit++;

}
return FALSE;
}

void CSound::DeleteSounds()
{
SLIT slit = sounds.begin();

while (slit != sounds.end())
{
delete *slit;
slit++;
}
}

void CSound::DeleteSound(int id)
{
SLIT slit = sounds.begin();

while(slit != sounds.end())
{
if((*slit)->id == id)
{
sounds.erase(slit);
}
slit++;
}
MessageBox(NULL, “Delete ok”, “Ok”, MB_OK);
}

SoundSample::SoundSample()
{

}

SoundSample::~SoundSample()
{
FSOUND_Sample_Free(sample);
}
[/code:1dde6k4q]

main.cpp:

[code:1dde6k4q]

include “sound.h”

CSound *sound = NULL;

int main()
{
sound = new CSound();

if((sound->LoadSound(“./blah.mp3”, 1, FALSE)) == NULL)
MessageBox(NULL, “Uh oh”, “Error”, MB_OK);

if(!(sound->PlaySound(1)))
MessageBox(NULL, “Uh oh”, “Error”, MB_OK);

}
[/code:1dde6k4q]

All it does is pause and then close. I get no sound, and all my functions return correctly.

  • You must to post comments
0
0

The problem is that your application closes before fmod can even play the first chunk of the sound. FSOUND_PlaySound is non-blocking, meaning it returns immediately, as opposed to returning when the sound has finished playing.
try adding some Sleep() to hear the sound 😉

Some things i noted:
you’re calling FSOUND_SetOutput after FSOUND_Init. This has no effect, you have to call it before the fmod initialization.
Your CSound::~CSound dtor won’t work, it misses a slit++.
you should delete your CSound instance at the end, otherwise your dtor wont even get called.

  • You must to post comments
0
0

I made the changes and it works now, but there is a pause before the sound plays (and it’s not from a sleep)

Is it because of my code, or is it normal behavior of FMOD?

It’s about a 5 second pause

  • You must to post comments
0
0

You’re loading an mp3 as a sample in your program. That means it is decoded entirely into memory on load; could take some seconds on a 1-2min mp3.

If you don’t want this, stream the mp3 using FSOUND_Stream_*

  • You must to post comments
0
0

no problem =)

in a way, you are working for us [free-software-programmers], so I think it’s appropiate if we work a little bit for you and save you some time in the forums :)

  • You must to post comments
Showing 4 results
Your Answer

Please first to submit.