c++ - Allegro 5 一次播放多个样本

标签 c++ audio allegro5

我在播放样本 Allegro 5 时遇到了问题。当我播放样本时,我无法再次播放该样本,直到它完成播放。有时,如果另一个不同的样本正在播放,它也不会播放样本。

无论如何要解决这个问题?

我用“声音”类播放音频,它只有一个播放功能。剩下的就是构造函数和成员变量,都是在play函数中使用的。

void Sound::play()
{
    al_play_sample(
        pSample,    // ALLEGRO_SAMPLE
        mGain,      // float
        mPan,       // float
        mSpeed,     // float
        getPlaymode(mPlaymode), // I use my own non-AL playmode enums. This is a private function that returns the AL version.
        NULL);      // ALLEGRO_SAMPLE_ID
}

全类:

声音.h
class ContentManager;

enum Playmode
{
    BiDir,
    Loop,
    Once,
    StreamOnce,
    StreamOneDir
};

class Sound : public Trackable
{
private:
    /*  Variables
    * * * * * * * * * * * * */
    ALLEGRO_SAMPLE* pSample;

    float
        mGain,
        mPan,
        mSpeed;

    Playmode mPlaymode;

    std::string
        mAssetPath,
        mAssetName;

    /*  Private Functions
    * * * * * * * * * * * * */
    ALLEGRO_PLAYMODE getPlaymode(Playmode playmode)
    {
        switch (playmode)
        {
            case BiDir:
                return ALLEGRO_PLAYMODE::ALLEGRO_PLAYMODE_BIDIR;

            case Loop:
                return ALLEGRO_PLAYMODE::ALLEGRO_PLAYMODE_LOOP;

            case Once:
                return ALLEGRO_PLAYMODE::ALLEGRO_PLAYMODE_ONCE;

            case StreamOnce:
                return ALLEGRO_PLAYMODE::_ALLEGRO_PLAYMODE_STREAM_ONCE;

            case StreamOneDir:
                return ALLEGRO_PLAYMODE::_ALLEGRO_PLAYMODE_STREAM_ONEDIR;

            // Default to once
            default:
                return ALLEGRO_PLAYMODE::ALLEGRO_PLAYMODE_ONCE;
        }
    }

public:
    /*  Constructors/Destructor
    * * * * * * * * * * * * */
    Sound();

    Sound(
        // assetPath, assetName, gain, pan, speed, playmode
        std::string assetPath, 
        std::string assetName, 
        float gain = 1.0f, 
        float pan = 0.0f, 
        float speed = 1.0f,
        Playmode playmode = Once);

    Sound(const Sound& other);

    ~Sound();

    friend class ContentManager; // My content system.

    void play();

};

声音.cpp
#include "Sound.h"

/*  Constructors/Destructor
* * * * * * * * * * * * */
Sound::Sound()
{
    this->mAssetPath = "";
    this->mAssetName = "";
    this->mGain = 1.0f;
    this->mPan = 0.0f;
    this->mSpeed = 1.0f;
    this->mPlaymode = Once;

    this->pSample = NULL;
}

Sound::Sound(std::string assetPath, std::string assetName, float gain, float pan, float speed, Playmode playmode)
{
    this->mAssetPath = assetPath;
    this->mAssetName = assetName;
    this->mGain = gain;
    this->mPan = pan;
    this->mSpeed = speed;
    this->mPlaymode = playmode;

    this->pSample = al_load_sample((assetPath + assetName).c_str());
}

Sound::Sound(const Sound& other)
{
    this->mAssetPath = other.mAssetPath;
    this->mAssetName = other.mAssetName;    
    this->mGain = other.mGain;
    this->mPan = other.mPan;
    this->mSpeed = other.mSpeed;
    this->mPlaymode = other.mPlaymode;

    this->pSample = al_load_sample((mAssetPath + mAssetName).c_str());
}

Sound::~Sound()
{
    al_destroy_sample(pSample);
}


void Sound::play()
{
    al_play_sample(
        pSample,
        mGain,
        mPan,
        mSpeed,
        getPlaymode(mPlaymode),
        NULL);
}

我通过系统的其余部分调用 play 函数,它看起来像这样:
// Game->ContentManager->Sound->play()
Game::instance()->content()->getSound("somesound.wav")->play();

内容管理器包含我的 Assets map 。

这是我正在为一个类(class)做的一个更大的项目的一部分,但是不,这部分不是家庭作业。我的教授不允许我们拥有任何公共(public)/顶级 AL 代码(例如,没有公共(public) AL 返回等)。

如果我需要澄清任何事情,请告诉我。任何帮助总是受到赞赏。

最佳答案

我可能错了,但听起来您必须使用 al_reserve_samples(number_of_samples); 保留更多样本

关于c++ - Allegro 5 一次播放多个样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35763079/

相关文章:

c++ - glfwGetGLVersion() 在 OS X Lion 上返回 2.1

iphone - iPhone AVAudioPlayer循环卡死了吗?

c++ - 如何将像素转换为索引

c - 将键盘事件保存到带有图形窗口的数组中

c++ - `const T` 和 `T` 在采用嵌套类型时没有区别吗?

c++ - 具有自动说明符和static_cast的基于范围的for循环

android - 音频在Samsung Galaxy设备上突然停止工作(Ionic Cordova Apps)

c - 当 Allegro 中一个位图跟随另一个位图时闪烁

c++ - 打开文件并使用 fseek 获取文件大小 - C++

c# - 在Windows Phone 7上,如何在音频效果中使用压缩音频?