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.

190 lines
3.9KB

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