Otclient  14/8/2020
soundsource.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2020 OTClient <https://github.com/edubart/otclient>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include "soundsource.h"
24 #include "soundbuffer.h"
25 
26 #include <framework/core/clock.h>
27 
29 {
30  m_sourceId = 0;
31  m_channel = 0;
33  m_fadeTime = 0;
34  m_fadeStartTime = 0;
35  m_fadeGain = 0;
36  m_gain = 1.0f;
37 
38  alGenSources(1, &m_sourceId);
39  assert(alGetError() == AL_NO_ERROR);
41 }
42 
44 {
45  if(m_sourceId != 0) {
46  stop();
47  alDeleteSources(1, &m_sourceId);
48  assert(alGetError() == AL_NO_ERROR);
49  }
50 }
51 
53 {
54  alSourcePlay(m_sourceId);
55  assert(alGetError() == AL_NO_ERROR);
56 }
57 
59 {
60  alSourceStop(m_sourceId);
61  assert(alGetError() == AL_NO_ERROR);
62  if(m_buffer) {
63  alSourcei(m_sourceId, AL_BUFFER, AL_NONE);
64  assert(alGetError() == AL_NO_ERROR);
65  m_buffer = nullptr;
66  }
67 }
68 
70 {
71  int state = AL_PLAYING;
72  alGetSourcei(m_sourceId, AL_SOURCE_STATE, &state);
73  return state != AL_STOPPED;
74 }
75 
77 {
78  alSourcei(m_sourceId, AL_BUFFER, buffer->getBufferId());
79  assert(alGetError() == AL_NO_ERROR);
80  m_buffer = buffer;
81 }
82 
83 void SoundSource::setLooping(bool looping)
84 {
85  alSourcei(m_sourceId, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);
86 }
87 
88 void SoundSource::setRelative(bool relative)
89 {
90  alSourcei(m_sourceId, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE);
91 }
92 
94 {
95  alSourcef(m_sourceId, AL_REFERENCE_DISTANCE, distance);
96 }
97 
98 void SoundSource::setGain(float gain)
99 {
100  alSourcef(m_sourceId, AL_GAIN, gain);
101  m_gain = gain;
102 }
103 
104 void SoundSource::setPitch(float pitch)
105 {
106  alSourcef(m_sourceId, AL_PITCH, pitch);
107 }
108 
110 {
111  alSource3f(m_sourceId, AL_POSITION, pos.x, pos.y, 0);
112 }
113 
114 void SoundSource::setVelocity(const Point& velocity)
115 {
116  alSource3f(m_sourceId, AL_VELOCITY, velocity.x, velocity.y, 0);
117 }
118 
119 void SoundSource::setFading(FadeState state, float fadeTime)
120 {
121  float now = stdext::millis() / 1000.0f;
122  if(m_fadeState != NoFading) {
123  float elapsed = now - m_fadeStartTime;
124  float add;
125  if(m_fadeState == FadingOn)
126  add = -(1-(elapsed / m_fadeTime))*fadeTime;
127  else
128  add = -(elapsed / m_fadeTime)*fadeTime;
129  m_fadeStartTime = now + add;
130  } else
131  m_fadeStartTime = now;
132 
133  m_fadeState = state;
134  m_fadeTime = fadeTime;
135  m_fadeGain = m_gain;
136 
137  if(m_fadeState == FadingOn)
138  setGain(0.0);
139 }
140 
142 {
143  float now = stdext::millis() / 1000.0f;
144  if(m_fadeState == FadingOn) {
145  float elapsed = now - m_fadeStartTime;
146  if(elapsed >= m_fadeTime) {
148  } else {
149  setGain((elapsed / m_fadeTime) * m_fadeGain);
150  }
151  } else if(m_fadeState == FadingOff) {
152  float elapsed = now - m_fadeStartTime;
153  if(elapsed >= m_fadeTime) {
155  stop();
157  } else {
158  setGain(((m_fadeTime - elapsed) / m_fadeTime) * m_fadeGain);
159  }
160  }
161 }
stdext::millis
ticks_t millis()
Definition: time.cpp:37
SoundSource::NoFading
@ NoFading
Definition: soundsource.h:36
SoundSource::m_fadeTime
float m_fadeTime
Definition: soundsource.h:75
TPoint::y
T y
Definition: point.h:83
SoundSource::~SoundSource
virtual ~SoundSource()
Definition: soundsource.cpp:43
SoundSource::FadeState
FadeState
Definition: soundsource.h:36
SoundSource::FadingOff
@ FadingOff
Definition: soundsource.h:36
SoundSource::isBuffering
virtual bool isBuffering()
Definition: soundsource.cpp:69
SoundSource::m_fadeStartTime
float m_fadeStartTime
Definition: soundsource.h:74
soundbuffer.h
SoundSource::FadingOn
@ FadingOn
Definition: soundsource.h:36
clock.h
SoundSource::setFading
virtual void setFading(FadeState state, float fadetime)
Definition: soundsource.cpp:119
SoundSource::setVelocity
virtual void setVelocity(const Point &velocity)
Definition: soundsource.cpp:114
SoundSource::m_channel
uchar m_channel
Definition: soundsource.h:70
SoundSource::setReferenceDistance
virtual void setReferenceDistance(float distance)
Definition: soundsource.cpp:93
SoundSource::m_buffer
SoundBufferPtr m_buffer
Definition: soundsource.h:72
SoundSource::stop
virtual void stop()
Definition: soundsource.cpp:58
SoundSource::play
virtual void play()
Definition: soundsource.cpp:52
SoundSource::m_sourceId
uint m_sourceId
Definition: soundsource.h:69
SoundSource::m_fadeGain
float m_fadeGain
Definition: soundsource.h:76
TPoint::x
T x
Definition: point.h:83
SoundSource::m_fadeState
FadeState m_fadeState
Definition: soundsource.h:73
SoundSource::setGain
virtual void setGain(float gain)
Definition: soundsource.cpp:98
SoundSource::setPitch
virtual void setPitch(float pitch)
Definition: soundsource.cpp:104
SoundSource::setPosition
virtual void setPosition(const Point &pos)
Definition: soundsource.cpp:109
SoundSource::m_gain
float m_gain
Definition: soundsource.h:77
SoundSource::setLooping
virtual void setLooping(bool looping)
Definition: soundsource.cpp:83
SoundSource::SoundSource
SoundSource()
Definition: soundsource.cpp:28
SoundBuffer::getBufferId
uint getBufferId()
Definition: soundbuffer.h:39
SoundSource::setBuffer
void setBuffer(const SoundBufferPtr &buffer)
Definition: soundsource.cpp:76
stdext::shared_object_ptr< SoundBuffer >
SoundSource::update
virtual void update()
Definition: soundsource.cpp:141
soundsource.h
TPoint< int >
SoundSource::setRelative
virtual void setRelative(bool relative)
Definition: soundsource.cpp:88