使用ds5原始的音量调节范围

This commit is contained in:
awalol
2026-05-07 22:28:29 +08:00
parent 33c44907b6
commit 8bb0c816df
4 changed files with 33 additions and 12 deletions
+4 -2
View File
@@ -7,6 +7,7 @@
#include "resample.h"
#include "tusb.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#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);
+2 -2
View File
@@ -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) {
+1 -1
View File
@@ -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
+26 -7
View File
@@ -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<float>(tu_unaligned_read16(pBuff)) / 256;
volume[index] = static_cast<float>(*reinterpret_cast<int16_t const *>(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