Music.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/Music.hpp"
30 
31 namespace nom {
32  namespace OpenAL {
33 
34 Music::Music ( void )
35 {
36 NOM_LOG_TRACE ( NOM );
37 
38  // Initialize here
39 }
40 
41 Music::Music ( const SoundBuffer& copy )
42 {
43  this->setBuffer ( copy );
44 }
45 
46 Music::~Music ( void )
47 {
48 NOM_LOG_TRACE ( NOM );
49 
50  //this->Stop();
51 }
52 
53 void Music::setBuffer ( const SoundBuffer& copy )
54 {
55 NOM_LOG_TRACE ( NOM );
56 
57 AL_CHECK_ERR ( alSourcei ( source_id, AL_BUFFER, copy.get() ) );
58 }
59 
60 void Music::Play ( void )
61 {
62 AL_CHECK_ERR ( alSourcePlay ( source_id ) );
63 }
64 
65 void Music::Stop ( void )
66 {
67 AL_CHECK_ERR ( alSourceStop ( source_id ) );
68 }
69 
70 void Music::Pause ( void )
71 {
72 AL_CHECK_ERR ( alSourcePause ( source_id ) );
73 }
74 
75 void Music::togglePause ( void )
76 {
77  if ( this->getStatus() == SoundStatus::Paused )
78  {
79  this->Play();
80  }
81  else if ( this->getStatus() == SoundStatus::Playing )
82  {
83  this->Pause();
84  }
85 }
86 
87 void Music::fadeOut ( float seconds )
88 {
89  float current_volume = this->getVolume();
90  float fade_step = current_volume / seconds;
91 
92  while ( this->getStatus() != SoundStatus::Paused && this->getStatus() != SoundStatus::Stopped )
93  {
94  if ( current_volume > 0.0 )
95  {
96  std::cout << "\nFading out\n";
97  this->setVolume ( current_volume );
98 NOM_DUMP_VAR ( current_volume );
99  }
100  else
101  {
102  std::cout << "\nStopped\n";
103  this->Pause();
104  }
105 
106  current_volume = current_volume - fade_step;
107  sleep ( 1000 ); // FIXME
108 
109  } // while getStatus loop
110 }
111 
112 
113  } // namespace OpenAL
114 } // namespace nom
void setVolume(float gain)
SoundStatus getStatus(void) const
Obtain current state of sound.
float getVolume(void) const
Definition: SoundSource.cpp:50
uint32 source_id
Source identification; used by OpenAL.