You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
6.2KB

  1. #pragma once
  2. #include <vector>
  3. #include <set>
  4. #include <mutex>
  5. #include <jansson.h>
  6. #include <common.hpp>
  7. #include <context.hpp>
  8. namespace rack {
  9. /** Abstraction for all audio drivers in Rack */
  10. namespace audio {
  11. ////////////////////
  12. // Driver
  13. ////////////////////
  14. struct Device;
  15. struct Port;
  16. /** Wraps an audio driver API containing any number of audio devices.
  17. */
  18. struct Driver {
  19. virtual ~Driver() {}
  20. /** Returns the name of the driver. E.g. "ALSA". */
  21. virtual std::string getName() {
  22. return "";
  23. }
  24. /** Returns a list of all device IDs that can be subscribed to. */
  25. virtual std::vector<int> getDeviceIds() {
  26. return {};
  27. }
  28. /** Returns the default device to use when the driver is selected, or -1 for none. */
  29. virtual int getDefaultDeviceId() {
  30. return -1;
  31. }
  32. /** Returns the name of a device without obtaining it. */
  33. virtual std::string getDeviceName(int deviceId) {
  34. return "";
  35. }
  36. /** Returns the number of inputs of a device without obtaining it. */
  37. virtual int getDeviceNumInputs(int deviceId) {
  38. return 0;
  39. }
  40. /** Returns the number of output of a device without obtaining it. */
  41. virtual int getDeviceNumOutputs(int deviceId) {
  42. return 0;
  43. }
  44. /** Adds the given port as a reference holder of a device and returns the it.
  45. Creates the Device if no ports are subscribed before calling.
  46. */
  47. virtual Device* subscribe(int deviceId, Port* port) {
  48. return NULL;
  49. }
  50. /** Removes the give port as a reference holder of a device.
  51. Deletes the Device if no ports are subscribed after calling.
  52. */
  53. virtual void unsubscribe(int deviceId, Port* port) {}
  54. };
  55. ////////////////////
  56. // Device
  57. ////////////////////
  58. /** A single audio device of a driver API.
  59. Modules and the UI should not interact with this API directly. Use Port instead.
  60. Methods throw `rack::Exception` if the driver API has an exception.
  61. */
  62. struct Device {
  63. std::set<Port*> subscribed;
  64. /** Ensures that ports do not subscribe/unsubscribe while processBuffer() is called. */
  65. std::mutex processMutex;
  66. virtual ~Device() {}
  67. virtual std::string getName() {
  68. return "";
  69. }
  70. virtual int getNumInputs() {
  71. return 0;
  72. }
  73. virtual int getNumOutputs() {
  74. return 0;
  75. }
  76. /** Returns a list of all valid (user-selectable) sample rates.
  77. The device may accept sample rates not in this list, but it *must* accept sample rates in the list.
  78. */
  79. virtual std::set<float> getSampleRates() {
  80. return {};
  81. }
  82. /** Returns the current sample rate. */
  83. virtual float getSampleRate() {
  84. return 0;
  85. }
  86. /** Sets the sample rate of the device, re-opening it if needed. */
  87. virtual void setSampleRate(float sampleRate) {}
  88. /** Returns a list of all valid (user-selectable) block sizes.
  89. The device may accept block sizes not in this list, but it *must* accept block sizes in the list.
  90. */
  91. virtual std::set<int> getBlockSizes() {
  92. return {};
  93. }
  94. /** Returns the current block size. */
  95. virtual int getBlockSize() {
  96. return 0;
  97. }
  98. /** Sets the block size of the device, re-opening it if needed. */
  99. virtual void setBlockSize(int blockSize) {}
  100. /** Adds Port to set of subscribed Ports.
  101. Called by Driver::subscribe().
  102. */
  103. virtual void subscribe(Port* port);
  104. /** Removes Port from set of subscribed Ports.
  105. Called by Driver::unsubscribe().
  106. */
  107. virtual void unsubscribe(Port* port);
  108. /** Processes audio for each subscribed Port.
  109. Called by driver code.
  110. `input` and `output` must be non-overlapping.
  111. Overwrites all `output`, so it is unnecessary to initialize.
  112. */
  113. void processBuffer(const float* input, int inputStride, float* output, int outputStride, int frames);
  114. /** Called by driver code when stream starts. */
  115. void onStartStream();
  116. /** Called by driver code when stream stops. */
  117. void onStopStream();
  118. };
  119. ////////////////////
  120. // Port
  121. ////////////////////
  122. /** A handle to a Device, typically owned by modules to have shared access to a single Device.
  123. All Port methods safely wrap Drivers methods.
  124. That is, if the active Device throws a `rack::Exception`, it is caught and logged inside all Port methods, so they do not throw exceptions.
  125. */
  126. struct Port {
  127. /** The first channel index of the device to process. */
  128. int inputOffset = 0;
  129. int outputOffset = 0;
  130. /** Maximum number of channels to process. */
  131. int maxInputs = 8;
  132. int maxOutputs = 8;
  133. // private
  134. int driverId = -1;
  135. int deviceId = -1;
  136. /** Not owned */
  137. Driver* driver = NULL;
  138. Device* device = NULL;
  139. Context* context;
  140. Port();
  141. virtual ~Port();
  142. void reset();
  143. Driver* getDriver();
  144. int getDriverId();
  145. void setDriverId(int driverId);
  146. std::string getDriverName();
  147. Device* getDevice();
  148. std::vector<int> getDeviceIds();
  149. int getDeviceId();
  150. void setDeviceId(int deviceId);
  151. int getDeviceNumInputs(int deviceId);
  152. int getDeviceNumOutputs(int deviceId);
  153. std::string getDeviceName(int deviceId);
  154. std::string getDeviceDetail(int deviceId, int offset);
  155. std::set<float> getSampleRates();
  156. float getSampleRate();
  157. void setSampleRate(float sampleRate);
  158. std::set<int> getBlockSizes();
  159. int getBlockSize();
  160. void setBlockSize(int blockSize);
  161. /** Returns the number of active Port inputs, considering inputOffset and maxInputs. */
  162. int getNumInputs();
  163. int getNumOutputs();
  164. json_t* toJson();
  165. void fromJson(json_t* rootJ);
  166. /** Callback for processing the audio stream.
  167. `inputStride` and `outputStride` are the number of array elements between frames in the buffers.
  168. */
  169. virtual void processBuffer(const float* input, int inputStride, float* output, int outputStride, int frames) {}
  170. /** Called before processBuffer() is called for all Ports of the same device.
  171. Splitting the processBuffer() into these calls is useful for synchronizing Ports of the same device.
  172. Called even if there are no inputs.
  173. */
  174. virtual void processInput(const float* input, int inputStride, int frames) {}
  175. /** Called after processBuffer() is called for all Ports of the same device.
  176. */
  177. virtual void processOutput(float* output, int outputStride, int frames) {}
  178. virtual void onStartStream() {}
  179. virtual void onStopStream() {}
  180. };
  181. PRIVATE void init();
  182. PRIVATE void destroy();
  183. /** Registers a new audio driver. Takes pointer ownership.
  184. Driver ID is stored in patches and must be unique. -1 is reserved.
  185. */
  186. void addDriver(int driverId, Driver* driver);
  187. std::vector<int> getDriverIds();
  188. Driver* getDriver(int driverId);
  189. } // namespace audio
  190. } // namespace rack