D:/Programmation/Cpp/SFML/src/SFML/Audio/SoundBuffer.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/SoundBuffer.hpp>
00029 #include <SFML/Audio/SoundFile.hpp>
00030 #include <SFML/Utilities/SharedPtr.hpp>
00031 #include <iostream>
00032 
00033 
00037 sfSoundBuffer::sfSoundBuffer() :
00038 myFrequency    (0),
00039 myChannelsCount(0)
00040 {
00041 
00042 }
00043 
00044 
00048 bool sfSoundBuffer::LoadFromFile(const std::string& Filename)
00049 {
00050     // Create the sound file
00051     sfSharedPtr<sf_private::sfSoundFile> File(sf_private::sfSoundFile::Create(Filename));
00052 
00053     // Open the sound file
00054     std::size_t NbSamples = 0;
00055     if (File->OpenRead(Filename, NbSamples, myChannelsCount, myFrequency))
00056     {
00057         // Read the samples from the opened file
00058         mySamples.resize(NbSamples);
00059         if (File->Read(&mySamples[0], NbSamples) == NbSamples)
00060         {
00061             // Create a new implentation with loaded data
00062             myImpl = new sf_private::sfSoundBufferImpl(&mySamples[0], mySamples.size(), myChannelsCount, myFrequency);
00063             
00064             return true;
00065         }
00066         else
00067         {
00068             // Error : destroy the implementation
00069             myImpl = NULL;
00070             std::cerr << "Failed to read audio data from file \"" << Filename << "\"" << std::endl;
00071 
00072             return false;
00073         }
00074     }
00075     else
00076     {
00077         // Error : destroy the implementation
00078         myImpl = NULL;
00079         std::cerr << "Failed to load sound buffer from file \"" << Filename << "\"" << std::endl;
00080 
00081         return false;
00082     }
00083 }
00084 
00085 
00089 bool sfSoundBuffer::LoadFromMemory(const sfInt16* Samples, std::size_t SamplesCount, unsigned int ChannelsCount, unsigned int Frequency)
00090 {
00091     if (Samples && SamplesCount && ChannelsCount && Frequency)
00092     {
00093         // Store sound parameters
00094         mySamples.assign(Samples, Samples + SamplesCount);
00095         myChannelsCount = ChannelsCount;
00096         myFrequency     = Frequency;
00097 
00098         // Create a new implentation with specified data
00099         myImpl = new sf_private::sfSoundBufferImpl(Samples, SamplesCount, ChannelsCount, Frequency);
00100 
00101         return true;
00102     }
00103     else
00104     {
00105         // Error : destroy the implementation
00106         myImpl = NULL;
00107         std::cerr << "Failed to load sound buffer from memory ("
00108                   << "Samples : "        << Samples       << ", "
00109                   << "Samples count : "  << SamplesCount  << ", "
00110                   << "Channels count : " << ChannelsCount << ", "
00111                   << "Frequency : "      << Frequency     << ")"
00112                   << std::endl;
00113 
00114         return false;
00115     }
00116 }
00117 
00118 
00122 bool sfSoundBuffer::SaveToFile(const std::string& Filename) const
00123 {
00124     // Create the sound file
00125     sfSharedPtr<sf_private::sfSoundFile> File(sf_private::sfSoundFile::Create(Filename));
00126 
00127     // Open a sound file
00128     std::size_t NbSamples = 0;
00129     if (File->OpenWrite(Filename, myChannelsCount, myFrequency))
00130     {
00131         // Write the samples to the opened file
00132         File->Write(&mySamples[0], mySamples.size());
00133 
00134         return true;
00135     }
00136     else
00137     {
00138         // Error...
00139         std::cerr << "Failed to save sound buffer to file \"" << Filename << "\"" << std::endl;
00140 
00141         return false;
00142     }
00143 }
00144 
00145 
00149 bool sfSoundBuffer::IsEmpty() const
00150 {
00151     return mySamples.empty();
00152 }
00153 
00154 
00158 const std::vector<sfInt16>& sfSoundBuffer::GetSamples() const
00159 {
00160     return mySamples;
00161 }
00162 
00163 
00167 unsigned int sfSoundBuffer::GetFrequency() const
00168 {
00169     return myFrequency;
00170 }
00171 
00172 
00176 unsigned int sfSoundBuffer::GetChannelsCount() const
00177 {
00178     return myChannelsCount;
00179 }
00180 
00181 
00185 float sfSoundBuffer::GetDuration() const
00186 {
00187     if ((myFrequency == 0) || (myChannelsCount == 0))
00188         return 0;
00189     else
00190         return static_cast<float>(mySamples.size()) / myFrequency / myChannelsCount;
00191 }
00192 
00193 
00197 unsigned int sfSoundBuffer::GetId() const
00198 {
00199     return myImpl ? myImpl->Buffer : 0;
00200 }

Generated for SFML by  doxygen 1.5.2