Compare commits

..

3 Commits

  1. 7
      Makefile
  2. 83
      src/main.cpp
  3. 10
      src/midi.cpp
  4. 4
      src/spi.cpp
  5. 14
      src/spi.h

7
Makefile

@ -3,6 +3,11 @@ CXX=g++
CXXFLAGS= -std=c++11 -g -ggdb3 -Wall
LDFLAGS=-lasound -lgpiod
# NOTE: add an environment variable to detect breaking API changes with
# libgpiod
LIBGPIOD_VERSION := $(shell pkgconf --modversion libgpiod | cut -f 1 -d.)
CXXFLAGS += -D LIBGPIOD_VERSION=$(LIBGPIOD_VERSION)
OBJDIR = build
SRCDIR = src
BINDIR = bin
@ -13,7 +18,7 @@ OBJECTS = $(patsubst $(SRCDIR)/%.cpp, $(OBJDIR)/%.o, $(SOURCES))
all: mkdirs $(OBJECTS)
ctags -R ./
ctags -R ./ /usr/include/alsa/
$(CXX) -o $(BIN) $(OBJECTS) $(LDFLAGS)
.PHONY: all

83
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
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;
}
}
// 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_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:

10
src/midi.cpp

@ -83,7 +83,15 @@ midiProcess(const snd_seq_event_t* ev)
nev.note = ev->data.note.note;
nev.channel = ev->data.note.channel;
nev.note_on = (ev->type == SND_SEQ_EVENT_NOTEON) ? true : false;
nev.note_on = false;
// NOTE: NOTE_ON with velocity of 0 is also a common way to store NOTE_OFF in
// MIDI files
if ( ev->type == SND_SEQ_EVENT_NOTEON &&
ev->data.note.velocity > 0)
{
nev.note_on = true;
}
}
else if(ev->type == SND_SEQ_EVENT_CONTROLLER)
{

4
src/spi.cpp

@ -12,6 +12,7 @@
// GPIO
#if LIBGPIOD_VERSION == 1
bool
gpioInit(gpio_info* gpio, const char* chip_name, u32 line_num)
{
@ -57,6 +58,9 @@ gpioSetValue(gpio_info* gpio, u32 value)
return true;
}
#elif LIBGPIOD_VERSION == 2
#error "FIXME: libgpiod version 2 not implemented"
#endif
// SPI

14
src/spi.h

@ -10,6 +10,7 @@
// GPIO
#if LIBGPIOD_VERSION == 1
struct gpio_info
{
char chip_name[16];
@ -17,6 +18,19 @@ struct gpio_info
gpiod_chip* chip;
gpiod_line* line;
};
#elif LIBGPIOD_VERSION == 2
struct gpio_info
{
char chip_name[16];
u32 line_num;
gpiod_chip* chip;
gpiod_line_info* line;
};
#else
#error "FIXME: libgpiod version 2 not implemented"
#endif
bool gpioInit(gpio_info* gpio, const char* chip_name, u32 line_num);

Loading…
Cancel
Save