VAE - Virtual Audio Engine 1
Small Data Driven Audio Engine
vae::core::DeviceRtaudio Class Referencefinal

#include <vae_rtaudio.hpp>

Inheritance diagram for vae::core::DeviceRtaudio:
Collaboration diagram for vae::core::DeviceRtaudio:

Public Member Functions

 DeviceRtaudio (Backend &backend, const EngineConfig &config)
 
 ~DeviceRtaudio ()
 
bool openDevice (DeviceInfo &device) override
 Opens a specific audio device. More...
 
bool closeDevice () override
 Closes the currently open device. More...
 
- Public Member Functions inherited from vae::core::Device
 Device (Backend &backend, const EngineConfig &config)
 Only a Backend can construct a Device. More...
 
virtual ~Device ()
 
void setCallback (Callback callback)
 
virtual bool openDevice (bool input=false)
 Tries to open the default audio device whith desired in out channels. More...
 
Size push (const ScratchBuffer &buffer)
 Push samples to the audio device. More...
 
Size canPush () const
 Return amount of audio frames which can be pushed in buffer ! this is an estimate when resampling ! More...
 
void pop (ScratchBuffer &buffer)
 Get samples form audio device. More...
 
Size canPop () const
 
Size getChannelsOut () const
 
Size getChannelsIn () const
 
Size getSampleRate () const
 Get the sample rate. More...
 
Size getRealSampleRate () const
 Get the Real Sample Rate before resampling. More...
 
size_t getStreamTime () const
 
Size getOverruns () const
 
Size getUnderruns () const
 

Private Member Functions

bool cleanUp ()
 

Static Private Member Functions

static int AudioCallback (void *out, void *in, unsigned int frames, double streamTime, RtAudioStreamStatus status, void *data)
 Function called from the RtAudio thread. More...
 

Private Attributes

RtAudio mAudio
 

Additional Inherited Members

- Public Types inherited from vae::core::Device
using Resampler = tklb::ResamplerTpl< Sample, ScratchBuffer >
 
- Protected Types inherited from vae::core::Device
using Callback = tklb::Delegate< void(Device *)>
 
- Protected Member Functions inherited from vae::core::Device
void init (Size sampleRate, Uchar channelsIn, Uchar channelsOut, Size bufferSize)
 initializes buffers, queues and resamplers if needed Has to be called in openDevice once the samplerate and channel config is known More...
 
void postInit ()
 
- Protected Attributes inherited from vae::core::Device
BackendmBackend
 
const EngineConfigmConfig
 
Size mSampleRate = 0
 
Size mRealSampleRate = 0
 
Size mOverruns = 0
 
Size mUnderruns = 0
 
Resampler mResamplerToDevice
 
ScratchBuffer mResamplerBufferToDevice
 
AudioThreadWorker mWorker
 
- Static Protected Attributes inherited from vae::core::Device
static constexpr int _VAE_WORKER_SIZE = sizeof(AudioThreadWorker)
 

Detailed Description

Definition at line 12 of file vae_rtaudio.hpp.

Constructor & Destructor Documentation

◆ DeviceRtaudio()

vae::core::DeviceRtaudio::DeviceRtaudio ( Backend backend,
const EngineConfig config 
)
inline

Definition at line 50 of file vae_rtaudio.hpp.

52 : Device(backend, config) { };
Device(Backend &backend, const EngineConfig &config)
Only a Backend can construct a Device.
Definition: vae_device.hpp:184

◆ ~DeviceRtaudio()

vae::core::DeviceRtaudio::~DeviceRtaudio ( )
inline

Definition at line 54 of file vae_rtaudio.hpp.

54 {
55 cleanUp();
56 }
Here is the call graph for this function:

Member Function Documentation

◆ AudioCallback()

static int vae::core::DeviceRtaudio::AudioCallback ( void *  out,
void *  in,
unsigned int  frames,
double  streamTime,
RtAudioStreamStatus  status,
void *  data 
)
inlinestaticprivate

Function called from the RtAudio thread.

Definition at line 35 of file vae_rtaudio.hpp.

38 {
39 (void) streamTime; // Prevent unused variable warnings.
40 (void) status; // Prevent unused variable warnings.
41 auto inFloat = static_cast<const float*>(in);
42 auto outFloat = static_cast<float*>(out);
43 static_cast<AudioThreadWorker*>(data)
44 ->swapBufferInterleaved(inFloat, outFloat, frames);
45 return 0;
46 }
Here is the caller graph for this function:

◆ cleanUp()

bool vae::core::DeviceRtaudio::cleanUp ( )
inlineprivate

Definition at line 15 of file vae_rtaudio.hpp.

15 {
16 VAE_PROFILER_SCOPE_NAMED("Cleanup RtAudio")
17 if (mAudio.isStreamRunning()) {
18 VAE_PROFILER_SCOPE_NAMED("Stop RtAudio Stream")
19 auto result = mAudio.stopStream();
20 if (result != RTAUDIO_NO_ERROR) {
21 VAE_ASSERT(false)
22 return false;
23 }
24 }
25 if (mAudio.isStreamOpen()) {
26 VAE_PROFILER_SCOPE_NAMED("Close RtAudio Stream")
27 mAudio.closeStream();
28 }
29 return true;
30 }
#define VAE_PROFILER_SCOPE_NAMED(name)
Profiles a scope and names it.
#define VAE_ASSERT(condition)
Definition: vae_util.hpp:11
Here is the caller graph for this function:

◆ closeDevice()

bool vae::core::DeviceRtaudio::closeDevice ( )
inlineoverridevirtual

Closes the currently open device.

Otherwise does nothing.

Implements vae::core::Device.

Definition at line 114 of file vae_rtaudio.hpp.

114{ return cleanUp(); }
Here is the call graph for this function:

◆ openDevice()

bool vae::core::DeviceRtaudio::openDevice ( DeviceInfo device)
inlineoverridevirtual

Opens a specific audio device.

The device struct may be altered to match the actual hardware. (sampleRate, bufferSize and channel count)

Implements vae::core::Device.

Definition at line 58 of file vae_rtaudio.hpp.

58 {
59 if (mBackend.getDeviceCount() < Size(device.id)) {
60 VAE_ERROR("Failed to open deivce with index out of bounds")
61 return false;
62 }
63 device.channelsIn = tklb::clamp<int>(device.channelsIn, 0, StaticConfig::MaxChannels);
64 device.channelsOut = tklb::clamp<int>(device.channelsOut, 0, StaticConfig::MaxChannels);
65 RtAudio::StreamParameters inParams, outParams;
66 inParams.deviceId = device.id;
67 inParams.nChannels = device.channelsIn;
68 outParams.deviceId = device.id;
69 outParams.nChannels = device.channelsOut;
70 if (device.bufferSize == 0) {
71 device.bufferSize = mConfig.preferredBufferSize;
72 }
73
74 RtAudioErrorType result;
75 {
76 VAE_PROFILER_SCOPE_NAMED("Open RtAudio stream")
77 result = mAudio.openStream(
78 outParams.nChannels ? &outParams : nullptr,
79 inParams.nChannels ? &inParams : nullptr,
80 RTAUDIO_FLOAT32, device.sampleRate,
81 &device.bufferSize, &AudioCallback, &mWorker
82 );
83 }
84
85 if (result != RTAUDIO_NO_ERROR) {
86 VAE_ERROR("Failed to open RtAudio device with code %i", result)
87 return false;
88 }
89
90 device.sampleRate = mAudio.getStreamSampleRate();
91 device.channelsIn = inParams.nChannels;
92 device.channelsOut = outParams.nChannels;
93
94 init(
95 device.sampleRate, device.channelsIn, device.channelsOut,
96 device.bufferSize // RtAudio writes back to this on openStream
97 );
98 {
99 VAE_PROFILER_SCOPE_NAMED("Start RtAudio stream")
100 result = mAudio.startStream();
101 }
102
103 if (result != RTAUDIO_NO_ERROR) {
104 VAE_ERROR("Failed to open RtAudio device with code %i", result)
105 cleanUp();
106 return false;
107 }
108
109 postInit();
110
111 return true;
112 }
virtual Size getDeviceCount()=0
Gets number of devices, needed to iterate them.
AudioThreadWorker mWorker
Definition: vae_device.hpp:102
const EngineConfig & mConfig
Definition: vae_device.hpp:24
void init(Size sampleRate, Uchar channelsIn, Uchar channelsOut, Size bufferSize)
initializes buffers, queues and resamplers if needed Has to be called in openDevice once the samplera...
Definition: vae_device.hpp:114
Backend & mBackend
Definition: vae_device.hpp:23
static int AudioCallback(void *out, void *in, unsigned int frames, double streamTime, RtAudioStreamStatus status, void *data)
Function called from the RtAudio thread.
Definition: vae_rtaudio.hpp:35
Using template interfaces isn't an option so this simply defines a default Convolver type.
Definition: TAllocator.hpp:15
T clamp(const T &v, const T &_min, const T &_max)
Definition: TMath.hpp:26
constexpr unsigned char MaxChannels
Maximum channel count used to pre allocate buffers.
Definition: vae.hpp:268
unsigned int Size
How the elements are addressed in the heapbuffer.
Definition: vae.hpp:33
Size preferredBufferSize
Buffer size that will be requested from device.
Definition: vae.hpp:233
#define VAE_ERROR(msg,...)
Definition: vae_logger.hpp:80
Here is the call graph for this function:

Member Data Documentation

◆ mAudio

RtAudio vae::core::DeviceRtaudio::mAudio
private

Definition at line 13 of file vae_rtaudio.hpp.


The documentation for this class was generated from the following file: