So I'm not 100% sure what is going wrong here, but the following code is not working as expected.
void Cycle(float Time, float Pitch)
{
//Reduce the timer
m_PlayTimer -= Time;
//If it's zero, play the sample
if (m_PlayTimer <= 0.0)
{
//Add the sound!
m_SoundList.push_back(sf::Sound());
unsigned short NewSound = m_SoundList.size() - 1;
m_SoundList[NewSound].setBuffer(*SoundManager::GetSoundBuffer(1));
m_SoundList[NewSound].setVolume(30.0);
m_SoundList[NewSound].setPitch(Pitch);
m_SoundList[NewSound].play();
//Restore a positive value to the timer
m_PlayTimer = 1.0;
}
//Cull finished sounds
for (unsigned int ii = 0; ii < m_SoundList.size(); ++ii)
{
//Test whether the sound is finished or not; if so, remove it
if (m_SoundList[ii].getStatus() == sf::Sound::Status::Stopped)
{
m_SoundList.erase(m_SoundList.begin() + ii);
std::cout << "Sound deleted!\n";
//If ii is the same as the size, there are no further sounds
if (ii == m_SoundList.size())
{
break;
}
//Otherwise, there are further sounds! Decrement ii so as not to skip any
else
{
--ii;
}
}
}
}
What happens is that sometimes, when one Sound finishes after another has started (I also modify the pitch of the sound, so the lengths of each sf::Sound are slightly different),
both sounds are deleted in the same call, which kills the newer sound before it has had a chance to play out. It's as though the algorithm thinks that both sf::Sound objects return Stopped upon calling getStatus().
Any idea what might be causing this, and how I can get things to work as expected?