Kiwano Engine v1.3.x
Sound.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/core/Resource.h>
23#include <kiwano-audio/AudioData.h>
24
25namespace kiwano
26{
27namespace audio
28{
29class AudioModule;
30class Sound;
31class SoundPlayer;
32
42class KGE_API SoundCallback : public NativeObject
43{
44public:
47 static RefPtr<SoundCallback> OnStart(const Function<void(Sound* sound)>& cb);
48
51 static RefPtr<SoundCallback> OnLoopEnd(const Function<void(Sound* sound)>& cb);
52
55 static RefPtr<SoundCallback> OnEnd(const Function<void(Sound* sound)>& cb);
56
59 static RefPtr<SoundCallback> OnVolumeChanged(const Function<float(Sound* sound, float volume)>& cb);
60
63 virtual inline void OnStart(Sound* sound) {}
64
67 virtual inline void OnLoopEnd(Sound* sound) {}
68
71 virtual inline void OnEnd(Sound* sound) {}
72
75 virtual inline float OnVolumeChanged(Sound* sound, float volume)
76 {
77 return volume;
78 }
79};
80
85class KGE_API Sound : public NativeObject
86{
87 friend class AudioModule;
88 friend class SoundPlayer;
89
90public:
94 Sound(StringView file_path);
95
100 Sound(const Resource& res, StringView ext = "");
101
106
107 Sound();
108
109 virtual ~Sound();
110
114 void Play(int loop_count = 0);
115
118 void Pause();
119
122 void Resume();
123
126 void Stop();
127
130 void Close();
131
134 bool IsPlaying() const;
135
138 float GetVolume() const;
139
143 void SetVolume(float volume);
144
147 void AddCallback(RefPtr<SoundCallback> callback);
148
151 List<RefPtr<SoundCallback>>& GetCallbacks();
152
155 const List<RefPtr<SoundCallback>>& GetCallbacks() const;
156
157protected:
158 bool Load(RefPtr<AudioData> data);
159
160 RefPtr<SoundCallback> GetCallbackChain();
161
162 void ResetVolume();
163
164private:
165 bool opened_;
166 bool playing_;
167 float volume_;
168 RefPtr<AudioData> data_;
169
170 RefPtr<SoundCallback> callback_chain_;
171 List<RefPtr<SoundCallback>> callbacks_;
172};
173
176inline List<RefPtr<SoundCallback>>& kiwano::audio::Sound::GetCallbacks()
177{
178 return callbacks_;
179}
180
181inline const List<RefPtr<SoundCallback>>& kiwano::audio::Sound::GetCallbacks() const
182{
183 return callbacks_;
184}
185
187{
188 callbacks_.push_back(callback);
189}
190
191} // namespace audio
192} // namespace kiwano
Definition: Function.h:228
含有本地指针的对象
Definition: NativeObject.hpp:32
引用计数智能指针
Definition: RefBasePtr.hpp:35
资源
Definition: Resource.h:41
音频模块
Definition: AudioModule.h:50
音频回调
Definition: Sound.h:43
virtual float OnVolumeChanged(Sound *sound, float volume)
在音频修改音量时执行
Definition: Sound.h:75
virtual void OnEnd(Sound *sound)
在音频结束时执行
Definition: Sound.h:71
virtual void OnLoopEnd(Sound *sound)
在音频循环结束时执行
Definition: Sound.h:67
virtual void OnStart(Sound *sound)
在音频开始播放时执行
Definition: Sound.h:63
音频播放器
Definition: SoundPlayer.h:39
音频
Definition: Sound.h:86
void AddCallback(RefPtr< SoundCallback > callback)
添加回调
Definition: Sound.h:186
List< RefPtr< SoundCallback > > & GetCallbacks()
获取所有回调
Definition: Sound.h:176