arduino-audio-tools
Loading...
Searching...
No Matches
USBAudioHostESP32.h
Go to the documentation of this file.
1#pragma once
2#include <Arduino.h>
3
4#if !(CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 || \
5 CONFIG_IDF_TARGET_ESP32P4)
6#error "USB Audio Host needs an ESP32 with USB-OTG (S2/S3/P4); for a MAX3421E define USE_TINYUSB to use the Adafruit TinyUSB host stack"
7#endif
8
9#if !__has_include("usb/usb_host.h")
10#error "USB Audio Host: the ESP-IDF usb_host library is not available"
11#endif
12
17
18namespace audio_tools {
19
43 public:
44 USBAudioHostESP32() = default;
47
49 BaseBuffer<uint8_t>& bufferTx() override { return buffer_tx_; }
50
52 BaseBuffer<uint8_t>& bufferRx() override { return buffer_rx_; }
53
55 int getActualCore() const override { return xPortGetCoreID(); }
56
58 void serviceUSB() override {}
59
60 protected:
62 // TX: blocking write (5 ms) so that the copier waits for free space;
63 // RX: the USB task never blocks when writing.
67
69
70 bool beginUSB() override {
71 if (!USBAudioHostBase::beginUSB()) return false;
73 return true;
74 }
75
76 void resizeBuffers() override {
77 size_t size = (size_t)audioPacketSize() * config_.fifo_packets;
78 if (isEpOutEnabled()) buffer_tx_.resize(size);
79 if (isEpInEnabled()) buffer_rx_.resize(size);
80 }
81
82 void startUsbTask() {
83 if (usb_task_.getTaskHandle() != nullptr) return;
84 // the ESP32 FreeRTOS uses bytes for the stack size
85 int stack = config_.usb_task_stack_size < 4096 ? 4096
86 : config_.usb_task_stack_size;
87 usb_task_.create("usb_audio_host", stack, config_.usb_task_priority);
88 // processUSB() blocks for at most 1 tick waiting for USB events
89 usb_task_.begin([this]() { processUSB(); });
90 usb_task_active_ = true;
91 }
92
93 void stopUsbTask() {
94 if (usb_task_.getTaskHandle() == nullptr) return;
95 usb_task_active_ = false;
97 }
98};
99
100} // namespace audio_tools
If you want to use the framework w/o Arduino you need to provide the implementation of a couple of cl...
Shared functionality of all buffers.
Definition Buffers.h:23
Buffer implementation which is using a FreeRTOS StreamBuffer. The default allocator uses psram is ava...
Definition BufferRTOS.h:33
bool resize(size_t size)
Re-Allocats the memory and the queue.
Definition BufferRTOS.h:54
FreeRTOS task.
Definition Task.h:26
TaskHandle_t & getTaskHandle()
Definition Task.h:126
void remove()
deletes the FreeRTOS task
Definition Task.h:85
bool create(const char *name, int stackSizeWords, int priority=1, int core=-1)
Definition Task.h:54
bool begin(std::function< void()> process)
Definition Task.h:94
USBAudioHostBackend for the ESP-IDF usb_host library which is part of arduino-esp32 (ESP32-S2,...
Definition USBAudioHostBackendESP32.h:34
Abstract seam for every raw USB host stack call the UAC host class logic (USBAudioHostBase) needs....
Definition USBAudioHostBackend.h:73
USB Audio Class (UAC1 and UAC2) host: reads PCM audio from an attached USB microphone and/or writes P...
Definition USBAudioHostBase.h:48
bool beginUSB() override
Override in platform subclasses to start the USB stack.
Definition USBAudioHostBase.h:385
void processUSB()
Definition USBAudioHostBase.h:389
uint16_t audioPacketSize() const
Definition USBAudioHostBase.h:226
USB Audio host for ESP32-S2/S3/P4 using the ESP-IDF usb_host library: reads audio from an attached US...
Definition USBAudioHostESP32.h:42
void stopUsbTask()
Definition USBAudioHostESP32.h:93
BufferRTOS< uint8_t > buffer_rx_
Definition USBAudioHostESP32.h:65
BaseBuffer< uint8_t > & bufferTx() override
Returns the TX audio buffer (FreeRTOS stream buffer).
Definition USBAudioHostESP32.h:49
void serviceUSB() override
No-op: the USB task runs the stack continuously.
Definition USBAudioHostESP32.h:58
USBAudioHostBackend & backend() override
Returns the backend used for all USB host stack calls.
Definition USBAudioHostESP32.h:68
~USBAudioHostESP32()
Definition USBAudioHostESP32.h:46
Task usb_task_
Definition USBAudioHostESP32.h:66
void startUsbTask()
Definition USBAudioHostESP32.h:82
bool beginUSB() override
Override in platform subclasses to start the USB stack.
Definition USBAudioHostESP32.h:70
USBAudioHostESP32(USBAudioConfig cfg)
Definition USBAudioHostESP32.h:45
void resizeBuffers() override
Resize the platform audio buffers.
Definition USBAudioHostESP32.h:76
USBAudioHostBackendESP32 backend_impl_
Definition USBAudioHostESP32.h:61
BaseBuffer< uint8_t > & bufferRx() override
Returns the RX audio buffer (FreeRTOS stream buffer).
Definition USBAudioHostESP32.h:52
BufferRTOS< uint8_t > buffer_tx_
Definition USBAudioHostESP32.h:64
int getActualCore() const override
Returns the actual core on which the call is running.
Definition USBAudioHostESP32.h:55
bool isEpInEnabled() const
Returns true if the IN endpoint is enabled.
Definition USBAudioStreamBase.h:42
USBAudioConfig config_
Definition USBAudioStreamBase.h:235
bool usb_task_active_
Definition USBAudioStreamBase.h:223
bool isEpOutEnabled() const
Returns true if the OUT endpoint is enabled.
Definition USBAudioStreamBase.h:45
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
Configuration for USB Audio (inherits sample_rate / channels / bits_per_sample from AudioInfo).
Definition USBAudioConfig.h:117
uint8_t fifo_packets
Definition USBAudioConfig.h:148