Sound.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/Sound.hpp"
30 
31 namespace nom {
32  namespace OpenAL {
33 
34 Sound::Sound ( void ) : buffer ( nullptr )
35 {
36 NOM_LOG_TRACE ( NOM );
37 }
38 
39 Sound::Sound ( const SoundBuffer& copy ) : buffer ( nullptr )
40 {
41  this->setBuffer ( copy );
42 }
43 
44 Sound::~Sound ( void )
45 {
46 NOM_LOG_TRACE ( NOM );
47 
48  this->Stop();
49 
50  if ( this->buffer )
51  this->buffer->detach ( this );
52 }
53 
54 void Sound::setBuffer ( const SoundBuffer& copy )
55 {
56 NOM_LOG_TRACE ( NOM );
57 
58  // First, detach previous buffer
59  if ( this->buffer )
60  {
61  this->Stop();
62  this->buffer->detach ( this );
63  }
64 
65  // Assign new buffer & use it
66  this->buffer = &copy;
67  this->buffer->attach ( this );
68 
69 AL_CHECK_ERR ( alSourcei ( source_id, AL_BUFFER, this->buffer->get() ) );
70 }
71 
72 void Sound::Play ( void )
73 {
74 AL_CHECK_ERR ( alSourcePlay ( source_id ) );
75 }
76 
77 void Sound::Stop ( void )
78 {
79 AL_CHECK_ERR ( alSourceStop ( source_id ) );
80 }
81 
82 void Sound::Pause ( void )
83 {
84 AL_CHECK_ERR ( alSourcePause ( source_id ) );
85 }
86 
87 // TODO
88 /*
89 float Sound::getPlayPosition ( void ) const
90 {
91  ALfloat playback_position;
92 
93  alGetSourcef ( source_id, AL_SEC_OFFSET, &playback_position );
94 
95  return playback_position;
96 }
97 
98 void Sound::setPlayPosition ( float seconds )
99 {
100  alSourcef ( source_id, AL_SEC_OFFSET, seconds );
101 }
102 */
103 
104 void Sound::reset ( void )
105 {
106  this->Stop();
107 
108 AL_CHECK_ERR ( alSourcei ( source_id, AL_BUFFER, 0 ) );
109 
110  buffer = nullptr;
111 }
112 
113  } // namespace OpenAL
114 } // namespace nom
uint32 get(void) const
Obtain buffer data.
Definition: SoundBuffer.cpp:58
void detach(Sound *sound) const
Internally used for detaching a sound from our list.
uint32 source_id
Source identification; used by OpenAL.
void attach(Sound *sound) const
Internally used for attaching a sound to our list.
Definition: SoundBuffer.cpp:95
const SoundBuffer * buffer
Buffer that this sound is attached to.
Definition: Sound.hpp:69
void reset(void)
Obtain the current playback position of source in seconds.
Definition: Sound.cpp:104