HephAudio v3.1.0
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 <list>
15#include <thread>
16#include <mutex>
17
20namespace HephAudio
21{
22 namespace Native
23 {
29 {
30 protected:
35 static constexpr bool DEVICE_ENUMERATION_FAIL = false;
36
41 static constexpr bool DEVICE_ENUMERATION_SUCCESS = true;
42
43 protected:
48 std::shared_ptr<IAudioDecoder> pAudioDecoder;
49
54 std::shared_ptr<IAudioEncoder> pAudioEncoder;
55
60 std::list<AudioObject> audioObjects;
61
66 std::vector<AudioDevice> audioDevices;
67
72 std::thread::id mainThreadId;
73
78 std::thread renderThread;
79
84 std::thread captureThread;
85
90 std::thread deviceThread;
91
96 std::string renderDeviceId;
97
102 std::string captureDeviceId;
103
109
115
121
127
133
139
145
150 mutable std::mutex audioDevicesMutex;
151
156 mutable std::recursive_mutex audioObjectsMutex;
157
158 public:
164
170
176
177 public:
180
181 NativeAudio(const NativeAudio&) = delete;
182 NativeAudio& operator=(const NativeAudio&) = delete;
183
185 virtual ~NativeAudio() = default;
186
191 std::shared_ptr<IAudioDecoder> GetAudioDecoder() const;
192
198 void SetAudioDecoder(std::shared_ptr<IAudioDecoder> pNewDecoder);
199
204 std::shared_ptr<IAudioEncoder> GetAudioEncoder() const;
205
211 void SetAudioEncoder(std::shared_ptr<IAudioEncoder> pNewEncoder);
212
219 AudioObject* Play(const std::filesystem::path& filePath);
220
228 AudioObject* Play(const std::filesystem::path& filePath, uint32_t playCount);
229
230
237 AudioObject* Load(const std::filesystem::path& filePath);
238
246 AudioObject* Load(const std::filesystem::path& filePath, uint32_t playCount);
247
257 AudioObject* CreateAudioObject(const std::string& name, size_t bufferFrameCount, AudioChannelLayout channelLayout, uint32_t sampleRate);
258
265 bool DestroyAudioObject(AudioObject* pAudioObject);
266
273 bool DestroyAudioObject(const Heph::Guid& audioObjectId);
274
281 bool AudioObjectExists(AudioObject* pAudioObject) const;
282
289 bool AudioObjectExists(const Heph::Guid& audioObjectId) const;
290
298
305 AudioObject* GetAudioObject(const Heph::Guid& audioObjectId);
306
313 AudioObject* GetAudioObject(const std::string& audioObjectName);
314
319 size_t GetAudioObjectCount() const;
320
326
332
338 bool IsCapturePaused() const;
339
345
350 void SetDeviceEnumerationPeriod(uint32_t deviceEnumerationPeriod_ms);
351
358 virtual void SetMasterVolume(double volume) = 0;
359
364 virtual double GetMasterVolume() const = 0;
365
371
377
383
390 void InitializeRender(const AudioChannelLayout& channelLayout, uint32_t sampleRate);
391
398
406 void InitializeRender(AudioDevice* device, const AudioChannelLayout& channelLayout, uint32_t sampleRate);
407
414 virtual void InitializeRender(AudioDevice* device, AudioFormatInfo format) = 0;
415
420 virtual void StopRendering() = 0;
421
427
434 void InitializeCapture(const AudioChannelLayout& channelLayout, uint32_t sampleRate);
435
442
450 void InitializeCapture(AudioDevice* device, const AudioChannelLayout& channelLayout, uint32_t sampleRate);
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:124
stores encoded audio data.
Definition EncodedAudioBuffer.h:15
base class for the classes that interact with the native audio APIs.
Definition NativeAudio.h:29
std::thread captureThread
Definition NativeAudio.h:84
virtual ~NativeAudio()=default
std::string captureDeviceId
Definition NativeAudio.h:102
void InitializeCapture(const AudioChannelLayout &channelLayout, uint32_t sampleRate)
virtual double GetMasterVolume() const =0
AudioObject * Play(const std::filesystem::path &filePath)
AudioObject * GetAudioObject(const std::string &audioObjectName)
Heph::Event OnAudioDeviceAdded
Definition NativeAudio.h:163
bool disposing
Definition NativeAudio.h:120
std::thread renderThread
Definition NativeAudio.h:78
std::shared_ptr< IAudioDecoder > pAudioDecoder
Definition NativeAudio.h:48
const AudioFormatInfo & GetCaptureFormat() const
virtual void StopRendering()=0
std::thread deviceThread
Definition NativeAudio.h:90
bool AudioObjectExists(const Heph::Guid &audioObjectId) const
AudioFormatInfo captureFormat
Definition NativeAudio.h:114
bool isCaptureInitialized
Definition NativeAudio.h:132
bool isRenderInitialized
Definition NativeAudio.h:126
bool AudioObjectExists(AudioObject *pAudioObject) const
Heph::Event OnCapture
Definition NativeAudio.h:175
uint32_t GetDeviceEnumerationPeriod() const
virtual void InitializeCapture(AudioDevice *device, AudioFormatInfo format)=0
bool isCapturePaused
Definition NativeAudio.h:138
Heph::Event OnAudioDeviceRemoved
Definition NativeAudio.h:169
virtual void SetMasterVolume(double volume)=0
std::thread::id mainThreadId
Definition NativeAudio.h:72
size_t GetAudioObjectCount() const
void InitializeCapture(AudioDevice *device, const AudioChannelLayout &channelLayout, uint32_t sampleRate)
AudioObject * Play(const std::filesystem::path &filePath, uint32_t playCount)
AudioDevice GetRenderDevice() const
std::string renderDeviceId
Definition NativeAudio.h:96
std::vector< AudioDevice > audioDevices
Definition NativeAudio.h:66
AudioObject * Load(const std::filesystem::path &filePath)
AudioObject * Load(const std::filesystem::path &filePath, uint32_t playCount)
virtual bool EnumerateAudioDevices()=0
virtual double GetFinalAOVolume(AudioObject *pAudioObject) const
void InitializeCapture(AudioFormatInfo format)
void InitializeRender(AudioDevice *device, const AudioChannelLayout &channelLayout, uint32_t sampleRate)
void InitializeRender(const AudioChannelLayout &channelLayout, uint32_t sampleRate)
std::shared_ptr< IAudioEncoder > pAudioEncoder
Definition NativeAudio.h:54
AudioObject * GetAudioObject(const Heph::Guid &audioObjectId)
std::vector< AudioDevice > GetAudioDevices(AudioDeviceType deviceType) const
std::mutex audioDevicesMutex
Definition NativeAudio.h:150
virtual void StopCapturing()=0
AudioDevice GetDefaultAudioDevice(AudioDeviceType deviceType) const
void SetAudioDecoder(std::shared_ptr< IAudioDecoder > pNewDecoder)
AudioObject * CreateAudioObject(const std::string &name, size_t bufferFrameCount, AudioChannelLayout channelLayout, uint32_t sampleRate)
EncodedAudioBuffer Mix(uint32_t frameCount)
virtual void InitializeRender(AudioDevice *device, AudioFormatInfo format)=0
bool DestroyAudioObject(AudioObject *pAudioObject)
std::list< AudioObject > audioObjects
Definition NativeAudio.h:60
AudioFormatInfo renderFormat
Definition NativeAudio.h:108
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:144
std::recursive_mutex audioObjectsMutex
Definition NativeAudio.h:156
std::shared_ptr< IAudioEncoder > GetAudioEncoder() const
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:337
stores information about an audio device.
Definition AudioDevice.h:60
stores the properties of the audio signals
Definition AudioFormatInfo.h:20
stores information that's necessary to play audio.
Definition AudioObject.h:33
base class for storing the native audio API specific parameters.
Definition NativeAudioParams.h:15
struct for storing globally unique identifiers.
Definition Guid.h:14