SoundBuffer.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/SoundBuffer.hpp"
30 
31 namespace nom {
32  namespace OpenAL {
33 
34 SoundBuffer::SoundBuffer ( void ) : buffer ( 0 )
35 {
36 NOM_LOG_TRACE ( NOM );
37 
38 AL_CHECK_ERR ( alGenBuffers ( 1, &this->buffer ) );
39 }
40 
41 SoundBuffer::~SoundBuffer ( void )
42 {
43 NOM_LOG_TRACE ( NOM );
44 
45  // First, release attached sound resources from this buffer
46  for ( auto it = this->sounds.begin(); it != this->sounds.end(); ++it )
47  {
48  (*it)->reset();
49  }
50 
51  // Goodbye buffer!
52  if ( this->buffer )
53  {
54 AL_CHECK_ERR ( alDeleteBuffers ( 1, &this->buffer ) );
55  }
56 }
57 
58 uint32 SoundBuffer::get ( void ) const
59 {
60  return this->buffer;
61 }
62 
63 uint32 SoundBuffer::getDuration ( void ) const
64 {
65  return this->buffer_duration;
66 }
67 
68 bool SoundBuffer::load ( const std::string& filename )
69 {
70  SoundFile fp;
71 
72  if ( ! fp.open ( filename ) )
73  {
74 NOM_LOG_ERR ( NOM, "Could not load audio: " + filename );
75  return false;
76  }
77 
78  if ( ! fp.read ( this->samples ) )
79  {
80 NOM_LOG_ERR ( NOM, "Could not read audio samples: " + filename );
81  return false;
82  }
83 
84  this->buffer_duration = ( 1000 * fp.getSampleCount() / fp.getSampleRate() / fp.getChannelCount() );
85 
86 // Fill the audio buffer with loaded sample data
87 AL_CHECK_ERR ( alBufferData ( this->buffer, fp.getChannelFormat(),
88  &this->samples.front(), fp.getDataByteSize(),
89  fp.getSampleRate() )
90  );
91 
92  return true;
93 }
94 
95 void SoundBuffer::attach ( Sound* sound ) const
96 {
97  sounds.insert ( sound );
98 }
99 
100 void SoundBuffer::detach ( Sound* sound ) const
101 {
102  sounds.erase ( sound );
103 }
104 
105 
106  } // namespace OpenAL
107 } // namespace nom
uint32 getChannelCount(void) const
Obtain number of channels used by audio file.
Definition: SoundFile.cpp:54
uint32 getChannelFormat(void) const
Obtain channel format; used internally by OpenAL.
Definition: SoundFile.cpp:64
uint32 get(void) const
Obtain buffer data.
Definition: SoundBuffer.cpp:58
uint32 getSampleRate(void) const
Obtain audio sampling rate; this is the number of samples per second.
Definition: SoundFile.cpp:59
std::set< Sound * > sounds
Internal list of sounds loaded onto a buffer object.
Definition: SoundBuffer.hpp:71
void detach(Sound *sound) const
Internally used for detaching a sound from our list.
uint32 buffer
Used internally by OpenAL for identification of audio buffer.
Definition: SoundBuffer.hpp:80
uint32 getDuration(void) const
Definition: SoundBuffer.cpp:63
void attach(Sound *sound) const
Internally used for attaching a sound to our list.
Definition: SoundBuffer.cpp:95
size_t getDataByteSize(void) const
Obtain audio data size in bytes.
Definition: SoundFile.cpp:69
const SoundBuffer * buffer
Buffer that this sound is attached to.
Definition: Sound.hpp:69
size_t getSampleCount(void) const
Obtain number of samples in audio file.
Definition: SoundFile.cpp:49
std::vector< int16 > samples
We load our audio data into this buffer.
Definition: SoundBuffer.hpp:83