soundsource.cpp

main 165 lines · 4.3 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1/*
2 * Copyright (c) 2010-2017 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#ifdef FW_SOUND
24
25#include "soundsource.h"
26#include "soundbuffer.h"
27
28#include <framework/core/clock.h>
29
30SoundSource::SoundSource()
31{
32 m_sourceId = 0;
33 m_channel = 0;
34 m_fadeState = NoFading;
35 m_fadeTime = 0;
36 m_fadeStartTime = 0;
37 m_fadeGain = 0;
38 m_gain = 1.0f;
39
40 alGenSources(1, &m_sourceId);
41 VALIDATE(alGetError() == AL_NO_ERROR);
42 setReferenceDistance(128);
43}
44
45SoundSource::~SoundSource()
46{
47 if(m_sourceId != 0) {
48 stop();
49 alDeleteSources(1, &m_sourceId);
50 VALIDATE(alGetError() == AL_NO_ERROR);
51 }
52}
53
54void SoundSource::play()
55{
56 alSourcePlay(m_sourceId);
57 VALIDATE(alGetError() == AL_NO_ERROR);
58}
59
60void SoundSource::stop()
61{
62 alSourceStop(m_sourceId);
63 VALIDATE(alGetError() == AL_NO_ERROR);
64 if(m_buffer) {
65 alSourcei(m_sourceId, AL_BUFFER, AL_NONE);
66 VALIDATE(alGetError() == AL_NO_ERROR);
67 m_buffer = nullptr;
68 }
69}
70
71bool SoundSource::isBuffering()
72{
73 int state = AL_PLAYING;
74 alGetSourcei(m_sourceId, AL_SOURCE_STATE, &state);
75 return state != AL_STOPPED;
76}
77
78void SoundSource::setBuffer(const SoundBufferPtr& buffer)
79{
80 alSourcei(m_sourceId, AL_BUFFER, buffer->getBufferId());
81 VALIDATE(alGetError() == AL_NO_ERROR);
82 m_buffer = buffer;
83}
84
85void SoundSource::setLooping(bool looping)
86{
87 alSourcei(m_sourceId, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);
88}
89
90void SoundSource::setRelative(bool relative)
91{
92 alSourcei(m_sourceId, AL_SOURCE_RELATIVE, relative ? AL_TRUE : AL_FALSE);
93}
94
95void SoundSource::setReferenceDistance(float distance)
96{
97 alSourcef(m_sourceId, AL_REFERENCE_DISTANCE, distance);
98}
99
100void SoundSource::setGain(float gain)
101{
102 alSourcef(m_sourceId, AL_GAIN, gain);
103 m_gain = gain;
104}
105
106void SoundSource::setPitch(float pitch)
107{
108 alSourcef(m_sourceId, AL_PITCH, pitch);
109}
110
111void SoundSource::setPosition(const Point& pos)
112{
113 alSource3f(m_sourceId, AL_POSITION, pos.x, pos.y, 0);
114}
115
116void SoundSource::setVelocity(const Point& velocity)
117{
118 alSource3f(m_sourceId, AL_VELOCITY, velocity.x, velocity.y, 0);
119}
120
121void SoundSource::setFading(FadeState state, float fadeTime)
122{
123 float now = stdext::millis() / 1000.0f;
124 if(m_fadeState != NoFading) {
125 float elapsed = now - m_fadeStartTime;
126 float add;
127 if(m_fadeState == FadingOn)
128 add = -(1-(elapsed / m_fadeTime))*fadeTime;
129 else
130 add = -(elapsed / m_fadeTime)*fadeTime;
131 m_fadeStartTime = now + add;
132 } else
133 m_fadeStartTime = now;
134
135 m_fadeState = state;
136 m_fadeTime = fadeTime;
137 m_fadeGain = m_gain;
138
139 if(m_fadeState == FadingOn)
140 setGain(0.0);
141}
142
143void SoundSource::update()
144{
145 float now = stdext::millis() / 1000.0f;
146 if(m_fadeState == FadingOn) {
147 float elapsed = now - m_fadeStartTime;
148 if(elapsed >= m_fadeTime) {
149 m_fadeState = NoFading;
150 } else {
151 setGain((elapsed / m_fadeTime) * m_fadeGain);
152 }
153 } else if(m_fadeState == FadingOff) {
154 float elapsed = now - m_fadeStartTime;
155 if(elapsed >= m_fadeTime) {
156 setGain(m_fadeGain);
157 stop();
158 m_fadeState = NoFading;
159 } else {
160 setGain(((m_fadeTime - elapsed) / m_fadeTime) * m_fadeGain);
161 }
162 }
163}
164
165#endif
Top