HephAudio v3.0.6
A cross-platform C++ library for recording, playing, and processing audio on Windows, Android, Linux, iOS, and macOS.
Loading...
Searching...
No Matches
NativeAudio.h
Go to the documentation of this file.
1#pragma once
2#include "HephAudioShared.h"
3#include "AudioDevice.h"
4#include "AudioObject.h"
5#include "AudioFormatInfo.h"
6#include "IAudioDecoder.h"
7#include "IAudioEncoder.h"
9#include "Event.h"
10#include "StringHelpers.h"
11#include <memory>
12#include <string>
13#include <vector>
14#include <thread>
15#include <mutex>
16
19namespace HephAudio
20{
21 namespace Native
22 {
28 {
29 protected:
34 static constexpr bool DEVICE_ENUMERATION_FAIL = false;
35
40 static constexpr bool DEVICE_ENUMERATION_SUCCESS = true;
41
42 protected:
47 std::shared_ptr<IAudioDecoder> pAudioDecoder;
48
53 std::shared_ptr<IAudioEncoder> pAudioEncoder;
54
59 std::vector<AudioObject> audioObjects;
60
65 std::vector<AudioDevice> audioDevices;
66
71 std::thread::id mainThreadId;
72
77 std::thread renderThread;
78
83 std::thread captureThread;
84
89 std::thread deviceThread;
90
95 std::string renderDeviceId;
96
101 std::string captureDeviceId;
102
108
114
120
126
132
138
144
149 mutable std::mutex audioDevicesMutex;
150
155 mutable std::recursive_mutex audioObjectsMutex;
156
157 public:
163
169
175
176 public:
179
180 NativeAudio(const NativeAudio&) = delete;
181 NativeAudio& operator=(const NativeAudio&) = delete;
182
184 virtual ~NativeAudio() = default;
185
190 std::shared_ptr<IAudioDecoder> GetAudioDecoder() const;
191
197 void SetAudioDecoder(std::shared_ptr<IAudioDecoder> pNewDecoder);
198
203 std::shared_ptr<IAudioEncoder> GetAudioEncoder() const;
204
210 void SetAudioEncoder(std::shared_ptr<IAudioEncoder> pNewEncoder);
211
218 AudioObject* Play(const std::filesystem::path& filePath);
219
227 AudioObject* Play(const std::filesystem::path& filePath, uint32_t playCount);
228
237 AudioObject* Play(const std::filesystem::path& filePath, uint32_t playCount, bool isPaused);
238
245 AudioObject* Load(const std::filesystem::path& filePath);
246
254 AudioObject* Load(const std::filesystem::path& filePath, uint32_t playCount);
255
264 AudioObject* Load(const std::filesystem::path& filePath, uint32_t playCount, bool isPaused);
265
275 AudioObject* CreateAudioObject(const std::string& name, size_t bufferFrameCount, AudioChannelLayout channelLayout, uint16_t sampleRate);
276
283 bool DestroyAudioObject(AudioObject* pAudioObject);
284
291 bool DestroyAudioObject(const Heph::Guid& audioObjectId);
292
299 bool AudioObjectExists(AudioObject* pAudioObject) const;
300
307 bool AudioObjectExists(const Heph::Guid& audioObjectId) const;
308
316
323 AudioObject* GetAudioObject(const Heph::Guid& audioObjectId);
324
331 AudioObject* GetAudioObject(const std::string& audioObjectName);
332
337 size_t GetAudioObjectCount() const;
338
344
350
356 bool IsCapturePaused() const;
357
363
368 void SetDeviceEnumerationPeriod(uint32_t deviceEnumerationPeriod_ms);
369
376 virtual void SetMasterVolume(double volume) = 0;
377
382 virtual double GetMasterVolume() const = 0;
383
389
395
401
408 void InitializeRender(AudioChannelLayout channelLayout, uint32_t sampleRate);
409
416
423 virtual void InitializeRender(AudioDevice* device, AudioFormatInfo format) = 0;
424
429 virtual void StopRendering() = 0;
430
436
443 void InitializeCapture(AudioChannelLayout channelLayout, uint32_t sampleRate);
444
451
458 virtual void InitializeCapture(AudioDevice* device, AudioFormatInfo format) = 0;
459
464 virtual void StopCapturing() = 0;
465
470 virtual void GetNativeParams(NativeAudioParams& nativeParams) const = 0;
471
476 virtual void SetNativeParams(const NativeAudioParams& nativeParams) = 0;
477
482 AudioDevice GetAudioDeviceById(const std::string& deviceId) const;
483
489
495
504
510 std::vector<AudioDevice> GetAudioDevices(AudioDeviceType deviceType) const;
511
512 protected:
513
519 virtual bool EnumerateAudioDevices() = 0;
520
525 virtual void CheckAudioDevices();
526
532
538
544
550 EncodedAudioBuffer Mix(uint32_t frameCount);
551
556 size_t GetAOCountToMix() const;
557
562 virtual double GetFinalAOVolume(AudioObject* pAudioObject) const;
563 };
564 }
565}
AudioDeviceType
Definition AudioDevice.h:11
#define HEPH_API
Definition HephShared.h:132
stores encoded audio data.
Definition EncodedAudioBuffer.h:15
base class for the classes that interact with the native audio APIs.
Definition NativeAudio.h:28
std::thread captureThread
Definition NativeAudio.h:83
virtual ~NativeAudio()=default
std::string captureDeviceId
Definition NativeAudio.h:101
virtual double GetMasterVolume() const =0
std::vector< AudioObject > audioObjects
Definition NativeAudio.h:59
AudioObject * Play(const std::filesystem::path &filePath)
AudioObject * GetAudioObject(const std::string &audioObjectName)
void InitializeCapture(AudioChannelLayout channelLayout, uint32_t sampleRate)
Heph::Event OnAudioDeviceAdded
Definition NativeAudio.h:162
bool disposing
Definition NativeAudio.h:119
std::thread renderThread
Definition NativeAudio.h:77
std::shared_ptr< IAudioDecoder > pAudioDecoder
Definition NativeAudio.h:47
const AudioFormatInfo & GetCaptureFormat() const
virtual void StopRendering()=0
std::thread deviceThread
Definition NativeAudio.h:89
bool AudioObjectExists(const Heph::Guid &audioObjectId) const
AudioFormatInfo captureFormat
Definition NativeAudio.h:113
bool isCaptureInitialized
Definition NativeAudio.h:131
bool isRenderInitialized
Definition NativeAudio.h:125
bool AudioObjectExists(AudioObject *pAudioObject) const
Heph::Event OnCapture
Definition NativeAudio.h:174
uint32_t GetDeviceEnumerationPeriod() const
virtual void InitializeCapture(AudioDevice *device, AudioFormatInfo format)=0
bool isCapturePaused
Definition NativeAudio.h:137
AudioObject * CreateAudioObject(const std::string &name, size_t bufferFrameCount, AudioChannelLayout channelLayout, uint16_t sampleRate)
Heph::Event OnAudioDeviceRemoved
Definition NativeAudio.h:168
virtual void SetMasterVolume(double volume)=0
std::thread::id mainThreadId
Definition NativeAudio.h:71
size_t GetAudioObjectCount() const
AudioObject * Play(const std::filesystem::path &filePath, uint32_t playCount)
AudioDevice GetRenderDevice() const
std::string renderDeviceId
Definition NativeAudio.h:95
std::vector< AudioDevice > audioDevices
Definition NativeAudio.h:65
AudioObject * Load(const std::filesystem::path &filePath)
AudioObject * Load(const std::filesystem::path &filePath, uint32_t playCount)
AudioObject * Play(const std::filesystem::path &filePath, uint32_t playCount, bool isPaused)
virtual bool EnumerateAudioDevices()=0
AudioObject * Load(const std::filesystem::path &filePath, uint32_t playCount, bool isPaused)
virtual double GetFinalAOVolume(AudioObject *pAudioObject) const
void InitializeCapture(AudioFormatInfo format)
std::shared_ptr< IAudioEncoder > pAudioEncoder
Definition NativeAudio.h:53
AudioObject * GetAudioObject(const Heph::Guid &audioObjectId)
std::vector< AudioDevice > GetAudioDevices(AudioDeviceType deviceType) const
std::mutex audioDevicesMutex
Definition NativeAudio.h:149
virtual void StopCapturing()=0
AudioDevice GetDefaultAudioDevice(AudioDeviceType deviceType) const
void SetAudioDecoder(std::shared_ptr< IAudioDecoder > pNewDecoder)
EncodedAudioBuffer Mix(uint32_t frameCount)
virtual void InitializeRender(AudioDevice *device, AudioFormatInfo format)=0
bool DestroyAudioObject(AudioObject *pAudioObject)
AudioFormatInfo renderFormat
Definition NativeAudio.h:107
std::shared_ptr< IAudioDecoder > GetAudioDecoder() const
AudioObject * GetAudioObject(size_t index)
virtual void GetNativeParams(NativeAudioParams &nativeParams) const =0
void SetDeviceEnumerationPeriod(uint32_t deviceEnumerationPeriod_ms)
AudioDevice GetAudioDeviceById(const std::string &deviceId) const
void SetAudioEncoder(std::shared_ptr< IAudioEncoder > pNewEncoder)
bool DestroyAudioObject(const Heph::Guid &audioObjectId)
AudioDevice GetCaptureDevice() const
uint32_t deviceEnumerationPeriod_ms
Definition NativeAudio.h:143
std::recursive_mutex audioObjectsMutex
Definition NativeAudio.h:155
std::shared_ptr< IAudioEncoder > GetAudioEncoder() const
void InitializeRender(AudioChannelLayout channelLayout, uint32_t sampleRate)
void InitializeRender(AudioFormatInfo format)
const AudioFormatInfo & GetRenderFormat() const
virtual void SetNativeParams(const NativeAudioParams &nativeParams)=0
class for managing callback functions.
Definition Event.h:25
stores information about the channel layout.
Definition AudioChannelLayout.h:136
stores information about an audio device.
Definition AudioDevice.h:76
stores the properties of the audio signals
Definition AudioFormatInfo.h:20
stores information that's necessary to play audio.
Definition AudioObject.h:36
base class for storing the native audio API specific parameters.
Definition NativeAudioParams.h:15
struct for storing globally unique identifiers.
Definition Guid.h:14