Music.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/Audio/Music.hpp>
00029 #include <SFML/Audio/OpenAL.hpp>
00030 #include <SFML/Audio/SoundFile.hpp>
00031 #include <fstream>
00032 #include <iostream>
00033 
00034 
00035 namespace sf
00036 {
00040 Music::Music(std::size_t BufferSize) :
00041 myFile    (NULL),
00042 myLoop    (false),
00043 myDuration(0.f),
00044 mySamples (BufferSize)
00045 {
00046 
00047 }
00048 
00049 
00053 Music::~Music()
00054 {
00055     delete myFile;
00056 }
00057 
00058 
00062 bool Music::Open(const std::string& Filename)
00063 {
00064     // Store the filename for later use
00065     myFilename = Filename;
00066 
00067     // Create the sound file implementation
00068     delete myFile;
00069     myFile = priv::SoundFile::Create(myFilename);
00070 
00071     // Open the music file in read mode
00072     std::size_t  NbSamples;
00073     unsigned int ChannelsCount;
00074     unsigned int Frequency;
00075     if (!myFile->OpenRead(myFilename, NbSamples, ChannelsCount, Frequency))
00076     {
00077         std::cerr << "Failed to open \"" << myFilename << "\" for reading" << std::endl;
00078         return false;
00079     }
00080 
00081     // Compute the duration
00082     myDuration = static_cast<float>(NbSamples) / Frequency;
00083 
00084     // Initialize the stream
00085     Initialize(ChannelsCount, Frequency);
00086 
00087     return true;
00088 }
00089 
00090 
00094 void Music::SetLoop(bool Loop)
00095 {
00096     myLoop = Loop;
00097 }
00098 
00099 
00103 bool Music::GetLoop() const
00104 {
00105     return myLoop;
00106 }
00107 
00108 
00112 bool Music::OnStart()
00113 {
00114     // Re-open the music file to restart at the begining
00115 
00116     std::size_t  NbSamples;
00117     unsigned int ChannelsCount;
00118     unsigned int Frequency;
00119 
00120     return myFile->OpenRead(myFilename, NbSamples, ChannelsCount, Frequency);
00121 }
00122 
00123 
00127 bool Music::OnGetData(SoundStream::Chunk& Data)
00128 {
00129     // Fill the chunk parameters
00130     Data.Samples   = &mySamples[0];
00131     Data.NbSamples = myFile->Read(&mySamples[0], mySamples.size());
00132 
00133     // Check if we have reached the end of the audio file
00134     if (Data.NbSamples < mySamples.size())
00135     {
00136         // Check if we must loop
00137         if (myLoop)
00138         {
00139             if (OnStart())
00140             {
00141                 // We succeeded to restart the audio playback
00142                 Data.NbSamples += myFile->Read(&mySamples[Data.NbSamples], mySamples.size() - Data.NbSamples);
00143                 return true;
00144             }
00145         }
00146 
00147         // No more audio samples to read : we stop the playback
00148         return false;
00149     }
00150 
00151     // End of audio file has not been reached, continue playback
00152     return true;
00153 }
00154 
00155 
00159 float Music::GetDuration() const
00160 {
00161     return myDuration;
00162 }
00163 
00164 } // namespace sf