D:/Programmation/Cpp/SFML/src/SFML/Audio/SoundBufferImpl.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/SoundBufferImpl.hpp>
00029 #include <SFML/Audio/OpenAL.hpp>
00030 
00031 
00032 namespace sf_private
00033 {
00037 sfSoundBufferImpl::sfSoundBufferImpl(const sfInt16* Samples, std::size_t SamplesCount, unsigned int ChannelsCount, unsigned int Frequency)
00038 {
00039     // Create the buffer
00040     sfALCheck(alGenBuffers(1, &Buffer));
00041 
00042     // Fill it
00043     if (Samples && SamplesCount)
00044         Fill(Samples, SamplesCount, ChannelsCount, Frequency);
00045 }
00046 
00047 
00051 sfSoundBufferImpl::~sfSoundBufferImpl()
00052 {
00053     DestroyAudioResources();
00054 }
00055 
00056 
00060 void sfSoundBufferImpl::Fill(const sfInt16* Samples, std::size_t SamplesCount, unsigned int ChannelsCount, unsigned int Frequency)
00061 {
00062     // Find the good format according to the number of channels
00063     ALenum Format = 0;
00064     switch (ChannelsCount)
00065     {
00066         case 1 : Format = AL_FORMAT_MONO16;                    break;
00067         case 2 : Format = AL_FORMAT_STEREO16;                  break;
00068         case 4 : Format = alGetEnumValue("AL_FORMAT_QUAD16");  break;
00069         case 6 : Format = alGetEnumValue("AL_FORMAT_51CHN16"); break;
00070         case 7 : Format = alGetEnumValue("AL_FORMAT_61CHN16"); break;
00071         case 8 : Format = alGetEnumValue("AL_FORMAT_71CHN16"); break;
00072     }
00073 
00074     // Check if the format is valid
00075     if (!Format)
00076     {
00077         std::cerr << "Unsupported number of channels (" << ChannelsCount << ")" << std::endl;
00078         return;
00079     }
00080 
00081     // Fill the buffer
00082     ALsizei Size = static_cast<ALsizei>(SamplesCount) * sizeof(sfInt16);
00083     sfALCheck(alBufferData(Buffer, Format, Samples, Size, Frequency));
00084 }
00085 
00086 
00090 void sfSoundBufferImpl::DestroyAudioResources()
00091 {
00092     if (Buffer)
00093     {
00094         sfALCheck(alDeleteBuffers(1, &Buffer));
00095         Buffer = 0;
00096     }
00097 }
00098 
00099 } // namespace sf_private

Generated for SFML by  doxygen 1.5.2