VCV Rack API v2
Loading...
Searching...
No Matches
Port.hpp
Go to the documentation of this file.
1#pragma once
2#include <common.hpp>
3#include <engine/Light.hpp>
4
5
6namespace rack {
7namespace engine {
8
9
11static const int PORT_MAX_CHANNELS = 16;
12
13
14struct Port {
16 union {
20 float value;
21 };
22 union {
28 uint8_t channels = 0;
30 uint8_t active;
31 };
36
37 enum Type {
40 };
41
43 void setVoltage(float voltage, uint8_t channel = 0) {
44 voltages[channel] = voltage;
45 }
46
50 float getVoltage(uint8_t channel = 0) {
51 return voltages[channel];
52 }
53
55 float getPolyVoltage(uint8_t channel) {
56 return isMonophonic() ? getVoltage(0) : getVoltage(channel);
57 }
58
60 float getNormalVoltage(float normalVoltage, uint8_t channel = 0) {
61 return isConnected() ? getVoltage(channel) : normalVoltage;
62 }
63
64 float getNormalPolyVoltage(float normalVoltage, uint8_t channel) {
65 return isConnected() ? getPolyVoltage(channel) : normalVoltage;
66 }
67
71 float* getVoltages(uint8_t firstChannel = 0) {
72 return &voltages[firstChannel];
73 }
74
76 void readVoltages(float* v) {
77 for (uint8_t c = 0; c < channels; c++) {
78 v[c] = voltages[c];
79 }
80 }
81
85 void writeVoltages(const float* v) {
86 for (uint8_t c = 0; c < channels; c++) {
87 voltages[c] = v[c];
88 }
89 }
90
93 for (uint8_t c = 0; c < channels; c++) {
94 voltages[c] = 0.f;
95 }
96 }
97
99 float getVoltageSum() {
100 float sum = 0.f;
101 for (uint8_t c = 0; c < channels; c++) {
102 sum += voltages[c];
103 }
104 return sum;
105 }
106
111 if (channels == 0) {
112 return 0.f;
113 }
114 else if (channels == 1) {
115 return std::fabs(voltages[0]);
116 }
117 else {
118 float sum = 0.f;
119 for (uint8_t c = 0; c < channels; c++) {
120 sum += std::pow(voltages[c], 2);
121 }
122 return std::sqrt(sum);
123 }
124 }
125
126 template <typename T>
127 T getVoltageSimd(uint8_t firstChannel) {
128 return T::load(&voltages[firstChannel]);
129 }
130
131 template <typename T>
132 T getPolyVoltageSimd(uint8_t firstChannel) {
133 return isMonophonic() ? getVoltage(0) : getVoltageSimd<T>(firstChannel);
134 }
135
136 template <typename T>
137 T getNormalVoltageSimd(T normalVoltage, uint8_t firstChannel) {
138 return isConnected() ? getVoltageSimd<T>(firstChannel) : normalVoltage;
139 }
140
141 template <typename T>
142 T getNormalPolyVoltageSimd(T normalVoltage, uint8_t firstChannel) {
143 return isConnected() ? getPolyVoltageSimd<T>(firstChannel) : normalVoltage;
144 }
145
146 template <typename T>
147 void setVoltageSimd(T voltage, uint8_t firstChannel) {
148 voltage.store(&voltages[firstChannel]);
149 }
150
156 void setChannels(uint8_t channels) {
157 // If disconnected, keep the number of channels at 0.
158 if (this->channels == 0) {
159 return;
160 }
161 // Set higher channel voltages to 0
162 for (uint8_t c = channels; c < this->channels; c++) {
163 voltages[c] = 0.f;
164 }
165 // Don't allow caller to set port as disconnected
166 if (channels == 0) {
167 channels = 1;
168 }
169 this->channels = channels;
170 }
171
176 return channels;
177 }
178
182 bool isConnected() {
183 return channels > 0;
184 }
185
188 return channels == 1;
189 }
190
193 return channels > 1;
194 }
195
197 DEPRECATED float normalize(float normalVoltage) {
198 return getNormalVoltage(normalVoltage);
199 }
200};
201
202
203struct Output : Port {};
204
205struct Input : Port {};
206
207
208} // namespace engine
209} // namespace rack
#define DEPRECATED
Attribute for deprecated functions and symbols.
Definition common.hpp:24
static const int PORT_MAX_CHANNELS
This is inspired by the number of MIDI channels.
Definition Port.hpp:11
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9
Definition Port.hpp:205
Definition Light.hpp:9
Definition Port.hpp:203
Definition Port.hpp:14
bool isConnected()
Returns whether a cable is connected to the Port.
Definition Port.hpp:182
float getVoltageRMS()
Returns the root-mean-square of all voltages.
Definition Port.hpp:110
Light plugLights[3]
For rendering plug lights on cables.
Definition Port.hpp:35
void setVoltage(float voltage, uint8_t channel=0)
Sets the voltage of the given channel.
Definition Port.hpp:43
float getNormalVoltage(float normalVoltage, uint8_t channel=0)
Returns the voltage if a cable is connected, otherwise returns the given normal voltage.
Definition Port.hpp:60
void clearVoltages()
Sets all voltages to 0.
Definition Port.hpp:92
void writeVoltages(const float *v)
Copies an array of size at least channels to the port's voltages.
Definition Port.hpp:85
T getPolyVoltageSimd(uint8_t firstChannel)
Definition Port.hpp:132
float getVoltage(uint8_t channel=0)
Returns the voltage of the given channel.
Definition Port.hpp:50
float voltages[PORT_MAX_CHANNELS]
Unstable API.
Definition Port.hpp:18
float * getVoltages(uint8_t firstChannel=0)
Returns a pointer to the array of voltages beginning with firstChannel.
Definition Port.hpp:71
float getVoltageSum()
Returns the sum of all voltages.
Definition Port.hpp:99
uint8_t channels
Number of polyphonic channels.
Definition Port.hpp:28
DEPRECATED float normalize(float normalVoltage)
Use getNormalVoltage() instead.
Definition Port.hpp:197
T getNormalVoltageSimd(T normalVoltage, uint8_t firstChannel)
Definition Port.hpp:137
bool isPolyphonic()
Returns whether the cable exists and has more than 1 channel.
Definition Port.hpp:192
int getChannels()
Returns the number of channels.
Definition Port.hpp:175
bool isMonophonic()
Returns whether the cable exists and has 1 channel.
Definition Port.hpp:187
Type
Definition Port.hpp:37
@ OUTPUT
Definition Port.hpp:39
@ INPUT
Definition Port.hpp:38
float getPolyVoltage(uint8_t channel)
Returns the given channel's voltage if the port is polyphonic, otherwise returns the first voltage (c...
Definition Port.hpp:55
uint8_t active
DEPRECATED.
Definition Port.hpp:30
float value
DEPRECATED.
Definition Port.hpp:20
float getNormalPolyVoltage(float normalVoltage, uint8_t channel)
Definition Port.hpp:64
void setChannels(uint8_t channels)
Sets the number of polyphony channels.
Definition Port.hpp:156
T getVoltageSimd(uint8_t firstChannel)
Definition Port.hpp:127
void readVoltages(float *v)
Copies the port's voltages to an array of size at least channels.
Definition Port.hpp:76
void setVoltageSimd(T voltage, uint8_t firstChannel)
Definition Port.hpp:147
T getNormalPolyVoltageSimd(T normalVoltage, uint8_t firstChannel)
Definition Port.hpp:142