Hi!
Sorry if my question is stupid and if I've missed something in the doc but :
I've created a custom audio stream like explained in the tutorial :
void Stream::load(const sf::SoundBuffer& buffer)
{
m_file = nullptr;
// extract the audio samples from the sound buffer to our own container
m_samples.assign(buffer.getSamples(), buffer.getSamples() + buffer.getSampleCount());
// reset the current playing position
m_currentSample = 0;
// initialize the base class
SoundStream::initialize(buffer.getChannelCount(), buffer.getSampleRate());
}
////////////////////////////////////////////////////////////
bool Stream::openFromFile(const std::string& filename)
{
// First stop the music if it was already running
m_file = new priv::SoundFile();
stop();
// Open the underlying sound file
if (!m_file->openRead(filename))
return false;
// Perform common initializations
initialize();
return true;
}
////////////////////////////////////////////////////////////
bool Stream::openFromMemory(const void* data, std::size_t sizeInBytes)
{
m_file = new priv::SoundFile();
// First stop the music if it was already running
stop();
// Open the underlying sound file
if (!m_file->openRead(data, sizeInBytes))
return false;
// Perform common initializations
initialize();
return true;
}
////////////////////////////////////////////////////////////
bool Stream::openFromStream(InputStream& stream)
{
m_file = new priv::SoundFile();
// First stop the music if it was already running
stop();
// Open the underlying sound file
if (!m_file->openRead(stream))
return false;
// Perform common initializations
initialize();
return true;
}
bool Stream::onGetData(Chunk& data)
{
Lock lock(m_mutex);
if (m_file == nullptr) {
// number of samples to stream every time the function is called;
// in a more robust implementation, it would rather be a fixed
// amount of time rather than an arbitrary number of samples
const int samplesToStream = 50000;
// set the pointer to the next audio samples to be played
data.samples = &m_samples[m_currentSample];
// have we reached the end of the sound?
if (m_currentSample + samplesToStream <= m_samples.size())
{
// end not reached: stream the samples and continue
data.sampleCount = samplesToStream;
m_currentSample += samplesToStream;
return true;
}
else
{
// end of stream reached: stream the remaining samples and stop playback
data.sampleCount = m_samples.size() - m_currentSample;
m_currentSample = m_samples.size();
return false;
}
} else {
// Fill the chunk parameters
data.samples = &m_samples[0];
data.sampleCount = m_file->read(&m_samples[0], m_samples.size());
// Check if we have reached the end of the audio file
return data.sampleCount == m_samples.size();
}
}
void Stream::onSeek(sf::Time timeOffset)
{
// compute the corresponding sample index according to the sample rate and channel count
m_currentSample = static_cast<std::size_t>(timeOffset.asSeconds() * getSampleRate() * getChannelCount());
}
void Stream::initialize()
{
// Compute the music duration
m_duration = seconds(static_cast<float>(m_file->getSampleCount()) / m_file->getSampleRate() / m_file->getChannelCount());
// Resize the internal buffer so that it can contain 1 second of audio samples
m_samples.resize(m_file->getSampleRate() * m_file->getChannelCount());
// Initialize the stream
SoundStream::initialize(m_file->getChannelCount(), m_file->getSampleRate());
}
I've also created a class to play the song but I want to use only one class for playing sounds and musics :
Player::Player(SoundBuffer& buffer) {
Stream* stream = new Stream();
stream->load(buffer);
this->stream = stream;
}
void Player::setAudioStream(SoundStream* stream) {
this->stream = stream;
}
sf::SoundStream* Player::getAudioStream() {
return stream;
}
void Player::play(bool loop) {
stream->setLoop(loop);
stream->play();
}
void Player::stop() {
stream->stop();
}
void Player::pause() {
stream->pause();
}
Player::~Player() {
delete stream;
}
The problem is that the sound is playing repeatively :
int main (int argv,char* argc[]) {
sf::SoundBuffer buffer;
buffer.loadFromFile("sounds/ballhurtbar.wav");
odfaeg::audio::Stream stream;
stream.load(buffer);
odfaeg::audio::Player player;
player.setAudioStream(&stream);
player.play(false);
const auto delay = sf::milliseconds(1);
do {
sf::sleep(delay);
} while(stream.getStatus() == odfaeg::audio::Stream::Playing);
return 0;
Isn't the sound supposed to be stopped when the onGetData method return false ?
Or I've I missunderstand something ?