Advertisement
TexKiller

Playlist.cpp

Feb 22nd, 2012
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.98 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////
  2. //
  3. // SFML - Simple and Fast Multimedia Library
  4. // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
  5. //
  6. // This software is provided 'as-is', without any express or implied warranty.
  7. // In no event will the authors be held liable for any damages arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it freely,
  11. // subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented;
  14. //    you must not claim that you wrote the original software.
  15. //    If you use this software in a product, an acknowledgment
  16. //    in the product documentation would be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such,
  19. //    and must not be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source distribution.
  22. //
  23. ////////////////////////////////////////////////////////////
  24.  
  25. ////////////////////////////////////////////////////////////
  26. // Headers
  27. ////////////////////////////////////////////////////////////
  28. #include <SFML/Audio/Playlist.hpp>
  29. #include <SFML/Audio/ALCheck.hpp>
  30. #include <SFML/Audio/SoundFile.hpp>
  31. #include <SFML/System/Lock.hpp>
  32. #include <SFML/System/Err.hpp>
  33. #include <fstream>
  34.  
  35.  
  36. namespace sf {
  37.  
  38. const int Playlist::ALWAYS_RETURN = -1;
  39.  
  40. Playlist::Playlist(std::size_t BufferSize) : myDuration(Time::Zero), mySamples (BufferSize) {
  41.     currentItem = 0;
  42.     offset = Time::Zero;
  43.     reset = true;
  44.     repeat = false;
  45.     SetLoop(false);
  46. }
  47.  
  48. Playlist::~Playlist() {
  49.     unsigned int k;
  50.  
  51.     Stop();
  52.     for (k = 0; k < items.size(); k++) {
  53.         delete items[k]->myFile;
  54.         delete items[k];
  55.     }
  56. }
  57.  
  58. unsigned int Playlist::AddItem(std::string file, Time start, Time end, unsigned int returnTo, int returnCount) {
  59.     priv::SoundFile* myFile = new priv::SoundFile();
  60.     if (!myFile->OpenRead(file)) {
  61.         delete myFile;
  62.         return false;
  63.     }
  64.     priv::Item *aux = new priv::Item;
  65.     aux->myFile = myFile;
  66.     aux->start = start;
  67.     aux->duration = ((end > Time::Zero) ? end - start : Time::Zero);
  68.     aux->returnTo = returnTo;
  69.     aux->returnCount = returnCount;
  70.     aux->timesReturned = 0;
  71.     items.push_back(aux);
  72.     myDuration += Seconds(static_cast<float>(aux->myFile->GetSampleCount()) / aux->myFile->GetSampleRate() / aux->myFile->GetChannelCount());
  73.     Initialize(aux->myFile->GetChannelCount(), aux->myFile->GetSampleRate());
  74.     return items.size() - 1;
  75. }
  76.  
  77. void Playlist::SetRepeat(bool repeat) {
  78.  
  79.     this->repeat = repeat;
  80. }
  81.  
  82. void Playlist::Stop() {
  83.     sf::SoundStream::Stop();
  84.     for (int i = 0; i < items.size(); i++) {
  85.         items[i]->timesReturned = 0;
  86.     }
  87.     currentItem = 0;
  88.     offset = Time::Zero;
  89.     reset = true;
  90. }
  91.  
  92. void Playlist::OnSeek(Time time) {
  93.     if (currentItem < items.size()) {
  94.         offset = items[currentItem]->start + time;
  95.         Lock lock(myMutex);
  96.         items[currentItem]->myFile->Seek(offset);
  97.     }
  98. }
  99.  
  100. bool Playlist::OnStart() {
  101.  
  102.     reset = true;
  103.     return true;
  104. }
  105.  
  106. bool Playlist::OnGetData(sf::SoundStream::Chunk& data) {
  107.     if (currentItem < items.size()) {
  108.         priv::Item* item = items[currentItem];
  109.         if (reset) {
  110.             OnSeek(Seconds(0.f));
  111.             reset = false;
  112.         }
  113.         if (items[currentItem]->duration == Time::Zero) {
  114.             Lock lock(myMutex);
  115.             data.Samples = &mySamples[0];
  116.             data.SampleCount = item->myFile->Read(&mySamples[0], mySamples.size());
  117.             if (!(data.SampleCount == mySamples.size())) {
  118.                 reset = true;
  119.                 if (item->returnCount >= 0) {
  120.                     if (item->timesReturned == item->returnCount) {
  121.                         item->timesReturned = 0;
  122.                         currentItem++;
  123.                     } else {
  124.                         item->timesReturned++;
  125.                         currentItem = item->returnTo;
  126.                     }
  127.                 } else {
  128.                     currentItem = item->returnTo;
  129.                 }
  130.             }
  131.             offset += Seconds((data.SampleCount * 1000.f) / (GetSampleRate() * GetChannelCount()));
  132.         } else {
  133.             Time test = item->start + item->duration - offset;
  134.             if (test <= Time::Zero) {
  135.                 data.SampleCount = 0;
  136.                 if (item->returnCount >= 0) {
  137.                     if (item->timesReturned == item->returnCount) {
  138.                         item->timesReturned = 0;
  139.                         currentItem++;
  140.                     } else {
  141.                         item->timesReturned++;
  142.                         currentItem = item->returnTo;
  143.                     }
  144.                 } else {
  145.                     currentItem = item->returnTo;
  146.                 }
  147.             } else {
  148.                 Lock lock(myMutex);
  149.                 data.Samples = &mySamples[0];
  150.                 data.SampleCount = item->myFile->Read(&mySamples[0], mySamples.size());
  151.                 Time num = Seconds(static_cast<float>(data.SampleCount) / (GetSampleRate() * GetChannelCount()));
  152.                 if ((test <= Seconds(1)) && (num >= test)) {
  153.                     data.SampleCount = static_cast<std::size_t>(test.AsSeconds() * GetSampleRate() * GetChannelCount());
  154.                     num = test;
  155.                     reset = true;
  156.                     if (item->returnCount >= 0) {
  157.                         if (item->timesReturned == item->returnCount) {
  158.                             item->timesReturned = 0;
  159.                             currentItem++;
  160.                         } else {
  161.                             item->timesReturned++;
  162.                             currentItem = item->returnTo;
  163.                         }
  164.                     } else {
  165.                         currentItem = item->returnTo;
  166.                     }
  167.                 } else if (data.SampleCount != mySamples.size()) {
  168.                     reset = true;
  169.                     if (item->returnCount >= 0) {
  170.                         if (item->timesReturned == item->returnCount) {
  171.                             item->timesReturned = 0;
  172.                             currentItem++;
  173.                         } else {
  174.                             item->timesReturned++;
  175.                             currentItem = item->returnTo;
  176.                         }
  177.                     } else {
  178.                         currentItem = item->returnTo;
  179.                     }
  180.                 }
  181.                 offset += num;
  182.             }
  183.         }
  184.     }
  185.     if (currentItem == items.size()) {
  186.         reset = true;
  187.         currentItem = 0;
  188.     }
  189.     return true;
  190. }
  191.  
  192. ////////////////////////////////////////////////////////////
  193. /// Get the sound duration
  194. ////////////////////////////////////////////////////////////
  195. Time Playlist::GetDuration() const
  196. {
  197.     return myDuration;
  198. }
  199.  
  200. } // namespace sf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement