AudioDevice.cpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
26// Headers
28#include <SFML/Audio/AudioDevice.hpp>
29#include <SFML/Audio/AudioResource.hpp>
30#include <SFML/Audio/Listener.hpp>
31#include <algorithm>
32#include <iostream>
33
34
35namespace sf
36{
37namespace priv
38{
40// Static member data
42AudioDevice* AudioDevice::ourInstance;
43
44
48AudioDevice::AudioDevice() :
49myRefCount(0)
50{
51 // Create the device
52 myDevice = alcOpenDevice(NULL);
53
54 if (myDevice)
55 {
56 // Create the context
57 myContext = alcCreateContext(myDevice, NULL);
58
59 if (myContext)
60 {
61 // Set the context as the current one (we'll only need one)
62 alcMakeContextCurrent(myContext);
63
64 // Initialize the listener, located at the origin and looking along the Z axis
65 Listener::SetPosition(0.f, 0.f, 0.f);
66 Listener::SetTarget(0.f, 0.f, -1.f);
67 }
68 else
69 {
70 std::cerr << "Failed to create the audio context" << std::endl;
71 }
72 }
73 else
74 {
75 std::cerr << "Failed to open the audio device" << std::endl;
76 }
77}
78
79
83AudioDevice::~AudioDevice()
84{
85 // Destroy the context
86 alcMakeContextCurrent(NULL);
87 if (myContext)
88 alcDestroyContext(myContext);
89
90 // Destroy the device
91 if (myDevice)
92 alcCloseDevice(myDevice);
93}
94
95
99AudioDevice& AudioDevice::GetInstance()
100{
101 // Create the audio device if it doesn't exist
102 if (!ourInstance)
103 ourInstance = new AudioDevice;
104
105 return *ourInstance;
106}
107
108
112void AudioDevice::AddReference()
113{
114 // Create the audio device if it doesn't exist
115 if (!ourInstance)
116 ourInstance = new AudioDevice;
117
118 // Increase the references count
119 ourInstance->myRefCount++;
120}
121
122
126void AudioDevice::RemoveReference()
127{
128 // Decrease the references count
129 ourInstance->myRefCount--;
130
131 // Destroy the audio device if the references count reaches 0
132 if (ourInstance->myRefCount == 0)
133 {
134 delete ourInstance;
135 ourInstance = NULL;
136 }
137}
138
139
143ALCdevice* AudioDevice::GetDevice() const
144{
145 return myDevice;
146}
147
148
152ALenum AudioDevice::GetFormatFromChannelsCount(unsigned int ChannelsCount) const
153{
154 // Find the good format according to the number of channels
155 switch (ChannelsCount)
156 {
157 case 1 : return AL_FORMAT_MONO16;
158 case 2 : return AL_FORMAT_STEREO16;
159 case 4 : return alGetEnumValue("AL_FORMAT_QUAD16");
160 case 6 : return alGetEnumValue("AL_FORMAT_51CHN16");
161 case 7 : return alGetEnumValue("AL_FORMAT_61CHN16");
162 case 8 : return alGetEnumValue("AL_FORMAT_71CHN16");
163 }
164
165 return 0;
166}
167
168} // namespace priv
169
170} // namespace sf