2016-03-03 133 views
0

我在播放樣本Allegro 5時遇到了問題。當我播放樣本時,我無法再播放該樣本,直到它完成播放。如果另一個不同的樣本正在播放,有時它也不會播放樣本。Allegro 5一次播放多個樣本

無論如何解決這個問題?

我用一個只有播放功能的「聲音」類播放音頻。其餘的是構造函數和成員變量,所有這些都用於播放函數中。

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 
} 

全班:

Sound.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(); 

}; 

Sound.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); 
} 

我所說的播放功能,通過我的系統的休息,這看起來像這樣:

// Game->ContentManager->Sound->play() 
Game::instance()->content()->getSound("somesound.wav")->play(); 

內容管理器包含我的資產的地圖。

這是我正在爲一個班級工作的較大項目的一部分,但不是,這部分不是作業。我的教授不允許我們擁有任何公共/頂級AL代碼(例如,沒有公開AL返回等)。

讓我知道我是否需要澄清任何事情。任何幫助總是感激。

回答

0

我可能是錯的,但它聽起來像是你有使用al_reserve_samples(number_of_samples);

+0

我會嘗試,當我得到一個機會。謝謝。 – Gurman8r

0

基於關閉ppsz的答案,以預留更多的樣本,我做了一些挖掘和下面根據我發現了什麼一樣。

int numSamples = /*Some int*/ 
int reservedSamples = 0; 
int i = (numSamples >= 1 ? numSamples : 1); 
bool success = false; 

do 
{ 
    success = al_reserve_samples(i); 

    i -= 1; 
} 
while (success == false || i > 0); 

Source