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.

Port.hpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #pragma once
  2. #include <common.hpp>
  3. #include <engine/Light.hpp>
  4. namespace rack {
  5. namespace engine {
  6. /** This is inspired by the number of MIDI channels. */
  7. static const int PORT_MAX_CHANNELS = 16;
  8. struct Port {
  9. /** Voltage of the port. */
  10. union {
  11. /** Unstable API. Use getVoltage() and setVoltage() instead. */
  12. float voltages[PORT_MAX_CHANNELS] = {};
  13. /** DEPRECATED. Unstable API. Use getVoltage() and setVoltage() instead. */
  14. float value;
  15. };
  16. union {
  17. /** Number of polyphonic channels.
  18. DEPRECATED. Unstable API. Use set/getChannels() instead.
  19. May be 0 to PORT_MAX_CHANNELS.
  20. 0 channels means disconnected.
  21. */
  22. uint8_t channels = 0;
  23. /** DEPRECATED. Unstable API. Use isConnected() instead. */
  24. uint8_t active;
  25. };
  26. /** For rendering plug lights on cables.
  27. Green for positive, red for negative, and blue for polyphonic.
  28. */
  29. Light plugLights[3];
  30. enum Type {
  31. INPUT,
  32. OUTPUT,
  33. };
  34. /** Sets the voltage of the given channel. */
  35. void setVoltage(float voltage, uint8_t channel = 0) {
  36. voltages[channel] = voltage;
  37. }
  38. /** Returns the voltage of the given channel.
  39. Because of proper bookkeeping, all channels higher than the input port's number of channels should be 0V.
  40. */
  41. float getVoltage(uint8_t channel = 0) {
  42. return voltages[channel];
  43. }
  44. /** Returns the given channel's voltage if the port is polyphonic, otherwise returns the first voltage (channel 0). */
  45. float getPolyVoltage(uint8_t channel) {
  46. return isMonophonic() ? getVoltage(0) : getVoltage(channel);
  47. }
  48. /** Returns the voltage if a cable is connected, otherwise returns the given normal voltage. */
  49. float getNormalVoltage(float normalVoltage, uint8_t channel = 0) {
  50. return isConnected() ? getVoltage(channel) : normalVoltage;
  51. }
  52. float getNormalPolyVoltage(float normalVoltage, uint8_t channel) {
  53. return isConnected() ? getPolyVoltage(channel) : normalVoltage;
  54. }
  55. /** Returns a pointer to the array of voltages beginning with firstChannel.
  56. The pointer can be used for reading and writing.
  57. */
  58. float* getVoltages(uint8_t firstChannel = 0) {
  59. return &voltages[firstChannel];
  60. }
  61. /** Copies the port's voltages to an array of size at least `channels`. */
  62. void readVoltages(float* v) {
  63. for (uint8_t c = 0; c < channels; c++) {
  64. v[c] = voltages[c];
  65. }
  66. }
  67. /** Copies an array of size at least `channels` to the port's voltages.
  68. Remember to set the number of channels *before* calling this method.
  69. */
  70. void writeVoltages(const float* v) {
  71. for (uint8_t c = 0; c < channels; c++) {
  72. voltages[c] = v[c];
  73. }
  74. }
  75. /** Sets all voltages to 0. */
  76. void clearVoltages() {
  77. for (uint8_t c = 0; c < channels; c++) {
  78. voltages[c] = 0.f;
  79. }
  80. }
  81. /** Returns the sum of all voltages. */
  82. float getVoltageSum() {
  83. float sum = 0.f;
  84. for (uint8_t c = 0; c < channels; c++) {
  85. sum += voltages[c];
  86. }
  87. return sum;
  88. }
  89. /** Returns the root-mean-square of all voltages.
  90. Uses sqrt() which is slow, so use a custom approximation if calling frequently.
  91. */
  92. float getVoltageRMS() {
  93. if (channels == 0) {
  94. return 0.f;
  95. }
  96. else if (channels == 1) {
  97. return std::fabs(voltages[0]);
  98. }
  99. else {
  100. float sum = 0.f;
  101. for (uint8_t c = 0; c < channels; c++) {
  102. sum += std::pow(voltages[c], 2);
  103. }
  104. return std::sqrt(sum);
  105. }
  106. }
  107. template <typename T>
  108. T getVoltageSimd(uint8_t firstChannel) {
  109. return T::load(&voltages[firstChannel]);
  110. }
  111. template <typename T>
  112. T getPolyVoltageSimd(uint8_t firstChannel) {
  113. return isMonophonic() ? getVoltage(0) : getVoltageSimd<T>(firstChannel);
  114. }
  115. template <typename T>
  116. T getNormalVoltageSimd(T normalVoltage, uint8_t firstChannel) {
  117. return isConnected() ? getVoltageSimd<T>(firstChannel) : normalVoltage;
  118. }
  119. template <typename T>
  120. T getNormalPolyVoltageSimd(T normalVoltage, uint8_t firstChannel) {
  121. return isConnected() ? getPolyVoltageSimd<T>(firstChannel) : normalVoltage;
  122. }
  123. template <typename T>
  124. void setVoltageSimd(T voltage, uint8_t firstChannel) {
  125. voltage.store(&voltages[firstChannel]);
  126. }
  127. /** Sets the number of polyphony channels.
  128. Also clears voltages of higher channels.
  129. If disconnected, this does nothing (`channels` remains 0).
  130. If 0 is given, `channels` is set to 1 but all voltages are cleared.
  131. */
  132. void setChannels(uint8_t channels) {
  133. // If disconnected, keep the number of channels at 0.
  134. if (this->channels == 0) {
  135. return;
  136. }
  137. // Set higher channel voltages to 0
  138. for (uint8_t c = channels; c < this->channels; c++) {
  139. voltages[c] = 0.f;
  140. }
  141. // Don't allow caller to set port as disconnected
  142. if (channels == 0) {
  143. channels = 1;
  144. }
  145. this->channels = channels;
  146. }
  147. /** Returns the number of channels.
  148. If the port is disconnected, it has 0 channels.
  149. */
  150. int getChannels() {
  151. return channels;
  152. }
  153. /** Returns whether a cable is connected to the Port.
  154. You can use this for skipping code that generates output voltages.
  155. */
  156. bool isConnected() {
  157. return channels > 0;
  158. }
  159. /** Returns whether the cable exists and has 1 channel. */
  160. bool isMonophonic() {
  161. return channels == 1;
  162. }
  163. /** Returns whether the cable exists and has more than 1 channel. */
  164. bool isPolyphonic() {
  165. return channels > 1;
  166. }
  167. /** Use getNormalVoltage() instead. */
  168. DEPRECATED float normalize(float normalVoltage) {
  169. return getNormalVoltage(normalVoltage);
  170. }
  171. };
  172. struct Output : Port {};
  173. struct Input : Port {};
  174. } // namespace engine
  175. } // namespace rack