Added SetPlayingOffset to sf::SoundStream and sf::Music

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1076 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
laurentgom 2009-04-10 22:50:08 +00:00
parent 364b0ae9b1
commit 22c225c64f
17 changed files with 202 additions and 166 deletions

View file

@ -28,6 +28,7 @@
#include <SFML/Audio/Music.hpp>
#include <SFML/Audio/OpenAL.hpp>
#include <SFML/Audio/SoundFile.hpp>
#include <SFML/System/Lock.hpp>
#include <fstream>
#include <iostream>
@ -109,11 +110,11 @@ bool Music::OpenFromMemory(const char* Data, std::size_t SizeInBytes)
////////////////////////////////////////////////////////////
/// /see SoundStream::OnStart
/// Get the sound duration
////////////////////////////////////////////////////////////
bool Music::OnStart()
float Music::GetDuration() const
{
return myFile && myFile->Restart();
return myDuration;
}
@ -122,28 +123,25 @@ bool Music::OnStart()
////////////////////////////////////////////////////////////
bool Music::OnGetData(SoundStream::Chunk& Data)
{
if (myFile)
{
// Fill the chunk parameters
Data.Samples = &mySamples[0];
Data.NbSamples = myFile->Read(&mySamples[0], mySamples.size());
sf::Lock Lock(myMutex);
// Check if we have reached the end of the audio file
return Data.NbSamples == mySamples.size();
}
else
{
return false;
}
// Fill the chunk parameters
Data.Samples = &mySamples[0];
Data.NbSamples = myFile->Read(&mySamples[0], mySamples.size());
// Check if we have reached the end of the audio file
return Data.NbSamples == mySamples.size();
}
////////////////////////////////////////////////////////////
/// Get the sound duration
/// /see SoundStream::OnSeek
////////////////////////////////////////////////////////////
float Music::GetDuration() const
void Music::OnSeek(float TimeOffset)
{
return myDuration;
sf::Lock Lock(myMutex);
myFile->Seek(TimeOffset);
}
} // namespace sf

View file

@ -84,30 +84,6 @@ unsigned int SoundFile::GetSampleRate() const
}
////////////////////////////////////////////////////////////
/// Restart the sound from the beginning
////////////////////////////////////////////////////////////
bool SoundFile::Restart()
{
if (myData)
{
// Reopen from memory
return OpenRead(myData, mySize);
}
else if (myFilename != "")
{
// Reopen from file
return OpenRead(myFilename);
}
else
{
// Trying to reopen a file opened in write mode... error
std::cerr << "Warning : trying to restart a sound opened in write mode, which is not allowed" << std::endl;
return false;
}
}
////////////////////////////////////////////////////////////
/// Open the sound file for reading
////////////////////////////////////////////////////////////
@ -130,9 +106,6 @@ bool SoundFile::OpenRead(const std::string& Filename)
myChannelsCount = FileInfos.channels;
mySampleRate = FileInfos.samplerate;
myNbSamples = static_cast<std::size_t>(FileInfos.frames) * myChannelsCount;
myFilename = Filename;
myData = NULL;
mySize = 0;
return true;
}
@ -173,9 +146,6 @@ bool SoundFile::OpenRead(const char* Data, std::size_t SizeInBytes)
myChannelsCount = FileInfos.channels;
mySampleRate = FileInfos.samplerate;
myNbSamples = static_cast<std::size_t>(FileInfos.frames) * myChannelsCount;
myFilename = "";
myData = Data;
mySize = SizeInBytes;
return true;
}
@ -217,9 +187,6 @@ bool SoundFile::OpenWrite(const std::string& Filename, unsigned int ChannelsCoun
myChannelsCount = ChannelsCount;
mySampleRate = SampleRate;
myNbSamples = 0;
myFilename = "";
myData = NULL;
mySize = 0;
return true;
}
@ -257,6 +224,19 @@ void SoundFile::Write(const Int16* Data, std::size_t NbSamples)
}
////////////////////////////////////////////////////////////
/// Move the current reading position in the file
////////////////////////////////////////////////////////////
void SoundFile::Seek(float TimeOffset)
{
if (myFile)
{
sf_count_t FrameOffset = static_cast<sf_count_t>(TimeOffset * mySampleRate);
sf_seek(myFile, FrameOffset, SEEK_SET);
}
}
////////////////////////////////////////////////////////////
/// Get the internal format of an audio file according to
/// its filename extension

View file

@ -81,14 +81,6 @@ public :
////////////////////////////////////////////////////////////
unsigned int GetSampleRate() const;
////////////////////////////////////////////////////////////
/// Restart the sound from the beginning
///
/// \return True if restart was successful
///
////////////////////////////////////////////////////////////
bool Restart();
////////////////////////////////////////////////////////////
/// Open the sound file for reading
///
@ -142,6 +134,14 @@ public :
////////////////////////////////////////////////////////////
void Write(const Int16* Data, std::size_t NbSamples);
////////////////////////////////////////////////////////////
/// Move the current reading position in the file
///
/// \param TimeOffset : New position, expressed in seconds
///
////////////////////////////////////////////////////////////
void Seek(float TimeOffset);
private :
////////////////////////////////////////////////////////////
@ -183,9 +183,6 @@ private :
std::size_t myNbSamples; ///< Total number of samples in the file
unsigned int myChannelsCount; ///< Number of channels used by the sound
unsigned int mySampleRate; ///< Number of samples per second
std::string myFilename; ///< Path of the file (valid if loaded from a file)
const char* myData; ///< Pointer to the file in memory (valid if loaded from memory)
std::size_t mySize; ///< Size of the file in memory (valid if loaded from memory)
};
} // namespace priv

View file

@ -98,14 +98,13 @@ void SoundStream::Play()
return;
}
// Notify the derived class
if (OnStart())
{
// Start updating the stream in a separate thread to avoid blocking the application
mySamplesProcessed = 0;
myIsStreaming = true;
Launch();
}
// Move to the beginning
OnSeek(0);
// Start updating the stream in a separate thread to avoid blocking the application
mySamplesProcessed = 0;
myIsStreaming = true;
Launch();
}
@ -153,6 +152,24 @@ Sound::Status SoundStream::GetStatus() const
}
////////////////////////////////////////////////////////////
/// Set the current playing position of the stream
////////////////////////////////////////////////////////////
void SoundStream::SetPlayingOffset(float TimeOffset)
{
// Stop the stream
Stop();
// Let the derived class update the current position
OnSeek(TimeOffset);
// Restart streaming
mySamplesProcessed = static_cast<unsigned int>(TimeOffset * mySampleRate * myChannelsCount);
myIsStreaming = true;
Launch();
}
////////////////////////////////////////////////////////////
/// Get the current playing position of the stream
///
@ -245,15 +262,16 @@ void SoundStream::Run()
if (FillAndPushBuffer(Buffer))
{
// User requested to stop: check if we must loop or really stop
if (myLoop && OnStart())
if (myLoop)
{
// Looping: mark the current buffer as the last one
// Looping: restart and mark the current buffer as the last one
// (to know when to reset the sample count)
OnSeek(0);
EndBuffer = Buffer;
}
else
{
// Not looping or restart failed: request stop
// Not looping: request stop
RequestStop = true;
}
}
@ -337,15 +355,4 @@ void SoundStream::ClearQueue()
ALCheck(alSourceUnqueueBuffers(Sound::mySource, 1, &Buffer));
}
////////////////////////////////////////////////////////////
/// Called when the sound restarts
////////////////////////////////////////////////////////////
bool SoundStream::OnStart()
{
// Does nothing by default
return true;
}
} // namespace sf