VAE - Virtual Audio Engine 1
Small Data Driven Audio Engine
vae::core::Device Class Referenceabstract

Interface for audio devices. More...

#include <vae_device.hpp>

Inheritance diagram for vae::core::Device:
Collaboration diagram for vae::core::Device:

Classes

struct  AudioThreadWorker
 Data shared with audio thread. More...
 

Public Types

using Resampler = tklb::ResamplerTpl< Sample, ScratchBuffer >
 

Public Member Functions

 Device (Backend &backend, const EngineConfig &config)
 Only a Backend can construct a Device. More...
 
virtual ~Device ()
 
void setCallback (Callback callback)
 
virtual bool openDevice (DeviceInfo &device)=0
 Opens a specific audio device. More...
 
virtual bool openDevice (bool input=false)
 Tries to open the default audio device whith desired in out channels. More...
 
virtual bool closeDevice ()=0
 Closes the currently open device. 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
 

Protected Types

using Callback = tklb::Delegate< void(Device *)>
 

Protected Member Functions

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

BackendmBackend
 
const EngineConfigmConfig
 
Size mSampleRate = 0
 
Size mRealSampleRate = 0
 
Size mOverruns = 0
 
Size mUnderruns = 0
 
Resampler mResamplerToDevice
 
ScratchBuffer mResamplerBufferToDevice
 
AudioThreadWorker mWorker
 

Static Protected Attributes

static constexpr int _VAE_WORKER_SIZE = sizeof(AudioThreadWorker)
 

Detailed Description

Interface for audio devices.

Default implementation for resampling already provided.

Definition at line 18 of file vae_device.hpp.

Member Typedef Documentation

◆ Callback

Definition at line 25 of file vae_device.hpp.

◆ Resampler

Constructor & Destructor Documentation

◆ Device()

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

Only a Backend can construct a Device.

Definition at line 184 of file vae_device.hpp.

186 : mBackend(backend), mConfig(config) { }
const EngineConfig & mConfig
Definition: vae_device.hpp:24
Backend & mBackend
Definition: vae_device.hpp:23

◆ ~Device()

virtual vae::core::Device::~Device ( )
inlinevirtual

Definition at line 188 of file vae_device.hpp.

188 {
189 VAE_DEBUG(
190 "Device destructed. Underruns: %i %i Overruns:%i %i",
193 )
194 }
AudioThreadWorker mWorker
Definition: vae_device.hpp:102
#define VAE_DEBUG(msg,...)
Definition: vae_logger.hpp:83

Member Function Documentation

◆ canPop()

Size vae::core::Device::canPop ( ) const
inline

Definition at line 278 of file vae_device.hpp.

278 {
280 }
Size filled() const
Returns how many elements are in the buffer.
Here is the call graph for this function:

◆ canPush()

Size vae::core::Device::canPush ( ) const
inline

Return amount of audio frames which can be pushed in buffer ! this is an estimate when resampling !

Returns
Size

Definition at line 254 of file vae_device.hpp.

254 {
256 auto remaining = mWorker.queueToDevice.remaining();
257 if (mResamplerToDevice.isInitialized()) {
258 return mResamplerToDevice.estimateNeed(remaining);
259 }
260 return remaining;
261 }
Size estimateNeed(const Size out) const
Estimate how many samples need to be put in to get n samples out.
Resampler mResamplerToDevice
Definition: vae_device.hpp:31
#define VAE_PROFILER_SCOPE()
Profiles a scope.
Here is the call graph for this function:

◆ closeDevice()

virtual bool vae::core::Device::closeDevice ( )
pure virtual

Closes the currently open device.

Otherwise does nothing.

Implemented in vae::core::DeviceDummy, vae::core::DevicePortaudio, and vae::core::DeviceRtaudio.

◆ getChannelsIn()

Size vae::core::Device::getChannelsIn ( ) const
inline

Definition at line 284 of file vae_device.hpp.

◆ getChannelsOut()

Size vae::core::Device::getChannelsOut ( ) const
inline

Definition at line 282 of file vae_device.hpp.

282{ return mWorker.channelsOut; }

◆ getOverruns()

Size vae::core::Device::getOverruns ( ) const
inline

Definition at line 301 of file vae_device.hpp.

301{ return mWorker.overruns; }

◆ getRealSampleRate()

Size vae::core::Device::getRealSampleRate ( ) const
inline

Get the Real Sample Rate before resampling.

Returns
Size

Definition at line 297 of file vae_device.hpp.

297{ return mRealSampleRate; }

◆ getSampleRate()

Size vae::core::Device::getSampleRate ( ) const
inline

Get the sample rate.

Returns
samplerate after resampling

Definition at line 290 of file vae_device.hpp.

290{ return mSampleRate; }

◆ getStreamTime()

size_t vae::core::Device::getStreamTime ( ) const
inline

Definition at line 299 of file vae_device.hpp.

◆ getUnderruns()

Size vae::core::Device::getUnderruns ( ) const
inline

Definition at line 302 of file vae_device.hpp.

302{ return mWorker.underruns; }

◆ init()

void vae::core::Device::init ( Size  sampleRate,
Uchar  channelsIn,
Uchar  channelsOut,
Size  bufferSize 
)
inlineprotected

initializes buffers, queues and resamplers if needed Has to be called in openDevice once the samplerate and channel config is known

Parameters
sampleRateThe actual samplerate the device has, might not match requested rate
channelsInThe real channelcount, might not match requested
channelsOutThe real channelcount, might not match requested
bufferSizeThe amount of frames a callback will provide/request

Definition at line 114 of file vae_device.hpp.

114 {
115 VAE_PROFILER_SCOPE_NAMED("Init Device")
116 mWorker.channelsIn = channelsIn;
117 mWorker.channelsOut = channelsOut;
118 mRealSampleRate = sampleRate;
119
120 if (sampleRate != mConfig.internalSampleRate) {
121 if (0 < channelsIn) {
122 // we get full device buffersize for these buffers
124 sampleRate, mConfig.internalSampleRate, bufferSize
125 );
128 channelsIn
129 );
133 }
134 if (0 < channelsOut) {
135 // But only DSP max block size in this direction
138 );
141 channelsOut
142 );
145 }
146 } else {
148 }
149
150 if (0 < channelsIn) {
152 bufferSize * mConfig.bufferPeriods,
153 channelsIn
154 );
155 }
156 if (0 < channelsOut) {
158 bufferSize * mConfig.bufferPeriods,
159 channelsOut
160 );
162 }
163
164 mWorker.convertBuffer.sampleRate = sampleRate;
166 bufferSize,
167 std::max(channelsOut, channelsIn) // buffer is shared for in and out, so make sure it has space for both
168 );
169 }
bool resize(const Size length, uchar channels)
! Will not keep the contents! Resizes the buffer to the desired length and channel count.
ushort sampleRate
Only relevant for resampling and oversampling.
void set(const T2 *samples, Size length, const uchar channel=0, const Size offsetDst=0)
Set a single channel from an array.
Size calculateBufferSize(Size initialSize)
Calculate a buffersize fit for the resampled result.
bool init(uint rateIn, uint rateOut, uint maxBlock=512, uchar maxChannels=2, uchar quality=5)
setup the resampler
ScratchBuffer mResamplerBufferToDevice
Definition: vae_device.hpp:32
T max(const T &v1, const T &v2)
Definition: TMath.hpp:21
constexpr Size MaxBlock
Maximum block size.
Definition: vae.hpp:276
Size internalSampleRate
Samplerate requested from device.
Definition: vae.hpp:168
Size bufferPeriods
Number of blocks/buffers to processed ahead.
Definition: vae.hpp:240
#define VAE_PROFILER_SCOPE_NAMED(name)
Profiles a scope and names it.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ openDevice() [1/2]

virtual bool vae::core::Device::openDevice ( bool  input = false)
inlinevirtual

Tries to open the default audio device whith desired in out channels.

Reimplemented in vae::core::DeviceDummy.

Definition at line 212 of file vae_device.hpp.

212 {
213 DeviceInfo device =
215 return openDevice(device);
216 };
virtual DeviceInfo getDefaultOutputDevice()=0
virtual DeviceInfo getDefaultInputDevice()=0
virtual bool openDevice(DeviceInfo &device)=0
Opens a specific audio device.
Here is the call graph for this function:

◆ openDevice() [2/2]

virtual bool vae::core::Device::openDevice ( DeviceInfo device)
pure virtual

Opens a specific audio device.

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

Implemented in vae::core::DeviceDummy, vae::core::DevicePortaudio, and vae::core::DeviceRtaudio.

Here is the caller graph for this function:

◆ pop()

void vae::core::Device::pop ( ScratchBuffer buffer)
inline

Get samples form audio device.

Parameters
bufferGets the amount of valid samples, might actualy get less

Definition at line 267 of file vae_device.hpp.

267 {
269 VAE_ASSERT(0 < mWorker.channelsIn)
270 auto frames = buffer.validSize();
271 VAE_ASSERT(frames != 0) // need to have valid frames
272 Lock lock(mWorker.mutex);
273 const auto popped = mWorker.queueFromDevice.pop(buffer, frames);
274 mUnderruns += (frames - popped);
275 VAE_PROFILER_PLOT(profiler::engineUnderruns, int64_t(mOverruns));
276 }
void pop(ScratchBuffer &buffer)
Get samples form audio device.
Definition: vae_device.hpp:267
const char *const engineUnderruns
std::unique_lock< Mutex > Lock
Definition: vae_types.hpp:53
#define VAE_PROFILER_PLOT(name, value)
Records a value.
#define VAE_ASSERT(condition)
Definition: vae_util.hpp:11
Here is the call graph for this function:

◆ postInit()

void vae::core::Device::postInit ( )
inlineprotected

Definition at line 171 of file vae_device.hpp.

171 {
172 VAE_DEBUG("Opened Audio Device on %s with samplerate %i", mBackend.getName(), mRealSampleRate)
174 VAE_DEBUG("Audio Device resamples to %i", mSampleRate)
175 }
176 }
virtual const char * getName() const =0
Returns name of the api.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ push()

Size vae::core::Device::push ( const ScratchBuffer buffer)
inline

Push samples to the audio device.

Parameters
bufferPushes the amount of valid samples

Definition at line 228 of file vae_device.hpp.

228 {
230 VAE_ASSERT(0 < mWorker.channelsOut)
231 const auto frames = buffer.validSize();
232 VAE_ASSERT(frames != 0) // need to have valid frames
233 if (mResamplerToDevice.isInitialized()) {
235 Lock lock(mWorker.mutex);
238 return pushed;
239 } else {
240 Lock lock(mWorker.mutex);
241 const auto pushed = mWorker.queueToDevice.push(buffer);
242 mOverruns += (frames - pushed);
243 return pushed;
244 }
246 return 0;
247 }
Size validSize() const
Returns the length of actually valid audio in the buffer.
Size push(const AudioBufferTpl< T2, STORAGE2 > &in, Size offsetSrc=0)
Adds validSize() amount of frames to the buffer.
Size process(const Buffer &in, Buffer &out)
Resample function Make sure the out buffer has enough space.
const char *const engineOverruns
Here is the call graph for this function:

◆ setCallback()

void vae::core::Device::setCallback ( Callback  callback)
inline

Definition at line 196 of file vae_device.hpp.

196 {
197 mWorker.device = this;
198 mWorker.callback = callback;
199 VAE_DEBUG("Device uses sync callback")
200 }
Here is the caller graph for this function:

Member Data Documentation

◆ _VAE_WORKER_SIZE

constexpr int vae::core::Device::_VAE_WORKER_SIZE = sizeof(AudioThreadWorker)
staticconstexprprotected

Definition at line 100 of file vae_device.hpp.

◆ mBackend

Backend& vae::core::Device::mBackend
protected

Definition at line 23 of file vae_device.hpp.

◆ mConfig

const EngineConfig& vae::core::Device::mConfig
protected

Definition at line 24 of file vae_device.hpp.

◆ mOverruns

Size vae::core::Device::mOverruns = 0
protected

Definition at line 28 of file vae_device.hpp.

◆ mRealSampleRate

Size vae::core::Device::mRealSampleRate = 0
protected

Definition at line 27 of file vae_device.hpp.

◆ mResamplerBufferToDevice

ScratchBuffer vae::core::Device::mResamplerBufferToDevice
protected

Definition at line 32 of file vae_device.hpp.

◆ mResamplerToDevice

Resampler vae::core::Device::mResamplerToDevice
protected

Definition at line 31 of file vae_device.hpp.

◆ mSampleRate

Size vae::core::Device::mSampleRate = 0
protected

Definition at line 26 of file vae_device.hpp.

◆ mUnderruns

Size vae::core::Device::mUnderruns = 0
protected

Definition at line 29 of file vae_device.hpp.

◆ mWorker

AudioThreadWorker vae::core::Device::mWorker
protected

Definition at line 102 of file vae_device.hpp.


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