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.

99 lines
2.3KB

  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 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 = 0;
  20. /** Unstable API. Use isConnected() instead. */
  21. bool active = false;
  22. /** For rendering plug lights on cables.
  23. Green for positive, red for negative, and blue for polyphonic.
  24. */
  25. Light plugLights[3];
  26. void setVoltage(float voltage, int channel = 0) {
  27. voltages[channel] = voltage;
  28. }
  29. /** Returns the voltage of the given channel.
  30. Because of proper bookkeeping, all channels higher than the input port's number of channels should be 0V.
  31. */
  32. float getVoltage(int channel = 0) {
  33. return voltages[channel];
  34. }
  35. /** Returns the given channel's voltage if the port is polyphonic, otherwise returns the first voltage (channel 0). */
  36. float getPolyVoltage(int channel) {
  37. return (channels == 1) ? getVoltage(channel) : getVoltage(0);
  38. }
  39. /** Returns the voltage if a cable is connected, otherwise returns the given normal voltage. */
  40. float getNormalVoltage(float normalVoltage, int channel = 0) {
  41. return isConnected() ? getVoltage(channel) : normalVoltage;
  42. }
  43. float getNormalPolyVoltage(float normalVoltage, int channel) {
  44. return isConnected() ? getPolyVoltage(channel) : normalVoltage;
  45. }
  46. /** Sets the number of polyphony channels. */
  47. void setChannels(int channels) {
  48. // Set higher channel voltages to 0
  49. for (int c = channels; c < this->channels; c++) {
  50. voltages[c] = 0.f;
  51. }
  52. this->channels = channels;
  53. }
  54. int getChannels() {
  55. return channels;
  56. }
  57. /** Returns if a cable is connected to the Port.
  58. You can use this for skipping code that generates output voltages.
  59. */
  60. bool isConnected() {
  61. return active;
  62. }
  63. void step();
  64. /** Use getNormalVoltage() instead. */
  65. DEPRECATED float normalize(float normalVoltage) {
  66. return getNormalVoltage(normalVoltage);
  67. }
  68. };
  69. struct Output : Port {
  70. Output() {
  71. channels = 1;
  72. }
  73. };
  74. struct Input : Port {};
  75. } // namespace engine
  76. } // namespace rack