Add NoiseSensor radar example and fix short sound playback

The sound task used to zero the I2S TX DMA ring as soon as the event
queue drained. i2s_write() only queues samples, so anything shorter than
the ~64 ms ring was erased before it reached the speaker. Enable
tx_desc_auto_clear instead, which clears each descriptor after the DMA
has actually transmitted it.

Also add the NoiseSensor radar example and point the default PlatformIO
environment at it.
This commit is contained in:
Dejvino
2026-11-12 08:14:19 +01:00
parent 951c56168c
commit 7dfdc06e6f
6 changed files with 1439 additions and 1 deletions
File diff suppressed because it is too large Load Diff
+219
View File
@@ -0,0 +1,219 @@
/**
* @file sound_engine.cpp
* @brief I2S speaker rendering task (8 kHz, 16-bit, mono).
*
* Based on the wiring used by examples/SimpleTone:
* BCK = GPIO7, WS = GPIO5, DOUT = GPIO6.
*
* Sounds are rendered strictly in queue order; layering is achieved by
* queueing several events back to back (e.g. a rumble sweep followed by a
* noise tail reads as one big explosion).
*/
#include "sound_engine.h"
#include "utilities.h"
#include <driver/i2s.h>
#define SND_SAMPLE_RATE 8000
#define SND_I2S_PORT I2S_NUM_0
#define SND_CHUNK 128 // samples per i2s_write call
#define SND_FADE_MS 2 // fade in/out to avoid clicks
bool SoundEngine::begin()
{
i2s_config_t cfg = {};
cfg.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX);
cfg.sample_rate = SND_SAMPLE_RATE;
cfg.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT;
cfg.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT;
cfg.communication_format = I2S_COMM_FORMAT_STAND_I2S;
cfg.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1;
cfg.dma_buf_count = 8;
cfg.dma_buf_len = 64;
// Zero each DMA descriptor as the ISR finishes sending it. Without this,
// the descriptor ring keeps re-transmitting old audio after the queue
// drains (the repeated-tail bug). Clearing here is safe: it happens after
// that buffer has actually been played, so pending audio is untouched.
cfg.tx_desc_auto_clear = true;
if (i2s_driver_install(SND_I2S_PORT, &cfg, 0, NULL) != ESP_OK) {
return false;
}
i2s_pin_config_t pins = {};
pins.bck_io_num = BOARD_I2S_BCK;
pins.ws_io_num = BOARD_I2S_WS;
pins.data_out_num = BOARD_I2S_DOUT;
pins.data_in_num = I2S_PIN_NO_CHANGE;
i2s_set_pin(SND_I2S_PORT, &pins);
q_ = xQueueCreate(12, sizeof(SndEvent));
if (q_ == nullptr) {
return false;
}
// Render on core 0; the game loop runs on core 1.
return xTaskCreatePinnedToCore(trampoline, "sound", 4096, this, 5, nullptr, 0) == pdTRUE;
}
void SoundEngine::play(const SndEvent &e)
{
if (q_) {
xQueueSend(q_, &e, 0); // drop instead of blocking the game
}
}
void SoundEngine::trampoline(void *arg)
{
static_cast<SoundEngine *>(arg)->task();
}
void SoundEngine::task()
{
for (;;) {
SndEvent e;
xQueueReceive(q_, &e, portMAX_DELAY);
switch (e.type) {
case SND_TONE: renderTone(e.freq, e.dur, e.amp); break;
case SND_SWEEP: renderSweep(e.freq, e.freq2, e.dur, e.amp); break;
case SND_NOISE: renderNoise(e.dur, e.amp); break;
}
}
}
void SoundEngine::volumeUp()
{
if (vol_ < VOL_MAX) vol_++;
}
void SoundEngine::volumeDown()
{
if (vol_ > 0) vol_--;
}
void SoundEngine::toggleMute()
{
if (vol_ > 0) {
lastVol_ = vol_;
vol_ = 0;
} else {
vol_ = lastVol_ > 0 ? lastVol_ : VOL_MAX / 2;
}
}
// Restore a saved volume level (from NVS at boot); 0 keeps the device muted
// while lastVol_ stays at its default so unmute still works.
void SoundEngine::setVolume(uint8_t v)
{
if (v > VOL_MAX) v = VOL_MAX;
vol_ = v;
if (vol_ > 0) lastVol_ = vol_;
}
// scaled peak amplitude for the current volume level
static inline uint32_t scaledAmp(uint16_t amp, uint8_t vol)
{
return ((uint32_t)amp * vol) / SoundEngine::VOL_MAX;
}
// fade envelope over the first/last SND_FADE_MS of the whole event
static inline uint32_t envelope(uint32_t pos, uint32_t total, uint32_t fade, uint32_t amp)
{
if (fade * 2 > total) fade = total / 2;
uint32_t s = amp;
if (pos < fade) s = s * pos / fade;
uint32_t tail = total - pos;
if (tail < fade) s = s * tail / fade;
return s;
}
void SoundEngine::renderTone(uint16_t freq, uint16_t durMs, uint16_t amp)
{
static int16_t buf[SND_CHUNK];
uint32_t a = scaledAmp(amp, vol_);
if (a == 0) return;
uint32_t total = (uint32_t)SND_SAMPLE_RATE * durMs / 1000;
if (total == 0) return;
uint32_t half = (freq > 0) ? (SND_SAMPLE_RATE / 2) / freq : 1;
if (half == 0) half = 1;
uint32_t fade = (uint32_t)SND_SAMPLE_RATE * SND_FADE_MS / 1000;
if (fade < 2) fade = 2;
uint32_t phase = 0;
for (uint32_t i = 0; i < total; ) {
uint32_t n = (total - i < SND_CHUNK) ? (total - i) : SND_CHUNK;
for (uint32_t k = 0; k < n; k++) {
int32_t s = ((phase / half) & 1) ? (int32_t)a : -(int32_t)a;
buf[k] = (int16_t)(s * (int32_t)envelope(i + k, total, fade, 256) / 256);
phase++;
}
size_t bw;
i2s_write(SND_I2S_PORT, buf, n * sizeof(int16_t), &bw, portMAX_DELAY);
i += n;
}
}
// Square wave gliding from freq0 to freq1 (Hz) over the duration.
// Phase is carried in a float cycle counter (0..1) so frequency changes
// never click.
void SoundEngine::renderSweep(uint16_t freq0, uint16_t freq1, uint16_t durMs, uint16_t amp)
{
static int16_t buf[SND_CHUNK];
uint32_t a = scaledAmp(amp, vol_);
if (a == 0) return;
uint32_t total = (uint32_t)SND_SAMPLE_RATE * durMs / 1000;
if (total == 0) return;
uint32_t fade = (uint32_t)SND_SAMPLE_RATE * SND_FADE_MS / 1000;
if (fade < 2) fade = 2;
float phase = 0.0f;
for (uint32_t i = 0; i < total; ) {
uint32_t n = (total - i < SND_CHUNK) ? (total - i) : SND_CHUNK;
for (uint32_t k = 0; k < n; k++) {
uint32_t pos = i + k;
float f = (float)freq0 +
((float)(int16_t)(freq1 - freq0) * (float)pos) / (float)total;
if (f < 20.0f) f = 20.0f;
phase += f / (float)SND_SAMPLE_RATE;
if (phase >= 1.0f) phase -= 1.0f;
int32_t s = (phase < 0.5f) ? (int32_t)a : -(int32_t)a;
buf[k] = (int16_t)(s * (int32_t)envelope(pos, total, fade, 256) / 256);
}
size_t bw;
i2s_write(SND_I2S_PORT, buf, n * sizeof(int16_t), &bw, portMAX_DELAY);
i += n;
}
}
void SoundEngine::renderNoise(uint16_t durMs, uint16_t amp)
{
static int16_t buf[SND_CHUNK];
uint32_t a = scaledAmp(amp, vol_);
if (a == 0) return;
uint32_t total = (uint32_t)SND_SAMPLE_RATE * durMs / 1000;
if (total == 0) return;
uint32_t seed = esp_random() | 1;
for (uint32_t i = 0; i < total; ) {
uint32_t n = (total - i < SND_CHUNK) ? (total - i) : SND_CHUNK;
for (uint32_t k = 0; k < n; k++) {
// xorshift32 for cheap white noise
seed ^= seed << 13;
seed ^= seed >> 17;
seed ^= seed << 5;
int32_t signedS = (int32_t)(seed & 0xFFFF) - 32768;
// linear decay over the burst
int32_t s = (int32_t)(((int64_t)signedS * a * (int64_t)(total - i - k)) /
((int64_t)32768 * (int64_t)total));
buf[k] = (int16_t)s;
}
size_t bw;
i2s_write(SND_I2S_PORT, buf, n * sizeof(int16_t), &bw, portMAX_DELAY);
i += n;
}
}
+55
View File
@@ -0,0 +1,55 @@
/**
* @file sound_engine.h
* @brief Tiny non-blocking I2S sound engine for the T-Deck speaker.
*
* A FreeRTOS task owns the I2S peripheral (speaker, I2S_NUM_0) and renders
* queued sound events one after another. The game only ever pushes events,
* so it never blocks. Event types:
*
* SND_TONE fixed square-wave beep
* SND_SWEEP square wave gliding from freq down/up to freq2 (sirens, booms)
* SND_NOISE white-noise burst with linear decay (explosions)
*/
#pragma once
#include <Arduino.h>
enum SndType : uint8_t {
SND_TONE = 0, // fixed square wave
SND_SWEEP, // square wave gliding freq -> freq2
SND_NOISE, // white noise burst
};
struct SndEvent {
SndType type;
uint16_t freq; // Hz (tone, or sweep start)
uint16_t freq2; // Hz (sweep end; ignored for tone/noise)
uint16_t dur; // duration in ms
uint16_t amp; // peak amplitude 0..32767
};
class SoundEngine {
public:
static const uint8_t VOL_MAX = 8;
bool begin();
void play(const SndEvent &e);
// Master volume, applied to every rendered sound
void volumeUp();
void volumeDown();
void toggleMute();
void setVolume(uint8_t v); // restore a saved level (0 = muted)
uint8_t volume() const { return vol_; }
private:
static void trampoline(void *arg);
void task();
void renderTone(uint16_t freq, uint16_t durMs, uint16_t amp);
void renderSweep(uint16_t freq0, uint16_t freq1, uint16_t durMs, uint16_t amp);
void renderNoise(uint16_t durMs, uint16_t amp);
QueueHandle_t q_ = nullptr;
uint8_t vol_ = 5; // default: moderate
uint8_t lastVol_ = 5; // remembered for mute toggle
};
+85
View File
@@ -0,0 +1,85 @@
/**
* @file utilities.h
* @author Lewis He (lewishe@outlook.com)
* @license MIT
* @copyright Copyright (c) 2023 Shenzhen Xin Yuan Electronic Technology Co., Ltd
* @date 2023-04-11
*
*/
#pragma once
//! The board peripheral power control pin needs to be set to HIGH when using the peripheral
#define BOARD_POWERON 10
#define BOARD_I2S_WS 5
#define BOARD_I2S_BCK 7
#define BOARD_I2S_DOUT 6
#define BOARD_I2C_SDA 18
#define BOARD_I2C_SCL 8
#define BOARD_BAT_ADC 4
#define BOARD_TOUCH_INT 16
#define BOARD_KEYBOARD_INT 46
#define BOARD_SDCARD_CS 39
#define BOARD_TFT_CS 12
#define RADIO_CS_PIN 9
#define BOARD_TFT_DC 11
#define BOARD_TFT_BACKLIGHT 42
#define BOARD_SPI_MOSI 41
#define BOARD_SPI_MISO 38
#define BOARD_SPI_SCK 40
#define BOARD_TBOX_G02 2
#define BOARD_TBOX_G01 3
#define BOARD_TBOX_G04 1
#define BOARD_TBOX_G03 15
#define BOARD_ES7210_MCLK 48
#define BOARD_ES7210_LRCK 21
#define BOARD_ES7210_SCK 47
#define BOARD_ES7210_DIN 14
#define RADIO_BUSY_PIN 13
#define RADIO_RST_PIN 17
#define RADIO_DIO1_PIN 45
#define BOARD_BOOT_PIN 0
#define BOARD_BL_PIN 42
#define BOARD_GPS_TX_PIN 43
#define BOARD_GPS_RX_PIN 44
#ifndef RADIO_FREQ
#define RADIO_FREQ 868.0
#endif
#ifndef RADIO_BANDWIDTH
#define RADIO_BANDWIDTH 125.0
#endif
#ifndef RADIO_SF
#define RADIO_SF 10
#endif
#ifndef RADIO_CR
#define RADIO_CR 6
#endif
#ifndef RADIO_TX_POWER
#define RADIO_TX_POWER 22
#endif
#define DEFAULT_OPA 100