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.

85 lines
1.9KB

  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 set/getVoltage() 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. /** Unstable API. Use isConnected() instead. */
  21. bool active;
  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. float getVoltage(int channel = 0) {
  30. return voltages[channel];
  31. }
  32. /** Returns the voltage if `channel` is a valid channel, otherwise returns the first voltage (channel 0) */
  33. float getPolyVoltage(int channel) {
  34. return (channel < channels) ? getVoltage(channel) : getVoltage(0);
  35. }
  36. /** Returns the voltage if a cable is connected, otherwise returns the given normal voltage */
  37. float getNormalVoltage(float normalVoltage, int channel = 0) {
  38. return isConnected() ? getVoltage(channel) : normalVoltage;
  39. }
  40. float getNormalPolyVoltage(float normalVoltage, int channel) {
  41. return isConnected() ? getPolyVoltage(channel) : normalVoltage;
  42. }
  43. void setChannels(int channels) {
  44. // Set higher channel voltages to 0
  45. for (int c = channels; c < this->channels; c++) {
  46. voltages[c] = 0.f;
  47. }
  48. this->channels = channels;
  49. }
  50. int getChannels() {
  51. return channels;
  52. }
  53. bool isConnected() {
  54. return active;
  55. }
  56. void step();
  57. DEPRECATED float normalize(float normalVoltage) {
  58. return getNormalVoltage(normalVoltage);
  59. }
  60. };
  61. struct Output : Port {};
  62. struct Input : Port {};
  63. } // namespace engine
  64. } // namespace rack