From 8bb0c816df0ef6e00a6956466cf3c0848272a8ef Mon Sep 17 00:00:00 2001 From: awalol Date: Thu, 7 May 2026 22:28:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8ds5=E5=8E=9F=E5=A7=8B?= =?UTF-8?q?=E7=9A=84=E9=9F=B3=E9=87=8F=E8=B0=83=E8=8A=82=E8=8C=83=E5=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/audio.cpp | 6 ++++-- src/config.cpp | 4 ++-- src/config.h | 2 +- src/usb.cpp | 33 ++++++++++++++++++++++++++------- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/audio.cpp b/src/audio.cpp index 41d65a4..4c02769 100644 --- a/src/audio.cpp +++ b/src/audio.cpp @@ -7,6 +7,7 @@ #include "resample.h" #include "tusb.h" #include +#include #include #include "opus.h" #include "utils.h" @@ -59,9 +60,10 @@ void audio_loop() { WDL_ResampleSample *in_buf; int nframes = resampler.ResamplePrepare(frames, OUTPUT_CHANNELS, &in_buf); + float gain = mute[0] ? 0.0f : powf(10.0f, get_config().speaker_volume / 20.0f); for (int i = 0; i < nframes; i++) { - 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]; + audio_buf[audio_buf_pos++] = raw[i * INPUT_CHANNELS] / 32768.0f * gain; + audio_buf[audio_buf_pos++] = raw[i * INPUT_CHANNELS + 1] / 32768.0f * gain; 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 8a3084c..c1974e4 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -50,8 +50,8 @@ void config_valid() { body->haptics_gain = 1.0f; printf("[Config] Haptics Gain value is invalid\n"); } - if (std::isnan(body->speaker_volume) || body->speaker_volume < 1.0f || body->speaker_volume > 2.0f) { - body->speaker_volume = 2.0f; + if (std::isnan(body->speaker_volume) || body->speaker_volume < -100 || body->speaker_volume > 0) { + body->speaker_volume = -100; printf("[Config] Speaker Volume is invalid\n"); } if (body->inactive_time < 5 || body->inactive_time > 60) { diff --git a/src/config.h b/src/config.h index e25210b..edca21d 100644 --- a/src/config.h +++ b/src/config.h @@ -9,7 +9,7 @@ struct __attribute__((packed)) Config_body { float haptics_gain; // [1.0,2.0] - float speaker_volume; // [1.0,2.0] + float speaker_volume; // [-100,0] uint8_t inactive_time; // [10,60] min uint8_t disable_inactive_disconnect; // bool: 0 disable,1 enable uint8_t disable_pico_led; // bool diff --git a/src/usb.cpp b/src/usb.cpp index 5cf1a34..0096954 100644 --- a/src/usb.cpp +++ b/src/usb.cpp @@ -7,7 +7,7 @@ #include "config.h" uint8_t mute[2]; // 0: SPEAKER(0x02) 1: MIC(0x05) -float volume[2] = {1.0f}; // 0: SPEAKER(0x02) 1: MIC(0x05) +float volume[2] = {-100.0f,0.0f}; // 0: SPEAKER(0x02) 1: MIC(0x05) #define UAC1_ENTITY_SPK_FEATURE_UNIT 0x02 #define UAC1_ENTITY_MIC_FEATURE_UNIT 0x05 @@ -66,7 +66,7 @@ static bool audio10_set_req_entity(tusb_control_request_t const *p_request, uint // Only 1st form is supported TU_VERIFY(p_request->wLength == 2); - volume[index] = static_cast(tu_unaligned_read16(pBuff)) / 256; + volume[index] = static_cast(*reinterpret_cast(pBuff)) / 256; if (entityID == UAC1_ENTITY_SPK_FEATURE_UNIT) { auto config = get_config(); config.speaker_volume = volume[index]; @@ -118,21 +118,40 @@ static bool audio10_get_req_entity(uint8_t rhport, tusb_control_request_t const case AUDIO10_CS_REQ_GET_MIN: TU_LOG2(" Get Volume min of entity: %u\r\n", entityID); { - int16_t min = 1; // 1 dB - min = min * 256; // convert to 1/256 dB units + uint8_t min[2]; + if (entityID == UAC1_ENTITY_SPK_FEATURE_UNIT) { + min[0] = 0x00; + min[1] = 0x9c; + }else { + min[0] = 0x00; + min[1] = 0x00; + } return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &min, sizeof(min)); } case AUDIO10_CS_REQ_GET_MAX: TU_LOG2(" Get Volume max of entity: %u\r\n", entityID); { - int16_t max = 2; // 2 dB - max = max * 256; // convert to 1/256 dB units + uint8_t max[2]; + if (entityID == UAC1_ENTITY_SPK_FEATURE_UNIT) { + max[0] = 0x00; + max[1] = 0x00; + }else { + max[0] = 0x00; + max[1] = 0x30; + } return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &max, sizeof(max)); } case AUDIO10_CS_REQ_GET_RES: TU_LOG2(" Get Volume res of entity: %u\r\n", entityID); { - int16_t res = 0.1 * 256; // 0.1 dB + uint8_t res[2]; + if (entityID == UAC1_ENTITY_SPK_FEATURE_UNIT) { + res[0] = 0x00; + res[1] = 0x01; + }else { + res[0] = 0x7a; + res[1] = 0x00; + } return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &res, sizeof(res)); } // Unknown/Unsupported control