将音量调节的范围设为 -100 到 0 (当前无作用), 明确增益的位置

This commit is contained in:
awalol
2026-03-17 16:52:38 +08:00
parent 958adb9cdb
commit 94530dd9a4
2 changed files with 10 additions and 8 deletions
+7 -6
View File
@@ -10,9 +10,10 @@
#define INPUT_CHANNELS 4 #define INPUT_CHANNELS 4
#define OUTPUT_CHANNELS 2 #define OUTPUT_CHANNELS 2
#define SAMPLE_SIZE 64 #define SAMPLE_SIZE 64
#define REPORT_SIZE 142 #define REPORT_SIZE 142
#define REPORT_ID 0x32 #define REPORT_ID 0x32
#define VOLUME_GAIN 2
static WDL_Resampler resampler; static WDL_Resampler resampler;
static uint8_t reportSeqCounter = 0; static uint8_t reportSeqCounter = 0;
@@ -47,9 +48,9 @@ void audio_loop() {
// 4. 转换为int8并缓冲,满64字节即组包发送 // 4. 转换为int8并缓冲,满64字节即组包发送
for (int i = 0; i < out_frames; i++) { for (int i = 0; i < out_frames; i++) {
int val_l = (int) (out_buf[i * 2] * 254.0f); int val_l = (int) (out_buf[i * 2] * 127.0f * VOLUME_GAIN);
int val_r = (int) (out_buf[i * 2 + 1] * 254.0f); int val_r = (int) (out_buf[i * 2 + 1] * 127.0f * VOLUME_GAIN);
haptic_buf[haptic_buf_pos++] = (int8_t) std::clamp(val_l, -128, 127); haptic_buf[haptic_buf_pos++] = (int8_t) std::clamp(val_l, -128, 127); // 似乎clamp有点多余?还是以防万一吧
haptic_buf[haptic_buf_pos++] = (int8_t) std::clamp(val_r, -128, 127); haptic_buf[haptic_buf_pos++] = (int8_t) std::clamp(val_r, -128, 127);
if (haptic_buf_pos != SAMPLE_SIZE) { if (haptic_buf_pos != SAMPLE_SIZE) {
+3 -2
View File
@@ -86,6 +86,7 @@ static bool audio10_get_req_entity(uint8_t rhport, tusb_control_request_t const
uint8_t channelNum = TU_U16_LOW(p_request->wValue); uint8_t channelNum = TU_U16_LOW(p_request->wValue);
uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue); uint8_t ctrlSel = TU_U16_HIGH(p_request->wValue);
uint8_t entityID = TU_U16_HIGH(p_request->wIndex); uint8_t entityID = TU_U16_HIGH(p_request->wIndex);
// 当前音量的调节不会影响音量大小,看看后面有没有用再补全吧
// If request is for our speaker feature unit // If request is for our speaker feature unit
if (entityID == UAC1_ENTITY_SPK_FEATURE_UNIT || entityID == UAC1_ENTITY_MIC_FEATURE_UNIT) { if (entityID == UAC1_ENTITY_SPK_FEATURE_UNIT || entityID == UAC1_ENTITY_MIC_FEATURE_UNIT) {
@@ -107,14 +108,14 @@ static bool audio10_get_req_entity(uint8_t rhport, tusb_control_request_t const
case AUDIO10_CS_REQ_GET_MIN: case AUDIO10_CS_REQ_GET_MIN:
TU_LOG2(" Get Volume min of channel: %u\r\n", channelNum); { TU_LOG2(" Get Volume min of channel: %u\r\n", channelNum); {
int16_t min = -90; // -90 dB int16_t min = -100; // -100 dB
min = min * 256; // convert to 1/256 dB units min = min * 256; // convert to 1/256 dB units
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &min, sizeof(min)); return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &min, sizeof(min));
} }
case AUDIO10_CS_REQ_GET_MAX: case AUDIO10_CS_REQ_GET_MAX:
TU_LOG2(" Get Volume max of channel: %u\r\n", channelNum); { TU_LOG2(" Get Volume max of channel: %u\r\n", channelNum); {
int16_t max = 30; // +30 dB int16_t max = 0; // 0 dB
max = max * 256; // convert to 1/256 dB units max = max * 256; // convert to 1/256 dB units
return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &max, sizeof(max)); return tud_audio_buffer_and_schedule_control_xfer(rhport, p_request, &max, sizeof(max));
} }