00001 00002 // 00003 // SFML - Simple and Fast Multimedia Library 00004 // Copyright (C) 2007-2009 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/ALCheck.hpp> 00030 #include <SFML/Audio/SoundFile.hpp> 00031 #include <SFML/System/Lock.hpp> 00032 #include <SFML/System/Err.hpp> 00033 #include <fstream> 00034 00035 00036 namespace sf 00037 { 00039 Music::Music() : 00040 myFile (new priv::SoundFile), 00041 myDuration(0.f) 00042 { 00043 00044 } 00045 00046 00048 Music::~Music() 00049 { 00050 // We must stop before destroying the file :) 00051 Stop(); 00052 00053 delete myFile; 00054 } 00055 00056 00058 bool Music::OpenFromFile(const std::string& filename) 00059 { 00060 // First stop the music if it was already running 00061 Stop(); 00062 00063 // Create the sound file implementation, and open it in read mode 00064 if (!myFile->OpenRead(filename)) 00065 { 00066 Err() << "Failed to open \"" << filename << "\" for reading" << std::endl; 00067 return false; 00068 } 00069 00070 // Compute the duration 00071 myDuration = static_cast<float>(myFile->GetSamplesCount()) / myFile->GetSampleRate() / myFile->GetChannelsCount(); 00072 00073 // Resize the internal buffer so that it can contain 1 second of audio samples 00074 mySamples.resize(myFile->GetSampleRate()); 00075 00076 // Initialize the stream 00077 Initialize(myFile->GetChannelsCount(), myFile->GetSampleRate()); 00078 00079 return true; 00080 } 00081 00082 00084 bool Music::OpenFromMemory(const void* data, std::size_t sizeInBytes) 00085 { 00086 // First stop the music if it was already running 00087 Stop(); 00088 00089 // Create the sound file implementation, and open it in read mode 00090 if (!myFile->OpenRead(data, sizeInBytes)) 00091 { 00092 Err() << "Failed to open music from memory for reading" << std::endl; 00093 return false; 00094 } 00095 00096 // Compute the duration 00097 myDuration = static_cast<float>(myFile->GetSamplesCount()) / myFile->GetSampleRate() / myFile->GetChannelsCount(); 00098 00099 // Resize the internal buffer so that it can contain 1 second of audio samples 00100 mySamples.resize(myFile->GetSampleRate()); 00101 00102 // Initialize the stream 00103 Initialize(myFile->GetChannelsCount(), myFile->GetSampleRate()); 00104 00105 return true; 00106 } 00107 00108 00110 float Music::GetDuration() const 00111 { 00112 return myDuration; 00113 } 00114 00115 00117 bool Music::OnGetData(SoundStream::Chunk& data) 00118 { 00119 Lock lock(myMutex); 00120 00121 // Fill the chunk parameters 00122 data.Samples = &mySamples[0]; 00123 data.NbSamples = myFile->Read(&mySamples[0], mySamples.size()); 00124 00125 // Check if we have reached the end of the audio file 00126 return data.NbSamples == mySamples.size(); 00127 } 00128 00129 00133 void Music::OnSeek(float timeOffset) 00134 { 00135 Lock lock(myMutex); 00136 00137 myFile->Seek(timeOffset); 00138 } 00139 00140 } // namespace sf
:: Copyright © 2007-2008 Laurent Gomila, all rights reserved :: Documentation generated by doxygen 1.5.2 ::