1 ////////////////////////////////////////////////////////////////////////////////
2 // Copyright (C) 2004-2010 by The Allacrost Project
3 // All Rights Reserved
4 //
5 // This code is licensed under the GNU GPL version 2. It is free software
6 // and you may modify it and/or redistribute it under the terms of this license.
7 // See http://www.gnu.org/copyleft/gpl.html for details.
8 ////////////////////////////////////////////////////////////////////////////////
9
10 /** ****************************************************************************
11 *** \file audio_effects.cpp
12 *** \author Mois�s Ferrer Serra, byaku@allacrost.org
13 *** \brief Source file for audio effects
14 *** ***************************************************************************/
15
16 #include "audio_effects.h"
17 #include "audio_descriptor.h"
18
19 using namespace std;
20
21 namespace hoa_audio {
22
23 namespace private_audio {
24
25 ////////////////////////////////////////////////////////////////////////////////
26 // FadeInEffect class methods
27 ////////////////////////////////////////////////////////////////////////////////
28
FadeInEffect(AudioDescriptor & audio,float time)29 FadeInEffect::FadeInEffect(AudioDescriptor& audio, float time) :
30 AudioEffect(),
31 _original_volume(audio.GetVolume()),
32 _effect_time(time),
33 _audio(audio)
34 {
35 _audio.SetVolume(0.0f);
36 }
37
38
39
Update()40 void FadeInEffect::Update() {
41 // If the sound is not playing, there's nothing to be done
42 if (_audio.GetState() != AUDIO_STATE_PLAYING) {
43 return;
44 }
45
46 float new_volume = _audio.GetVolume() + (1.0f / _effect_time) * 0.00025f;
47
48 // If the volume is over the original audio volume, mark the effect as over
49 if (new_volume >= _original_volume) {
50 _audio.SetVolume(_original_volume);
51 active = false;
52 }
53 // Otherwise, update the volume for the audio
54 else {
55 _audio.SetVolume(new_volume);
56 }
57 }
58
59 ////////////////////////////////////////////////////////////////////////////////
60 // FadeOutEffect class methods
61 ////////////////////////////////////////////////////////////////////////////////
62
FadeOutEffect(AudioDescriptor & audio,float time)63 FadeOutEffect::FadeOutEffect(AudioDescriptor& audio, float time) :
64 AudioEffect(),
65 _original_volume(audio.GetVolume()),
66 _effect_time(time),
67 _audio(audio)
68 {}
69
70
71
Update()72 void FadeOutEffect::Update () {
73 // If the sound is not playing, there's nothing to be done
74 if (_audio.GetState() != AUDIO_STATE_PLAYING) {
75 return;
76 }
77
78 float new_volume = _audio.GetVolume() - (1.0f / _effect_time) * 0.00025f;
79
80 // Stop the audio, reset the original volume, and terminate the effect if the volume drops to 0.0f or below
81 if (new_volume <= 0.0f) {
82 _audio.Stop();
83 _audio.SetVolume(_original_volume);
84 active = false;
85 }
86 // Otherwise, update the volume for the audio
87 else {
88 _audio.SetVolume(new_volume);
89 }
90 }
91
92 } // namespace private_audio
93
94 } // namespace hoa_audio
95