2020-10-11 20:29:04 -06:00
|
|
|
#include <Audio.h>
|
|
|
|
#include <Wire.h>
|
2020-11-11 16:29:09 -07:00
|
|
|
#include <Adafruit_GFX.h>
|
|
|
|
#include <Adafruit_SSD1306.h>
|
2020-10-18 13:27:32 -06:00
|
|
|
#include <SparkFun_Qwiic_Button.h>
|
2020-10-11 20:29:04 -06:00
|
|
|
#include <Adafruit_MPR121.h>
|
2020-11-11 16:29:09 -07:00
|
|
|
#include <paj7620.h>
|
2020-10-11 20:29:04 -06:00
|
|
|
#include "synth.h"
|
|
|
|
#include "patches.h"
|
|
|
|
#include "notes.h"
|
|
|
|
#include "fingering.h"
|
|
|
|
|
2020-11-11 16:29:09 -07:00
|
|
|
#define DRONES
|
2020-10-25 11:36:58 -06:00
|
|
|
#define DEBUG
|
2020-10-11 20:29:04 -06:00
|
|
|
#define KEY_OFFSET 2
|
|
|
|
|
2020-10-24 20:15:18 -06:00
|
|
|
FMVoice Chanter;
|
|
|
|
FMVoice Drones[3];
|
|
|
|
FMVoice Regulators[3];
|
|
|
|
|
2020-11-11 16:29:09 -07:00
|
|
|
AudioFilterBiquad biquad1;
|
|
|
|
AudioMixer4 mixDrones;
|
|
|
|
AudioMixer4 mixRegulators;
|
|
|
|
AudioMixer4 mixL;
|
|
|
|
AudioMixer4 mixR;
|
|
|
|
AudioOutputI2S out1;
|
|
|
|
AudioSynthNoiseWhite noise;
|
2020-10-25 11:36:58 -06:00
|
|
|
|
2020-10-24 20:15:18 -06:00
|
|
|
AudioConnection FMVoicePatchCords[] = {
|
2020-11-11 16:29:09 -07:00
|
|
|
//{0, 0, 0, 0}, // For some reason, the first one is ignored
|
|
|
|
|
|
|
|
{noise, 0, mixDrones, 3},
|
|
|
|
{noise, 0, mixL, 3},
|
|
|
|
{noise, 0, mixR, 3},
|
2020-10-25 11:36:58 -06:00
|
|
|
|
2020-10-24 20:15:18 -06:00
|
|
|
{Chanter.outputMixer, 0, biquad1, 0},
|
|
|
|
{biquad1, 0, mixL, 0},
|
|
|
|
{biquad1, 0, mixR, 0},
|
|
|
|
|
|
|
|
{Drones[0].outputMixer, 0, mixDrones, 0},
|
|
|
|
{Drones[1].outputMixer, 0, mixDrones, 1},
|
|
|
|
{Drones[2].outputMixer, 0, mixDrones, 2},
|
|
|
|
{mixDrones, 0, mixL, 1},
|
|
|
|
{mixDrones, 0, mixR, 1},
|
|
|
|
|
|
|
|
{Regulators[0].outputMixer, 0, mixRegulators, 0},
|
|
|
|
{Regulators[1].outputMixer, 0, mixRegulators, 1},
|
|
|
|
{Regulators[2].outputMixer, 0, mixRegulators, 2},
|
|
|
|
{mixRegulators, 0, mixL, 2},
|
|
|
|
{mixRegulators, 0, mixR, 2},
|
|
|
|
|
2020-11-11 16:29:09 -07:00
|
|
|
{mixL, 0, out1, 0},
|
|
|
|
{mixR, 0, out1, 1},
|
2020-10-24 20:15:18 -06:00
|
|
|
|
|
|
|
FMVoiceWiring(Chanter),
|
|
|
|
FMVoiceWiring(Drones[0]),
|
|
|
|
FMVoiceWiring(Drones[1]),
|
|
|
|
FMVoiceWiring(Drones[2]),
|
|
|
|
FMVoiceWiring(Regulators[0]),
|
|
|
|
FMVoiceWiring(Regulators[1]),
|
|
|
|
FMVoiceWiring(Regulators[2]),
|
|
|
|
};
|
2020-10-11 20:29:04 -06:00
|
|
|
|
|
|
|
int currentPatch = 0;
|
|
|
|
|
|
|
|
Adafruit_MPR121 cap = Adafruit_MPR121();
|
2020-11-11 16:29:09 -07:00
|
|
|
Adafruit_SSD1306 display(128, 32, &Wire, -1);
|
2020-10-18 13:27:32 -06:00
|
|
|
QwiicButton bag;
|
2020-11-11 16:29:09 -07:00
|
|
|
bool use_bag;
|
|
|
|
|
|
|
|
void blink(bool forever) {
|
|
|
|
for (;;) {
|
|
|
|
digitalWrite(LED_BUILTIN, true);
|
|
|
|
delay(200);
|
|
|
|
digitalWrite(LED_BUILTIN, false);
|
|
|
|
delay(200);
|
|
|
|
if (! forever) break;
|
|
|
|
}
|
|
|
|
}
|
2020-10-11 20:29:04 -06:00
|
|
|
|
2020-10-24 20:15:18 -06:00
|
|
|
void setup() {
|
2020-10-11 20:29:04 -06:00
|
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
2020-11-11 16:29:09 -07:00
|
|
|
digitalWrite(LED_BUILTIN, true);
|
|
|
|
|
|
|
|
setupJustPitches(NOTE_D4, PITCH_D4);
|
2020-10-11 20:29:04 -06:00
|
|
|
|
|
|
|
// Wire.begin needs a moment
|
|
|
|
delay(100);
|
|
|
|
Wire.begin();
|
|
|
|
|
2020-11-11 16:29:09 -07:00
|
|
|
// Initialize gesture/proximity sensor
|
|
|
|
if (paj7620Init()) {
|
|
|
|
// XXX: Error handling
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize display display
|
|
|
|
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3c)) {
|
|
|
|
blink(true);
|
|
|
|
}
|
|
|
|
display.clearDisplay();
|
|
|
|
display.setTextSize(1);
|
|
|
|
display.setTextColor(SSD1306_WHITE);
|
|
|
|
display.print("Starting");
|
|
|
|
display.display();
|
2020-10-18 09:57:06 -06:00
|
|
|
|
2020-10-18 13:27:32 -06:00
|
|
|
// Initialize bag
|
|
|
|
bag.begin();
|
2020-11-11 16:29:09 -07:00
|
|
|
use_bag = bag.isConnected();
|
2020-10-11 20:29:04 -06:00
|
|
|
|
|
|
|
// Initialize touch sensor
|
2020-10-18 09:57:06 -06:00
|
|
|
while (!cap.begin(0x5A)) {
|
2020-11-11 16:29:09 -07:00
|
|
|
display.clearDisplay();
|
|
|
|
display.setCursor(0, 0);
|
|
|
|
display.print("Pipe?");
|
|
|
|
display.display();
|
|
|
|
blink(false);
|
2020-10-11 20:29:04 -06:00
|
|
|
}
|
|
|
|
|
2020-10-18 09:57:06 -06:00
|
|
|
// Set aside some memory for the audio library
|
2020-10-11 20:29:04 -06:00
|
|
|
AudioMemory(120);
|
|
|
|
|
2020-10-18 09:57:06 -06:00
|
|
|
// initialize tunables
|
|
|
|
updateTunables(3, 0);
|
2020-10-11 20:29:04 -06:00
|
|
|
|
|
|
|
// Initialize processor and memory measurements
|
|
|
|
AudioProcessorUsageMaxReset();
|
|
|
|
AudioMemoryUsageMaxReset();
|
2020-10-25 11:36:58 -06:00
|
|
|
|
2020-10-25 19:20:18 -06:00
|
|
|
// Turn on drones
|
|
|
|
for (int i=0; i<3; i++) {
|
|
|
|
FMVoiceLoadPatch(&Drones[i], &Bank[0]);
|
|
|
|
FMVoiceNoteOn(&Drones[i], JustPitches[NOTE_D4 - 12*i] + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Turn on all mixer channels
|
|
|
|
for (int i=0; i<4; i++) {
|
|
|
|
mixL.gain(i, 0.5);
|
|
|
|
mixR.gain(i, 0.6);
|
|
|
|
}
|
|
|
|
|
2020-10-25 11:36:58 -06:00
|
|
|
#ifdef DEBUG
|
2020-11-11 16:29:09 -07:00
|
|
|
noise.amplitude(0.1);
|
2020-10-25 11:36:58 -06:00
|
|
|
mixL.gain(3, 0.1);
|
|
|
|
mixR.gain(3, 0.1);
|
|
|
|
#endif
|
2020-10-11 20:29:04 -06:00
|
|
|
}
|
|
|
|
|
2020-10-18 09:57:06 -06:00
|
|
|
#define INIT_PITCH_ADJUST 0
|
2020-10-25 19:20:18 -06:00
|
|
|
#define INIT_GAIN 0.7
|
2020-10-18 09:57:06 -06:00
|
|
|
#define INIT_PATCH 0
|
|
|
|
|
|
|
|
int16_t pitchAdjust;
|
2020-10-25 19:20:18 -06:00
|
|
|
float chanterGain;
|
2020-10-18 09:57:06 -06:00
|
|
|
int patch;
|
|
|
|
|
|
|
|
void updateTunables(uint8_t buttons, int note) {
|
|
|
|
// Pitch adjust if playing A
|
|
|
|
if (!note || (note == NOTE_A4)) {
|
|
|
|
switch (buttons) {
|
|
|
|
case 3:
|
|
|
|
pitchAdjust = INIT_PITCH_ADJUST;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
pitchAdjust += 4;
|
|
|
|
break;
|
|
|
|
case 1:
|
2020-10-17 15:25:19 -06:00
|
|
|
pitchAdjust -= 4;
|
2020-10-18 09:57:06 -06:00
|
|
|
break;
|
2020-10-17 15:25:19 -06:00
|
|
|
}
|
2020-10-18 09:57:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
float adj = pow(2, pitchAdjust / 32768.0);
|
|
|
|
setupJustPitches(NOTE_D4, PITCH_D4*adj);
|
|
|
|
|
|
|
|
if (!note || (note == NOTE_G4)) {
|
|
|
|
// Volume adjust if playing G
|
|
|
|
switch (buttons) {
|
|
|
|
case 3:
|
2020-10-25 19:20:18 -06:00
|
|
|
chanterGain = INIT_GAIN;
|
2020-10-18 09:57:06 -06:00
|
|
|
break;
|
|
|
|
case 2:
|
2020-10-25 19:20:18 -06:00
|
|
|
chanterGain = min(chanterGain+0.005, 1.0);
|
2020-10-18 09:57:06 -06:00
|
|
|
break;
|
|
|
|
case 1:
|
2020-10-25 19:20:18 -06:00
|
|
|
chanterGain = max(chanterGain-0.005, 0.0);
|
2020-10-18 09:57:06 -06:00
|
|
|
break;
|
2020-10-17 15:25:19 -06:00
|
|
|
}
|
2020-10-18 09:57:06 -06:00
|
|
|
}
|
2020-10-25 11:36:58 -06:00
|
|
|
for (int i=0; i<3; i++) {
|
2020-10-25 19:20:18 -06:00
|
|
|
mixL.gain(i, chanterGain);
|
|
|
|
mixR.gain(i, chanterGain);
|
2020-10-25 11:36:58 -06:00
|
|
|
}
|
2020-10-18 09:57:06 -06:00
|
|
|
|
|
|
|
if (!note || (note == NOTE_CS5)) {
|
|
|
|
if (buttons == 3) {
|
|
|
|
patch = INIT_PATCH;
|
2020-10-11 20:29:04 -06:00
|
|
|
}
|
2020-10-17 15:25:19 -06:00
|
|
|
|
2020-10-18 09:57:06 -06:00
|
|
|
// wrap
|
|
|
|
int bankSize = sizeof(Bank) / sizeof(Bank[0]);
|
|
|
|
patch = (patch + bankSize) % bankSize;
|
|
|
|
|
2020-10-24 20:15:18 -06:00
|
|
|
FMPatch *p = &Bank[patch];
|
|
|
|
FMVoiceLoadPatch(&Chanter, p);
|
2020-10-18 09:57:06 -06:00
|
|
|
|
2020-11-11 16:29:09 -07:00
|
|
|
display.clearDisplay();
|
|
|
|
display.setCursor(0, 0);
|
|
|
|
display.print(p->name);
|
|
|
|
display.setCursor(0, 10);
|
|
|
|
display.print("Patch ");
|
|
|
|
display.print(patch);
|
|
|
|
display.display();
|
2020-10-11 20:29:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 12:35:50 -06:00
|
|
|
const uint8_t CLOSEDVAL = 0x30;
|
|
|
|
const uint8_t OPENVAL = 0x70;
|
|
|
|
const uint8_t GLISSANDO_STEPS = OPENVAL - CLOSEDVAL;
|
|
|
|
|
2020-11-11 16:29:09 -07:00
|
|
|
uint8_t loopno = 0;
|
|
|
|
uint8_t last_note = 0;
|
2020-10-11 20:29:04 -06:00
|
|
|
|
|
|
|
void loop() {
|
|
|
|
uint8_t keys = 0;
|
|
|
|
uint8_t note;
|
2020-10-18 09:57:06 -06:00
|
|
|
uint8_t glissandoKeys = 0;
|
2020-10-18 12:35:50 -06:00
|
|
|
uint8_t glissandoNote;
|
|
|
|
float glissandoOpenness = 0;
|
2020-10-11 20:29:04 -06:00
|
|
|
bool silent = false;
|
2020-11-11 16:29:09 -07:00
|
|
|
uint8_t paj_knee = 127;
|
|
|
|
bool knee = false;
|
2020-10-11 20:29:04 -06:00
|
|
|
|
2020-11-11 16:29:09 -07:00
|
|
|
loopno++;
|
|
|
|
|
|
|
|
paj7620ReadReg(0x6c, 1, &paj_knee);
|
|
|
|
if (paj_knee > 240) {
|
|
|
|
knee = true;
|
|
|
|
}
|
2020-10-11 20:29:04 -06:00
|
|
|
|
|
|
|
for (int i = 0; i < 8; i++) {
|
2020-10-18 12:35:50 -06:00
|
|
|
uint16_t val = max(cap.filteredData(i+KEY_OFFSET), CLOSEDVAL);
|
|
|
|
float openness = ((val - CLOSEDVAL) / float(GLISSANDO_STEPS));
|
|
|
|
|
|
|
|
// keys = all keys which are at least touched
|
|
|
|
// glissandoKeys = all keys which are fully closed
|
|
|
|
// The glissando operation computes the difference.
|
|
|
|
if (openness < 1.0) {
|
|
|
|
glissandoOpenness = max(glissandoOpenness, openness);
|
2020-10-11 20:29:04 -06:00
|
|
|
bitSet(keys, i);
|
2020-10-18 12:35:50 -06:00
|
|
|
|
|
|
|
if (openness == 0.0) {
|
2020-10-18 09:57:06 -06:00
|
|
|
bitSet(glissandoKeys, i);
|
2020-10-11 20:29:04 -06:00
|
|
|
}
|
|
|
|
}
|
2020-10-18 12:35:50 -06:00
|
|
|
|
2020-10-11 20:29:04 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
note = uilleann_matrix[keys];
|
2020-10-18 12:35:50 -06:00
|
|
|
glissandoNote = uilleann_matrix[glissandoKeys];
|
2020-10-11 20:29:04 -06:00
|
|
|
|
|
|
|
bool alt = note & 0x80;
|
2020-10-18 12:35:50 -06:00
|
|
|
bool galt = glissandoNote & 0x80;
|
2020-10-11 20:29:04 -06:00
|
|
|
note = note & 0x7f;
|
2020-10-18 12:35:50 -06:00
|
|
|
glissandoNote = glissandoNote & 0x7f;
|
2020-10-17 15:25:19 -06:00
|
|
|
|
2020-10-11 20:29:04 -06:00
|
|
|
// All keys closed + knee = no sound
|
|
|
|
if (knee) {
|
|
|
|
if (keys == 0xff) {
|
|
|
|
silent = true;
|
|
|
|
}
|
|
|
|
}
|
2020-11-11 16:29:09 -07:00
|
|
|
// Look up the note name
|
|
|
|
char *note_name = NoteNames[note % 12];
|
|
|
|
if (silent) {
|
|
|
|
note_name = "-";
|
|
|
|
}
|
2020-10-11 20:29:04 -06:00
|
|
|
|
|
|
|
// Jump octave if the bag is squished
|
|
|
|
//bag = !digitalRead(BAG);
|
2020-11-11 16:29:09 -07:00
|
|
|
if (use_bag && bag.isPressed()) {
|
2020-10-11 20:29:04 -06:00
|
|
|
if (keys & bit(7)) {
|
|
|
|
note += 12;
|
2020-10-18 12:35:50 -06:00
|
|
|
glissandoNote += 12;
|
2020-10-11 20:29:04 -06:00
|
|
|
}
|
2020-10-18 09:57:06 -06:00
|
|
|
}
|
|
|
|
|
2020-11-11 16:29:09 -07:00
|
|
|
#if 0
|
|
|
|
display.clearDisplay();
|
|
|
|
display.setCursor(0, 0);
|
|
|
|
display.print("mem: ");
|
|
|
|
display.print(AudioMemoryUsageMax());
|
|
|
|
display.print(" prx: ");
|
|
|
|
display.print(paj_knee);
|
|
|
|
display.setCursor(0, 24);
|
|
|
|
display.print("Note: ");
|
|
|
|
display.print(note);
|
|
|
|
display.print(" n: ");
|
|
|
|
display.print(loopno);
|
|
|
|
display.display();
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
|
2020-10-11 20:29:04 -06:00
|
|
|
|
|
|
|
if (silent) {
|
2020-10-24 20:15:18 -06:00
|
|
|
FMVoiceNoteOff(&Chanter);
|
2020-10-11 20:29:04 -06:00
|
|
|
} else {
|
|
|
|
// Calculate pitch, and glissando pitch
|
|
|
|
uint16_t pitch = JustPitches[note];
|
2020-10-18 12:35:50 -06:00
|
|
|
uint16_t glissandoPitch = JustPitches[glissandoNote];
|
2020-10-11 20:29:04 -06:00
|
|
|
|
|
|
|
if (alt) {
|
2020-10-18 12:35:50 -06:00
|
|
|
biquad1.setLowShelf(0, 2000, 0.2, 1);
|
2020-10-11 20:29:04 -06:00
|
|
|
} else {
|
|
|
|
biquad1.setHighShelf(0, 1000, 1.0, 1);
|
|
|
|
}
|
|
|
|
|
2020-10-18 12:35:50 -06:00
|
|
|
// Bend pitch if fewer than 3 half steps away
|
|
|
|
if (abs(glissandoNote - note) < 3) {
|
|
|
|
float diff = glissandoPitch - pitch;
|
|
|
|
pitch += diff * glissandoOpenness;
|
2020-10-11 20:29:04 -06:00
|
|
|
}
|
|
|
|
|
2020-10-25 19:20:18 -06:00
|
|
|
if (Chanter.playing) {
|
2020-10-24 20:15:18 -06:00
|
|
|
FMVoiceSetPitch(&Chanter, pitch);
|
2020-10-11 20:29:04 -06:00
|
|
|
} else {
|
2020-10-24 20:15:18 -06:00
|
|
|
FMVoiceNoteOn(&Chanter, pitch);
|
2020-10-11 20:29:04 -06:00
|
|
|
}
|
|
|
|
}
|
2020-11-11 16:29:09 -07:00
|
|
|
|
|
|
|
if (note != last_note) {
|
|
|
|
display.clearDisplay();
|
|
|
|
display.setCursor(0, 0);
|
|
|
|
display.print(note_name);
|
|
|
|
display.display();
|
|
|
|
last_note = note;
|
|
|
|
}
|
2020-10-11 20:29:04 -06:00
|
|
|
}
|