I previously built a midi to control voltage converter and used an array to keep track of which keys were being pressed and in which order. When a note is released, it's corresponding element is changed to zero and then I used a subroutine to reorganize the array so the zeros are at the end.
unsigned int pitchVoltage[6] = {0};
for (int i = 0; i < 6; i++) {
if (pitchVoltage[i] == voltage) {
pitchVoltage[i] = 0;
removeZeros(pitchVoltage, sizeof(pitchVoltage) / sizeof(pitchVoltage[0]));
}
}
void removeZeros(int array[], size_t length) {
if (array != nullptr && length > 0) {
for (size_t i = 0; i < length; i++) {
if (array[i] == 0) {
for (size_t j = i + 1; j < length; j++) {
if (array[j] != 0) {
array[i] = array[j];
array[j] = 0;
break;
}
}
}
}
}
}
this works, but admittedly I found the "remove zeros" subroutine online and only have a superficial understanding of what is happening.
Currently, Im adapting my single channel midi to cv device to be a two channel one and have changed the single line array into a 2d one to keep track of which notes are being held on each of the two respective midi channels. Ive (attempted to) modify the "remove zeros" subroutine to accommodation the two levels of the array like so:
unsigned int pitchVoltage[2][6] {0};
for (int i = 0; i < 6; i++) {
if (pitchVoltage[x][i] == voltage) {
pitchVoltage[x][i] = 0;
removeZeros( pitchVoltage[x], sizeof(pitchVoltage[x]) / sizeof(pitchVoltage[x][0]));
}
}
void removeZeros(int array[], size_t length) {
if (array != nullptr && length > 0) {
for (size_t i = 0; i < length; i++) {
if (array[i] == 0) {
for (size_t j = i + 1; j < length; j++) {
if (array[j] != 0) {
array[i] = array[j];
array[j] = 0;
break;
}
}
}
}
}
}
this seems to be working, but only for the first level of the array, when x=0. When x=1, it does not work.
Any ideas of what is the issue, or how to solve it, or even a better way of how to do this whole process?
This is so that the order of the array keeps in line with my noteCount tally and allows the program to keep track of Last note priority. Does that make sense?
Sorry I dont currently have "real" data from a serial monitor. Im using a pro mini and the serial port is being used for the DIN midi.
For the record, my "removeZeros" compression routine works fine. The problem I was experiencing was from a typo in another part of the code (DUH!) It turns out that when I adapted the one channel version to the two channel version, I forgot to change one of my variables into an array: i had CV, when it should have been CV[x]. oof!
Im surprised that it wasnt causing deeper problems.
gcjr, thanks for taking the time to help me out. While i never need to use you routine, it was helpful. Just seeing another take on what was basically the same function helps (a bit) to understand the process that it performs.