2016-03-03 3 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

Based от ответа 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