Skip to content

Commit bc7eb8c

Browse files
committed
Merge branch 'fix/usb_audio_player' into 'master'
fix(usb_audio_player): fix esp32p4 stream suspend bug Closes AEG-1589 and AEG-1942 See merge request ae_group/esp-iot-solution!1097
2 parents cc286d8 + 50a438f commit bc7eb8c

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dependencies:
22
chmorgan/esp-audio-player: "1.0.*"
33
idf: ">=5.0" # The version of the ESP-IDF that esp-audio-player requires
4-
usb_host_uac: "1.0.*"
4+
usb_host_uac:
5+
version: "1.2.*"

examples/usb/host/usb_audio_player/main/usb_audio_player_main.c

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,15 @@ static const char *TAG = "usb_audio_player";
2525
#define MP3_FILE_NAME "/new_epic.mp3"
2626
#define BIT1_SPK_START (0x01 << 0)
2727
#define DEFAULT_VOLUME 45
28+
#define DEFAULT_UAC_FREQ 48000
29+
#define DEFAULT_UAC_BITS 16
30+
#define DEFAULT_UAC_CH 2
31+
2832
static QueueHandle_t s_event_queue = NULL;
2933
static uac_host_device_handle_t s_spk_dev_handle = NULL;
34+
static uint32_t s_spk_curr_freq = DEFAULT_UAC_FREQ;
35+
static uint8_t s_spk_curr_bits = DEFAULT_UAC_BITS;
36+
static uint8_t s_spk_curr_ch = DEFAULT_UAC_CH;
3037
static FILE *s_fp = NULL;
3138
static void uac_device_callback(uac_host_device_handle_t uac_device_handle, const uac_host_device_event_t event, void *arg);
3239
/**
@@ -69,9 +76,16 @@ static esp_err_t _audio_player_mute_fn(AUDIO_PLAYER_MUTE_SETTING setting)
6976
if (s_spk_dev_handle == NULL) {
7077
return ESP_ERR_INVALID_STATE;
7178
}
72-
ESP_LOGI(TAG, "mute setting: %s", setting == 0 ? "mute" : "unmute");
73-
74-
return uac_host_device_set_mute(s_spk_dev_handle, (setting == 0 ? true : false));
79+
ESP_LOGI(TAG, "mute setting: %s", setting == AUDIO_PLAYER_MUTE ? "mute" : "unmute");
80+
// some uac devices may not support mute, so we not check the return value
81+
if (setting == AUDIO_PLAYER_UNMUTE) {
82+
uac_host_device_set_volume(s_spk_dev_handle, DEFAULT_VOLUME);
83+
uac_host_device_set_mute(s_spk_dev_handle, false);
84+
} else {
85+
uac_host_device_set_volume(s_spk_dev_handle, 0);
86+
uac_host_device_set_mute(s_spk_dev_handle, true);
87+
}
88+
return ESP_OK;
7589
}
7690

7791
static esp_err_t _audio_player_write_fn(void *audio_buffer, size_t len, size_t *bytes_written, uint32_t timeout_ms)
@@ -92,13 +106,19 @@ static esp_err_t _audio_player_std_clock(uint32_t rate, uint32_t bits_cfg, i2s_s
92106
if (s_spk_dev_handle == NULL) {
93107
return ESP_ERR_INVALID_STATE;
94108
}
109+
if (rate == s_spk_curr_freq && bits_cfg == s_spk_curr_bits && ch == s_spk_curr_ch) {
110+
return ESP_OK;
111+
}
95112
ESP_LOGI(TAG, "Re-config: speaker rate %"PRIu32", bits %"PRIu32", mode %s", rate, bits_cfg, ch == 1 ? "MONO" : (ch == 2 ? "STEREO" : "INVALID"));
96113
ESP_ERROR_CHECK(uac_host_device_stop(s_spk_dev_handle));
97114
const uac_host_stream_config_t stm_config = {
98115
.channels = ch,
99116
.bit_resolution = bits_cfg,
100117
.sample_freq = rate,
101118
};
119+
s_spk_curr_freq = rate;
120+
s_spk_curr_bits = bits_cfg;
121+
s_spk_curr_ch = ch;
102122
return uac_host_device_start(s_spk_dev_handle, &stm_config);
103123
}
104124

@@ -128,7 +148,6 @@ static void _audio_player_callback(audio_player_cb_ctx_t *ctx)
128148
break;
129149
}
130150
ESP_ERROR_CHECK(uac_host_device_resume(s_spk_dev_handle));
131-
uac_host_device_set_volume(s_spk_dev_handle, DEFAULT_VOLUME);
132151
break;
133152
case AUDIO_PLAYER_CALLBACK_EVENT_PAUSE:
134153
ESP_LOGI(TAG, "AUDIO_PLAYER_REQUEST_PAUSE");
@@ -243,10 +262,11 @@ static void uac_lib_task(void *arg)
243262
ESP_ERROR_CHECK(uac_host_get_device_info(uac_device_handle, &dev_info));
244263
ESP_LOGI(TAG, "UAC Device connected: SPK");
245264
uac_host_printf_device_param(uac_device_handle);
265+
// Start usb speaker with the default configuration
246266
const uac_host_stream_config_t stm_config = {
247-
.channels = 2,
248-
.bit_resolution = 16,
249-
.sample_freq = 48000,
267+
.channels = s_spk_curr_ch,
268+
.bit_resolution = s_spk_curr_bits,
269+
.sample_freq = s_spk_curr_freq,
250270
};
251271
ESP_ERROR_CHECK(uac_host_device_start(uac_device_handle, &stm_config));
252272
s_spk_dev_handle = uac_device_handle;
@@ -271,6 +291,9 @@ static void uac_lib_task(void *arg)
271291
uac_host_device_event_t event = evt_queue.device_evt.event;
272292
switch (event) {
273293
case UAC_HOST_DRIVER_EVENT_DISCONNECTED:
294+
s_spk_curr_bits = DEFAULT_UAC_BITS;
295+
s_spk_curr_freq = DEFAULT_UAC_FREQ;
296+
s_spk_curr_ch = DEFAULT_UAC_CH;
274297
ESP_LOGI(TAG, "UAC Device disconnected");
275298
break;
276299
case UAC_HOST_DEVICE_EVENT_RX_DONE:

0 commit comments

Comments
 (0)