D:/Programmation/Cpp/SFML/src/SFML/Audio/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 <fstream>
00031 #include <iostream>
00032 
00033 
00037 sfMusic::sfMusic(std::size_t BufferSize) :
00038 myLoop    (false),
00039 myDuration(0.f),
00040 mySamples (BufferSize)
00041 {
00042 
00043 }
00044 
00045 
00049 bool sfMusic::Open(const std::string& Filename)
00050 {
00051     // Store the filename for later use
00052     myFilename = Filename;
00053 
00054     // Create the sound file implementation
00055     myFile = sf_private::sfSoundFile::Create(myFilename);
00056 
00057     // Open the music file in read mode
00058     std::size_t  NbSamples;
00059     unsigned int ChannelsCount;
00060     unsigned int Frequency;
00061     if (!myFile->OpenRead(myFilename, NbSamples, ChannelsCount, Frequency))
00062     {
00063         std::cerr << "Failed to open \"" << myFilename << "\" for reading" << std::endl;
00064         return false;
00065     }
00066 
00067     // Compute the duration
00068     myDuration = static_cast<float>(NbSamples) / Frequency;
00069 
00070     // Initialize the stream
00071     Initialize(ChannelsCount, Frequency);
00072 
00073     return true;
00074 }
00075 
00076 
00080 void sfMusic::SetLoop(bool Loop)
00081 {
00082     myLoop = Loop;
00083 }
00084 
00085 
00089 bool sfMusic::GetLoop() const
00090 {
00091     return myLoop;
00092 }
00093 
00094 
00098 bool sfMusic::OnStart()
00099 {
00100     // Re-open the music file to restart at the begining
00101 
00102     std::size_t  NbSamples;
00103     unsigned int ChannelsCount;
00104     unsigned int Frequency;
00105 
00106     return myFile->OpenRead(myFilename, NbSamples, ChannelsCount, Frequency);
00107 }
00108 
00109 
00113 bool sfMusic::OnGetData(sfSoundStream::Chunk& Data)
00114 {
00115     // Fill the chunk parameters
00116     Data.Samples   = &mySamples[0];
00117     Data.NbSamples = myFile->Read(&mySamples[0], mySamples.size());
00118 
00119     // Check if we have reached the end of the audio file
00120     if (Data.NbSamples < mySamples.size())
00121     {
00122         // Check if we must loop
00123         if (myLoop)
00124         {
00125             if (OnStart())
00126             {
00127                 // We succeeded to restart the audio playback
00128                 Data.NbSamples += myFile->Read(&mySamples[Data.NbSamples], mySamples.size() - Data.NbSamples);
00129                 return true;
00130             }
00131         }
00132 
00133         // No more audio samples to read : we stop the playback
00134         return false;
00135     }
00136 
00137     // End of audio file has not been reached, continue playback
00138     return true;
00139 }
00140 
00141 
00145 float sfMusic::GetDuration() const
00146 {
00147     return myDuration;
00148 }

Generated for SFML by  doxygen 1.5.2