diff --git a/examples/NoiseSensor/NoiseSensor.ino b/examples/NoiseSensor/NoiseSensor.ino index 1091751..e8d0b96 100644 --- a/examples/NoiseSensor/NoiseSensor.ino +++ b/examples/NoiseSensor/NoiseSensor.ino @@ -37,6 +37,7 @@ // Hardware // --------------------------------------------------------------------------- TFT_eSPI tft; +TFT_eSprite spr(&tft); TouchDrvGT911 touch; SoundEngine sound; SoundDetector soundDetector; @@ -351,7 +352,7 @@ static void newline() scrollUp(); } cursorDirty = true; - sndBlip(); + //sndBlip(); } static void putChar(char ch) @@ -362,7 +363,7 @@ static void putChar(char ch) if (ch < 32 || ch > 126) ch = '?'; grid[curRow][curCol] = ch; renderCell(curCol, curRow, 0, fxOn && bandRows[curRow]); - sndBlip(); + //sndBlip(); curCol++; if (curCol >= COLS) newline(); } @@ -393,6 +394,7 @@ static void printPrompt() // --------------------------------------------------------------------------- static void sndBlip() { + return; // Keep the device's own radar ping out of the microphone detector. soundDetector.suppress(200); SndEvent e = { SND_TONE, 540, 0, 10, 8000 }; @@ -402,6 +404,7 @@ static void sndBlip() static void sndClick() { + return; soundDetector.suppress(200); SndEvent e = { SND_TONE, 660, 0, 152, 5000 }; sound.play(e); @@ -611,15 +614,18 @@ struct RadarBlip { float distance; // Normalized 0.0 (near) to 1.0 (far boundary) uint8_t alpha; // Decay intensity (0 - 255) bool active; + int cycle; // sweep cycle in which it was placed + bool revealed; // true once the sweep has revealed it }; #define RADAR_CX 160 #define RADAR_CY 120 -#define MAX_BLIPS 5 +#define MAX_BLIPS 30 RadarBlip blips[MAX_BLIPS]; // Sweep line parameters (sweeps back and forth across the front wedge) float sweepAngle = -M_PI / 4.0; // Start at -45 degrees (left) +int sweepCycle = 0; int sweepDirection = 1; // 1 = sweeping right, -1 = sweeping left const float SWEEP_SPEED = 0.04; // Adjust sweep rate @@ -629,9 +635,9 @@ void initRadar() { } } -// Index of the blip currently driven by the microphone. Reused every frame so -// a continuous sound slides around instead of filling all MAX_BLIPS slots. -static int liveBlip = -1; +// Rotating index into the blip array: each sweep completion writes the next +// slot, keeping up to MAX_BLIPS recent positions. +static int nextBlip = 0; static int allocBlip() { for (int i = 0; i < MAX_BLIPS; i++) { @@ -650,36 +656,40 @@ static void placeBlip(int i, float lrBalance, float loudness) { // Loudness maps to distance: loudest is near origin, quiet is near edge blips[i].distance = constrain(1.0f - loudness, 0.15f, 0.95f); blips[i].alpha = 255; + blips[i].cycle = sweepCycle; + blips[i].revealed = false; } // One-shot blip (manual trigger / future events) void triggerBlip(float lrBalance, float loudness) { placeBlip(allocBlip(), lrBalance, loudness); - sndClick(); + //sndClick(); } -// Continuous update from the dual-microphone detector. -static void updateSoundBlip(float lrBalance, float loudness) { - if (liveBlip < 0 || !blips[liveBlip].active) { - liveBlip = allocBlip(); - } - placeBlip(liveBlip, lrBalance, loudness); -} + static void drawArc(float r, uint32_t color) { for (float a = -M_PI / 4.0; a <= M_PI / 4.0; a += 0.05) { int ax = RADAR_ORIGIN_X + (int)(r * sin(a)); int ay = RADAR_ORIGIN_Y - (int)(r * cos(a)); - tft.drawPixel(ax, ay, color); + spr.drawPixel(ax, ay, color); } } void radarUpdate() { + spr.fillSprite(C_BG); + spr.drawString("M314 MOTION TRACKER", 8, 2, 2); // 1. Draw Forward-Facing Sector Grid (Distance Arcs) // 50m, 100m, 150m, 200m equivalent scale lines for (int r = 50; r <= MAX_RADIUS; r += 50) { // Draw arc segments within the -45 to +45 degree wedge drawArc(r, C_DIM_GRN); + // Range readout at the right edge of the arc. + int rx = RADAR_ORIGIN_X + (int)(r * sin(M_PI / 4.0f)); + int ry = RADAR_ORIGIN_Y - (int)(r * cos(M_PI / 4.0f)); + char buf[8]; + snprintf(buf, sizeof(buf), "%d", r); + spr.drawString(buf, rx + 2, ry - 6, 1); } // 2. Draw Vector Boundary Lines (-45°, 0° Center, +45°) @@ -688,62 +698,94 @@ void radarUpdate() { int xRight = RADAR_ORIGIN_X + (int)(MAX_RADIUS * sin(M_PI / 4.0)); int yRight = RADAR_ORIGIN_Y - (int)(MAX_RADIUS * cos(M_PI / 4.0)); - tft.drawLine(RADAR_ORIGIN_X, RADAR_ORIGIN_Y, xLeft, yLeft, C_DIM_GRN); - tft.drawLine(RADAR_ORIGIN_X, RADAR_ORIGIN_Y, xRight, yRight, C_DIM_GRN); - tft.drawLine(RADAR_ORIGIN_X, RADAR_ORIGIN_Y, RADAR_ORIGIN_X, RADAR_ORIGIN_Y - MAX_RADIUS, C_DIM_GRN); // Center line + spr.drawLine(RADAR_ORIGIN_X, RADAR_ORIGIN_Y, xLeft, yLeft, C_DIM_GRN); + spr.drawLine(RADAR_ORIGIN_X, RADAR_ORIGIN_Y, xRight, yRight, C_DIM_GRN); + spr.drawLine(RADAR_ORIGIN_X, RADAR_ORIGIN_Y, RADAR_ORIGIN_X, RADAR_ORIGIN_Y - MAX_RADIUS, C_DIM_GRN); // Center line - // 3. Update & Draw Oscillating Forward Sweep Line - /*sweepAngle += SWEEP_SPEED * sweepDirection; - if (sweepAngle >= M_PI / 4.0) { - sweepAngle = M_PI / 4.0; - sweepDirection = -1; // Reverse sweep direction - } else if (sweepAngle <= -M_PI / 4.0) { - sweepAngle = -M_PI / 4.0; - sweepDirection = 1; + // 3. Oscillating sweep across the wedge. Read the mics each frame to know + // the target position; place the blip when the sweep passes it. + SoundReading snd = soundDetector.read(); + float lv = snd.slots[2]; + float rv = snd.slots[3]; + if (lv < 0.0f) lv = 0.0f; + if (rv < 0.0f) rv = 0.0f; + + // Side bars showing each channel's level. + int barH = 120; + int lh = (int)(lv * 10.0f * barH); + if (lh > barH) lh = barH; + int rh = (int)(rv * 10.0f * barH); + if (rh > barH) rh = barH; + spr.fillRect(2, SCR_H - 10 - barH, 4, barH, C_BG); + spr.fillRect(2, SCR_H - 10 - lh, 4, lh, C_GLOW); + spr.fillRect(SCR_W - 6, SCR_H - 10 - barH, 4, barH, C_BG); + spr.fillRect(SCR_W - 6, SCR_H - 10 - rh, 4, rh, C_GLOW); + + float total = lv + rv; + float threshold = 0.0005f; + float targetBalance = 0.0f; + float targetLoud = 0.0f; + bool hasTarget = (total > threshold); + if (hasTarget) { + targetBalance = (rv - lv) / total; + float stronger = (lv > rv) ? lv : rv; + targetLoud = stronger * 10.0f; + if (targetLoud > 1.0f) targetLoud = 1.0f; } - int sx = RADAR_ORIGIN_X + (int)(MAX_RADIUS * sin(sweepAngle)); - int sy = RADAR_ORIGIN_Y - (int)(MAX_RADIUS * cos(sweepAngle)); - tft.drawLine(RADAR_ORIGIN_X, RADAR_ORIGIN_Y, sx, sy, C_GLOW);*/ - sweepAngle += 10.0f;//SWEEP_SPEED; + // Place blips immediately when sound is detected, with a cooldown so a + // sustained sound doesn't flood all slots. + static uint32_t lastPlaceMs = 0; + if (hasTarget && (millis() - lastPlaceMs > 100)) { + placeBlip(nextBlip, targetBalance, targetLoud); + nextBlip = (nextBlip + 1) % MAX_BLIPS; + lastPlaceMs = millis(); + } + + // Arc sweep from center to outside. Place the blip when a full sweep + // completes (reaches MAX_RADIUS), using the latest detected target. + sweepAngle += 10.0f; if (sweepAngle > MAX_RADIUS * 2) { sweepAngle = 0; + sweepCycle++; sndBlip(); } else if (sweepAngle < MAX_RADIUS) { drawArc(sweepAngle, C_GLOW); } - // Feed the live microphone measurement into the blip layer. Balance moves - // it left/right; loudness pulls it toward the centre (closer target). - SoundReading snd = soundDetector.read(); - if (snd.active) { - updateSoundBlip(snd.balance, snd.loudness); - } - // 4. Render Active Target Blips with Phosphor Decay + for (int i = 0; i < MAX_BLIPS; i++) { if (blips[i].active) { float r = blips[i].distance * MAX_RADIUS; + // Reveal condition: placed in an earlier sweep and sweep passed it. + if (!blips[i].revealed) { + if (blips[i].cycle >= sweepCycle) continue; + if (r > sweepAngle) continue; + blips[i].revealed = true; + } // 0 angle = straight up (forward), negative = left, positive = right int bx = RADAR_ORIGIN_X + (int)(r * sin(blips[i].angle)); int by = RADAR_ORIGIN_Y - (int)(r * cos(blips[i].angle)); // Render blip core and outer glow if (blips[i].alpha > 128) { - tft.fillCircle(bx, by, 4, C_WHITE); - tft.drawCircle(bx, by, 7, C_GLOW); + spr.fillCircle(bx, by, 4, C_WHITE); + spr.drawCircle(bx, by, 7, C_GLOW); } else { - tft.drawCircle(bx, by, 4, C_GLOW); + spr.drawCircle(bx, by, 4, C_GLOW); } - // Decay blip intensity over time - if (blips[i].alpha > 15) { - blips[i].alpha -= 15; + // Decay blip intensity over time (own lifecycle) + if (blips[i].alpha > 3) { + blips[i].alpha -= 3; } else { blips[i].active = false; + blips[i].revealed = false; } } } + spr.pushSprite(0, 0); } // --------------------------------------------------------------------------- @@ -1012,6 +1054,7 @@ static void hardwareInit() tft.begin(); tft.setRotation(1); tft.fillScreen(TFT_BLACK); + spr.createSprite(SCR_W, SCR_H); pinMode(BOARD_BL_PIN, OUTPUT); for (int i = 0; i <= 16; ++i) { @@ -1043,8 +1086,8 @@ static void hardwareInit() static void bootSequence() { clearScreen(); - SndEvent e = { SND_SWEEP, 300, 900, 320, 10000 }; - sound.play(e); + //SndEvent e = { SND_SWEEP, 300, 900, 320, 10000 }; + //sound.play(e); bigPrintCentered("T-DECK", Y0 + 6 * CELL_H); bigPrintCentered("MOTION SENSOR", Y0 + 11 * CELL_H); delay(1600); @@ -1106,7 +1149,6 @@ void loop() pollTouch(); pollKeyboard(); - clearScreen(); //fxUpdate(); radarUpdate(); diff --git a/examples/NoiseSensor/sound_detector.cpp b/examples/NoiseSensor/sound_detector.cpp index e5d20d0..cf59890 100644 --- a/examples/NoiseSensor/sound_detector.cpp +++ b/examples/NoiseSensor/sound_detector.cpp @@ -26,7 +26,7 @@ #define MIC_I2S_PORT I2S_NUM_1 #define MIC_BLOCK_FRAMES 256 // 16 ms at 16 kHz #define MIC_SLOTS 4 // 4-slot TDM -#define MIC_GAIN GAIN_6DB +#define MIC_GAIN GAIN_30DB // Set to 1 to print detected levels while tuning the microphones on hardware. #ifndef SOUND_DETECTOR_DEBUG @@ -43,10 +43,10 @@ static constexpr float ENV_ATTACK = 0.55f; static constexpr float ENV_RELEASE = 0.18f; static constexpr float FLOOR_FALL = 0.12f; // fast follow when quieter static constexpr float FLOOR_RISE = 0.0008f; // very slow drift upward -static constexpr float FLOOR_MARGIN = 1.25f; +static constexpr float FLOOR_MARGIN = 1.0f; static constexpr float ABS_GATE = 0.0008f; -static constexpr float BALANCE_DEADBAND = 0.02f; -static constexpr float BALANCE_GAIN = 1.8f; // widen the usable stereo range +static constexpr float BALANCE_DEADBAND = 0.005f; +static constexpr float BALANCE_GAIN = 4.0f; // widen the usable stereo range static constexpr float LOUD_MIN_DB = -55.0f; static constexpr float LOUD_MAX_DB = -6.0f; static constexpr uint32_t HOLD_MS = 260; @@ -63,7 +63,7 @@ static constexpr float SLOT_MIN_RISE = 0.002f; // slow, so gaps keep the flo // A channel parked at the rails (overdriven analogue gain) carries no usable // audio; ignore it rather than reporting a permanent maximum. -static constexpr float RAIL_LEVEL = 0.50f; +static constexpr float RAIL_LEVEL = 0.95f; // The codec always delivers four TDM slots, but only two are fitted. The // power-up transient is very loud and can be mistaken for signal. @@ -213,9 +213,9 @@ void SoundDetector::taskLoop() // Still drain I2S so the DMA doesn't overflow while muted. Drop the // envelopes too, otherwise the device's own ping or the ADC // power-up transient would be the first thing we report. - envL_ = 0.0f; - envR_ = 0.0f; - publish(0.0f, 0.0f, 0.0f, 0.0f, false); + for (int s = 0; s < MIC_SLOTS; s++) { env_[s] = 0.0f; floor_[s] = 0.01f; } + float z[4] = {0,0,0,0}; + publish(0.0f, 0.0f, z, 0.0f, 0.0f, false); continue; } @@ -338,10 +338,7 @@ void SoundDetector::updateSlots(const float *rms) leftSlot_ = lo; rightSlot_ = hi; candCount_ = 0; - envL_ = 0.0f; - envR_ = 0.0f; - floorL_ = 0.01f; - floorR_ = 0.01f; + for (int s = 0; s < MIC_SLOTS; s++) { env_[s] = 0.0f; floor_[s] = 0.01f; } Serial.printf("Sound detector mic slots L=%d R=%d " "(swing %.4f %.4f %.4f %.4f)\n", leftSlot_, rightSlot_, @@ -389,46 +386,38 @@ void SoundDetector::processBlock(const int16_t *samples, size_t sampleCount) } #endif - float rmsL = rms[MIC_SWAP_LR ? rightSlot_ : leftSlot_]; - float rmsR = rms[MIC_SWAP_LR ? leftSlot_ : rightSlot_]; - if (rmsL > RAIL_LEVEL) rmsL = 0.0f; - if (rmsR > RAIL_LEVEL) rmsR = 0.0f; - - // Normalise to full scale, then smooth with a fast attack / slow release. - envL_ += (rmsL - envL_) * (rmsL > envL_ ? ENV_ATTACK : ENV_RELEASE); - envR_ += (rmsR - envR_) * (rmsR > envR_ ? ENV_ATTACK : ENV_RELEASE); - - // Per-channel noise floor: fall quickly when it gets quieter, and only - // creep up while the input is actually close to the floor. Without that - // guard a sustained loud sound (or a clipped channel) drags the floor up to - // its own level and the detector goes permanently deaf. - if (envL_ < floorL_) { - floorL_ += (envL_ - floorL_) * FLOOR_FALL; - } else if (envL_ < floorL_ * 1.5f) { - floorL_ += (envL_ - floorL_) * FLOOR_RISE; + // Monitor all four TDM slots independently. A railed slot is zeroed. + float sig[MIC_SLOTS]; + float peak = 0.0f; + bool present = false; + for (int s = 0; s < MIC_SLOTS; s++) { + float r = rms[s]; + if (r > RAIL_LEVEL) r = 0.0f; + env_[s] += (r - env_[s]) * (r > env_[s] ? ENV_ATTACK : ENV_RELEASE); + if (env_[s] < floor_[s]) { + floor_[s] += (env_[s] - floor_[s]) * FLOOR_FALL; + } else if (env_[s] < floor_[s] * 1.5f) { + floor_[s] += (env_[s] - floor_[s]) * FLOOR_RISE; + } else { + floor_[s] += (env_[s] - floor_[s]) * 0.05f; + } + if (floor_[s] < 1e-5f) floor_[s] = 1e-5f; + float v = env_[s] - floor_[s] * FLOOR_MARGIN; + if (v < 0.0f) v = 0.0f; + sig[s] = v; + if (v > peak) peak = v; + if (v > ABS_GATE) present = true; } - if (envR_ < floorR_) { - floorR_ += (envR_ - floorR_) * FLOOR_FALL; - } else if (envR_ < floorR_ * 1.5f) { - floorR_ += (envR_ - floorR_) * FLOOR_RISE; - } - if (floorL_ < 1e-5f) floorL_ = 1e-5f; - if (floorR_ < 1e-5f) floorR_ = 1e-5f; - - float sigL = envL_ - floorL_ * FLOOR_MARGIN; - float sigR = envR_ - floorR_ * FLOOR_MARGIN; - if (sigL < 0.0f) sigL = 0.0f; - if (sigR < 0.0f) sigR = 0.0f; - - // Adaptive gate: a sound must stand clearly above its channel's floor. - float gateL = fmaxf(floorL_ * 1.5f, ABS_GATE); - float gateR = fmaxf(floorR_ * 1.5f, ABS_GATE); - bool present = (sigL > gateL) || (sigR > gateR); uint32_t now = millis(); if (present) lastActiveMs_ = now; bool active = present || (uint32_t)(now - lastActiveMs_) < HOLD_MS; + // Keep a left/right summary for compatibility (max of the two left-ish / + // right-ish slots). + float sigL = fmaxf(sig[0], sig[2]); + float sigR = fmaxf(sig[1], sig[3]); + float total = sigL + sigR; float balance = 0.0f; if (total > 1e-6f) { @@ -436,9 +425,6 @@ void SoundDetector::processBlock(const int16_t *samples, size_t sampleCount) if (fabsf(balance) < BALANCE_DEADBAND) { balance = 0.0f; } else { - // Remove the deadband, rescale the remaining range to +/-1 and then - // widen it: the two microphones are close together, so a sound at - // the side only produces a few dB of level difference. float sign = balance < 0.0f ? -1.0f : 1.0f; balance = (balance - sign * BALANCE_DEADBAND) / (1.0f - BALANCE_DEADBAND); balance *= BALANCE_GAIN; @@ -447,7 +433,6 @@ void SoundDetector::processBlock(const int16_t *samples, size_t sampleCount) } } - float peak = fmaxf(sigL, sigR); float db = 20.0f * log10f(peak + 1e-9f); float loudness = (db - LOUD_MIN_DB) / (LOUD_MAX_DB - LOUD_MIN_DB); if (loudness < 0.0f) loudness = 0.0f; @@ -457,25 +442,25 @@ void SoundDetector::processBlock(const int16_t *samples, size_t sampleCount) static uint32_t lastPrintMs = 0; if ((uint32_t)(millis() - lastPrintMs) > 300) { lastPrintMs = millis(); - Serial.printf("mic rms %.4f %.4f %.4f %.4f | range %.4f %.4f %.4f %.4f\n", + Serial.printf("mic rms %.4f %.4f %.4f %.4f | sig %.3f %.3f %.3f %.3f%s\n", rms[0], rms[1], rms[2], rms[3], - slotMax_[0] - slotMin_[0], slotMax_[1] - slotMin_[1], - slotMax_[2] - slotMin_[2], slotMax_[3] - slotMin_[3]); - Serial.printf(" pair L%d R%d | sig %.3f %.3f | bal %+.2f loud %.2f%s\n", - leftSlot_, rightSlot_, sigL, sigR, balance, loudness, + sig[0], sig[1], sig[2], sig[3], active ? "" : " (quiet)"); } #endif - publish(sigL, sigR, balance, loudness, active); + publish(sigL, sigR, sig, balance, loudness, active); } -void SoundDetector::publish(float left, float right, float balance, float loudness, - bool active) +void SoundDetector::publish(float left, float right, const float *slots, float balance, + float loudness, bool active) { portENTER_CRITICAL(&mux_); reading_.left = left; reading_.right = right; + if (slots) { + for (int s = 0; s < 4; s++) reading_.slots[s] = slots[s]; + } reading_.balance = balance; reading_.loudness = loudness; reading_.active = active; diff --git a/examples/NoiseSensor/sound_detector.h b/examples/NoiseSensor/sound_detector.h index 53fb1ec..b0eebf0 100644 --- a/examples/NoiseSensor/sound_detector.h +++ b/examples/NoiseSensor/sound_detector.h @@ -17,6 +17,7 @@ struct SoundReading { float left; // smoothed level above the noise floor (0..1) float right; // smoothed level above the noise floor (0..1) + float slots[4]; // per-slot smoothed level above the noise floor (0..1) float balance; // -1 = far left, 0 = centre, +1 = far right float loudness; // 0 = quiet, 1 = loud (dB-mapped) bool active; // true while a sound is present (with a short hold) @@ -45,7 +46,7 @@ private: void updateSlots(const float *rms); size_t sampleOffset(int slot) const; void processBlock(const int16_t *samples, size_t sampleCount); - void publish(float left, float right, float balance, float loudness, bool active); + void publish(float left, float right, const float *slots, float balance, float loudness, bool active); uint32_t sampleRate_ = 16000; TaskHandle_t task_ = nullptr; @@ -56,10 +57,8 @@ private: SoundReading reading_ = {}; uint32_t lastActiveMs_ = 0; - float envL_ = 0.0f; - float envR_ = 0.0f; - float floorL_ = 0.01f; - float floorR_ = 0.01f; + float env_[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; + float floor_[4] = { 0.01f, 0.01f, 0.01f, 0.01f }; // The two TDM slots carrying the populated microphones. Seeded with the // documented T-Deck pair (MIC1 = slot 0, MIC3 = slot 2) and corrected at