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.

audio.hpp 5.7KB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #pragma once
  2. #include <vector>
  3. #include <set>
  4. #include <jansson.h>
  5. #include <common.hpp>
  6. #include <context.hpp>
  7. namespace rack {
  8. /** Abstraction for all audio drivers in Rack
  9. */
  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. virtual ~Device() {}
  65. // Called by Driver::subscribe().
  66. void subscribe(Port* port);
  67. void unsubscribe(Port* port);
  68. virtual std::string getName() {
  69. return "";
  70. }
  71. virtual int getNumInputs() {
  72. return 0;
  73. }
  74. virtual int getNumOutputs() {
  75. return 0;
  76. }
  77. /** Returns a list of all valid (user-selectable) sample rates.
  78. The device may accept sample rates not in this list, but it *must* accept sample rates in the list.
  79. */
  80. virtual std::set<float> getSampleRates() {
  81. return {};
  82. }
  83. /** Returns the current sample rate. */
  84. virtual float getSampleRate() {
  85. return 0;
  86. }
  87. /** Sets the sample rate of the device, re-opening it if needed. */
  88. virtual void setSampleRate(float sampleRate) {}
  89. /** Returns a list of all valid (user-selectable) block sizes.
  90. The device may accept block sizes not in this list, but it *must* accept block sizes in the list.
  91. */
  92. virtual std::set<int> getBlockSizes() {
  93. return {};
  94. }
  95. /** Returns the current block size. */
  96. virtual int getBlockSize() {
  97. return 0;
  98. }
  99. /** Sets the block size of the device, re-opening it if needed. */
  100. virtual void setBlockSize(int blockSize) {}
  101. // Called by this Device class, forwards to subscribed Ports.
  102. void processBuffer(const float* input, int inputStride, float* output, int outputStride, int frames);
  103. void onStartStream();
  104. void onStopStream();
  105. };
  106. ////////////////////
  107. // Port
  108. ////////////////////
  109. /** A handle to a Device, typically owned by modules to have shared access to a single Device.
  110. All Port methods safely wrap Drivers methods.
  111. 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.
  112. */
  113. struct Port {
  114. /** The first channel index of the device to process. */
  115. int inputOffset = 0;
  116. int outputOffset = 0;
  117. /** Maximum number of channels to process. */
  118. int maxInputs = 8;
  119. int maxOutputs = 8;
  120. // private
  121. int driverId = -1;
  122. int deviceId = -1;
  123. /** Not owned */
  124. Driver* driver = NULL;
  125. Device* device = NULL;
  126. Context* context;
  127. Port();
  128. virtual ~Port();
  129. void reset();
  130. Driver* getDriver();
  131. int getDriverId();
  132. void setDriverId(int driverId);
  133. std::string getDriverName();
  134. Device* getDevice();
  135. std::vector<int> getDeviceIds();
  136. int getDeviceId();
  137. void setDeviceId(int deviceId);
  138. int getDeviceNumInputs(int deviceId);
  139. int getDeviceNumOutputs(int deviceId);
  140. std::string getDeviceName(int deviceId);
  141. std::string getDeviceDetail(int deviceId, int offset);
  142. std::set<float> getSampleRates();
  143. float getSampleRate();
  144. void setSampleRate(float sampleRate);
  145. std::set<int> getBlockSizes();
  146. int getBlockSize();
  147. void setBlockSize(int blockSize);
  148. /** Returns the number of active Port inputs, considering inputOffset and maxInputs. */
  149. int getNumInputs();
  150. int getNumOutputs();
  151. json_t* toJson();
  152. void fromJson(json_t* rootJ);
  153. /** Callback for processing the audio stream.
  154. `inputStride` and `outputStride` are the number of array elements between frames in the buffers.
  155. */
  156. virtual void processBuffer(const float* input, int inputStride, float* output, int outputStride, int frames) {}
  157. /** Called before processBuffer() is called for all Ports of the same device.
  158. Splitting the processBuffer() into these calls is useful for synchronizing Ports of the same device.
  159. Called even if there are no inputs.
  160. */
  161. virtual void processInput(const float* input, int inputStride, int frames) {}
  162. /** Called after processBuffer() is called for all Ports of the same device.
  163. */
  164. virtual void processOutput(float* output, int outputStride, int frames) {}
  165. virtual void onStartStream() {}
  166. virtual void onStopStream() {}
  167. };
  168. void init();
  169. void destroy();
  170. /** Registers a new audio driver. Takes pointer ownership.
  171. Driver ID is stored in patches and must be unique. -1 is reserved.
  172. */
  173. void addDriver(int driverId, Driver* driver);
  174. std::vector<int> getDriverIds();
  175. Driver* getDriver(int driverId);
  176. } // namespace audio
  177. } // namespace rack