Skylicht Engine
Loading...
Searching...
No Matches
CAudioEmitter.h
1/*
2!@
3MIT License
4
5Copyright (c) 2012 - 2019 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#ifndef _SKYLICHTAUDIO_AUDIOEMITTER_
26#define _SKYLICHTAUDIO_AUDIOEMITTER_
27
28#include "Driver/ISoundSource.h"
29#include "Driver/ISoundDriver.h"
30
31#include "Decoder/IAudioDecoder.h"
32#include "Thread/IMutex.h"
33
34#define SKYLICHTAUDIO_MIN_PITCH 0.25f
35#define SKYLICHTAUDIO_MAX_PITCH 4.0f
36
37#define SKYLICHTAUDIO_MIN_GAIN 0.0f
38#define SKYLICHTAUDIO_MAX_GAIN 4.0f
39
40using namespace Skylicht::System;
41
42namespace Skylicht
43{
44 namespace Audio
45 {
69 class CAudioEmitter
70 {
71 protected:
72 std::string m_name;
73 std::string m_fileName;
74 bool m_cache;
75
76 IAudioDecoder* m_decoder;
77
78 IMutex* m_mutex;
79 IStream* m_stream;
80 ISoundSource* m_source;
81 ISoundDriver* m_driver;
82
83 ISoundSource::ESourceState m_state;
84 IAudioDecoder::EDecoderType m_decodeType;
85
86 unsigned char** m_buffer;
87 unsigned char* m_decodeBuffer;
88
89 float m_bufferLengthTime;
90 int m_currentBuffer;
91 int m_numBuffer;
92 int m_bufferSize;
93 int m_allocBufferSize;
94
95 bool m_init;
96 int m_initState;
97
98 float m_gain;
99 bool m_loop;
100 float m_pitch;
101 bool m_forcePlay;
102
103 bool m_is3DSound;
104
105 SVector3 m_position;
106 float m_rollOff;
107
108 float m_fadeGain;
109 float m_fadeout;
110 float m_stopWithFade;
111 float m_playWithFade;
112
113 float m_currentTime;
114 public:
115
116 static IAudioDecoder::EDecoderType getDecode(const char* fileName);
117
118 protected:
119
120 EStatus initEmitter();
121
122 void updateSourceBuffer();
123
124 public:
125 CAudioEmitter(IStream* stream, IAudioDecoder::EDecoderType type, ISoundDriver* driver);
126
127 CAudioEmitter(const char* file, bool cache, ISoundDriver* driver);
128
129 virtual ~CAudioEmitter();
130
135 void setName(const char* name)
136 {
137 m_name = name;
138 }
139
144 const char* getName()
145 {
146 return m_name.c_str();
147 }
148
153 IAudioDecoder::EDecoderType getDecoderType()
154 {
155 return m_decodeType;
156 }
157
163 {
164 return m_stream;
165 }
166
171 ISoundSource::ESourceState getState()
172 {
173 return m_state;
174 }
175
180
185 bool init();
186
191 virtual void update();
192
197 virtual void play(bool fromBegin = true);
198
202 virtual void stop();
203
207 virtual void pause();
208
212 virtual void reset();
213
217 virtual void stopStream();
218
223 virtual void seek(float time);
224
229 void stopWithFade(float time);
230
236 void playWithFade(float gain, float time);
237
242 virtual void setLoop(bool loop);
243
248 virtual bool isLoop()
249 {
250 return m_loop;
251 }
252
257 virtual void setPitch(float p);
258
263 virtual float getPitch()
264 {
265 return m_pitch;
266 }
267
272 virtual void setGain(float g);
273
278 virtual float getGain()
279 {
280 return m_gain;
281 }
282
287 bool isPlaying();
288
295 void setPosition(float x, float y, float z);
296
301 void setRollOff(float rollOff);
302
308 {
309 return m_currentTime;
310 }
311
317 {
318 return m_decoder;
319 }
320 };
321 }
322}
323
324#endif
virtual void play(bool fromBegin=true)
Start or resume playback.
virtual void pause()
Pause playback.
ISoundSource::ESourceState getState()
Get current state of the sound source.
Definition CAudioEmitter.h:171
virtual void setGain(float g)
Set playback volume (gain).
virtual void reset()
Reset emitter state to Stopped.
bool isPlaying()
Check if the sound is currently playing.
virtual void seek(float time)
Seek to a specific time in the audio.
virtual void setLoop(bool loop)
Enable or disable looping.
virtual void setPitch(float p)
Set playback pitch.
bool init()
Initialize the emitter (streaming and decoding).
virtual bool isLoop()
Check if looping is enabled.
Definition CAudioEmitter.h:248
void initDefaultValue()
Initialize default values for the emitter.
virtual void stop()
Stop playback.
virtual float getPitch()
Get current playback pitch.
Definition CAudioEmitter.h:263
void setRollOff(float rollOff)
Set roll-off factor for 3D distance attenuation.
void stopWithFade(float time)
Stop playback with a fade-out effect.
float getCurrentTime()
Get current playback time in milliseconds.
Definition CAudioEmitter.h:307
IAudioDecoder * getDecoder()
Get underlying audio decoder.
Definition CAudioEmitter.h:316
void playWithFade(float gain, float time)
Start playback with a fade-in effect.
const char * getName()
Get name of this emitter.
Definition CAudioEmitter.h:144
void setName(const char *name)
Set name for this emitter.
Definition CAudioEmitter.h:135
virtual float getGain()
Get current playback volume (gain).
Definition CAudioEmitter.h:278
IAudioDecoder::EDecoderType getDecoderType()
Get decoder type.
Definition CAudioEmitter.h:153
void setPosition(float x, float y, float z)
Set 3D position of the sound source.
IStream * getStream()
Get underlying stream.
Definition CAudioEmitter.h:162
virtual void update()
Update the emitter state, handles decoding and streaming buffers. This is called automatically by CAu...
virtual void stopStream()
Stop streaming and decoding.
Definition IAudioDecoder.h:46
Definition ISoundDriver.h:36
Definition ISoundSource.h:93
Interface for an audio data stream (File, Memory, etc.).
Definition IStream.h:107
Definition IMutex.h:34
Handles all core audio functionality.
Definition AudioDebugLog.h:31
The foundation for thread and process handling.
Definition CNullMutex.h:32
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
Definition ISoundSource.h:72