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 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #pragma once
  2. #include "common.hpp"
  3. #include "engine/Light.hpp"
  4. namespace rack {
  5. static const int PORT_MAX_CHANNELS = 16;
  6. struct Port {
  7. /** Voltage of the port */
  8. union {
  9. /** Unstable API. Use set/getVoltage() instead. */
  10. float voltages[PORT_MAX_CHANNELS] = {};
  11. /** DEPRECATED. Unstable API. Use getVoltage() and setVoltage() instead. */
  12. float value;
  13. };
  14. /** Number of polyphonic channels
  15. Unstable API. Use set/getChannels() instead.
  16. May be 0 to PORT_MAX_CHANNELS.
  17. */
  18. uint8_t channels = 1;
  19. /** Unstable API. Use isConnected() instead. */
  20. bool active;
  21. /** For rendering plug lights on cables
  22. Green for positive, red for negative, and blue for polyphonic
  23. */
  24. Light plugLights[3];
  25. void setVoltage(float voltage, int channel = 0) {
  26. voltages[channel] = voltage;
  27. }
  28. float getVoltage(int channel = 0) {
  29. return voltages[channel];
  30. }
  31. /** Returns the voltage if `channel` is a valid channel, otherwise returns the first voltage (channel 0) */
  32. float getPolyVoltage(int channel) {
  33. return (channel < channels) ? getVoltage(channel) : getVoltage(0);
  34. }
  35. /** Returns the voltage if a cable is connected, otherwise returns the given normal voltage */
  36. float getNormalVoltage(float normalVoltage, int channel = 0) {
  37. return isConnected() ? getVoltage(channel) : normalVoltage;
  38. }
  39. float getNormalPolyVoltage(float normalVoltage, int channel) {
  40. return isConnected() ? getPolyVoltage(channel) : normalVoltage;
  41. }
  42. void setChannels(int channels) {
  43. // Set higher channel voltages to 0
  44. for (int c = channels; c < this->channels; c++) {
  45. voltages[c] = 0.f;
  46. }
  47. this->channels = channels;
  48. }
  49. int getChannels() {
  50. return channels;
  51. }
  52. bool isConnected() {
  53. return active;
  54. }
  55. void step();
  56. DEPRECATED float normalize(float normalVoltage) {
  57. return getNormalVoltage(normalVoltage);
  58. }
  59. };
  60. struct Output : Port {};
  61. struct Input : Port {};
  62. } // namespace rack