Otclient  14/8/2020
soundchannel.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 "soundchannel.h"
24 #include "streamsoundsource.h"
25 #include "soundmanager.h"
26 
27 SoundSourcePtr SoundChannel::play(const std::string& filename, float fadetime, float gain)
28 {
29  if(!g_sounds.isAudioEnabled() || !m_enabled)
30  return nullptr;
31 
32  if(m_currentSource)
33  m_currentSource->stop();
34 
35  m_currentSource = g_sounds.play(filename, fadetime, m_gain*gain);
36  return m_currentSource;
37 }
38 
39 void SoundChannel::stop(float fadetime)
40 {
41  m_queue.clear();
42 
43  if(m_currentSource) {
44  if(fadetime > 0)
45  m_currentSource->setFading(StreamSoundSource::FadingOff, fadetime);
46  else {
47  m_currentSource->stop();
48  m_currentSource = nullptr;
49  }
50  }
51 }
52 
53 void SoundChannel::enqueue(const std::string& filename, float fadetime, float gain)
54 {
55  if(gain == 0)
56  gain = 1.0f;
57  m_queue.push_back(QueueEntry{g_sounds.resolveSoundFile(filename), fadetime, gain});
58  std::random_shuffle(m_queue.begin(), m_queue.end());
59  //update();
60 }
61 
63 {
64  if(m_currentSource && !m_currentSource->isPlaying())
65  m_currentSource = nullptr;
66 
67  if(!m_currentSource && !m_queue.empty() && g_sounds.isAudioEnabled() && m_enabled) {
68  QueueEntry entry = m_queue.front();
69  m_queue.pop_front();
70  m_queue.push_back(entry);
71  play(entry.filename, entry.fadetime, entry.gain);
72  }
73 }
74 
75 void SoundChannel::setEnabled(bool enable)
76 {
77  if(m_enabled == enable)
78  return;
79 
80  if(enable) {
81  m_enabled = true;
82  update();
83  } else {
84  m_enabled = false;
85  if(m_currentSource) {
86  m_currentSource->stop();
87  m_currentSource = nullptr;
88  }
89  }
90 }
91 
92 void SoundChannel::setGain(float gain)
93 {
94  if(m_currentSource)
95  m_currentSource->setGain(gain);
96  m_gain = gain;
97 }
SoundChannel::play
SoundSourcePtr play(const std::string &filename, float fadetime=0, float gain=1.0f)
Definition: soundchannel.cpp:27
SoundChannel::enable
void enable()
Definition: soundchannel.h:37
SoundManager::isAudioEnabled
bool isAudioEnabled()
Definition: soundmanager.h:42
SoundChannel::enqueue
void enqueue(const std::string &filename, float fadetime=0, float gain=1.0f)
Definition: soundchannel.cpp:53
SoundSource::FadingOff
@ FadingOff
Definition: soundsource.h:36
SoundManager::resolveSoundFile
std::string resolveSoundFile(std::string file)
Definition: soundmanager.cpp:288
SoundSource::isPlaying
virtual bool isPlaying()
Definition: soundsource.h:45
g_sounds
SoundManager g_sounds
Definition: soundmanager.cpp:36
SoundSource::setFading
virtual void setFading(FadeState state, float fadetime)
Definition: soundsource.cpp:119
soundchannel.h
SoundChannel::setGain
void setGain(float gain)
Definition: soundchannel.cpp:92
SoundSource::stop
virtual void stop()
Definition: soundsource.cpp:58
SoundManager::play
SoundSourcePtr play(std::string filename, float fadetime=0, float gain=0)
Definition: soundmanager.cpp:169
SoundChannel::setEnabled
void setEnabled(bool enable)
Definition: soundchannel.cpp:75
soundmanager.h
SoundSource::setGain
virtual void setGain(float gain)
Definition: soundsource.cpp:98
SoundChannel::update
void update()
Definition: soundchannel.cpp:62
SoundChannel::stop
void stop(float fadetime=0)
Definition: soundchannel.cpp:39
stdext::shared_object_ptr< SoundSource >
streamsoundsource.h