AudioDevice.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/AudioDevice.hpp>
00029 #include <SFML/Audio/AudioResource.hpp>
00030 #include <algorithm>
00031 #include <iostream>
00032 
00033 
00034 namespace sf_private
00035 {
00039 sfAudioDevice::sfAudioDevice()
00040 {
00041     // Create the device
00042     myDevice = alcOpenDevice(NULL);
00043 
00044     if (myDevice)
00045     {
00046         // Create the context
00047         myContext = alcCreateContext(myDevice, NULL);
00048 
00049         if (myContext)
00050         {
00051             // Set the context as the current one (we'll only need one)
00052             alcMakeContextCurrent(myContext);
00053 
00054             // Setup a default listener
00055             float ListenerPos[] = {0.f, 0.f, 0.f};
00056             float ListenerVel[] = {0.f, 0.f, 0.f};
00057             float ListenerOri[] = {0.f, 0.f, 0.f, 0.f, 1.f, 0.f};
00058             sfALCheck(alListenerfv(AL_POSITION,    ListenerPos));
00059             sfALCheck(alListenerfv(AL_VELOCITY,    ListenerVel));
00060             sfALCheck(alListenerfv(AL_ORIENTATION, ListenerOri));
00061         }
00062         else
00063         {
00064             std::cerr << "Failed to create audio context" << std::endl;
00065         }
00066     }
00067     else
00068     {
00069         std::cerr << "Failed to open audio device" << std::endl;
00070     }
00071 }
00072 
00073 
00077 sfAudioDevice::~sfAudioDevice()
00078 {
00079     // Destroy all device-dependant resources
00080     std::for_each(myResources.begin(), myResources.end(), std::mem_fun(&sfAudioResource::DestroyAudioResources));
00081 
00082     // Destroy the context
00083     alcMakeContextCurrent(NULL);
00084     if (myContext)
00085         alcDestroyContext(myContext);
00086 
00087     // Destroy the device
00088     if (myDevice)
00089         alcCloseDevice(myDevice);
00090 }
00091 
00092 
00096 sfAudioDevice& sfAudioDevice::GetInstance()
00097 {
00098     static sfAudioDevice Instance;
00099 
00100     return Instance;
00101 }
00102 
00103 
00107 void sfAudioDevice::AddAudioResource(sfAudioResource* Resource)
00108 {
00109     if (Resource)
00110         myResources.insert(Resource);
00111 }
00112 
00113 
00117 void sfAudioDevice::RemoveAudioResource(sfAudioResource* Resource)
00118 {
00119     if (Resource)
00120         myResources.erase(Resource);
00121 }
00122 
00123 
00127 ALCdevice* sfAudioDevice::GetDevice() const
00128 {
00129     return myDevice;
00130 }
00131 
00132 
00136 ALenum sfAudioDevice::GetFormatFromChannelsCount(unsigned int ChannelsCount) const
00137 {
00138     // Find the good format according to the number of channels
00139     switch (ChannelsCount)
00140     {
00141         case 1 : return AL_FORMAT_MONO16;
00142         case 2 : return AL_FORMAT_STEREO16;
00143         case 4 : return alGetEnumValue("AL_FORMAT_QUAD16");
00144         case 6 : return alGetEnumValue("AL_FORMAT_51CHN16");
00145         case 7 : return alGetEnumValue("AL_FORMAT_61CHN16");
00146         case 8 : return alGetEnumValue("AL_FORMAT_71CHN16");
00147     }
00148 
00149     return 0;
00150 }
00151 
00152 } // namespace sf_private