AudioDevice.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/audio/AL/AudioDevice.hpp"
30 
31 namespace nom {
32  namespace priv {
33 
34 void AL_FreeAudioDevice ( ALCdevice* dev )
35 {
36 NOM_LOG_TRACE ( NOM );
37 
38  if ( dev != nullptr )
39  {
40  alcCloseDevice ( dev );
41  }
42 }
43 
44 void AL_FreeAudioContext ( ALCcontext* ctx )
45 {
46 NOM_LOG_TRACE ( NOM );
47 
48  if ( ctx != nullptr )
49  {
50  // Disable context
51  alcMakeContextCurrent ( nullptr );
52 
53  // Release context
54  alcDestroyContext ( ctx );
55  }
56 }
57 
58 
59  } // namespace priv
60 } // namespace nom
61 
62 namespace nom {
63  namespace OpenAL {
64 
66 
68 {
69  if ( ! this->initialized() )
70  initialize ( nullptr );
71 }
72 
73 AudioDevice::AudioDevice ( const std::string& device_name )
74 {
75  if ( ! this->initialized() )
76  initialize ( device_name.c_str() );
77 }
78 
79 AudioDevice::~AudioDevice ( void )
80 {
81 NOM_LOG_TRACE ( NOM );
82 }
83 
84 bool AudioDevice::initialize ( const ALCchar* device_name )
85 {
86 NOM_LOG_TRACE ( NOM );
87 
88  audio_device.reset();
89  audio_context.reset();
90 
91  // audio device handle
92  this->audio_device = std::shared_ptr<ALCdevice> ( alcOpenDevice ( device_name ), nom::priv::AL_FreeAudioDevice );
93 
94  // attach a context (think: listener) to device
95  if ( this->audio_device )
96  {
97  // Store the audio device name now that it is confirmed valid
98  this->device_name = alcGetString ( this->audio_device.get(), ALC_DEFAULT_DEVICE_SPECIFIER );
99 
100  this->audio_context = std::shared_ptr<ALCcontext> ( alcCreateContext ( this->audio_device.get(), nullptr ), nom::priv::AL_FreeAudioContext );
101 
102  if ( this->audio_context )
103  {
104  alcMakeContextCurrent ( this->audio_context.get() );
105  }
106  else
107  {
108 NOM_LOG_ERR ( NOM, "Failed to create the audio context." );
109  return false;
110  }
111  }
112  else
113  {
114 NOM_LOG_ERR ( NOM, "Failed to open the audio device." );
115  return false;
116  }
117 
118  this->audio_initialized = true;
119 
120  return true;
121 }
122 
123 bool AudioDevice::initialized ( void ) const
124 {
125  if ( this->audio_initialized )
126  return true;
127  else
128  return false;
129 }
130 
131 std::shared_ptr<ALCdevice> AudioDevice::getAudioDevice ( void ) const
132 {
133  return this->audio_device;
134 }
135 
136 const std::string AudioDevice::getDeviceName ( void ) const
137 {
138  return this->device_name;
139 }
140 
141 bool AudioDevice::isExtensionSupported ( const std::string& extension ) const
142 {
143  if ( ( extension.length() > 2 ) && ( extension.substr ( 0, 3 ) == "ALC" ) )
144  {
145  if ( alcIsExtensionPresent ( this->audio_device.get(), extension.c_str() ) != AL_FALSE )
146  return true;
147  }
148  else
149  {
150  if ( alIsExtensionPresent ( extension.c_str() ) != AL_FALSE )
151  return true;
152  }
153 
154  return false;
155 }
156 
157 
158  } // namespace OpenAL
159 } // namespace nom
const std::string getDeviceName(void) const
Obtain the initialized audio device name.
bool initialized(void) const
...
AudioDevice(void)
Default constructor for initializing the default audio device.
Definition: AudioDevice.cpp:67
static bool audio_initialized
Definition: AudioDevice.hpp:81
std::shared_ptr< ALCdevice > getAudioDevice(void) const
Obtain the initialized OpenAL audio device.
std::shared_ptr< ALCcontext > audio_context
Audio device context.
Definition: AudioDevice.hpp:88
bool isExtensionSupported(const std::string &extension) const
Obtain support info regarding a particular extension.
const ALCchar * device_name
device name
Definition: AudioDevice.hpp:90
std::shared_ptr< ALCdevice > audio_device
Audio device handle.
Definition: AudioDevice.hpp:86