From b736cc91d7672020dfa091074070cdc452f1f0d1 Mon Sep 17 00:00:00 2001 From: awalol Date: Thu, 7 May 2026 19:11:42 +0800 Subject: [PATCH] fix: usbaudio set_cur cmd set volume --- src/audio.cpp | 5 +++-- src/config.cpp | 5 +++++ src/config.h | 1 + src/usb.cpp | 12 ++++++++++-- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/audio.cpp b/src/audio.cpp index 2ba7fe4..41d65a4 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -13,6 +13,7 @@ #include "pico/multicore.h" #include "pico/util/queue.h" #include "config.h" +#include "usb.h" #define INPUT_CHANNELS 4 #define OUTPUT_CHANNELS 2 @@ -59,8 +60,8 @@ void audio_loop() { int nframes = resampler.ResamplePrepare(frames, OUTPUT_CHANNELS, &in_buf); for (int i = 0; i < nframes; i++) { - audio_buf[audio_buf_pos++] = raw[i * INPUT_CHANNELS] / 32768.0f * (get_config().speaker_volume - 1.0f); - audio_buf[audio_buf_pos++] = raw[i * INPUT_CHANNELS + 1] / 32768.0f * (get_config().speaker_volume - 1.0f); + audio_buf[audio_buf_pos++] = raw[i * INPUT_CHANNELS] / 32768.0f * (get_config().speaker_volume - 1.0f) * !mute[0]; + audio_buf[audio_buf_pos++] = raw[i * INPUT_CHANNELS + 1] / 32768.0f * (get_config().speaker_volume - 1.0f) * !mute[0]; if (audio_buf_pos == 512 * 2) { static audio_raw_element element{}; memcpy(element.data,audio_buf,512 * 2 * 4); diff --git a/src/config.cpp b/src/config.cpp index bc0c7b4..8a3084c 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -122,3 +122,8 @@ void set_config(const uint8_t *new_config, const uint16_t len) { cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, true); } } + +void set_config(const Config_body &new_config) { + config.body = new_config; + config_valid(); +} diff --git a/src/config.h b/src/config.h index b723861..e25210b 100644 --- a/src/config.h +++ b/src/config.h @@ -32,5 +32,6 @@ bool config_save(); const Config_body& get_config(); void set_config(const uint8_t *new_config, const uint16_t len); void config_valid(); +void set_config(const Config_body &new_config); #endif //DS5_BRIDGE_CONFIG_H diff --git a/src/usb.cpp b/src/usb.cpp index 55eaac8..5cf1a34 100644 --- a/src/usb.cpp +++ b/src/usb.cpp @@ -4,6 +4,7 @@ #include "tusb.h" #include "bsp/board_api.h" +#include "config.h" uint8_t mute[2]; // 0: SPEAKER(0x02) 1: MIC(0x05) float volume[2] = {1.0f}; // 0: SPEAKER(0x02) 1: MIC(0x05) @@ -66,6 +67,11 @@ static bool audio10_set_req_entity(tusb_control_request_t const *p_request, uint TU_VERIFY(p_request->wLength == 2); volume[index] = static_cast(tu_unaligned_read16(pBuff)) / 256; + if (entityID == UAC1_ENTITY_SPK_FEATURE_UNIT) { + auto config = get_config(); + config.speaker_volume = volume[index]; + set_config(config); + } TU_LOG2(" Set Volume: %d dB of entity: %u\r\n", volume[index], entityID); return true; @@ -103,8 +109,10 @@ static bool audio10_get_req_entity(uint8_t rhport, tusb_control_request_t const switch (p_request->bRequest) { case AUDIO10_CS_REQ_GET_CUR: TU_LOG2(" Get Volume of entity: %u\r\n", entityID); { - int16_t vol = volume[index]; - vol = vol * 256; // convert to 1/256 dB units + if (entityID == UAC1_ENTITY_SPK_FEATURE_UNIT) { + volume[index] = get_config().speaker_volume; + } + int16_t vol = volume[index] * 256; // convert to 1/256 dB units return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &vol, sizeof(vol)); }