VCV Rack API v2
Loading...
Searching...
No Matches
audio.hpp
Go to the documentation of this file.
1#pragma once
2#include <vector>
3#include <set>
4#include <mutex>
5
6#include <jansson.h>
7
8#include <common.hpp>
9#include <context.hpp>
10
11namespace rack {
13namespace audio {
14
15
17// Driver
19
20struct Device;
21struct Port;
22
25struct Driver {
26 virtual ~Driver() {}
28 virtual std::string getName() {
29 return "";
30 }
32 virtual std::vector<int> getDeviceIds() {
33 return {};
34 }
36 virtual int getDefaultDeviceId() {
37 return -1;
38 }
40 virtual std::string getDeviceName(int deviceId) {
41 return "";
42 }
44 virtual int getDeviceNumInputs(int deviceId) {
45 return 0;
46 }
48 virtual int getDeviceNumOutputs(int deviceId) {
49 return 0;
50 }
51
55 virtual Device* subscribe(int deviceId, Port* port) {
56 return NULL;
57 }
61 virtual void unsubscribe(int deviceId, Port* port) {}
62};
63
65// Device
67
74struct Device {
75 std::set<Port*> subscribed;
77 std::mutex processMutex;
78
79 virtual ~Device() {}
80
81 virtual std::string getName() {
82 return "";
83 }
84 virtual int getNumInputs() {
85 return 0;
86 }
87 virtual int getNumOutputs() {
88 return 0;
89 }
90
94 virtual std::set<float> getSampleRates() {
95 return {};
96 }
98 virtual float getSampleRate() {
99 return 0;
100 }
102 virtual void setSampleRate(float sampleRate) {}
103
107 virtual std::set<int> getBlockSizes() {
108 return {};
109 }
111 virtual int getBlockSize() {
112 return 0;
113 }
115 virtual void setBlockSize(int blockSize) {}
116
120 virtual void subscribe(Port* port);
124 virtual void unsubscribe(Port* port);
125
131 void processBuffer(const float* input, int inputStride, float* output, int outputStride, int frames);
136};
137
139// Port
141
147struct Port {
149 int inputOffset = 0;
152 int maxInputs = 8;
153 int maxOutputs = 8;
154
155 // private
156 int driverId = -1;
157 int deviceId = -1;
159 Driver* driver = NULL;
160 Device* device = NULL;
162
164 virtual ~Port();
165 void reset();
166
170 std::string getDriverName();
171
173 std::vector<int> getDeviceIds();
178 std::string getDeviceName(int deviceId);
179 std::string getDeviceDetail(int deviceId, int offset);
180
181 std::set<float> getSampleRates();
183 void setSampleRate(float sampleRate);
184
185 std::set<int> getBlockSizes();
187 void setBlockSize(int blockSize);
188
192
193 json_t* toJson();
194 void fromJson(json_t* rootJ);
195
199 virtual void processBuffer(const float* input, int inputStride, float* output, int outputStride, int frames) {}
204 virtual void processInput(const float* input, int inputStride, int frames) {}
207 virtual void processOutput(float* output, int outputStride, int frames) {}
208 virtual void onStartStream() {}
209 virtual void onStopStream() {}
210};
211
212
218void addDriver(int driverId, Driver* driver);
219std::vector<int> getDriverIds();
220Driver* getDriver(int driverId);
221
222
223} // namespace audio
224} // namespace rack
#define PRIVATE
Attribute for private functions not intended to be called by plugins.
Definition common.hpp:32
std::vector< int > getDriverIds()
PRIVATE void init()
Driver * getDriver(int driverId)
PRIVATE void destroy()
void addDriver(int driverId, Driver *driver)
Registers a new audio driver.
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
Rack instance state.
Definition context.hpp:45
A single audio device of a driver API.
Definition audio.hpp:74
virtual ~Device()
Definition audio.hpp:79
virtual std::string getName()
Definition audio.hpp:81
virtual void setSampleRate(float sampleRate)
Sets the sample rate of the device, re-opening it if needed.
Definition audio.hpp:102
void onStartStream()
Called by driver code when stream starts.
virtual int getNumOutputs()
Definition audio.hpp:87
void processBuffer(const float *input, int inputStride, float *output, int outputStride, int frames)
Processes audio for each subscribed Port.
virtual std::set< int > getBlockSizes()
Returns a list of all valid (user-selectable) block sizes.
Definition audio.hpp:107
virtual void setBlockSize(int blockSize)
Sets the block size of the device, re-opening it if needed.
Definition audio.hpp:115
void onStopStream()
Called by driver code when stream stops.
virtual float getSampleRate()
Returns the current sample rate.
Definition audio.hpp:98
virtual int getNumInputs()
Definition audio.hpp:84
std::mutex processMutex
Ensures that ports do not subscribe/unsubscribe while processBuffer() is called.
Definition audio.hpp:77
std::set< Port * > subscribed
Definition audio.hpp:75
virtual std::set< float > getSampleRates()
Returns a list of all valid (user-selectable) sample rates.
Definition audio.hpp:94
virtual void subscribe(Port *port)
Adds Port to set of subscribed Ports.
virtual int getBlockSize()
Returns the current block size.
Definition audio.hpp:111
virtual void unsubscribe(Port *port)
Removes Port from set of subscribed Ports.
Wraps an audio driver API containing any number of audio devices.
Definition audio.hpp:25
virtual ~Driver()
Definition audio.hpp:26
virtual Device * subscribe(int deviceId, Port *port)
Adds the given port as a reference holder of a device and returns the it.
Definition audio.hpp:55
virtual std::string getDeviceName(int deviceId)
Returns the name of a device without obtaining it.
Definition audio.hpp:40
virtual std::string getName()
Returns the name of the driver.
Definition audio.hpp:28
virtual int getDeviceNumOutputs(int deviceId)
Returns the number of output of a device without obtaining it.
Definition audio.hpp:48
virtual std::vector< int > getDeviceIds()
Returns a list of all device IDs that can be subscribed to.
Definition audio.hpp:32
virtual void unsubscribe(int deviceId, Port *port)
Removes the give port as a reference holder of a device.
Definition audio.hpp:61
virtual int getDefaultDeviceId()
Returns the default device to use when the driver is selected, or -1 for none.
Definition audio.hpp:36
virtual int getDeviceNumInputs(int deviceId)
Returns the number of inputs of a device without obtaining it.
Definition audio.hpp:44
A handle to a Device, typically owned by modules to have shared access to a single Device.
Definition audio.hpp:147
std::string getDeviceDetail(int deviceId, int offset)
int maxOutputs
Definition audio.hpp:153
virtual void processInput(const float *input, int inputStride, int frames)
Called before processBuffer() is called for all Ports of the same device.
Definition audio.hpp:204
Context * context
Definition audio.hpp:161
std::set< float > getSampleRates()
Driver * getDriver()
void setSampleRate(float sampleRate)
json_t * toJson()
void setDeviceId(int deviceId)
int inputOffset
The first channel index of the device to process.
Definition audio.hpp:149
virtual void processBuffer(const float *input, int inputStride, float *output, int outputStride, int frames)
Callback for processing the audio stream.
Definition audio.hpp:199
std::set< int > getBlockSizes()
Device * getDevice()
std::vector< int > getDeviceIds()
void fromJson(json_t *rootJ)
virtual void onStartStream()
Definition audio.hpp:208
int outputOffset
Definition audio.hpp:150
Device * device
Definition audio.hpp:160
void setDriverId(int driverId)
virtual void processOutput(float *output, int outputStride, int frames)
Called after processBuffer() is called for all Ports of the same device.
Definition audio.hpp:207
int getDeviceNumInputs(int deviceId)
int deviceId
Definition audio.hpp:157
Driver * driver
Not owned.
Definition audio.hpp:159
int getDeviceNumOutputs(int deviceId)
void setBlockSize(int blockSize)
int getNumInputs()
Returns the number of active Port inputs, considering inputOffset and maxInputs.
std::string getDriverName()
std::string getDeviceName(int deviceId)
int maxInputs
Maximum number of channels to process.
Definition audio.hpp:152
virtual void onStopStream()
Definition audio.hpp:209
int driverId
Definition audio.hpp:156