cAudio  2.3.0
3d Audio Engine
cOpenALDeviceContext.cpp
1 // Copyright (c) 2008-2011 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones, Murat (wolfmanfx) Sari
2 // This file is part of the "cAudio Engine"
3 // For conditions of distribution and use, see copyright notice in cAudio.h
4 
5 #include "cOpenALDeviceContext.h"
6 #include "cAudio.h"
7 #include "cLogger.h"
8 #include "cAudioEffects.h"
9 
10 #if CAUDIO_EFX_ENABLED == 1
11 # ifdef CAUDIO_PLATFORM_WIN
12 # include <efx.h>
13 # include <efx-creative.h>
14 # include <xram.h>
15 # elif defined(CAUDIO_PLATFORM_LINUX)
16 # include <AL/alext.h>
17 # endif
18 #endif
19 
20 namespace cAudio
21 {
22  cOpenALDeviceContext::cOpenALDeviceContext(IAudioManager* audioManager) : AudioManager(audioManager), Context(NULL), Device(NULL), Initialized(false)
23  {
24 
25  }
26 
27  cOpenALDeviceContext::~cOpenALDeviceContext()
28  {
29 
30  }
31 
32  bool cOpenALDeviceContext::initialize(const char* deviceName, int outputFrequency, int eaxEffectSlots)
33  {
34  cAudioMutexBasicLock lock(Mutex);
35 
36  if(Initialized)
37  return false;
38 
39  //Stores the context attributes (MAX of 4, with 2 zeros to terminate)
40  ALint attribs[6] = { 0 };
41 
42  unsigned int currentAttrib = 0;
43  if(outputFrequency > 0)
44  {
45  attribs[currentAttrib++] = ALC_FREQUENCY;
46  attribs[currentAttrib++] = outputFrequency;
47  }
48 #if CAUDIO_EFX_ENABLED == 1
49  if(eaxEffectSlots > 0)
50  {
51  attribs[currentAttrib++] = ALC_MAX_AUXILIARY_SENDS;
52  attribs[currentAttrib++] = eaxEffectSlots;
53  }
54 #endif
55 
56  //Create a new device
57  Device = alcOpenDevice(deviceName);
58  //Check if device can be created
59  if (Device == NULL)
60  {
61  getLogger()->logError("AudioManager", "Failed to Create OpenAL Device.");
62  checkError();
63  return false;
64  }
65 
66  Context = alcCreateContext(Device, attribs);
67  if (Context == NULL)
68  {
69  getLogger()->logError("AudioManager", "Failed to Create OpenAL Context.");
70  checkError();
71  alcCloseDevice(Device);
72  Device = NULL;
73  return false;
74  }
75 
76  if(!alcMakeContextCurrent(Context))
77  {
78  getLogger()->logError("AudioManager", "Failed to make OpenAL Context current.");
79  checkError();
80  alcDestroyContext(Context);
81  alcCloseDevice(Device);
82  Context = NULL;
83  Device = NULL;
84  return false;
85  }
86 
87  getLogger()->logWarning("AudioManager", "OpenAL Version: %s", alGetString(AL_VERSION));
88  getLogger()->logWarning("AudioManager", "Vendor: %s", alGetString(AL_VENDOR));
89  getLogger()->logWarning("AudioManager", "Renderer: %s", alGetString(AL_RENDERER));
90 
91 #if CAUDIO_EFX_ENABLED == 1
92  initEffects.getEFXInterface()->Mutex.lock();
93  EFXSupported = initEffects.getEFXInterface()->CheckEFXSupport(Device);
94  initEffects.getEFXInterface()->Mutex.unlock();
95  initEffects.checkEFXSupportDetails();
96 
97  if(EFXSupported)
98  {
99  int EFXMajorVersion = 0;
100  int EFXMinorVersion = 0;
101  alcGetIntegerv(Device, ALC_EFX_MAJOR_VERSION, 1, &EFXMajorVersion);
102  alcGetIntegerv(Device, ALC_EFX_MINOR_VERSION, 1, &EFXMinorVersion);
103  getLogger()->logInfo("AudioManager", "EFX Version: %i.%i", EFXMajorVersion, EFXMinorVersion);
104  getLogger()->logInfo("AudioManager", "EFX supported and enabled.");
105  }
106  else
107  {
108  getLogger()->logWarning("AudioManager", "EFX is not supported, EFX disabled.");
109  }
110 #endif
111  getLogger()->logWarning("AudioManager", "Supported Extensions: %s", alGetString(AL_EXTENSIONS));
112  Initialized = true;
113 
114  return true;
115  }
116 
117  void cOpenALDeviceContext::shutDown()
118  {
119  if(Initialized)
120  {
121  cAudioMutexBasicLock lock(Mutex);
122  //Reset context to null
123  alcMakeContextCurrent(NULL);
124  //Delete the context
125  alcDestroyContext(Context);
126  Context = NULL;
127  //Close the device
128  if (!alcCloseDevice(Device))
129  getLogger()->logError("AudioManager", "alcCloseDevice failed");
130 
131  Device = NULL;
132  Initialized = false;
133  }
134  }
135 
136  void cOpenALDeviceContext::update()
137  {
138 
139  }
140 
141  IAudioManager* cOpenALDeviceContext::getAudioManager() const
142  {
143  return AudioManager;
144  }
145 
146  IAudioEffects* cOpenALDeviceContext::getEffects() const
147  {
148 #if CAUDIO_EFX_ENABLED == 1
149  if(EFXSupported)
150  return (IAudioEffects*)&initEffects;
151 #endif
152 
153  return NULL;
154  }
155 
156  ALCcontext* cOpenALDeviceContext::getOpenALContext() const
157  {
158  return Context;
159  }
160 
161  bool cOpenALDeviceContext::checkError()
162  {
163  int error = alGetError();
164  const char* errorString;
165 
166  if (error != AL_NO_ERROR)
167  {
168  errorString = alGetString(error);
169  getLogger()->logError("AudioManager", "OpenAL Error: %s.", errorString);
170  return true;
171  }
172 
173  //if(Device)
174  {
175  error = alcGetError(Device);
176  if (error != AL_NO_ERROR)
177  {
178  errorString = alcGetString(Device, error);
179  getLogger()->logError("AudioManager", "OpenAL Error: %s.", errorString);
180  return true;
181  }
182  }
183  return false;
184  }
185 }
cAudio::ILogger::logError
virtual void logError(const char *sender, const char *msg,...)=0
Used to log an error message to the logging system.
cAudio::ILogger::logWarning
virtual void logWarning(const char *sender, const char *msg,...)=0
Used to log a warning to the logging system.
cAudio
Main namespace for the entire cAudio library.
Definition: cAudioCapture.h:15
cAudio::ILogger::logInfo
virtual void logInfo(const char *sender, const char *msg,...)=0
Used to log an informational message to the logging system.
cAudio::getLogger
CAUDIO_API ILogger * getLogger()
Gets the interface to the logger.
Definition: cAudio.cpp:45