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.

195 lines
4.0KB

  1. #pragma once
  2. #include <common.hpp>
  3. #include <context.hpp>
  4. #include <jansson.h>
  5. #include <vector>
  6. #include <set>
  7. namespace rack {
  8. /** Audio driver
  9. */
  10. namespace audio {
  11. ////////////////////
  12. // Driver
  13. ////////////////////
  14. struct Device;
  15. struct Port;
  16. struct Driver {
  17. virtual ~Driver() {}
  18. virtual std::string getName() {
  19. return "";
  20. }
  21. virtual std::vector<int> getDeviceIds() {
  22. return {};
  23. }
  24. /** Gets the name of a device without subscribing to it. */
  25. virtual std::string getDeviceName(int deviceId) {
  26. return "";
  27. }
  28. virtual int getDeviceNumInputs(int deviceId) {
  29. return 0;
  30. }
  31. virtual int getDeviceNumOutputs(int deviceId) {
  32. return 0;
  33. }
  34. std::string getDeviceDetail(int deviceId, int offset, int maxChannels);
  35. virtual Device* subscribe(int deviceId, Port* port) {
  36. return NULL;
  37. }
  38. virtual void unsubscribe(int deviceId, Port* port) {}
  39. };
  40. ////////////////////
  41. // Device
  42. ////////////////////
  43. struct Device {
  44. std::set<Port*> subscribed;
  45. virtual ~Device() {}
  46. // Called by Driver::subscribe().
  47. void subscribe(Port* port);
  48. void unsubscribe(Port* port);
  49. virtual std::string getName() {
  50. return "";
  51. }
  52. virtual int getNumInputs() {
  53. return 0;
  54. }
  55. virtual int getNumOutputs() {
  56. return 0;
  57. }
  58. std::string getDetail(int offset, int maxChannels);
  59. virtual std::vector<int> getSampleRates() {
  60. return {};
  61. }
  62. virtual int getSampleRate() {
  63. return 0;
  64. }
  65. virtual void setSampleRate(int sampleRate) {}
  66. virtual std::vector<int> getBlockSizes() {
  67. return {};
  68. }
  69. virtual int getBlockSize() {
  70. return 0;
  71. }
  72. virtual void setBlockSize(int blockSize) {}
  73. // Called by this Device class, forwards to subscribed Ports.
  74. void processBuffer(const float* input, int inputStride, float* output, int outputStride, int frames);
  75. void onOpenStream();
  76. void onCloseStream();
  77. };
  78. ////////////////////
  79. // Port
  80. ////////////////////
  81. struct Port {
  82. /** The first channel index of the device to process. */
  83. int offset = 0;
  84. /** Maximum number of channels to process. */
  85. int maxChannels = 8;
  86. // private
  87. int driverId = -1;
  88. int deviceId = -1;
  89. /** Not owned */
  90. Driver* driver = NULL;
  91. Device* device = NULL;
  92. Context* context;
  93. Port();
  94. virtual ~Port();
  95. void reset();
  96. Driver* getDriver() {
  97. return driver;
  98. }
  99. int getDriverId() {
  100. return driverId;
  101. }
  102. void setDriverId(int driverId);
  103. Device* getDevice() {
  104. return device;
  105. }
  106. int getDeviceId() {
  107. return deviceId;
  108. }
  109. void setDeviceId(int deviceId);
  110. std::vector<int> getSampleRates() {
  111. if (!device)
  112. return {};
  113. return device->getSampleRates();
  114. }
  115. int getSampleRate() {
  116. if (!device)
  117. return 0;
  118. return device->getSampleRate();
  119. }
  120. void setSampleRate(int sampleRate) {
  121. if (device)
  122. device->setSampleRate(sampleRate);
  123. }
  124. std::vector<int> getBlockSizes() {
  125. if (!device)
  126. return {};
  127. return device->getBlockSizes();
  128. }
  129. int getBlockSize() {
  130. if (!device)
  131. return 0;
  132. return device->getBlockSize();
  133. }
  134. void setBlockSize(int blockSize) {
  135. if (device)
  136. device->setBlockSize(blockSize);
  137. }
  138. int getNumInputs();
  139. int getNumOutputs();
  140. json_t* toJson();
  141. void fromJson(json_t* rootJ);
  142. /** Callback for processing the audio stream.
  143. `inputStride` and `outputStride` are the number of array elements between frames in the buffers.
  144. */
  145. virtual void processBuffer(const float* input, int inputStride, float* output, int outputStride, int frames) {}
  146. /** Called before processBuffer() is called for all Ports of the same device.
  147. Splitting the processBuffer() into these calls is useful for synchronizing Ports of the same device.
  148. Called even if there are no inputs.
  149. */
  150. virtual void processInput(const float* input, int inputStride, int frames) {}
  151. /** Called after processBuffer() is called for all Ports of the same device.
  152. */
  153. virtual void processOutput(float* output, int outputStride, int frames) {}
  154. virtual void onOpenStream() {}
  155. virtual void onCloseStream() {}
  156. };
  157. void init();
  158. void destroy();
  159. /** Registers a new audio driver. Takes pointer ownership. */
  160. void addDriver(int driverId, Driver* driver);
  161. std::vector<int> getDriverIds();
  162. Driver* getDriver(int driverId);
  163. } // namespace audio
  164. } // namespace rack