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