This commit is contained in:
Patrick Moessler 2025-03-17 03:57:33 +01:00
parent 5a726f7bb6
commit 1c2b2be596
5 changed files with 376 additions and 3 deletions

View file

@ -25,6 +25,21 @@ experimental = ["esp-idf-svc/experimental"]
[dependencies]
log = "0.4"
esp-idf-svc = { version = "0.51", features = ["critical-section", "embassy-time-driver", "embassy-sync"] }
anyhow = "1.0.97"
bytemuck = { version="1.22.0", features = ["derive"] }
[build-dependencies]
embuild = "0.33"
[[package.metadata.esp-idf-sys.extra_components]]
remote_component = { name = "espressif/esp-dsp", version = "*" }
bindings_header = "src/esp_dsp_bindings.h"
bindings_module = "esp_dsp"
[package.metadata.esp-idf-sys.extra_components.0.remote_component]
# The name of the remote component. Corresponds to a key in the dependencies of
# `idf_component.yml`.
name = "espressif/esp-dsp"
# The version of the remote component. Corresponds to the `version` field of the
# `idf_component.yml`.
version = "*"

20
components_esp32s3.lock Normal file
View file

@ -0,0 +1,20 @@
dependencies:
espressif/esp-dsp:
component_hash: fa7fe74305df6da25867437ebcd4213e047cbfc0556cf92067ab657fce537c6e
dependencies:
- name: idf
require: private
version: '>=4.2'
source:
registry_url: https://components.espressif.com/
type: service
version: 1.5.2
idf:
source:
type: idf
version: 5.3.2
direct_dependencies:
- espressif/esp-dsp
manifest_hash: e234a49edacc9ce143168b1332239d9088e5921b4ebee61eec7ff3a7cbcd370a
target: esp32s3
version: 2.0.0

View file

@ -1,5 +1,5 @@
# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K)
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000
CONFIG_ESP_MAIN_TASK_STACK_SIZE=20000
# Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default).
# This allows to use 1 ms granularity for thread sleeps (10 ms by default).

5
src/esp_dsp_bindings.h Normal file
View file

@ -0,0 +1,5 @@
#if defined(ESP_IDF_COMP_ESPRESSIF__ESP_DSP_ENABLED)
#include "esp_dsp.h"
#endif
#include "esp_random.h"

View file

@ -1,4 +1,246 @@
fn main() {
use bytemuck::{bytes_of, bytes_of_mut, Pod, Zeroable};
use esp_idf_svc::{
hal::{gpio::AnyIOPin, i2s, peripherals::Peripherals, spi, units::FromValueType},
sys::TickType_t,
};
use esp_idf_svc::sys::esp_dsp;
use anyhow::{bail, Result};
const LED_COUNT: usize = 72;
const AUDIO_SAMPLES_PER_BUF: usize = 1024;
const AUDIO_BUFFERS: usize = 2;
const AUDIO_BANDS: usize = 3;
type AudioBuffer = [i32; AUDIO_SAMPLES_PER_BUF];
type DspBuffer = [f32; AUDIO_SAMPLES_PER_BUF];
#[derive(Clone, Copy, Eq, PartialEq, Pod, Zeroable)]
#[repr(C, align(4))]
struct Rgbv {
r: u8,
g: u8,
b: u8,
_o: u8,
}
impl Rgbv {
pub fn new(r: u8, g: u8, b: u8, o: u8) -> Self {
Self {
r,
g,
b,
_o: o | 0xE0,
}
}
pub fn o(self) -> u8 {
self._o & !0xE0
}
#[inline(always)]
pub fn set_o(mut self, o: u8) -> Self {
self._o = o | 0xE0;
self
}
#[inline(always)]
pub fn decrease(mut self, r: u8, g: u8, b: u8, o: u8) -> Self {
self.r -= r;
self.g -= g;
self.b -= b;
self.set_o(self.o() - o);
self
}
/// Converts hue, saturation, value to RGB // copied from rmt_neopixel example
pub fn from_hsv(h: u32, s: u32, v: u32, o: u8) -> Result<Self> {
if h > 360 || s > 100 || v > 100 {
bail!("The given HSV values are not in valid range");
}
let s = s as f64 / 100.0;
let v = v as f64 / 100.0;
let c = s * v;
let x = c * (1.0 - (((h as f64 / 60.0) % 2.0) - 1.0).abs());
let m = v - c;
let (r, g, b) = match h {
0..=59 => (c, x, 0.0),
60..=119 => (x, c, 0.0),
120..=179 => (0.0, c, x),
180..=239 => (0.0, x, c),
240..=299 => (x, 0.0, c),
_ => (c, 0.0, x),
};
Ok(Self {
r: ((r + m) * 255.0) as u8,
g: ((g + m) * 255.0) as u8,
b: ((b + m) * 255.0) as u8,
_o: o | 0xE0,
})
}
}
type LedColors = [Rgbv; LED_COUNT];
#[repr(C, align(4))]
#[derive(Clone, Copy, Eq, PartialEq, Pod, Zeroable)]
struct LedData {
zeros: u32,
leds: LedColors,
ones: u32,
}
impl LedData {
pub fn new() -> Self {
Self {
zeros: 0,
leds: [Rgbv::new(0, 0, 0, 0); LED_COUNT],
ones: 0, // sic. works as well, and triggers PWM change at the end of frame transfer instead of next one.
}
}
}
fn falloff(old: i32, new: i32) -> i32 {
(old >> 1) + (old >> 2) + (new >> 2)
}
fn falloff_f(old: f32, new: f32) -> f32 {
(old / 2.0f32) + (old / 4.0f32) + (new / 4.0f32)
}
struct AudioProcessor {
floating_max: i32,
current_powers: [f32; AUDIO_BANDS],
avg_powers: [f32; AUDIO_BANDS],
fft_buffer: [DspBuffer; AUDIO_BUFFERS],
next_fft_buf: usize,
fft_window: DspBuffer,
}
impl AudioProcessor {
pub fn new() -> Self {
let mut buf: DspBuffer = [0f32; AUDIO_SAMPLES_PER_BUF];
unsafe {
esp_dsp::dsps_wind_hann_f32(buf.as_mut_ptr(), AUDIO_SAMPLES_PER_BUF as i32);
}
AudioProcessor {
floating_max: 0i32,
current_powers: [0f32; AUDIO_BANDS],
avg_powers: [0f32; AUDIO_BANDS],
fft_buffer: [[0f32; AUDIO_SAMPLES_PER_BUF]; AUDIO_BUFFERS],
next_fft_buf: 0,
fft_window: buf,
}
}
pub fn process(&mut self, audio: &AudioBuffer) -> usize {
let &(mut proc_fft_buffer) = &self.fft_buffer[self.next_fft_buf];
/* calculate floating max */
let mut new_max = 0i32;
for value in audio {
new_max = std::cmp::max(new_max, value.saturating_abs());
}
/* get maximum */
self.floating_max = std::cmp::max(
10000000,
if new_max > self.floating_max {
new_max
} else {
falloff(self.floating_max, falloff(self.floating_max, new_max))
},
);
/* convert to floats for input to fft */
for it in audio.iter().zip(proc_fft_buffer.iter_mut()) {
let (audio_it, fft_it) = it;
*fft_it = (*audio_it as f32) / (i32::MAX as f32);
}
/* do fft */
let half_sample_count = (AUDIO_SAMPLES_PER_BUF / 2) as i32;
unsafe {
esp_dsp::dsps_mul_f32_ae32(
proc_fft_buffer.as_ptr(),
self.fft_window.as_ptr(),
proc_fft_buffer.as_mut_ptr(),
AUDIO_SAMPLES_PER_BUF as i32,
1,
1,
1,
);
esp_dsp::dsps_fft2r_fc32_aes3_(
proc_fft_buffer.as_mut_ptr(),
half_sample_count,
esp_dsp::dsps_fft_w_table_fc32,
); // operating on half length but complex
esp_dsp::dsps_bit_rev2r_fc32(proc_fft_buffer.as_mut_ptr(), half_sample_count); // operating on half length but complex
esp_dsp::dsps_cplx2real_fc32_ae32_(
proc_fft_buffer.as_mut_ptr(),
half_sample_count,
esp_dsp::dsps_fft_w_table_fc32,
esp_dsp::dsps_fft_w_table_size,
); // operating on half length but complex
for i in 0..half_sample_count as usize {
proc_fft_buffer[i] = (proc_fft_buffer[i * 2] * proc_fft_buffer[i * 2]
+ proc_fft_buffer[i * 2 + 1] * proc_fft_buffer[i * 2 + 1])
.sqrt();
}
}
/* do band stats */
self.current_powers[0] = proc_fft_buffer[1..8].iter().sum::<f32>() / 8f32;
self.current_powers[1] = proc_fft_buffer[9..86].iter().sum::<f32>() / 78f32;
self.current_powers[2] = proc_fft_buffer[87..470].iter().sum::<f32>() / 384f32;
for it in self.current_powers.iter().zip(self.avg_powers.iter_mut()) {
let (current, avg) = it;
*avg = falloff_f(*avg, *current);
}
let last_fft_buf = self.next_fft_buf;
self.next_fft_buf = (self.next_fft_buf + 1) % AUDIO_BUFFERS;
last_fft_buf
}
}
trait LedEffect {
fn render(&mut self, processed: &AudioProcessor, fft_buf: usize, leds: &LedColors);
}
struct LedEffectBassSparks {}
impl LedEffect for LedEffectBassSparks {
fn render(&mut self, processed: &AudioProcessor, _fft_buf: usize, &(mut leds): &LedColors) {
let bass_color = if processed.floating_max > 10100000
&& (processed.current_powers[0] > 1.25 * processed.avg_powers[0])
{
Rgbv::new(127, 0, 255, 4)
} else {
Rgbv::new(0, 0, 0, 0)
};
leds.fill(bass_color);
bass_color.decrease(3, 5, 5, 0);
if processed.floating_max > 10100000
&& (processed.current_powers[1] > 1.35 * processed.avg_powers[1])
&& (processed.current_powers[2] > 1.35 * processed.avg_powers[2])
{
for i in 0..10 {
let led_index = 0;
}
}
}
}
fn main() -> anyhow::Result<()> {
// It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_svc::sys::link_patches();
@ -6,5 +248,96 @@ fn main() {
// Bind the log crate to the ESP Logging facilities
esp_idf_svc::log::EspLogger::initialize_default();
log::info!("Hello, world!");
let peripherals = Peripherals::take().unwrap();
// leds
let mut leds = LedData::new();
// audio buffers
let mut audio: [AudioBuffer; AUDIO_BUFFERS] = [[0; AUDIO_SAMPLES_PER_BUF]; AUDIO_BUFFERS];
let mut next_audio_buf: usize = 0;
// interfaces
let led_spi_per = peripherals.spi2;
let mic_i2s_per = peripherals.i2s0;
// pins
let led_spi_sdo = peripherals.pins.gpio11;
let led_spi_sck = peripherals.pins.gpio12;
let mic_i2s_sd = peripherals.pins.gpio5;
let mic_i2s_sclk = peripherals.pins.gpio4;
let mic_i2s_ws = peripherals.pins.gpio6;
// i2s config
let mic_i2s_std_cfg = i2s::config::StdConfig::new(
i2s::config::Config::new().role(i2s::config::Role::Controller),
i2s::config::StdClkConfig::new(
24.kHz().into(),
i2s::config::ClockSource::default(),
i2s::config::MclkMultiple::M256,
),
i2s::config::StdSlotConfig::msb_slot_default(
i2s::config::DataBitWidth::Bits32,
i2s::config::SlotMode::Mono,
)
.slot_bit_width(i2s::config::SlotBitWidth::Bits32)
.slot_mode_mask(i2s::config::SlotMode::Mono, i2s::config::StdSlotMask::Left),
i2s::config::StdGpioConfig::new(false, false, false),
);
let mut mic_drv = i2s::I2sDriver::new_std_rx(
mic_i2s_per,
&mic_i2s_std_cfg,
mic_i2s_sclk,
mic_i2s_sd,
AnyIOPin::none(),
mic_i2s_ws,
)?;
// spi config
let mut led_drv = spi::SpiDeviceDriver::new_single(
led_spi_per,
led_spi_sck,
led_spi_sdo,
AnyIOPin::none(),
AnyIOPin::none(),
&spi::config::DriverConfig::new(),
&spi::config::Config::new()
.baudrate(1.MHz().into())
.data_mode(spi::config::MODE_3),
)?;
unsafe {
let esp_err =
esp_dsp::dsps_fft2r_init_fc32(std::ptr::null_mut(), (AUDIO_SAMPLES_PER_BUF / 2) as i32);
if esp_err != esp_dsp::ESP_OK {
log::error!("fft2 failed to init")
};
let esp_err =
esp_dsp::dsps_fft4r_init_fc32(std::ptr::null_mut(), (AUDIO_SAMPLES_PER_BUF / 2) as i32);
if esp_err != esp_dsp::ESP_OK {
log::error!("fft4 failed to init")
};
}
let mut processor = AudioProcessor::new();
let mut effect = LedEffectBassSparks {};
mic_drv.rx_enable()?;
loop {
// let buffer: &mut [u8; AUDIO_SAMPLES_PER_BUF*4] = cast_slice_mut(&mut audio[next_audio_buf]);
let buffer = bytes_of_mut(&mut audio[next_audio_buf]);
let num_bytes_read = mic_drv.read(buffer, TickType_t::MAX)?;
if num_bytes_read != AUDIO_SAMPLES_PER_BUF * 4 {
log::error!("buffer underflow");
}
let current_fft_buf = processor.process(&audio[next_audio_buf]);
effect.render(&processor, current_fft_buf, &leds.leds);
let output_buffer = bytes_of(&leds);
next_audio_buf = (next_audio_buf + 1) % AUDIO_BUFFERS;
}
}