gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:55:51 26 4
gpt4 key购买 nike

我在播放样本 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com