Browse Source

In Port method arguments, change `channel` type to uint8_t.

tags/v2.4.0
Andrew Belt 1 year ago
parent
commit
afdbb98f7f
1 changed files with 18 additions and 18 deletions
  1. +18
    -18
      include/engine/Port.hpp

+ 18
- 18
include/engine/Port.hpp View File

@@ -40,41 +40,41 @@ struct Port {
}; };


/** Sets the voltage of the given channel. */ /** Sets the voltage of the given channel. */
void setVoltage(float voltage, int channel = 0) {
void setVoltage(float voltage, uint8_t channel = 0) {
voltages[channel] = voltage; voltages[channel] = voltage;
} }


/** Returns the voltage of the given channel. /** Returns the voltage of the given channel.
Because of proper bookkeeping, all channels higher than the input port's number of channels should be 0V. Because of proper bookkeeping, all channels higher than the input port's number of channels should be 0V.
*/ */
float getVoltage(int channel = 0) {
float getVoltage(uint8_t channel = 0) {
return voltages[channel]; return voltages[channel];
} }


/** Returns the given channel's voltage if the port is polyphonic, otherwise returns the first voltage (channel 0). */ /** Returns the given channel's voltage if the port is polyphonic, otherwise returns the first voltage (channel 0). */
float getPolyVoltage(int channel) {
float getPolyVoltage(uint8_t channel) {
return isMonophonic() ? getVoltage(0) : getVoltage(channel); return isMonophonic() ? getVoltage(0) : getVoltage(channel);
} }


/** Returns the voltage if a cable is connected, otherwise returns the given normal voltage. */ /** Returns the voltage if a cable is connected, otherwise returns the given normal voltage. */
float getNormalVoltage(float normalVoltage, int channel = 0) {
float getNormalVoltage(float normalVoltage, uint8_t channel = 0) {
return isConnected() ? getVoltage(channel) : normalVoltage; return isConnected() ? getVoltage(channel) : normalVoltage;
} }


float getNormalPolyVoltage(float normalVoltage, int channel) {
float getNormalPolyVoltage(float normalVoltage, uint8_t channel) {
return isConnected() ? getPolyVoltage(channel) : normalVoltage; return isConnected() ? getPolyVoltage(channel) : normalVoltage;
} }


/** Returns a pointer to the array of voltages beginning with firstChannel. /** Returns a pointer to the array of voltages beginning with firstChannel.
The pointer can be used for reading and writing. The pointer can be used for reading and writing.
*/ */
float* getVoltages(int firstChannel = 0) {
float* getVoltages(uint8_t firstChannel = 0) {
return &voltages[firstChannel]; return &voltages[firstChannel];
} }


/** Copies the port's voltages to an array of size at least `channels`. */ /** Copies the port's voltages to an array of size at least `channels`. */
void readVoltages(float* v) { void readVoltages(float* v) {
for (int c = 0; c < channels; c++) {
for (uint8_t c = 0; c < channels; c++) {
v[c] = voltages[c]; v[c] = voltages[c];
} }
} }
@@ -83,14 +83,14 @@ struct Port {
Remember to set the number of channels *before* calling this method. Remember to set the number of channels *before* calling this method.
*/ */
void writeVoltages(const float* v) { void writeVoltages(const float* v) {
for (int c = 0; c < channels; c++) {
for (uint8_t c = 0; c < channels; c++) {
voltages[c] = v[c]; voltages[c] = v[c];
} }
} }


/** Sets all voltages to 0. */ /** Sets all voltages to 0. */
void clearVoltages() { void clearVoltages() {
for (int c = 0; c < channels; c++) {
for (uint8_t c = 0; c < channels; c++) {
voltages[c] = 0.f; voltages[c] = 0.f;
} }
} }
@@ -98,7 +98,7 @@ struct Port {
/** Returns the sum of all voltages. */ /** Returns the sum of all voltages. */
float getVoltageSum() { float getVoltageSum() {
float sum = 0.f; float sum = 0.f;
for (int c = 0; c < channels; c++) {
for (uint8_t c = 0; c < channels; c++) {
sum += voltages[c]; sum += voltages[c];
} }
return sum; return sum;
@@ -116,7 +116,7 @@ struct Port {
} }
else { else {
float sum = 0.f; float sum = 0.f;
for (int c = 0; c < channels; c++) {
for (uint8_t c = 0; c < channels; c++) {
sum += std::pow(voltages[c], 2); sum += std::pow(voltages[c], 2);
} }
return std::sqrt(sum); return std::sqrt(sum);
@@ -124,27 +124,27 @@ struct Port {
} }


template <typename T> template <typename T>
T getVoltageSimd(int firstChannel) {
T getVoltageSimd(uint8_t firstChannel) {
return T::load(&voltages[firstChannel]); return T::load(&voltages[firstChannel]);
} }


template <typename T> template <typename T>
T getPolyVoltageSimd(int firstChannel) {
T getPolyVoltageSimd(uint8_t firstChannel) {
return isMonophonic() ? getVoltage(0) : getVoltageSimd<T>(firstChannel); return isMonophonic() ? getVoltage(0) : getVoltageSimd<T>(firstChannel);
} }


template <typename T> template <typename T>
T getNormalVoltageSimd(T normalVoltage, int firstChannel) {
T getNormalVoltageSimd(T normalVoltage, uint8_t firstChannel) {
return isConnected() ? getVoltageSimd<T>(firstChannel) : normalVoltage; return isConnected() ? getVoltageSimd<T>(firstChannel) : normalVoltage;
} }


template <typename T> template <typename T>
T getNormalPolyVoltageSimd(T normalVoltage, int firstChannel) {
T getNormalPolyVoltageSimd(T normalVoltage, uint8_t firstChannel) {
return isConnected() ? getPolyVoltageSimd<T>(firstChannel) : normalVoltage; return isConnected() ? getPolyVoltageSimd<T>(firstChannel) : normalVoltage;
} }


template <typename T> template <typename T>
void setVoltageSimd(T voltage, int firstChannel) {
void setVoltageSimd(T voltage, uint8_t firstChannel) {
voltage.store(&voltages[firstChannel]); voltage.store(&voltages[firstChannel]);
} }


@@ -153,13 +153,13 @@ struct Port {
If disconnected, this does nothing (`channels` remains 0). If disconnected, this does nothing (`channels` remains 0).
If 0 is given, `channels` is set to 1 but all voltages are cleared. If 0 is given, `channels` is set to 1 but all voltages are cleared.
*/ */
void setChannels(int channels) {
void setChannels(uint8_t channels) {
// If disconnected, keep the number of channels at 0. // If disconnected, keep the number of channels at 0.
if (this->channels == 0) { if (this->channels == 0) {
return; return;
} }
// Set higher channel voltages to 0 // Set higher channel voltages to 0
for (int c = channels; c < this->channels; c++) {
for (uint8_t c = channels; c < this->channels; c++) {
voltages[c] = 0.f; voltages[c] = 0.f;
} }
// Don't allow caller to set port as disconnected // Don't allow caller to set port as disconnected


Loading…
Cancel
Save