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
229 LOGE(
"Unsupported bits_per_sample: %d (must be 16, 24, or 32)",
245 "Could not change channel count from %d to %d: channel count is "
251 "Could not change bits per sample from %d to %d: bits per sample is "
289 LOGE(
"Unsupported bits_per_sample: %d (must be 16, 24, or 32)",
303 uint16_t cb_sz = 64u;
307 if (needed > cb_sz) cb_sz = needed;
322 LOGE(
"beginUSB failed");
332 for (uint8_t ch = 0; ch < (uint8_t)
volume_.size(); ch++) {
365 if (fct.ep_in != 0)
return true;
373 if (fct.ep_out != 0)
return true;
542 int normalized = percent;
543 if (normalized < 0) normalized = 0;
544 if (normalized > 100) normalized = 100;
556 std::fill(audio.lin_buf_in.begin(), audio.lin_buf_in.end(), 0);
557 std::fill(audio.lin_buf_out.begin(), audio.lin_buf_out.end(), 0);
651 uint16_t xferred_bytes)>
743 LOGW(
"Sample rate changed to %u Hz", rate);
748 for (
auto& fct :
audiod_fct_) fct.sample_rate_tx = rate;
783 for (uint8_t i = 0; i < (uint8_t)
audiod_fct_.size(); i++) {
820 uint8_t frame_shift) {
845 ((uint32_t)ff_count << 8);
882 (
float)(1UL << (16u - (frame_shift - 1u))));
958 uint8_t
const* raw_desc, uint16_t max_len) {
977 uint8_t ep_in = 0, ep_out = 0, ep_fb = 0;
978 uint16_t ep_in_size = 0, ep_out_size = 0;
980 bool has_ep_out_view =
false;
982 uint8_t
const* p_desc_end =
984 while (p_desc_end - p_desc > 0) {
991 if (desc_ep.
usage == 0) {
995 ep_in_size = sz > ep_in_size ? sz : ep_in_size;
1000 ep_out_size = sz > ep_out_size ? sz : ep_out_size;
1001 desc_ep_out = desc_ep;
1002 has_ep_out_view =
true;
1011 LOGD(
"iso_alloc IN ep=0x%02x sz=%u: %s", ep_in, ep_in_size,
1012 alloc_ok ?
"OK" :
"FAIL");
1016 LOGD(
"iso_alloc OUT ep=0x%02x sz=%u: %s", ep_out, ep_out_size,
1017 alloc_ok ?
"OK" :
"FAIL");
1018 if (has_ep_out_view)
audiod_fct_[i].ep_out_view = desc_ep_out;
1028 LOGD(
"iso_activate OUT: done");
1043 uint8_t
const* p_desc_end =
1045 while (p_desc_end - p_desc > 0) {
1071 uint8_t
const* p_desc_end =
1073 while (p_desc_end - p_desc > 0) {
1082 LOGE(
" UAC2: interrupt EP 0x%02x open failed", ep_addr);
1122 if (entityID != 0) {
1125 uint8_t* cb =
audiod_fct_[func_id].ctrl_buf.data();
1159 this, rhport, p_request,
1174 this, rhport, p_request,
audiod_fct_[func_id].ctrl_buf.data());
1187 uint32_t xferred_bytes) {
1189 (void)xferred_bytes;
1190 for (uint8_t func_id = 0; func_id <
getAudioCount(); func_id++) {
1214 if (n < frame_bytes) memset(dst + n, 0, frame_bytes - n);
1223 if (xferred_bytes > 0) {
1225 (
int)xferred_bytes);
1226 if ((uint32_t)written < xferred_bytes)
1240 rx_done_cb_(
this, rhport, audio, (uint16_t)xferred_bytes);
1258 if (audio->
ep_fb != 0) {
1260 uint32_t
const interval =
1262 if (0 == (frame_count & (interval - 1))) {
1285 memcpy(cb, &rate, 4);
1292 uint16_t cnt = (uint16_t)rates.size();
1293 memcpy(cb, &cnt, 2);
1294 for (
size_t i = 0; i < rates.size(); i++) {
1295 uint32_t r = rates[i];
1297 memcpy(cb + 2 + i * 12, &r, 4);
1298 memcpy(cb + 2 + i * 12 + 4, &r, 4);
1299 memcpy(cb + 2 + i * 12 + 8, &z, 4);
1302 rhport, p_request, cb,
1303 (uint16_t)(2 + rates.size() * 12));
1308 memcpy(cb, &cnt, 2);
1309 memcpy(cb + 2, &rate, 4);
1310 memcpy(cb + 6, &rate, 4);
1311 memcpy(cb + 10, &z, 4);
1327 cb[0] =
isMute(channel) ? 1 : 0;
1338 int16_t vmin = -25600, vmax = 0, vres = 256;
1339 memcpy(cb + 0, &cnt, 2);
1340 memcpy(cb + 2, &vmin, 2);
1341 memcpy(cb + 4, &vmax, 2);
1342 memcpy(cb + 6, &vres, 2);
1353 uint8_t func_id = 0;
1354 uint8_t* cb =
audiod_fct_[func_id].ctrl_buf.data();
1433 return (entityID != 0)
1459 uint8_t
const* p_desc =
1464 uint8_t
const* p_desc_end =
descU16(p_desc + 6) + p_desc;
1469 while (p_desc_end - p_desc > 0) {
1470 if (p_desc[3] == entityID)
1487 uint8_t
const* p_desc_end =
1492 p_desc +=
descU16(p_desc + 6);
1496 while (p_desc_end - p_desc > 0) {
1515 uint8_t
const* p_desc_end =
audiod_fct_[i].p_desc +
1520 while (p_desc_end - p_desc > 0) {
1534 uint8_t
const* p_desc) {
1581 uint8_t
const** pp_desc_int) {
1584 uint8_t
const* p_desc_end =
1589 p_desc +=
descU16(p_desc + 6);
1594 while (p_desc_end - p_desc > 0) {
1601 *pp_desc_int = p_desc;
1621 uint8_t
const** pp_desc_int) {
1640 uint8_t func_id, idxItf;
1641 uint8_t
const* dummy;
1645 if (!
backend().controlTransfer(
1646 rhport, p_request, &
audiod_fct_[func_id].alt_setting[idxItf], 1))
1649 LOGI(
" Get itf: %u - current alt: %u", itf,
1656 bool apply_correction =
1659 if (apply_correction) {
1660 uint8_t* fb = (uint8_t*)audio->
fb_buf.data();
1686 (uint8_t*)audio->
fb_buf.data(),
1687 apply_correction ? 3 : 4);
1712 if (!
backend().usesIsoAlloc()) {
1742 if (
backend().usesIsoAlloc()) {
1758 uint8_t
const* p_desc_for_params) {
1819 switch (fb_param.
method) {
1829 if (fifo_depth == 0) fifo_depth = 1;
1830 uint16_t fifo_lvl_thr = fifo_depth / 2;
1840 ((uint32_t)fifo_lvl_thr) << 8;
1842 ((fb_param.
sample_freq / 100) << 16) / (frame_div / 100);
1860 uint8_t func_id, uint8_t itf, uint8_t alt) {
1861 uint8_t
const* p_desc = audio->
p_desc;
1862 uint8_t
const* p_desc_end =
1864 LOGD(
" openEPs: p_desc=%p end=%p len=%u itf=%u alt=%u",
1865 p_desc, p_desc_end, audio->
desc_length, itf, alt);
1867 while (p_desc_end - p_desc > 0) {
1871 uint8_t
const* p_desc_for_params =
1873 uint8_t foundEPs = 0;
1875 LOGD(
" matched itf=%u alt=%u nEps=%u", itf, alt, nEps);
1877 while (foundEPs < nEps && (p_desc_end - p_desc > 0)) {
1878 LOGD(
" scan: type=0x%02x len=%u offset=%d",
1879 p_desc[1], p_desc[0], (
int)(p_desc - audio->
p_desc));
1883 LOGD(
" activating ep=0x%02x type=%u...",
1886 LOGD(
" activateEndpoint FAILED");
1890 LOGD(
" activated OK");
1899 desc_ep.
usage == 0x00)
1900 openEpIn(rhport, audio, itf, desc_ep, p_desc_for_params);
1914 if (foundEPs != nEps)
return true;
1932 LOGD(
"SET_ITF itf=%u alt=%u [start]", itf, alt);
1934 uint8_t func_id, idxItf;
1935 uint8_t
const* p_desc;
1938 LOGD(
" AS interface %u not found", itf);
1942 LOGD(
" found func=%u idx=%u", func_id, idxItf);
1948 closeEpIn(rhport, audio, itf, p_request);
1959 bool enable_sof =
false;
1975 return value != 0 && (value & (value - 1)) == 0;
1980 while ((value >>= 1u) != 0u) result++;
1985 uint32_t mclk_freq) {
1995 if ((((1UL << k) * sample_freq / mclk_freq) + 1) > n_frame) {
1996 LOGE(
" UAC2 feedback interval too small");
2001 if ((mclk_freq % sample_freq) == 0 &&
2011 (float)sample_freq / (
float)mclk_freq *
2032 const uint8_t interval =
2035 const uint16_t sample_normimal = (uint16_t)(audio->
sample_rate_tx *
2036 interval / (full_speed ? 1000 : 8000));
2037 const uint16_t sample_reminder = (uint16_t)(audio->
sample_rate_tx *
2038 interval % (full_speed ? 1000 : 8000));
2040 const uint16_t packet_sz_tx_min =
2043 const uint16_t packet_sz_tx_norm =
2046 const uint16_t packet_sz_tx_max =
2051 if (packet_sz_tx_max > audio->
ep_in_sz)
return false;
2054 if (sample_reminder) {
2083 const uint32_t denom = full_speed ? 1000u : 8000u;
2085 const uint32_t interval = full_speed ? iv : (1u << (iv - 1));
2094 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