From 06642bd570566aac116fca5c396917b683a269b0 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 17 Jun 2025 16:19:42 +0200 Subject: [PATCH] beeps and clicks and more config options for colors --- README.md | 40 ++++++- soundcube-firmware/soundcube-firmware.ino | 121 +++++++++++++++------- 2 files changed, 118 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index cf80b32..ac427e3 100644 --- a/README.md +++ b/README.md @@ -34,11 +34,33 @@ Json formatted config file. For now only edge led color working. ``` { + "boxid":"", "edge": { "color":{ - "r":50, - "g":0, - "b":0 + "idle":{ + "r":50, + "g":0, + "b":50 + }, + "active":{ + "r":0, + "g":50, + "b":50 + } + } + }, + "ring": { + "color":{ + "idle":{ + "r":0, + "g":50, + "b":50 + }, + "active":{ + "r":0, + "g":80, + "b":50 + } } } } @@ -58,21 +80,33 @@ Put all sounds into the `/sound` subfolder. Name them `1.wav, 2.wav, 3.wav...` Export all sounds except `click.wav` as **48000Hz (48kHz) 16bit Stereo**. +`click.wav` needs to be **22050Hz (22kHz) 16bit Mono** + ### Audacity You can use Audacity to export all soundfiles to WAV format. +Download it here [Github](https://github.com/audacity/audacity/releases) + 1. Load file into Audacity 2. Select Track 3. File -> Export Audio #### Format options in Audacity +**Sounds** + - WAV(Microsoft) - Chanels: Stereo - Samplerate: 48000Hz - Encoding: Signed 16-bit PCM +**click.wav** + +- WAV(Microsoft) +- Channels: Mono +- Samplerate: 22050Hz +- Encoding: Signed 16-bit PCM ---- diff --git a/soundcube-firmware/soundcube-firmware.ino b/soundcube-firmware/soundcube-firmware.ino index 431a94a..3048548 100644 --- a/soundcube-firmware/soundcube-firmware.ino +++ b/soundcube-firmware/soundcube-firmware.ino @@ -56,22 +56,34 @@ CRGB ui_leds[74]; CRGB edge_leds[11]; volatile bool buttonChanged = false; -volatile bool click = false; +volatile bool ui_click = false; +volatile bool ui_beep = false; volatile bool amp = false; int counter = 0; bool buttons[16] = {false}; -int16_t beep[UI_SAMPLERATE]; +int16_t ui_click_snd[UI_SAMPLERATE]; +uint16_t click_length = 0; + +int16_t ui_beep_snd[UI_SAMPLERATE]; uint16_t beep_length = 0; bool sd_card_detected = false; +struct Color { + int r; + int g; + int b; + int r_active; + int g_active; + int b_active; +}; + struct Config { - int edge_color_r = 0; - int edge_color_g = 0; - int edge_color_b = 50; + Color edge_color = {0,0,50,0,50,0}; + Color ring_color = {0,50,50,0,80,50}; }; Config config; @@ -83,10 +95,20 @@ void loadConfiguration(Config& config) { DeserializationError error = deserializeJson(doc, file); if(error) Serial.println(F("Failed to read file, using default configuration")); - config.edge_color_r = doc["edge"]["color"]["r"] | 0; - config.edge_color_g = doc["edge"]["color"]["g"] | 0; - config.edge_color_b = doc["edge"]["color"]["b"] | 50; + config.edge_color.r = doc["edge"]["color"]["idle"]["r"] | 0; + config.edge_color.g = doc["edge"]["color"]["idle"]["g"] | 0; + config.edge_color.b = doc["edge"]["color"]["idle"]["b"] | 50; + config.edge_color.r_active = doc["edge"]["color"]["active"]["r"] | 0; + config.edge_color.g_active = doc["edge"]["color"]["active"]["g"] | 50; + config.edge_color.b_active = doc["edge"]["color"]["active"]["b"] | 0; + config.ring_color.r = doc["ring"]["color"]["idle"]["r"] | 0; + config.ring_color.g = doc["ring"]["color"]["idle"]["g"] | 50; + config.ring_color.b = doc["ring"]["color"]["idle"]["b"] | 50; + config.ring_color.r_active = doc["ring"]["color"]["active"]["r"] | 0; + 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)); file.close(); @@ -116,13 +138,24 @@ void codec_receive(){ void pwm_audio_callback() { while (ui_snd.availableForWrite()) { - if(click) { - ui_snd.write(beep[counter]); - counter++; - if(counter == beep_length) { - counter = 0; - click = false; + if(ui_click || ui_beep) { + if(ui_beep) { + ui_snd.write(ui_beep_snd[counter]); + counter++; + if(counter == beep_length) { + counter = 0; + ui_beep = false; + } + continue; } + if(ui_click) { + ui_snd.write(ui_click_snd[counter]); + counter++; + if(counter == click_length) { + counter = 0; + ui_click = false; + } + } } else { digitalWrite(7, LOW); ui_snd.write(0); @@ -131,6 +164,32 @@ void pwm_audio_callback() { } } +bool load_ui_sounds(const char* file, int16_t *buffer, uint16_t &length){ + if(SD.exists(file)) { + File f_click = SD.open(file); + f_click.seek(44, SeekSet); + int click_bytes = 0; + while (f_click.available() && click_bytes < UI_SAMPLERATE) { + uint8_t samplebyte[2]; + f_click.read(samplebyte, 2); + int16_t sample = (samplebyte[1] << 8) + samplebyte[0]; + buffer[click_bytes] = sample; + click_bytes++; + } + f_click.close(); + length = click_bytes; + return true; + } else { + for(int i = 0; i < UI_SAMPLERATE; i++){ + float sine_pos = (2.0f * M_PI * 3000.0f * (float)i) / (float)UI_SAMPLERATE; + buffer[i] = (int16_t)(sin(sine_pos) * 8000.0f); + } + Serial.println("Synthesized 8kHz sine into beep array."); + length = UI_SAMPLERATE; + } + return false; +} + void tca_irq() { buttonChanged = true; } @@ -170,27 +229,9 @@ void setup() { if(sdInitialized) loadConfiguration(config); - if(sdInitialized && SD.exists("/click.wav")) { - File click = SD.open("/click.wav"); - click.seek(44, SeekSet); - int click_bytes = 0; - while (click.available() || click_bytes == UI_SAMPLERATE-1) { - uint8_t samplebyte[2]; - click.read(samplebyte, 2); - int16_t sample = (samplebyte[1] << 8) + samplebyte[0]; - beep[click_bytes] = sample; - click_bytes++; - } - click.close(); - beep_length = click_bytes; - - } else { - for(int i = 0; i < UI_SAMPLERATE; i++){ - float sine_pos = (2.0f * M_PI * 8000.0f * (float)i) / (float)UI_SAMPLERATE; - beep[i] = (int16_t)(sin(sine_pos) * 8000.0f); - } - Serial.println("Synthesized 8kHz sine into beep array."); - beep_length = UI_SAMPLERATE; + if(sdInitialized){ + load_ui_sounds("/ui/click.wav", ui_click_snd, click_length); + load_ui_sounds("/ui/beep.wav", ui_beep_snd, beep_length); } Serial.println("INIT WIRE"); @@ -220,7 +261,7 @@ void setup() { ui_snd.onTransmit(pwm_audio_callback); ui_snd.begin(UI_SAMPLERATE); - digitalWrite(7, LOW); + digitalWrite(7, LOW); // UI amp off pinMode(19, OUTPUT); // MCLK enable digitalWrite(19, HIGH); // enable MCLK @@ -294,7 +335,6 @@ void setup() { digitalWrite(6, HIGH); delay(25); digitalWrite(6, LOW); - } bool dactest = false; @@ -308,7 +348,7 @@ void loop() { // EDGE LEDs for (int i = 0; i < 8; i++) { - edge_leds[i] = CRGB(config.edge_color_r, config.edge_color_g, config.edge_color_b); + edge_leds[i] = CRGB(config.edge_color.r, config.edge_color.g, config.edge_color.b); } // LED Ring leeren @@ -353,7 +393,7 @@ void loop() { // Make beep if (AURAL && buttonDown) { digitalWrite(7, HIGH); - click = true; + ui_click = true; } // Flash encoder leds @@ -390,6 +430,7 @@ void loop() { if(buttons[SELECT]){ stream[active % NSTREAMS].toggle(); + ui_beep = true; } Serial.println(codec.getVolumeL()); @@ -418,7 +459,7 @@ void loop() { // set active LED ring LED for(int i = 0; i < active_led_ring; i++){ - ui_leds[lut_ring_ccw[i]] = CRGB(50, 0, 25); + ui_leds[lut_ring_ccw[i]] = CRGB(config.ring_color.r, config.ring_color.g, config.ring_color.b); } FastLED.show();