0
0

Hey guys,

I am new to this forum so excuse any mistakes.

I have got a problem while running my game, I have created AUDIO class that plays whatever audio file I have loaded.
The problem is that when I play back the sound it comes out distorted with buzzing ‘effect’. When i play the same sound with Direct Sound it plays back correctly .
Here are my files regarding Audio class.

Audio.h file
[code:2q15dm54]

pragma once

include <string>

include <vector>

include "HAPI\fmod.hpp"

namespace p7t {

class Sample
{
private:
    std::string name;

public:
    FMOD_SOUND    *sample;
    FMOD_CHANNEL *channel;

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

class Audio
{
private:
    FMOD_SYSTEM *system;
    typedef std::vector&lt;Sample*&gt; Samples;
    typedef std::vector&lt;Sample*&gt;::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);

};

};
[/code:2q15dm54]

Audio.cpp
[code:2q15dm54]

include "Audio.h"

namespace p7t {

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

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

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(&amp;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();

    try {
        FMOD_RESULT res;
        res = FMOD_System_CreateSound(
            system,             //FMOD system
            filename.c_str(),     //filename
            FMOD_DEFAULT,         //default audio
            NULL,                 //n/a
            &amp;sample-&gt;sample);    //pointer to sample

        if (res != FMOD_OK) {
            sample = NULL;
        }
    } catch (...) {
        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-&gt;setName(name);

    FMOD_RESULT res;
    res = FMOD_System_CreateSound(
        system,             //FMOD system
        filename.c_str(),     //filename
        FMOD_DEFAULT,         //default audio
        NULL,                 //n/a
        &amp;sample-&gt;sample);    //pointer to 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)-&gt;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-&gt;channel, &amp;index);

    // FMOD returns 99 if sample is playing, 0 if not
    return (index &gt; 0);

}

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


bool Audio::Play(std::string name)
{
    FMOD_RESULT res;
    Sample *sample = FindSample(name);

    if (sample-&gt;sample != NULL) {
        try {
            //sample found, play it
            res = FMOD_System_PlaySound(
                system,
                FMOD_CHANNEL_FREE, 
                sample-&gt;sample, 
                true, 
                &amp;sample-&gt;channel);

            if (res!= FMOD_OK) return false;

            FMOD_Channel_SetLoopCount(sample-&gt;channel, -1);
            FMOD_Channel_SetPaused(sample-&gt;channel, false);

        } catch (...) {
            return false;
        }
    }

    return true;
}

bool Audio::Play(Sample *sample)
{
    FMOD_RESULT res;
    if (sample == NULL) return false;
    if (sample-&gt;sample == NULL) return false;

    res = FMOD_System_PlaySound(
        system,
        FMOD_CHANNEL_FREE,
        sample-&gt;sample,
        true,
        &amp;sample-&gt;channel);

    if (res!= FMOD_OK) return false;

    FMOD_Channel_SetLoopCount(sample-&gt;channel, -1);
    FMOD_Channel_SetPaused(sample-&gt;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-&gt;channel);
}

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

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

};

[/code:2q15dm54]

in my main file I create audio instance , initialize it, load sound, call update function from audio class every frame, and play sound on mouse click.

Thanks for any help
David

  • You must to post comments
0
0

Sounds like you might be playing the same sound multiple times. Can you chuck a breakpoint inside the call to playsound and see if it gets called more times than it should?

  • You must to post comments
0
0

Yes it is the case of playing the same sound multiple times in very short period of time.
Thank You very much for help !
David

  • You must to post comments
0
0

You’re welcome David, I’m glad you fixed your issue. :)

  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.