Skylicht Engine
Loading...
Searching...
No Matches
CAudioEngine.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 _CSKYLICHT_AUDIO_H_
26#define _CSKYLICHT_AUDIO_H_
27
28#include "stdafx.h"
29
30#include "SkylichtAudioConfig.h"
31#include "Stream/IStreamFactory.h"
32
33#include "Decoder/IAudioDecoder.h"
34#include "Driver/ISoundDriver.h"
35#include "Thread/IMutex.h"
36#include "Thread/IThread.h"
37
38#include "CAudioEmitter.h"
39#include "CAudioReader.h"
40
41using namespace Skylicht::System;
42
43namespace Skylicht
44{
45 namespace Audio
46 {
52
58
64
83 class CAudioEngine : public IThreadCallback
84 {
85 protected:
86 IThread* m_thread;
87
88 IMutex* m_mutex;
89
90 ISoundDriver* m_driver;
91
92 IStreamFactory* m_defaultStreamFactory;
93
94 std::vector<IStreamFactory*> m_streamFactorys;
95
96 std::vector<CAudioEmitter*> m_emitters;
97
98 std::vector<CAudioReader*> m_readers;
99
100 std::map<std::string, IStream*> m_fileToStream;
101
102 SListener m_listener;
103
104 bool m_pause;
105
106 public:
111 static CAudioEngine* getSoundEngine();
112
116 static void shutdownEngine();
117
118 CAudioEngine();
119
120 virtual ~CAudioEngine();
121
125 void init();
126
130 void shutdown();
131
136
141
146
151
155 virtual void updateEmitter();
156
160 virtual void updateThread();
161
166 {
167 m_mutex->lock();
168
169 if (m_driver)
170 m_driver->lockThread();
171 }
172
177 {
178 m_mutex->unlock();
179
180 if (m_driver)
181 m_driver->unlockThread();
182 }
183
189 {
190 return m_driver;
191 }
192
198
204
212 IStream* createStreamFromMemory(unsigned char* buffer, int size, bool takeOwnerShip = false);
213
219 IStream* createStreamFromFile(const char* fileName);
220
227 IStream* createStreamFromFileAndCache(const char* fileName, bool cache);
228
234
241 CAudioEmitter* createAudioEmitter(IStream* stream, IAudioDecoder::EDecoderType decode);
242
249 CAudioEmitter* createAudioEmitter(const char* fileName, bool cache);
250
257
264 CAudioReader* createAudioReader(IStream* stream, IAudioDecoder::EDecoderType decode);
265
271 CAudioReader* createAudioReader(const char* fileName);
272
278
284
289
294
299
312 void setListener(float posx, float posy, float posz, float upx, float upy, float upz, float frontx, float fronty, float frontz);
313
318 inline const SListener& getListener()
319 {
320 return m_listener;
321 }
322
327 inline bool isPause()
328 {
329 return m_pause;
330 }
331 };
332 }
333};
334
335#endif
Audio emitter for playing and managing sound sources.
Definition CAudioEmitter.h:70
virtual void updateThread()
Background thread update loop.
CAudioReader * createAudioReader(const char *fileName)
Create an audio reader from a file.
void registerStreamFactory(IStreamFactory *streamFactory)
Register a custom stream factory.
void destroyReader(CAudioReader *reader)
Destroy an audio reader.
void lockThread()
Lock audio thread for safe state modification.
Definition CAudioEngine.h:165
IStream * createStreamFromMemory(unsigned char *buffer, int size, bool takeOwnerShip=false)
Create an audio stream from a memory buffer.
virtual void updateEmitter()
Update all active emitters. Called by updateThread or updateSkylichtAudio.
void unRegisterStreamFactory(IStreamFactory *streamFactory)
Unregister a stream factory.
IStream * createStreamFromFileAndCache(const char *fileName, bool cache)
Create an audio stream from a file with optional caching.
void setListener(float posx, float posy, float posz, float upx, float upy, float upz, float frontx, float fronty, float frontz)
Set the listener parameters for 3D sound.
const SListener & getListener()
Get current listener parameters.
Definition CAudioEngine.h:318
CAudioEmitter * createAudioEmitter(IStream *stream, IAudioDecoder::EDecoderType decode)
Create an audio emitter from a stream.
void destroyAllEmitter()
Destroy all audio emitters.
IStream * createStreamFromFile(const char *fileName)
Create an audio stream from a file.
void updateDriver()
Manually update the audio driver.
void init()
Initialize the audio engine, driver, and background thread.
static void shutdownEngine()
Shutdown and release the audio engine instance.
void shutdown()
Shutdown the audio engine and release all resources.
void destroyAllReader()
Destroy all audio readers.
void pauseEngine()
Suspend the audio engine and driver.
void stopAllSound()
Stop all active sounds.
CAudioEmitter * createAudioEmitter(const char *fileName, bool cache)
Create an audio emitter from a file.
CAudioEmitter * createRawAudioEmitter(IStream *stream)
Create an audio emitter for raw PCM data.
CAudioReader * createAudioReader(IStream *stream, IAudioDecoder::EDecoderType decode)
Create an audio reader for decoding audio data without playback.
void destroyEmitter(CAudioEmitter *emitter)
Destroy an audio emitter.
void resumeEngine()
Resume the audio engine and driver.
ISoundDriver * getSoundDriver()
Get the active sound driver.
Definition CAudioEngine.h:188
bool isPause()
Check if the audio engine is paused.
Definition CAudioEngine.h:327
static CAudioEngine * getSoundEngine()
Get the singleton instance of the audio engine.
IStream * createOnlineStream()
Create an empty online stream for streaming data over the network.
void unLockThread()
Unlock audio thread.
Definition CAudioEngine.h:176
void releaseAllStream()
Release all cached streams.
Audio reader for decoding audio data to raw PCM wave data. This class is intended for reading audio s...
Definition CAudioReader.h:46
Definition ISoundDriver.h:36
Interface for stream factories. Register a custom factory to CAudioEngine to support different data s...
Definition IStreamFactory.h:59
Interface for an audio data stream (File, Memory, etc.).
Definition IStream.h:107
Definition IMutex.h:34
Definition IThread.h:59
void updateSkylichtAudio()
Update the audio driver. Should be called every frame if multithreading is disabled.
void initSkylichtAudio()
Initialize the Skylicht Audio system.
void releaseSkylichtAudio()
Release the Skylicht Audio system.
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:86