sequencer timing basics done

This commit is contained in:
2025-06-18 12:54:34 +02:00
parent 4a04e95757
commit 6dd3acac2f
2 changed files with 100 additions and 69 deletions

View File

@ -19,7 +19,7 @@
#define UI_SAMPLERATE 22050
#define BUFFERSIZE 64
#define NSTREAMS 8
#define NSTREAMS 16
int16_t buffer[BUFFERSIZE];
@ -38,7 +38,11 @@ DAC8552 dac(9, &SPI1);
PWMAudio ui_snd(8);
enum BUTTON {CVINL, CVINR, INL, INR, OUTR, OUTL, CVOUTR, CVOUTL, RIGHT, LEFT, SELECT, DEBUG1, DEBUG2, DEBUG3};
enum STATE {IDLE, MODE_A, MODE_B, MODE_C};
STATE state = IDLE;
//enum BUTTON {CVINL, CVINR, INL, INR, OUTR, OUTL, CVOUTR, CVOUTL, RIGHT, LEFT, SELECT, DEBUG1, DEBUG2, DEBUG3};
enum BUTTON {MODE, LOOP, INL, INR, OUTR, OUTL, TEMPO, RESET, BACK, POWER, SELECT, DEBUG1, DEBUG2, DEBUG3};
int lut_ring_cw[48] = {39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,50,49,48,47,46,45,44,43,42,41,40};
int lut_ring_ccw[48] = {40,41,42,43,44,45,46,47,48,49,50,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39};
@ -82,8 +86,11 @@ struct Color {
};
struct Config {
String boxid = "";
Color edge_color = {0,0,50,0,50,0};
Color ring_color = {0,50,50,0,80,50};
String sampleFiles[16];
int bpm = 90;
};
Config config;
@ -109,7 +116,13 @@ void loadConfiguration(Config& config) {
config.ring_color.g_active = doc["ring"]["color"]["active"]["g"] | 80;
config.ring_color.b_active = doc["ring"]["color"]["active"]["b"] | 50;
//strlcpy(config.hostname, doc["hostname"] | "example.com", sizeof(config.hostname));
JsonArray _sampleFiles = doc["sampleFiles"];
config.boxid = doc["boxid"].as<String>();
int i = 0;
for(JsonVariant item : _sampleFiles){
config.sampleFiles[i] = item.as<String>();
i++;
}
file.close();
}
@ -190,6 +203,43 @@ bool load_ui_sounds(const char* file, int16_t *buffer, uint16_t &length){
return false;
}
bool load_samples(){
for(int i = 0; i < NSTREAMS; i++){
stream[i].begin();
char filename[64];
sprintf(filename, "/sound/%s/%s", config.boxid.c_str(), config.sampleFiles[i].c_str());
if(SD.exists(filename)){
bool loaded = stream[i].load(SD.open(filename));
if(loaded) {
Serial.print("file read: ");
Serial.print(stream[i].wavefile.length);
Serial.print(" bytes | ");
Serial.print(stream[i].wavefile.samplerate);
Serial.print(" kHz | ");
Serial.print(stream[i].wavefile.bitspersample);
Serial.print(" bits | ");
Serial.print(stream[i].wavefile.channels);
Serial.print(" channels | ");
Serial.print(stream[i].wavefile.blockalign);
Serial.println(" bytes");
//stream[i].wavefile.loop = true;
} else {
Serial.println("file loading error");
}
} else {
for(int k = 0; k < 3; k++){
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
delay(50);
}
return false;
}
}
return true;
}
void tca_irq() {
buttonChanged = true;
}
@ -254,6 +304,7 @@ void setup() {
// Rotary Encoder
ENC.begin(); // set direction pin.
ENC.setDirection(AS5600_COUNTERCLOCK_WISE);
ENC.resetCumulativePosition();
pinMode(6, OUTPUT); // Vibration Motor
pinMode(7, OUTPUT); // UI Amp Enable
@ -291,41 +342,7 @@ void setup() {
}
if(sdInitialized) {
for(int i = 0; i < NSTREAMS; i++){
stream[i].begin();
char filename[40];
sprintf(filename, "/sound/%d.wav", i+1);
if(SD.exists(filename)){
bool loaded = stream[i].load(SD.open(filename));
if(loaded) {
Serial.print("file read: ");
Serial.print(stream[i].wavefile.length);
Serial.print(" bytes | ");
Serial.print(stream[i].wavefile.samplerate);
Serial.print(" kHz | ");
Serial.print(stream[i].wavefile.bitspersample);
Serial.print(" bits | ");
Serial.print(stream[i].wavefile.channels);
Serial.print(" channels | ");
Serial.print(stream[i].wavefile.blockalign);
Serial.println(" bytes");
//stream[i].play();
stream[i].wavefile.loop = true;
} else {
Serial.println("file loading error");
}
} else {
for(int k = 0; k < 3; k++){
digitalWrite(6, HIGH);
delay(100);
digitalWrite(6, LOW);
delay(50);
}
}
}
streams_loaded = true;
streams_loaded = load_samples();
}
digitalWrite(6, HIGH);
@ -335,13 +352,28 @@ void setup() {
digitalWrite(6, HIGH);
delay(25);
digitalWrite(6, LOW);
}
bool dactest = false;
uint32_t last = 0;
bool tick = false;
int bar = 0;
int set_bar = 0;
void loop() {
position = ENC.getCumulativePosition();
uint32_t delta_bpm = floor((60000 / config.bpm) / 16);
if(millis() - last >= delta_bpm) {
tick = true;
last = millis();
}
if(tick) bar = (bar + 1) % 16;
tick = false;
sd_card_detected = !digitalRead(21);
if(sd_card_detected) edge_leds[8] = CRGB(0,10,0);
if(!sd_card_detected) edge_leds[8] = CRGB(10,0,0);
@ -396,50 +428,39 @@ void loop() {
ui_click = true;
}
// Flash encoder leds
ui_leds[0] = CRGB(0, 100, 50);
ui_leds[1] = CRGB(0, 100, 50);
ui_leds[2] = CRGB(0, 100, 50);
// Switch through LED matrix
if(buttons[RIGHT]) active++;
if(buttons[LEFT]) active--;
if(active == 13) active = 0;
if(active == -1) active = 12;
// Set bar
if(buttons[BACK]) set_bar++;
if(buttons[POWER]) set_bar--;
if(set_bar == 16) set_bar = 0;
if(set_bar == -1) set_bar = 15;
if(buttons[CVINL]) {
if(buttons[DEBUG1]) {
Serial.println("vol down");
codec.volumeDown();
}
if(buttons[CVOUTL]) {
if(buttons[DEBUG3]) {
Serial.println("vol up");
codec.volumeUp();
}
if(buttons[DEBUG3]) {
if(buttons[DEBUG2]) {
speakerToggle = !speakerToggle;
speaker(speakerToggle);
}
if(buttons[DEBUG2]) {
dactest = !dactest;
dac.setValue(0, dactest ? 0 : 32768);
dac.setValue(1, !dactest ? 0 : 65535);
}
if(buttons[SELECT]){
stream[active % NSTREAMS].toggle();
ui_beep = true;
// Flash encoder leds
ui_leds[0] = CRGB(0, 100, 50);
ui_leds[1] = CRGB(0, 100, 50);
ui_leds[2] = CRGB(0, 100, 50);
}
Serial.println(codec.getVolumeL());
//for(int i = 0; i < NSTREAMS; i++){
// stream[i].pause();
// }
//stream[active % NSTREAMS].play();
buttonChanged = false;
}
@ -454,14 +475,24 @@ void loop() {
// set active LED matrix LED
ui_leds[lut_matrix[active]] = CRGB(100, 100, 100);
if(position < 0) position += 4096;
active_led_ring = (position / 32) % 48;
// set active LED ring LED
for(int i = 0; i < active_led_ring; i++){
ui_leds[lut_ring_ccw[i]] = CRGB(config.ring_color.r, config.ring_color.g, config.ring_color.b);
for(int i = 0; i < 48; i++){
if(floor(i/3) == set_bar){
ui_leds[lut_ring_cw[i]] = CRGB(config.ring_color.r_active, config.ring_color.g_active, config.ring_color.b_active);
}
if(floor(i/3) == bar){
ui_leds[lut_ring_cw[i]] = CRGB(config.ring_color.r, config.ring_color.g, config.ring_color.b);
}
}
// if(position < 0) position += 4096;
// active_led_ring = (position / 32) % 48;
// // set active LED ring LED
// for(int i = 0; i < active_led_ring; i++){
// ui_leds[lut_ring_ccw[i]] = CRGB(config.ring_color.r, config.ring_color.g, config.ring_color.b);
// }
FastLED.show();
if(streams_loaded) {
for(int i = 0; i < NSTREAMS; i++){

View File

@ -110,7 +110,7 @@ class WaveStream{
WaveStream(){}
void begin(){
wavefile.buffer.setSize(24000);
wavefile.buffer.setSize(8000);
wavefile.buffer.begin();
}