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
00035 {
00036 namespace priv
00037 {
00039 // Static member data
00041 AudioDevice* AudioDevice::ourInstance;
00042 
00043 
00047 AudioDevice::AudioDevice() :
00048 myRefCount(0)
00049 {
00050     // Create the device
00051     myDevice = alcOpenDevice(NULL);
00052 
00053     if (myDevice)
00054     {
00055         // Create the context
00056         myContext = alcCreateContext(myDevice, NULL);
00057 
00058         if (myContext)
00059         {
00060             // Set the context as the current one (we'll only need one)
00061             alcMakeContextCurrent(myContext);
00062 
00063             // Setup a default listener
00064             float ListenerPos[] = {0.f, 0.f, 0.f};
00065             float ListenerVel[] = {0.f, 0.f, 0.f};
00066             float ListenerOri[] = {0.f, 0.f, 0.f, 0.f, 1.f, 0.f};
00067             ALCheck(alListenerfv(AL_POSITION,    ListenerPos));
00068             ALCheck(alListenerfv(AL_VELOCITY,    ListenerVel));
00069             ALCheck(alListenerfv(AL_ORIENTATION, ListenerOri));
00070         }
00071         else
00072         {
00073             std::cerr << "Failed to create audio context" << std::endl;
00074         }
00075     }
00076     else
00077     {
00078         std::cerr << "Failed to open audio device" << std::endl;
00079     }
00080 }
00081 
00082 
00086 AudioDevice::~AudioDevice()
00087 {
00088     // Destroy the context
00089     alcMakeContextCurrent(NULL);
00090     if (myContext)
00091         alcDestroyContext(myContext);
00092 
00093     // Destroy the device
00094     if (myDevice)
00095         alcCloseDevice(myDevice);
00096 }
00097 
00098 
00102 AudioDevice& AudioDevice::GetInstance()
00103 {
00104     // Create the audio device if it doesn't exist
00105     if (!ourInstance)
00106         ourInstance = new AudioDevice;
00107 
00108     return *ourInstance;
00109 }
00110 
00111 
00115 void AudioDevice::AddReference()
00116 {
00117     // Create the audio device if it doesn't exist
00118     if (!ourInstance)
00119         ourInstance = new AudioDevice;
00120 
00121     // Increase the references count
00122     ourInstance->myRefCount++;
00123 }
00124 
00125 
00129 void AudioDevice::RemoveReference()
00130 {
00131     // Decrease the references count
00132     ourInstance->myRefCount--;
00133 
00134     // Destroy the audio device if the references count reaches 0
00135     if (ourInstance->myRefCount == 0)
00136     {
00137         delete ourInstance;
00138         ourInstance = NULL;
00139     }
00140 }
00141 
00142 
00146 ALCdevice* AudioDevice::GetDevice() const
00147 {
00148     return myDevice;
00149 }
00150 
00151 
00155 ALenum AudioDevice::GetFormatFromChannelsCount(unsigned int ChannelsCount) const
00156 {
00157     // Find the good format according to the number of channels
00158     switch (ChannelsCount)
00159     {
00160         case 1 : return AL_FORMAT_MONO16;
00161         case 2 : return AL_FORMAT_STEREO16;
00162         case 4 : return alGetEnumValue("AL_FORMAT_QUAD16");
00163         case 6 : return alGetEnumValue("AL_FORMAT_51CHN16");
00164         case 7 : return alGetEnumValue("AL_FORMAT_61CHN16");
00165         case 8 : return alGetEnumValue("AL_FORMAT_71CHN16");
00166     }
00167 
00168     return 0;
00169 }
00170 
00171 } // namespace priv
00172 
00173 } // namespace sf