Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////
- //
- // SFML - Simple and Fast Multimedia Library
- // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
- //
- // This software is provided 'as-is', without any express or implied warranty.
- // In no event will the authors be held liable for any damages arising from the use of this software.
- //
- // Permission is granted to anyone to use this software for any purpose,
- // including commercial applications, and to alter it and redistribute it freely,
- // subject to the following restrictions:
- //
- // 1. The origin of this software must not be misrepresented;
- // you must not claim that you wrote the original software.
- // If you use this software in a product, an acknowledgment
- // in the product documentation would be appreciated but is not required.
- //
- // 2. Altered source versions must be plainly marked as such,
- // and must not be misrepresented as being the original software.
- //
- // 3. This notice may not be removed or altered from any source distribution.
- //
- ////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////
- // Headers
- ////////////////////////////////////////////////////////////
- #include <SFML/Audio/Playlist.hpp>
- #include <SFML/Audio/ALCheck.hpp>
- #include <SFML/Audio/SoundFile.hpp>
- #include <SFML/System/Lock.hpp>
- #include <SFML/System/Err.hpp>
- #include <fstream>
- namespace sf {
- const int Playlist::ALWAYS_RETURN = -1;
- Playlist::Playlist(std::size_t BufferSize) : myDuration(Time::Zero), mySamples (BufferSize) {
- currentItem = 0;
- offset = Time::Zero;
- reset = true;
- repeat = false;
- SetLoop(false);
- }
- Playlist::~Playlist() {
- unsigned int k;
- Stop();
- for (k = 0; k < items.size(); k++) {
- delete items[k]->myFile;
- delete items[k];
- }
- }
- unsigned int Playlist::AddItem(std::string file, Time start, Time end, unsigned int returnTo, int returnCount) {
- priv::SoundFile* myFile = new priv::SoundFile();
- if (!myFile->OpenRead(file)) {
- delete myFile;
- return false;
- }
- priv::Item *aux = new priv::Item;
- aux->myFile = myFile;
- aux->start = start;
- aux->duration = ((end > Time::Zero) ? end - start : Time::Zero);
- aux->returnTo = returnTo;
- aux->returnCount = returnCount;
- aux->timesReturned = 0;
- items.push_back(aux);
- myDuration += Seconds(static_cast<float>(aux->myFile->GetSampleCount()) / aux->myFile->GetSampleRate() / aux->myFile->GetChannelCount());
- Initialize(aux->myFile->GetChannelCount(), aux->myFile->GetSampleRate());
- return items.size() - 1;
- }
- void Playlist::SetRepeat(bool repeat) {
- this->repeat = repeat;
- }
- void Playlist::Stop() {
- sf::SoundStream::Stop();
- for (int i = 0; i < items.size(); i++) {
- items[i]->timesReturned = 0;
- }
- currentItem = 0;
- offset = Time::Zero;
- reset = true;
- }
- void Playlist::OnSeek(Time time) {
- if (currentItem < items.size()) {
- offset = items[currentItem]->start + time;
- Lock lock(myMutex);
- items[currentItem]->myFile->Seek(offset);
- }
- }
- bool Playlist::OnStart() {
- reset = true;
- return true;
- }
- bool Playlist::OnGetData(sf::SoundStream::Chunk& data) {
- if (currentItem < items.size()) {
- priv::Item* item = items[currentItem];
- if (reset) {
- OnSeek(Seconds(0.f));
- reset = false;
- }
- if (items[currentItem]->duration == Time::Zero) {
- Lock lock(myMutex);
- data.Samples = &mySamples[0];
- data.SampleCount = item->myFile->Read(&mySamples[0], mySamples.size());
- if (!(data.SampleCount == mySamples.size())) {
- reset = true;
- if (item->returnCount >= 0) {
- if (item->timesReturned == item->returnCount) {
- item->timesReturned = 0;
- currentItem++;
- } else {
- item->timesReturned++;
- currentItem = item->returnTo;
- }
- } else {
- currentItem = item->returnTo;
- }
- }
- offset += Seconds((data.SampleCount * 1000.f) / (GetSampleRate() * GetChannelCount()));
- } else {
- Time test = item->start + item->duration - offset;
- if (test <= Time::Zero) {
- data.SampleCount = 0;
- if (item->returnCount >= 0) {
- if (item->timesReturned == item->returnCount) {
- item->timesReturned = 0;
- currentItem++;
- } else {
- item->timesReturned++;
- currentItem = item->returnTo;
- }
- } else {
- currentItem = item->returnTo;
- }
- } else {
- Lock lock(myMutex);
- data.Samples = &mySamples[0];
- data.SampleCount = item->myFile->Read(&mySamples[0], mySamples.size());
- Time num = Seconds(static_cast<float>(data.SampleCount) / (GetSampleRate() * GetChannelCount()));
- if ((test <= Seconds(1)) && (num >= test)) {
- data.SampleCount = static_cast<std::size_t>(test.AsSeconds() * GetSampleRate() * GetChannelCount());
- num = test;
- reset = true;
- if (item->returnCount >= 0) {
- if (item->timesReturned == item->returnCount) {
- item->timesReturned = 0;
- currentItem++;
- } else {
- item->timesReturned++;
- currentItem = item->returnTo;
- }
- } else {
- currentItem = item->returnTo;
- }
- } else if (data.SampleCount != mySamples.size()) {
- reset = true;
- if (item->returnCount >= 0) {
- if (item->timesReturned == item->returnCount) {
- item->timesReturned = 0;
- currentItem++;
- } else {
- item->timesReturned++;
- currentItem = item->returnTo;
- }
- } else {
- currentItem = item->returnTo;
- }
- }
- offset += num;
- }
- }
- }
- if (currentItem == items.size()) {
- reset = true;
- currentItem = 0;
- }
- return true;
- }
- ////////////////////////////////////////////////////////////
- /// Get the sound duration
- ////////////////////////////////////////////////////////////
- Time Playlist::GetDuration() const
- {
- return myDuration;
- }
- } // namespace sf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement