Works with samples in program memory, but not in flash ram

This commit is contained in:
Oleksiy 2021-08-15 16:10:17 +03:00
commit 79a454eced
1 changed files with 336 additions and 0 deletions

336
NeoBreaks/NeoBreaks.ino Normal file
View File

@ -0,0 +1,336 @@
#include "Audio.h"
#include "Adafruit_NeoTrellisM4.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL343.h>
#include "AudioSampleG1_1.h"
#include "AudioSampleG1_2.h"
#include "AudioSampleG1_3.h"
#include "AudioSampleG1_4.h"
#include "AudioSampleG1_5.h"
#include "AudioSampleG1_6.h"
#include "AudioSampleG1_7.h"
//#include "AudioSampleG2_1.h"
//#include "AudioSampleG2_2.h"
//#include "AudioSampleG2_3.h"
//#include "AudioSampleG2_4.h"
//#include "AudioSampleG2_5.h"
//#include "AudioSampleG2_6.h"
//#include "AudioSampleG2_7.h"
#include "AudioSampleG3_1.h"
#include "AudioSampleG3_2.h"
#include "AudioSampleG3_3.h"
#include "AudioSampleG3_4.h"
#include "AudioSampleG3_5.h"
#include "AudioSampleG3_6.h"
#include "AudioSampleG3_7.h"
//#include "AudioSampleG4_1.h"
//#include "AudioSampleG4_2.h"
//#include "AudioSampleG4_3.h"
//#include "AudioSampleG4_4.h"
//#include "AudioSampleG4_5.h"
//#include "AudioSampleG4_6.h"
//#include "AudioSampleG4_7.h"
#define BPM 160
// GUItool: begin automatically generated code
AudioPlayMemory playMem1; //xy=275,499
AudioPlayMemory playMem2; //xy=284,532.5
AudioPlayMemory playMem3; //xy=294,566.5
AudioPlayMemory playMem4; //xy=302,599.5
AudioMixer4 mixer1; //xy=491,517
AudioSynthWaveformDc dc1; //xy=507,581
AudioFilterStateVariable filter1; //xy=649,502
AudioOutputAnalogStereo dacs1; //xy=830,510
AudioConnection patchCord1(playMem1, 0, mixer1, 0);
AudioConnection patchCord2(playMem2, 0, mixer1, 1);
AudioConnection patchCord3(playMem3, 0, mixer1, 2);
AudioConnection patchCord4(playMem4, 0, mixer1, 3);
AudioConnection patchCord5(mixer1, 0, filter1, 0);
AudioConnection patchCord6(dc1, 0, dacs1, 1);
AudioConnection patchCord7(filter1, 0, dacs1, 0);
// GUItool: end automatically generated code
int timeToClick;
Adafruit_NeoTrellisM4 trellis = Adafruit_NeoTrellisM4();
Adafruit_ADXL343 accel = Adafruit_ADXL343(50, &Wire1); //50 does not mean anything here, just a random id
int colors[] = {
0x180000,
0x100800,
0x180000,
0x100800
};
int G1[] = {0,1,8,9,16,17,24,25};
int G2[] = {2,3,10,11,18,19,26,27};
int G3[] = {4,5,12,13,20,21,28,29};
int G4[] = {6,7,14,15,22,23,30,31};
int G1Select = 7;
int G1Trigger = 0;
int G2Select = 7;
int G2Trigger = 0;
int G3Select = 7;
int G3Trigger = 0;
int G4Select = 7;
int G4Trigger = 0;
int EighthNote = 60000 / (BPM * 2);
int clickOn;
void clockTick(){
if(millis() >= timeToClick) {
timeToClick = millis() + EighthNote; //214 should be ~1/8 at 140 bpm
//Serial.println(timeToClick);
dc1.amplitude(1); //turn Korg click on
clickOn = millis();
updatePlay();
}
}
void clickOff() {
if (millis()-clickOn>15) {
dc1.amplitude(0); //turn Korg click off
}
}
void updatePlay() {
//if there's less than 30ms until the end of the sample, retriger it to prevent gap
if (playMem1.lengthMillis() - playMem1.positionMillis() < 30) {
G1Trigger = 1;
}
if (G1Select < 7 && G1Trigger == 1) {
G1Trigger = 0;
switch (G1Select) {
case 6:
playMem1.play(AudioSampleG1_1);
break;
case 4:
playMem1.play(AudioSampleG1_2);
break;
case 2:
playMem1.play(AudioSampleG1_3);
break;
case 0:
playMem1.play(AudioSampleG1_4);
break;
case 1:
playMem1.play(AudioSampleG1_5);
break;
case 3:
playMem1.play(AudioSampleG1_6);
break;
case 5:
playMem1.play(AudioSampleG1_7);
break;
}
}
if (playMem2.lengthMillis() - playMem2.positionMillis() < 30) {
G2Trigger = 1;
}
if (G2Select < 7 && G2Trigger == 1) {
G2Trigger = 0;
switch (G2Select) {
case 6:
//playMem2.play(AudioSampleG2_1);
break;
case 4:
//playMem2.play(AudioSampleG2_2);
break;
case 2:
//playMem2.play(AudioSampleG2_3);
break;
case 0:
//playMem2.play(AudioSampleG2_4);
break;
case 1:
//playMem2.play(AudioSampleG2_5);
break;
case 3:
//playMem2.play(AudioSampleG2_6);
break;
case 5:
//playMem2.play(AudioSampleG2_7);
break;
}
}
if (playMem3.lengthMillis() - playMem3.positionMillis() < 30) {
G3Trigger = 1;
}
if (G3Select < 7 && G3Trigger == 1) {
G3Trigger = 0;
switch (G3Select) {
case 6:
playMem3.play(AudioSampleG3_1);
break;
case 4:
playMem3.play(AudioSampleG3_2);
break;
case 2:
playMem3.play(AudioSampleG3_3);
break;
case 0:
playMem3.play(AudioSampleG3_4);
break;
case 1:
playMem3.play(AudioSampleG3_5);
break;
case 3:
playMem3.play(AudioSampleG3_6);
break;
case 5:
playMem3.play(AudioSampleG3_7);
break;
}
}
if (playMem4.lengthMillis() - playMem4.positionMillis() < 30) {
G4Trigger = 1;
}
if (G4Select < 7 && G4Trigger == 1) {
G4Trigger = 0;
switch (G4Select) {
case 6:
//playMem4.play(AudioSampleG4_1);
break;
case 4:
//playMem4.play(AudioSampleG4_2);
break;
case 2:
//playMem4.play(AudioSampleG4_3);
break;
case 0:
//playMem4.play(AudioSampleG4_4);
break;
case 1:
//playMem4.play(AudioSampleG4_5);
break;
case 3:
//playMem4.play(AudioSampleG4_6);
break;
case 5:
//playMem4.play(AudioSampleG4_7);
break;
}
}
}
void redrawAndPlay() {
for (int r=0; r<4; r++) {
for (int c=0; c<4; c++) {
for (int g=0; g<2; g++) {
if (r==3 && g==1) {
trellis.setPixelColor(c*2 + g + r*8, 0x000000);
} else {
trellis.setPixelColor(c*2 + g + r*8, colors[c]);
}
};
};
};
if (G1Select != 7) {
trellis.setPixelColor(G1[G1Select], 0x333333);
} else {
playMem1.stop();
trellis.setPixelColor(G1[G1Select], 0x000000);
}
if (G2Select != 7) {
trellis.setPixelColor(G2[G2Select], 0x333333);
} else {
playMem2.stop();
trellis.setPixelColor(G2[G2Select], 0x000000);
}
if (G3Select != 7) {
trellis.setPixelColor(G3[G3Select], 0x333333);
} else {
playMem3.stop();
trellis.setPixelColor(G3[G3Select], 0x000000);
}
if (G4Select != 7) {
trellis.setPixelColor(G4[G4Select], 0x333333);
} else {
playMem4.stop();
trellis.setPixelColor(G4[G4Select], 0x000000);
}
}
void setup() {
Serial.begin(115200);
trellis.begin();
trellis.setBrightness(127);
accel.begin();
AudioMemory(32);
redrawAndPlay();
filter1.resonance(2.2); //from 0.7 to 5
};
void loop() {
trellis.tick();
clockTick();
clickOff();
while (trellis.available()){
keypadEvent e = trellis.read();
if (e.bit.EVENT == KEY_JUST_PRESSED) {
int key = e.bit.KEY;
for (int i=0; i<8; i++) {
if (key == G1[i]) {
G1Select = i;
G1Trigger = 1;
redrawAndPlay();
break;
}
else if (key == G2[i]) {
G2Select = i;
G2Trigger = 1;
redrawAndPlay();
break;
}
if (key == G3[i]) {
G3Select = i;
G3Trigger = 1;
redrawAndPlay();
break;
}
else if (key == G4[i]) {
G4Select = i;
G4Trigger = 1;
redrawAndPlay();
break;
}
}
}
}
//Accelerometer and filter
sensors_event_t event;
accel.getEvent(&event);
if (event.acceleration.y < 0) {
filter1.frequency(1000 * (event.acceleration.y+10));
} else {
filter1.frequency(10000);
}
}