11#pragma push_macro("abs")
22#pragma pop_macro("abs")
28#ifndef ARDUINO_USB_MODE
29#error This Microcontroller has no Native USB interface
31#if ARDUINO_USB_MODE == 1
32#error This sketch should be used when USB is in OTG mode
36#if !defined(USE_TINYUSB) && !defined(ARDUINO_ARCH_STM32) && \
37 !defined(ARDUINO_ARCH_RENESAS) && !defined(IS_ZEPHYR)
38#error This Microcontroller has no Native USB interface
42#include "AudioLogger.h"
49#define USB_DESCR_MAX_LEN 512
238 LOGE(
"Unsupported bits_per_sample: %d (must be 16, 24, or 32)",
254 "Could not change channel count from %d to %d: channel count is "
260 "Could not change bits per sample from %d to %d: bits per sample is "
298 LOGE(
"Unsupported bits_per_sample: %d (must be 16, 24, or 32)",
312 uint16_t cb_sz = 64u;
316 if (needed > cb_sz) cb_sz = needed;
331 LOGE(
"beginUSB failed");
375 if (fct.ep_in != 0)
return true;
383 if (fct.ep_out != 0)
return true;
559 int normalized = percent;
560 if (normalized < 0) normalized = 0;
561 if (normalized > 100) normalized = 100;
576 if (ppm == INT32_MIN) ppm = INT32_MIN + 1;
598 std::fill(audio.lin_buf_in.begin(), audio.lin_buf_in.end(), 0);
599 std::fill(audio.lin_buf_out.begin(), audio.lin_buf_out.end(), 0);
693 uint16_t xferred_bytes)>
793 LOGW(
"Sample rate changed to %u Hz", rate);
798 for (
auto& fct :
audiod_fct_) fct.sample_rate_tx = rate;
938 (
float)(1UL << (16u - (frame_shift - 1u))));
1037 uint8_t const* raw_desc, uint16_t max_len) {
1056 uint8_t ep_in = 0, ep_out = 0, ep_fb = 0;
1057 uint16_t ep_in_size = 0, ep_out_size = 0;
1059 bool has_ep_out_view =
false;
1063 while (p_desc_end - p_desc > 0) {
1070 if (desc_ep.
usage == 0) {
1074 ep_in_size = sz > ep_in_size ? sz : ep_in_size;
1079 ep_out_size = sz > ep_out_size ? sz : ep_out_size;
1080 desc_ep_out = desc_ep;
1081 has_ep_out_view =
true;
1090 LOGD(
"iso_alloc IN ep=0x%02x sz=%u: %s", ep_in, ep_in_size,
1091 alloc_ok ?
"OK" :
"FAIL");
1095 LOGD(
"iso_alloc OUT ep=0x%02x sz=%u: %s", ep_out, ep_out_size,
1096 alloc_ok ?
"OK" :
"FAIL");
1097 if (has_ep_out_view)
audiod_fct_[i].ep_out_view = desc_ep_out;
1107 LOGD(
"iso_activate OUT: done");
1124 while (p_desc_end - p_desc > 0) {
1152 while (p_desc_end - p_desc > 0) {
1161 LOGE(
" UAC2: interrupt EP 0x%02x open failed", ep_addr);
1201 if (entityID != 0) {
1238 this, rhport, p_request,
1253 this, rhport, p_request,
audiod_fct_[func_id].ctrl_buf.data());
1268 (void)xferred_bytes;
1293 if (n < frame_bytes) memset(dst + n, 0, frame_bytes - n);
1302 if (xferred_bytes > 0) {
1304 (
int)xferred_bytes);
1305 if ((
uint32_t)written < xferred_bytes)
1320 rx_done_cb_(
this, rhport, audio, (uint16_t)xferred_bytes);
1341 if (audio->
ep_fb != 0) {
1345 if (0 == (frame_count & (interval - 1))) {
1368 memcpy(cb, &rate, 4);
1375 uint16_t cnt = (uint16_t)rates.size();
1376 memcpy(cb, &cnt, 2);
1377 for (
size_t i = 0; i < rates.size(); i++) {
1380 memcpy(cb + 2 + i * 12, &r, 4);
1381 memcpy(cb + 2 + i * 12 + 4, &r, 4);
1382 memcpy(cb + 2 + i * 12 + 8, &z, 4);
1385 rhport, p_request, cb,
1386 (uint16_t)(2 + rates.size() * 12));
1391 memcpy(cb, &cnt, 2);
1392 memcpy(cb + 2, &rate, 4);
1393 memcpy(cb + 6, &rate, 4);
1394 memcpy(cb + 10, &z, 4);
1410 cb[0] =
isMute(channel) ? 1 : 0;
1421 int16_t vmin = -25600, vmax = 0, vres = 256;
1422 memcpy(cb + 0, &cnt, 2);
1423 memcpy(cb + 2, &vmin, 2);
1424 memcpy(cb + 4, &vmax, 2);
1425 memcpy(cb + 6, &vres, 2);
1516 return (entityID != 0)
1552 while (p_desc_end - p_desc > 0) {
1553 if (p_desc[3] == entityID)
1575 p_desc +=
descU16(p_desc + 6);
1579 while (p_desc_end - p_desc > 0) {
1603 while (p_desc_end - p_desc > 0) {
1664 uint8_t const** pp_desc_int) {
1672 p_desc +=
descU16(p_desc + 6);
1677 while (p_desc_end - p_desc > 0) {
1684 *pp_desc_int = p_desc;
1704 uint8_t const** pp_desc_int) {
1728 if (!
backend().controlTransfer(
1729 rhport, p_request, &
audiod_fct_[func_id].alt_setting[idxItf], 1))
1732 LOGI(
" Get itf: %u - current alt: %u", itf,
1739 bool apply_correction =
1742 if (apply_correction) {
1770 apply_correction ? 3 : 4);
1795 if (!
backend().usesIsoAlloc()) {
1825 if (
backend().usesIsoAlloc()) {
1841 uint8_t const* p_desc_for_params) {
1910 switch (fb_param.
method) {
1920 if (fifo_depth == 0) fifo_depth = 1;
1921 uint16_t fifo_lvl_thr = fifo_depth / 2;
1933 ((fb_param.
sample_freq / 100) << 16) / (frame_div / 100);
1955 LOGD(
" openEPs: p_desc=%p end=%p len=%u itf=%u alt=%u",
1956 p_desc, p_desc_end, audio->
desc_length, itf, alt);
1958 while (p_desc_end - p_desc > 0) {
1962 uint8_t const* p_desc_for_params =
1966 LOGD(
" matched itf=%u alt=%u nEps=%u", itf, alt, nEps);
1968 while (foundEPs < nEps && (p_desc_end - p_desc > 0)) {
1969 LOGD(
" scan: type=0x%02x len=%u offset=%d",
1970 p_desc[1], p_desc[0], (
int)(p_desc - audio->
p_desc));
1974 LOGD(
" activating ep=0x%02x type=%u...",
1977 LOGD(
" activateEndpoint FAILED");
1981 LOGD(
" activated OK");
1990 desc_ep.
usage == 0x00)
1991 openEpIn(rhport, audio, itf, desc_ep, p_desc_for_params);
2005 if (foundEPs != nEps)
return true;
2032 LOGD(
"SET_ITF itf=%u alt=%u [start]", itf, alt);
2038 LOGD(
" AS interface %u not found", itf);
2042 LOGD(
" found func=%u idx=%u", func_id, idxItf);
2048 closeEpIn(rhport, audio, itf, p_request);
2059 bool enable_sof =
false;
2075 return value != 0 && (value & (value - 1)) == 0;
2080 while ((value >>= 1u) != 0u) result++;
2095 if ((((1UL << k) * sample_freq / mclk_freq) + 1) > n_frame) {
2096 LOGE(
" UAC2 feedback interval too small");
2101 if ((mclk_freq % sample_freq) == 0 &&
2111 (
float)sample_freq / (
float)mclk_freq *
2137 const uint16_t sample_normimal = (uint16_t)(audio->
sample_rate_tx *
2138 interval / (full_speed ? 1000 : 8000));
2139 const uint16_t sample_reminder = (uint16_t)(audio->
sample_rate_tx *
2140 interval % (full_speed ? 1000 : 8000));
2142 const uint16_t packet_sz_tx_min =
2145 const uint16_t packet_sz_tx_norm =
2148 const uint16_t packet_sz_tx_max =
2153 if (packet_sz_tx_max > audio->
ep_in_sz)
return false;
2156 if (sample_reminder) {
2185 const uint32_t denom = full_speed ? 1000u : 8000u;
2187 const uint32_t interval = full_speed ? iv : (1u << (iv - 1));
2209 if (level >= target) {
2221 }
else if (
tx_level_avg_ < (target - 1) * 256 && samples > 1) {
2228 uint32_t bytes = samples * frame_bytes;
2230 return (uint16_t)bytes;
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define USB_DESCR_MAX_LEN
Definition USBAudioDeviceBase.h:49
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition AudioTypes.h:26
@ RXTX_MODE
Definition AudioTypes.h:26
@ TX_MODE
Definition AudioTypes.h:26
@ RX_MODE
Definition AudioTypes.h:26