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.

151 lines
3.6KB

  1. #pragma once
  2. #include <common.hpp>
  3. #include <engine/Light.hpp>
  4. namespace rack {
  5. namespace engine {
  6. static const int PORT_MAX_CHANNELS = 16;
  7. struct alignas(32) Port {
  8. /** Voltage of the port. */
  9. union {
  10. /** Unstable API. Use getVoltage() and setVoltage() instead. */
  11. float voltages[PORT_MAX_CHANNELS] = {};
  12. /** DEPRECATED. Unstable API. Use getVoltage() and setVoltage() instead. */
  13. float value;
  14. };
  15. /** Number of polyphonic channels
  16. Unstable API. Use set/getChannels() instead.
  17. May be 0 to PORT_MAX_CHANNELS.
  18. */
  19. uint8_t channels = 1;
  20. /** For rendering plug lights on cables.
  21. Green for positive, red for negative, and blue for polyphonic.
  22. */
  23. Light plugLights[3];
  24. void setVoltage(float voltage, int channel = 0) {
  25. voltages[channel] = voltage;
  26. }
  27. /** Returns the voltage of the given channel.
  28. Because of proper bookkeeping, all channels higher than the input port's number of channels should be 0V.
  29. */
  30. float getVoltage(int channel = 0) {
  31. return voltages[channel];
  32. }
  33. /** Returns the given channel's voltage if the port is polyphonic, otherwise returns the first voltage (channel 0). */
  34. float getPolyVoltage(int channel) {
  35. return (channels == 1) ? getVoltage(0) : getVoltage(channel);
  36. }
  37. /** Returns the voltage if a cable is connected, otherwise returns the given normal voltage. */
  38. float getNormalVoltage(float normalVoltage, int channel = 0) {
  39. return isConnected() ? getVoltage(channel) : normalVoltage;
  40. }
  41. float getNormalPolyVoltage(float normalVoltage, int channel) {
  42. return isConnected() ? getPolyVoltage(channel) : normalVoltage;
  43. }
  44. /** Returns a pointer to the array of voltages beginning with firstChannel.
  45. The pointer can be used for reading and writing.
  46. Useful for SIMD.
  47. */
  48. float *getVoltages(int firstChannel = 0) {
  49. return &voltages[firstChannel];
  50. }
  51. /** Copies the port's voltages to an array of size at least `channels`. */
  52. void readVoltages(float *v) {
  53. for (int c = 0; c < channels; c++) {
  54. v[c] = voltages[c];
  55. }
  56. }
  57. /** Copies an array of size at least `channels` to the port's voltages.
  58. Remember to set the number of channels *before* calling this method.
  59. */
  60. void writeVoltages(const float *v) {
  61. for (int c = 0; c < channels; c++) {
  62. voltages[c] = v[c];
  63. }
  64. }
  65. void clearVoltages() {
  66. for (int c = 0; c < channels; c++) {
  67. voltages[c] = 0.f;
  68. }
  69. }
  70. /** Returns the sum of all voltages. */
  71. float getVoltageSum() {
  72. float sum = 0.f;
  73. for (int c = 0; c < channels; c++) {
  74. sum += voltages[c];
  75. }
  76. return sum;
  77. }
  78. /** Sets the number of polyphony channels.
  79. Also clears voltages of higher channels.
  80. If disconnected, this does nothing (`channels` remains 0).
  81. If 0 is given, `channels` is set to 1 but all voltages are cleared.
  82. */
  83. void setChannels(int channels) {
  84. // If disconnected, keep the number of channels at 0.
  85. if (this->channels == 0) {
  86. return;
  87. }
  88. // Set higher channel voltages to 0
  89. for (int c = channels; c < this->channels; c++) {
  90. voltages[c] = 0.f;
  91. }
  92. // Don't allow caller to set port as disconnected
  93. if (channels == 0) {
  94. channels = 1;
  95. }
  96. this->channels = channels;
  97. }
  98. int getChannels() {
  99. return channels;
  100. }
  101. /** Returns if a cable is connected to the Port.
  102. You can use this for skipping code that generates output voltages.
  103. */
  104. bool isConnected() {
  105. return channels > 0;
  106. }
  107. bool isMonophonic() {
  108. return channels == 1;
  109. }
  110. bool isPolyphonic() {
  111. return channels > 1;
  112. }
  113. void process(float deltaTime);
  114. /** Use getNormalVoltage() instead. */
  115. DEPRECATED float normalize(float normalVoltage) {
  116. return getNormalVoltage(normalVoltage);
  117. }
  118. };
  119. struct Output : Port {};
  120. struct Input : Port {};
  121. } // namespace engine
  122. } // namespace rack