Browse Source

simplify playback to only one note at a time

main
cinnaboot 1 month ago
parent
commit
b43933b580
  1. 81
      src/main.cpp

81
src/main.cpp

@ -33,7 +33,7 @@
// range
#define USE_10V_MAX_CV true
#define MAX_DAC_INPUT 4095 // 2^12 - 1
#define TEST_DELAY 5 // seconds to sleep in the test loop
#define TEST_DELAY 1 // seconds to sleep in the test loop
void
@ -70,12 +70,10 @@ convertNoteToDACInput(u8 midi_note)
void
loopMIDI(alsa_sequencer* seq, spi_connection* spi, gpio_info* gpio)
{
// TODO: there's probably a more elegant way to handle this, but this was
// the first idea that came up, and seems to work
// NOTE: we're starting off with monophonic output for testing
// NOTE: MIDI Note '0' is a vaild note, but 255 is not. So, we'll use 255
// as a placeholder to represent an unused PLAYING_NOTE
static const u32 MAX_NOTES = 6;
static u8 PLAYING_NOTES[MAX_NOTES] = {255,255,255,255,255,255};
static u8 PLAYING_NOTE = 255;
while(1)
{
@ -83,61 +81,54 @@ loopMIDI(alsa_sequencer* seq, spi_connection* spi, gpio_info* gpio)
snd_seq_event_t *ev = nullptr;
snd_seq_event_input(seq->seq_handle, &ev);
// FIXME: we should only be processing notes from the first channel
// TODO: add option to choose other channels?
// TODO: polyphonic on the same channel
// FIXME: probably a bug here since we don't have an early out for non-
// note events
alsa_note_event nev = midiProcess(ev);
bool note_tracked = false;
for (u32 i = 0; i < MAX_NOTES; i++) {
if (PLAYING_NOTES[i] == nev.note) {
note_tracked = true;
if (nev.note_on) { // do nothing
break;
} else { // remove note from tracked list
// NOTE: only process notes from the 0 (keyboard) or 1 (aplaymidi) channel
//if (nev.channel != 0 && nev.channel != 1) continue;
if (nev.channel != 1) continue;
// NOTE: the way we're going to handle overlapping note ons will be the
// easiest. the gate will be on for the previous note, and there won't
// be a distinct gate on or trigger for the overlapping note.
// TODO: fix overlapping trigger for envelope
if (PLAYING_NOTE == nev.note && nev.note_on == false) {
assert(gpioSetValue(gpio, 0)); // send set gate off
PLAYING_NOTES[i] = 255;
break;
}
}
}
// send note voltage to DAC, and add note to tracked list
if (!note_tracked && nev.note_on ) {
for (u32 i = 0; i < MAX_NOTES; i++) {
if (PLAYING_NOTES[i] == 255) {
PLAYING_NOTES[i] = nev.note;
assert(gpioSetValue(gpio, 1)); // send set gate on
break;
}
}
PLAYING_NOTE = 255;
} else if (nev.note_on) { // new note
PLAYING_NOTE = nev.note;
assert(gpioSetValue(gpio, 1)); // send gate on
i32 dac_in = convertNoteToDACInput(nev.note);
// TODO: could check return value here for SPI error
spiSendVoltage(spi, dac_in);
}
printf("PLAYING_NOTES: %d,%d,%d,%d,%d,%d\n",
PLAYING_NOTES[0],
PLAYING_NOTES[1],
PLAYING_NOTES[2],
PLAYING_NOTES[3],
PLAYING_NOTES[4],
PLAYING_NOTES[5]);
printf("PLAYING_NOTE: %d\n", PLAYING_NOTE);
}
}
void
loopTestVoltages(spi_connection* spi)
loopTestVoltages(spi_connection* spi, gpio_info* gpio)
{
u32 one_volt = 408;
u32 scale = 0;
for (u32 i = 0;; i++) {
#if 0
u32 volts = (i % 2 == 0) ? 0 : one_volt;
spiSendVoltage(spi, volts);
#else
spiSendVoltage(spi, 10 * one_volt);
#endif
//u32 volts = (i % 2 == 0) ? 0 : one_volt;
//spiSendVoltage(spi, volts);
spiSendVoltage(spi, scale * one_volt);
scale++;
if (scale > 10) scale = 0;
// NOTE: toggle gate on/off every other 'tick'
u32 gpio_value = (i % 2 == 0) ? 0 : 1;
assert(gpioSetValue(gpio, gpio_value));
//spiSendVoltage(spi, 10 * one_volt);
sleep(TEST_DELAY);
}
}
@ -182,7 +173,7 @@ main(i32 argc, char* argv[])
loopMIDI(&seq, &spi, &gpio);
break;
case 't':
loopTestVoltages(&spi);
loopTestVoltages(&spi, &gpio);
break;
case 'h':
default:

Loading…
Cancel
Save