libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
uac_mic.hpp
1#pragma once
2#include <cstdint>
3#include <cstring>
4
5#include "core.hpp"
6#include "desc_cfg.hpp"
7#include "dev_core.hpp"
8#include "ep.hpp"
9#include "libxr_def.hpp"
10
11namespace LibXR::USB
12{
13
20template <uint8_t CHANNELS, uint8_t BITS_PER_SAMPLE>
22{
23 static_assert(CHANNELS >= 1 && CHANNELS <= 8, "CHANNELS out of range");
24 static_assert(BITS_PER_SAMPLE == 8 || BITS_PER_SAMPLE == 16 || BITS_PER_SAMPLE == 24,
25 "BITS_PER_SAMPLE must be 8/16/24");
26
28 static const constexpr uint8_t K_SUBFRAME_SIZE = (BITS_PER_SAMPLE <= 8) ? 1
29 : (BITS_PER_SAMPLE <= 16) ? 2
30 : 3;
31
32 public:
44 UAC1MicrophoneQ(uint32_t sample_rate_hz, int16_t vol_min, int16_t vol_max,
45 int16_t vol_res, Speed speed, size_t queue_bytes = 8192,
46 uint8_t interval = 1,
48 : iso_in_ep_num_(iso_in_ep_num),
49 vol_min_(vol_min),
50 vol_max_(vol_max),
51 vol_res_(vol_res),
52 interval_(interval),
53 speed_(speed),
54 sr_hz_(sample_rate_hz),
55 pcm_queue_(queue_bytes)
56 {
58 // 缓存端点采样率(3 字节小端) / Cache current sampling frequency (3‑byte LE)
59 sf_cur_[0] = static_cast<uint8_t>(sr_hz_ & 0xFF);
60 sf_cur_[1] = static_cast<uint8_t>((sr_hz_ >> 8) & 0xFF);
61 sf_cur_[2] = static_cast<uint8_t>((sr_hz_ >> 16) & 0xFF);
62 }
63
64 // ===== 生产者接口 / Producer‑side API =====
69 ErrorCode WritePcm(const void* data, size_t nbytes)
70 {
71 return pcm_queue_.PushBatch(reinterpret_cast<const uint8_t*>(data), nbytes);
72 }
74 size_t QueueSize() const { return pcm_queue_.Size(); }
76 size_t QueueSpace() { return pcm_queue_.EmptySize(); }
78 void ResetQueue() { pcm_queue_.Reset(); }
79
80 // ===== UAC1 常量 / UAC1 constants =====
81 enum : uint8_t
82 {
83 USB_CLASS_AUDIO = 0x01,
84 SUBCLASS_AC = 0x01,
85 SUBCLASS_AS = 0x02
86 };
87 enum : uint8_t
88 {
89 CS_INTERFACE = 0x24,
90 CS_ENDPOINT = 0x25
91 };
92 enum : uint8_t
93 {
94 AC_HEADER = 0x01,
95 AC_INPUT_TERMINAL = 0x02,
96 AC_OUTPUT_TERMINAL = 0x03,
97 AC_FEATURE_UNIT = 0x06
98 };
99 enum : uint8_t
100 {
101 AS_GENERAL = 0x01,
102 AS_FORMAT_TYPE = 0x02
103 };
104 enum : uint8_t
105 {
106 EP_GENERAL = 0x01
107 };
108 enum : uint16_t
109 {
110 WFORMAT_PCM = 0x0001,
111 WFORMAT_UNKNOWN = 0xFFFF
112 };
113 enum : uint8_t
114 {
115 FORMAT_TYPE_I = 0x01
116 };
117
118 // UAC1 类请求 / Class‑specific requests
119 enum : uint8_t
120 {
121 SET_CUR = 0x01,
122 GET_CUR = 0x81,
123 SET_MIN = 0x02,
124 GET_MIN = 0x82,
125 SET_MAX = 0x03,
126 GET_MAX = 0x83,
127 SET_RES = 0x04,
128 GET_RES = 0x84
129 };
130 enum : uint8_t
131 {
132 FU_MUTE = 0x01,
133 FU_VOLUME = 0x02
134 };
135
136 // 实体 ID / Entity IDs
137 enum : uint8_t
138 {
139 ID_IT_MIC = 1,
140 ID_FU = 2,
141 ID_OT_USB = 3
142 };
143
144#pragma pack(push, 1)
150 {
151 uint8_t bLength = 9, bDescriptorType = CS_INTERFACE, bDescriptorSubtype = AC_HEADER;
152 uint16_t bcdADC = 0x0100, wTotalLength = 0;
153 uint8_t bInCollection = 1, baInterfaceNr = 0;
154 };
155
161 {
162 uint8_t bLength = 12, bDescriptorType = CS_INTERFACE,
163 bDescriptorSubtype = AC_INPUT_TERMINAL;
164 uint8_t bTerminalID = ID_IT_MIC;
165 uint16_t wTerminalType = 0x0201; // Microphone
166 uint8_t bAssocTerminal = 0, bNrChannels = CHANNELS;
167 uint16_t wChannelConfig = 0x0000; // 根据 CHANNELS 设定 / Set per CHANNELS
168 uint8_t iChannelNames = 0, iTerminal = 0;
169 };
170
176 {
177 uint8_t bLength = static_cast<uint8_t>(7 + (CHANNELS + 1) * 1);
178 uint8_t bDescriptorType = CS_INTERFACE, bDescriptorSubtype = AC_FEATURE_UNIT;
179 uint8_t bUnitID = ID_FU, bSourceID = ID_IT_MIC, bControlSize = 1;
180 uint8_t bmaControlsMaster = 0x03; // Mute + Volume
181 uint8_t bmaControlsCh[CHANNELS] = {0x03}; // Per‑channel supported
182 uint8_t iFeature = 0;
183 };
184
190 {
191 uint8_t bLength = 9, bDescriptorType = CS_INTERFACE,
192 bDescriptorSubtype = AC_OUTPUT_TERMINAL;
193 uint8_t bTerminalID = ID_OT_USB;
194 uint16_t wTerminalType = 0x0101; // USB Streaming
195 uint8_t bAssocTerminal = 0, bSourceID = ID_FU, iTerminal = 0;
196 };
197
203 {
204 uint8_t bLength = 7, bDescriptorType = CS_INTERFACE, bDescriptorSubtype = AS_GENERAL;
205 uint8_t bTerminalLink = ID_OT_USB, bDelay = 1;
206 uint16_t wFormatTag = WFORMAT_PCM;
207 };
208
214 {
215 uint8_t bLength = 11, bDescriptorType = CS_INTERFACE,
216 bDescriptorSubtype = AS_FORMAT_TYPE;
217 uint8_t bFormatType = FORMAT_TYPE_I, bNrChannels = CHANNELS;
218 uint8_t bSubframeSize = K_SUBFRAME_SIZE, bBitResolution = BITS_PER_SAMPLE,
219 bSamFreqType = 1;
220 uint8_t tSamFreq[3]; // 运行期填充 / Filled at runtime
221 };
222
228 {
229 uint8_t bLength = 7, bDescriptorType = CS_ENDPOINT, bDescriptorSubtype = EP_GENERAL;
230 uint8_t bmAttributes = 0x00;
231 uint8_t bLockDelayUnits = 0;
232 uint16_t wLockDelay = 0;
233 };
234
240 {
241 uint8_t bLength = 9;
242 uint8_t bDescriptorType = static_cast<uint8_t>(DescriptorType::ENDPOINT);
243 uint8_t bEndpointAddress = 0; // 含方向位 / includes direction bit
244 uint8_t bmAttributes = 0x05; // Isochronous + Async + Data
245 uint16_t wMaxPacketSize = 0; // 运行期填充 / filled at runtime
246 uint8_t bInterval = 0x01; // 1 ms
247 uint8_t bRefresh = 0x00; // 仅反馈端点使用 / feedback only
248 uint8_t bSynchAddress = 0x00; // 无伴随端点 / no sync/feedback ep
249 };
250
270#pragma pack(pop)
271
272 // ===== DeviceClass 接口实现 / DeviceClass implementation =====
280 void BindEndpoints(EndpointPool& endpoint_pool, uint8_t start_itf_num, bool) override
281 {
282 inited_ = false;
283 streaming_ = false;
284 acc_rem_ = 0;
285
286 if (speed_ == Speed::HIGH)
287 {
288 ASSERT(w_max_packet_size_ <= 1024);
289 }
290 else
291 {
292 ASSERT(interval_ == 1);
293 ASSERT(w_max_packet_size_ <= 1023);
294 }
295
296 // 仅分配端点,不立即开启(在 AS Alt=1 时开启)
297 auto ans = endpoint_pool.Get(ep_iso_in_, Endpoint::Direction::IN, iso_in_ep_num_);
298 ASSERT(ans == ErrorCode::OK);
299
301 static_cast<uint16_t>(w_max_packet_size_), true});
302
303 itf_ac_num_ = start_itf_num;
304 itf_as_num_ = static_cast<uint8_t>(start_itf_num + 1);
305
306 // IAD
307 desc_block_.iad = {8,
308 static_cast<uint8_t>(DescriptorType::IAD),
309 itf_ac_num_,
310 2,
311 USB_CLASS_AUDIO,
312 SUBCLASS_AC,
313 0x00,
314 0};
315
316 // AC 接口(标准接口)
317 desc_block_.ac_intf = {9,
318 static_cast<uint8_t>(DescriptorType::INTERFACE),
319 itf_ac_num_,
320 0,
321 0,
322 USB_CLASS_AUDIO,
323 SUBCLASS_AC,
324 0x00,
325 0};
326
327 // AC Header
328 desc_block_.ac_hdr.baInterfaceNr = itf_as_num_;
329 desc_block_.ac_hdr.wTotalLength =
330 static_cast<uint16_t>(sizeof(CSACHeader) + sizeof(ACInputTerminal) +
331 sizeof(ACFeatureUnit) + sizeof(ACOutputTerminal));
332
333 // AC Input Terminal 的 wChannelConfig 按通道数设置
334 if constexpr (CHANNELS == 2)
335 {
336 desc_block_.it_mic.wChannelConfig = 0x0003; // Left Front | Right Front
337 }
338 else
339 {
340 desc_block_.it_mic.wChannelConfig = 0x0000; // 未声明或单声道 / unspecified or mono
341 }
342
343 // AS Alt 0(无端点)
344 desc_block_.as_alt0 = {9,
345 static_cast<uint8_t>(DescriptorType::INTERFACE),
346 itf_as_num_,
347 0,
348 0,
349 USB_CLASS_AUDIO,
350 SUBCLASS_AS,
351 0x00,
352 0};
353
354 // AS Alt 1(1 个 Iso IN 端点)
355 desc_block_.as_alt1 = {9,
356 static_cast<uint8_t>(DescriptorType::INTERFACE),
357 itf_as_num_,
358 1,
359 1,
360 USB_CLASS_AUDIO,
361 SUBCLASS_AS,
362 0x00,
363 0};
364
365 // AS General
366 desc_block_.as_gen = {};
367 desc_block_.as_gen.bTerminalLink = ID_OT_USB;
368 desc_block_.as_gen.bDelay = 1;
369 desc_block_.as_gen.wFormatTag = WFORMAT_PCM;
370
371 // Type I Format(单一离散采样率)
372 desc_block_.fmt.bFormatType = FORMAT_TYPE_I;
373 desc_block_.fmt.bNrChannels = CHANNELS;
374 desc_block_.fmt.bSubframeSize = K_SUBFRAME_SIZE;
375 desc_block_.fmt.bBitResolution = BITS_PER_SAMPLE;
376 desc_block_.fmt.bSamFreqType = 1;
377 desc_block_.fmt.tSamFreq[0] = static_cast<uint8_t>(sr_hz_ & 0xFF);
378 desc_block_.fmt.tSamFreq[1] = static_cast<uint8_t>((sr_hz_ >> 8) & 0xFF);
379 desc_block_.fmt.tSamFreq[2] = static_cast<uint8_t>((sr_hz_ >> 16) & 0xFF);
380
381 // 标准 ISO IN 端点(9 字节)
382 desc_block_.ep_in = {};
383 desc_block_.ep_in.bEndpointAddress = static_cast<uint8_t>(
385 desc_block_.ep_in.bmAttributes = 0x05;
386 desc_block_.ep_in.wMaxPacketSize = static_cast<uint16_t>(w_max_packet_size_);
387 desc_block_.ep_in.bInterval = (speed_ == Speed::HIGH) ? interval_ : 0x01;
388 desc_block_.ep_in.bRefresh = 0x00;
389 desc_block_.ep_in.bSynchAddress = 0x00;
390
391 desc_block_.ep_cs = {};
392 desc_block_.ep_cs.bmAttributes = 0x00;
393 // IN 传输完成回调 → 继续投下一帧
394 ep_iso_in_->SetOnTransferCompleteCallback(on_in_complete_cb_);
395
396 // 设置整块配置数据
397 SetData(RawData{reinterpret_cast<uint8_t*>(&desc_block_), sizeof(desc_block_)});
398
399 inited_ = true;
400 }
401
409 void UnbindEndpoints(EndpointPool& endpoint_pool, bool) override
410 {
411 streaming_ = false;
412 inited_ = false;
413 if (ep_iso_in_)
414 {
415 ep_iso_in_->Close();
416 ep_iso_in_->SetActiveLength(0);
417 endpoint_pool.Release(ep_iso_in_);
418 ep_iso_in_ = nullptr;
419 }
420 }
421
426 ErrorCode OnGetDescriptor(bool, uint8_t, uint16_t, uint16_t, ConstRawData&) override
427 {
428 return ErrorCode::NOT_SUPPORT;
429 }
430
435 ErrorCode OnClassRequest(bool /*in_isr*/, uint8_t bRequest, uint16_t wValue,
436 uint16_t wLength, uint16_t wIndex,
438 {
439 // ===== 端点采样率控制(收件人:端点地址) / EP sampling‑freq control =====
440 const uint8_t EP_ADDR = static_cast<uint8_t>(wIndex & 0xFF);
441 const uint8_t CS_EP =
442 static_cast<uint8_t>((wValue >> 8) & 0xFF); // 0x01 = Sampling Freq
443
444 if (EP_ADDR == desc_block_.ep_in.bEndpointAddress && CS_EP == 0x01)
445 {
446 switch (bRequest)
447 {
448 case GET_CUR:
449 if (wLength == 3)
450 {
451 r.write_data = ConstRawData{sf_cur_, 3};
452 return ErrorCode::OK;
453 }
454 break;
455 case SET_CUR:
456 if (wLength == 3)
457 {
458 pending_set_sf_ = true;
459 r.read_data = RawData{sf_cur_, 3};
460 return ErrorCode::OK;
461 }
462 break;
463 case GET_MIN:
464 case GET_MAX:
465 if (wLength == 3)
466 {
467 r.write_data = ConstRawData{sf_cur_, 3};
468 return ErrorCode::OK;
469 } // 单一频点
470 break;
471 case GET_RES:
472 {
473 static uint8_t one_hz[3] = {1, 0, 0};
474 if (wLength == 3)
475 {
476 r.write_data = ConstRawData{one_hz, 3};
477 return ErrorCode::OK;
478 }
479 break;
480 }
481 default:
482 break;
483 }
484 ASSERT(false);
485 return ErrorCode::ARG_ERR; // 长度不符等 / length mismatch etc.
486 }
487
488 // ===== Feature Unit(收件人:Interface / AC 接口) / Feature Unit =====
489 const uint8_t CS = static_cast<uint8_t>((wValue >> 8) & 0xFF); // FU_MUTE / FU_VOLUME
490 const uint8_t CH = static_cast<uint8_t>(wValue & 0xFF); // 0=master, 1..N
491 const uint8_t ENT =
492 static_cast<uint8_t>((wIndex >> 8) & 0xFF); // 实体 ID / entity ID
493 const uint8_t ITF = static_cast<uint8_t>(wIndex & 0xFF); // 接口号 / interface num
494
495 if (ITF != itf_ac_num_ || ENT != ID_FU)
496 {
497 ASSERT(false);
498 return ErrorCode::NOT_SUPPORT;
499 }
500 if (CH > CHANNELS)
501 {
502 ASSERT(false);
503 return ErrorCode::ARG_ERR;
504 }
505
506 switch (CS)
507 {
508 case FU_MUTE:
509 if (bRequest == SET_CUR && wLength == 1)
510 {
511 r.read_data = RawData{&mute_, 1};
512 return ErrorCode::OK;
513 }
514 if (bRequest == GET_CUR && wLength == 1)
515 {
516 r.write_data = ConstRawData{&mute_, 1};
517 return ErrorCode::OK;
518 }
519 ASSERT(false);
520 return ErrorCode::ARG_ERR;
521
522 case FU_VOLUME:
523 switch (bRequest)
524 {
525 case SET_CUR:
526 if (wLength == 2)
527 {
528 r.read_data = RawData{reinterpret_cast<uint8_t*>(&vol_cur_), 2};
529 return ErrorCode::OK;
530 }
531 break;
532 case GET_CUR:
533 if (wLength == 2)
534 {
535 r.write_data = ConstRawData{reinterpret_cast<uint8_t*>(&vol_cur_), 2};
536 return ErrorCode::OK;
537 }
538 break;
539 case GET_MIN:
540 if (wLength == 2)
541 {
542 r.write_data = ConstRawData{reinterpret_cast<const uint8_t*>(&vol_min_), 2};
543 return ErrorCode::OK;
544 }
545 break;
546 case GET_MAX:
547 if (wLength == 2)
548 {
549 r.write_data = ConstRawData{reinterpret_cast<const uint8_t*>(&vol_max_), 2};
550 return ErrorCode::OK;
551 }
552 break;
553 case GET_RES:
554 if (wLength == 2)
555 {
556 r.write_data = ConstRawData{reinterpret_cast<const uint8_t*>(&vol_res_), 2};
557 return ErrorCode::OK;
558 }
559 break;
560 case SET_RES: // 吃下以避免 STALL / accept to avoid STALL
561 if (wLength == 2)
562 {
563 r.read_data = RawData{reinterpret_cast<uint8_t*>(&vol_res_), 2};
564 return ErrorCode::OK;
565 }
566 default:
567 break;
568 }
569 ASSERT(false);
570 return ErrorCode::ARG_ERR;
571
572 default:
573 ASSERT(false);
574 return ErrorCode::NOT_SUPPORT;
575 }
576 }
577
582 ErrorCode OnClassData(bool /*in_isr*/, uint8_t bRequest,
583 LibXR::ConstRawData& /*data*/) override
584 {
585 if (bRequest == SET_CUR && pending_set_sf_)
586 {
587 const uint32_t NEW_SR = static_cast<uint32_t>(sf_cur_[0]) |
588 (static_cast<uint32_t>(sf_cur_[1]) << 8) |
589 (static_cast<uint32_t>(sf_cur_[2]) << 16);
590 if (NEW_SR > 0 && NEW_SR != sr_hz_)
591 {
592 sr_hz_ = NEW_SR;
594 }
595 pending_set_sf_ = false;
596 return ErrorCode::OK;
597 }
598 return ErrorCode::OK;
599 }
600
605 ErrorCode GetAltSetting(uint8_t itf, uint8_t& alt) override
606 {
607 if (itf != itf_as_num_)
608 {
609 ASSERT(false);
610 return ErrorCode::NOT_SUPPORT;
611 }
612 alt = streaming_ ? 1 : 0;
613 return ErrorCode::OK;
614 }
615
620 ErrorCode SetAltSetting(uint8_t itf, uint8_t alt) override
621 {
622 if (itf != itf_as_num_)
623 {
624 ASSERT(false);
625 return ErrorCode::NOT_SUPPORT;
626 }
627 if (!ep_iso_in_)
628 {
629 ASSERT(false);
630 return ErrorCode::FAILED;
631 }
632
633 switch (alt)
634 {
635 case 0: // Alt 0:无端点
636 streaming_ = false;
637 ep_iso_in_->SetActiveLength(0);
638 ep_iso_in_->Close();
639 return ErrorCode::OK;
640
641 case 1: // Alt 1:一个 Iso IN 端点
643 static_cast<uint16_t>(w_max_packet_size_), false});
644 ep_iso_in_->SetActiveLength(0);
645 acc_rem_ = 0; // 重置余数累加器 / reset remainder accumulator
646 streaming_ = true;
647 KickOneFrame();
648 return ErrorCode::OK;
649
650 default:
651 ASSERT(false);
652 return ErrorCode::ARG_ERR;
653 }
654 }
655
657 size_t GetInterfaceCount() override { return 2; }
659 bool HasIAD() override { return true; }
661 size_t GetMaxConfigSize() override { return sizeof(UAC1DescBlock); }
662
663 bool OwnsEndpoint(uint8_t ep_addr) const override
664 {
665 if (!inited_)
666 {
667 return false;
668 }
669 return ep_iso_in_->GetAddress() == ep_addr;
670 }
671
672 private:
673 // ===== 端点传输 / Endpoint transfers =====
678 static void OnInCompleteStatic(bool in_isr, UAC1MicrophoneQ* self, ConstRawData& data)
679 {
680 if (!self->inited_)
681 {
682 return;
683 }
684 self->OnInComplete(in_isr, data);
685 }
686
692 {
693 if (!streaming_)
694 {
695 return;
696 }
697 KickOneFrame();
698 }
699
705 {
706 if (!streaming_)
707 {
708 return; // Alt=1 才允许
709 }
710 if (!ep_iso_in_ || ep_iso_in_->GetState() != Endpoint::State::IDLE)
711 {
712 return;
713 }
714
715 // 本帧应发送字节数 = floor + 余数累加决定是否 +1
716 uint16_t to_send = static_cast<uint16_t>(base_bytes_per_service_);
717 acc_rem_ += rem_bytes_per_service_;
718 if (acc_rem_ >= service_hz_)
719 {
720 ++to_send;
721 acc_rem_ -= service_hz_;
722 }
723
724 if (to_send > w_max_packet_size_)
725 {
726 to_send = static_cast<uint16_t>(w_max_packet_size_);
727 }
728
729 auto buf = ep_iso_in_->GetBuffer();
730 if (buf.size_ < to_send)
731 {
732 to_send = static_cast<uint16_t>(buf.size_);
733 }
734
735 // 从队列取可用字节 / pop available bytes from queue
736 size_t have = pcm_queue_.Size();
737 size_t take = (have >= to_send) ? to_send : have;
738
739 if (take)
740 {
741 pcm_queue_.PopBatch(reinterpret_cast<uint8_t*>(buf.addr_), take);
742 }
743
744 ep_iso_in_->Transfer(take);
745 }
746
752 {
753 // 1) 计算每秒服务次数(FS=1000Hz;HS=8000/2^(bInterval-1))
754 if (speed_ == Speed::HIGH)
755 {
756 uint8_t eff = interval_ ? interval_ : 1;
757 if (eff > 16)
758 {
759 eff = 16;
760 }
761 const uint32_t MICROFRAMES = 1u << (eff - 1u); // 2^(bInterval-1) 个微帧
762 service_hz_ = 8000u / MICROFRAMES; // 8000 微帧/秒
763 }
764 else
765 {
766 service_hz_ = 1000u; // FS 等时:规范上 bInterval 必须为 1 帧 => 1kHz
767 }
768
769 // 2) 计算每服务周期应送字节
770 bytes_per_sec_ = static_cast<uint32_t>(sr_hz_) * CHANNELS * K_SUBFRAME_SIZE;
771 base_bytes_per_service_ = bytes_per_sec_ / service_hz_;
772 rem_bytes_per_service_ = bytes_per_sec_ % service_hz_;
773 uint32_t ceil_bpt = base_bytes_per_service_ + (rem_bytes_per_service_ ? 1u : 0u);
774
775 // 3) 钳制每事务上限(FS 1023,HS 1024;此处只做单事务上限,未用 HS multiplier)
776 const uint32_t PER_TX_LIMIT = (speed_ == Speed::HIGH) ? 1024u : 1023u;
777 if (ceil_bpt > PER_TX_LIMIT)
778 {
779 ceil_bpt = PER_TX_LIMIT;
780 }
781
782 w_max_packet_size_ = static_cast<uint16_t>(ceil_bpt);
783
784 // 4) 若已构建过描述符,则运行时能力不得超过宣告值
785 if (desc_block_.ep_in.wMaxPacketSize != 0 &&
786 w_max_packet_size_ > desc_block_.ep_in.wMaxPacketSize)
787 {
788 w_max_packet_size_ = desc_block_.ep_in.wMaxPacketSize;
789 }
790 }
791
792 // 端点/接口 / Endpoints & interfaces
793 Endpoint::EPNumber iso_in_ep_num_;
794 Endpoint* ep_iso_in_ = nullptr;
795 uint8_t itf_ac_num_ = 0;
796 uint8_t itf_as_num_ = 0;
797
798 // 状态 / State
799 bool inited_ = false;
800 bool streaming_ = false;
801
802 // 音量/静音 / Volume & mute
803 uint8_t mute_ = 0;
804 int16_t vol_cur_ = 0; // 0 dB
805 int16_t vol_min_, vol_max_, vol_res_;
806
807 uint8_t interval_;
808 Speed speed_;
809
810 // 采样与帧切分 / Sampling & framing
811 uint32_t sr_hz_;
812 uint32_t bytes_per_sec_ = 0;
813 uint32_t base_bytes_per_service_ = 0;
814 uint32_t rem_bytes_per_service_ = 0;
815 uint32_t acc_rem_ = 0; // 0..999
816 uint16_t w_max_packet_size_ = 0;
817 uint32_t service_hz_ = 1000;
818
819 UAC1DescBlock desc_block_;
820
821 // 端点采样率缓存(Hz,小端 3 字节)与状态 / EP sampling‑freq cache & flag
822 uint8_t sf_cur_[3] = {0, 0, 0};
823 bool pending_set_sf_ = false;
824
825 // PCM 队列(字节) / PCM byte queue
827
828 // 端点回调包装 / Endpoint callback wrapper
829 LibXR::Callback<LibXR::ConstRawData&> on_in_complete_cb_ =
831};
832
833} // namespace LibXR::USB
通用回调包装,支持动态参数传递 / Generic callback wrapper supporting dynamic argument passing
Definition libxr_cb.hpp:150
static Callback Create(FunType fun, ArgType arg)
创建回调对象并绑定回调函数与参数 / Create a callback instance with bound function and argument
Definition libxr_cb.hpp:167
常量原始数据封装类。 A class for encapsulating constant raw data.
无锁队列实现 / Lock-free queue implementation
void Reset()
重置队列 / Resets the queue
ErrorCode PushBatch(const Data *data, size_t size)
批量推入数据 / Pushes multiple elements into the queue
size_t EmptySize()
计算队列剩余可用空间 / Calculates the remaining available space in the queue
size_t Size() const
获取当前队列中的元素数量 / Returns the number of elements currently in the queue
ErrorCode PopBatch(Data *data, size_t size)
批量弹出数据 / Pops multiple elements from the queue
原始数据封装类。 A class for encapsulating raw data.
void SetData(RawData data)
设置内部数据缓存 / Set internal data cache
Definition desc_cfg.hpp:194
USB 设备类接口基类 / USB device class interface base.
Definition dev_core.hpp:26
USB 端点基类 / USB Endpoint base class.
Definition ep.hpp:23
EPNumber
端点号 Endpoint number
Definition ep.hpp:41
@ EP_AUTO
自动分配端点号 / Auto allocate
uint8_t GetAddress() const
获取端点地址(方向 + 号) / Get endpoint address (dir + num)
Definition ep.hpp:194
@ IN
输入方向 / IN direction
static constexpr uint8_t EPNumberToAddr(EPNumber ep, Direction dir)
端点号转换为端点地址 / Convert endpoint number to endpoint address
Definition ep.hpp:104
void SetActiveLength(uint16_t len)
设置当前活动缓冲区有效长度 / Set active buffer valid length
Definition ep.hpp:270
void SetOnTransferCompleteCallback(Callback< ConstRawData & > cb)
设置传输完成回调 / Set transfer complete callback
Definition ep.hpp:261
virtual void Configure(const Config &cfg)=0
配置端点协议参数 / Configure endpoint protocol parameters
@ ISOCHRONOUS
等时端点 / Isochronous
EPNumber GetNumber() const
获取端点号 / Get endpoint number
Definition ep.hpp:169
virtual void Close()=0
关闭端点 / Close endpoint
State GetState() const
获取端点状态 / Get endpoint state
Definition ep.hpp:207
virtual ErrorCode Transfer(size_t size)=0
启动一次传输 / Start a transfer
RawData GetBuffer() const
获取当前可用于传输的缓冲区 / Get current transfer buffer
Definition ep.hpp:245
USB端点池类 / USB endpoint pool class.
Definition ep_pool.hpp:23
ErrorCode Get(Endpoint *&ep_info, Endpoint::Direction direction, Endpoint::EPNumber ep_num=Endpoint::EPNumber::EP_AUTO)
分配端点 / Allocate endpoint
Definition ep_pool.cpp:11
ErrorCode Release(Endpoint *ep_info)
回收端点 / Release endpoint
Definition ep_pool.cpp:37
UAC1 队列式麦克风设备类实现 UAC1 queue‑driven microphone device class implementation.
Definition uac_mic.hpp:22
size_t GetInterfaceCount() override
返回接口数量(AC+AS=2)| Get number of interfaces (AC+AS=2)
Definition uac_mic.hpp:657
void RecomputeTiming()
统一重算时序与包长(运行时 MPS 钳制到描述符宣告值) Recompute timing and packet sizing (runtime MPS clamped to descriptor)
Definition uac_mic.hpp:751
ErrorCode OnClassRequest(bool, uint8_t bRequest, uint16_t wValue, uint16_t wLength, uint16_t wIndex, DeviceClass::ControlTransferResult &r) override
处理类请求:端点采样率控制 + Feature Unit(静音/音量) Handle class requests: EP sampling‑freq control + Feature Unit (m...
Definition uac_mic.hpp:435
ErrorCode OnGetDescriptor(bool, uint8_t, uint16_t, uint16_t, ConstRawData &) override
UAC1 无类特定描述符读出(配置中已包含) UAC1 does not use GET_DESCRIPTOR(0x21/0x22) here.
Definition uac_mic.hpp:426
void UnbindEndpoints(EndpointPool &endpoint_pool, bool) override
反初始化设备,释放端点 Deinitialize device and release endpoints
Definition uac_mic.hpp:409
static const constexpr uint8_t K_SUBFRAME_SIZE
每通道每采样的子帧字节数 / Subframe size (bytes per channel per sample)
Definition uac_mic.hpp:28
bool HasIAD() override
包含 IAD | Has IAD
Definition uac_mic.hpp:659
size_t QueueSpace()
获取队列剩余空间 | Get remaining queue capacity (bytes)
Definition uac_mic.hpp:76
UAC1MicrophoneQ(uint32_t sample_rate_hz, int16_t vol_min, int16_t vol_max, int16_t vol_res, Speed speed, size_t queue_bytes=8192, uint8_t interval=1, Endpoint::EPNumber iso_in_ep_num=Endpoint::EPNumber::EP_AUTO)
构造 UAC1 队列式麦克风 Construct a queue‑backed UAC1 microphone
Definition uac_mic.hpp:44
size_t GetMaxConfigSize() override
配置描述符最大尺寸 | Get maximum configuration size
Definition uac_mic.hpp:661
void KickOneFrame()
计算并投递一帧(按 1ms 变包 + 零填充) Compute and submit one frame (1 ms variable size + zero fill)
Definition uac_mic.hpp:704
bool OwnsEndpoint(uint8_t ep_addr) const override
可选:端点归属判定 / Optional: endpoint ownership
Definition uac_mic.hpp:663
static void OnInCompleteStatic(bool in_isr, UAC1MicrophoneQ *self, ConstRawData &data)
IN 传输完成静态回调 Static callback for IN transfer completion.
Definition uac_mic.hpp:678
void OnInComplete(bool, ConstRawData &)
IN 传输完成处理:继续投下一帧 Handle IN transfer completion: kick next frame.
Definition uac_mic.hpp:691
void ResetQueue()
重置队列为空 | Reset queue to empty
Definition uac_mic.hpp:78
ErrorCode OnClassData(bool, uint8_t bRequest, LibXR::ConstRawData &) override
处理类请求数据阶段(应用 SET_CUR 的采样率) Handle class request data stage (apply SET_CUR sampling freq)
Definition uac_mic.hpp:582
void BindEndpoints(EndpointPool &endpoint_pool, uint8_t start_itf_num, bool) override
初始化设备(分配端点、填充描述符) Initialize device (allocate endpoints, populate descriptors)
Definition uac_mic.hpp:280
ErrorCode GetAltSetting(uint8_t itf, uint8_t &alt) override
获取接口的当前 Alternate Setting Get current alternate setting for interface
Definition uac_mic.hpp:605
size_t QueueSize() const
获取队列当前字节数 | Get current queued bytes
Definition uac_mic.hpp:74
ErrorCode WritePcm(const void *data, size_t nbytes)
写入 PCM 字节流(如 S16LE / S24_3LE) Write interleaved PCM bytes (e.g., S16LE / S24_3LE)
Definition uac_mic.hpp:69
ErrorCode SetAltSetting(uint8_t itf, uint8_t alt) override
设置接口的 Alternate Setting(切换数据流) Set interface alternate setting (toggle streaming)
Definition uac_mic.hpp:620
IAD(8 字节)/ Interface Association Descriptor (8 bytes)
Definition desc_cfg.hpp:61
接口描述符(9 字节)/ Interface descriptor (9 bytes)
Definition desc_cfg.hpp:76
控制请求(Class/Vendor)处理结果 / Control request (Class/Vendor) handling result
Definition dev_core.hpp:51
AC 特性单元(静音/音量) AC feature unit (mute/volume)
Definition uac_mic.hpp:176
AC 输入端子(麦克风) AC input terminal (Microphone)
Definition uac_mic.hpp:161
AC 输出端子(USB 流) AC output terminal (USB streaming)
Definition uac_mic.hpp:190
AS 通用描述符 AS general descriptor.
Definition uac_mic.hpp:203
AC 头描述符 AC header descriptor.
Definition uac_mic.hpp:150
类特定端点(通用) Class‑specific endpoint (general)
Definition uac_mic.hpp:228
标准等时 IN 端点(9 字节,含 bRefresh/bSynchAddress) Standard isoch IN endpoint (9 bytes, with bRefresh/bSynchAd...
Definition uac_mic.hpp:240
Type I 格式(单一离散采样率) Type I format (single discrete sampling frequency)
Definition uac_mic.hpp:214
UAC1 描述符块 UAC1 descriptor block.
Definition uac_mic.hpp:256
ACInputTerminal it_mic
输入端子 / Input terminal
Definition uac_mic.hpp:260
ACFeatureUnit fu
特性单元 / Feature unit
Definition uac_mic.hpp:261
InterfaceDescriptor ac_intf
AC 接口 / AC interface.
Definition uac_mic.hpp:258
CSACHeader ac_hdr
AC 头 / AC header.
Definition uac_mic.hpp:259
InterfaceDescriptor as_alt1
AS Alt 1(1 个 IN 端点)/ AS Alt 1 (1 IN EP)
Definition uac_mic.hpp:264
InterfaceDescriptor as_alt0
AS Alt 0(无端点)/ AS Alt 0 (no EP)
Definition uac_mic.hpp:263
IADDescriptor iad
接口关联描述符 / Interface association descriptor
Definition uac_mic.hpp:257
TypeIFormat1 fmt
格式描述符 / format descriptor
Definition uac_mic.hpp:266
ASGeneral as_gen
AS 通用 / AS general.
Definition uac_mic.hpp:265
CSEndpointGeneral ep_cs
类特定端点 / CS EP
Definition uac_mic.hpp:268
ACOutputTerminal ot_usb
输出端子 / Output terminal
Definition uac_mic.hpp:262
EndpointDescriptorIso9 ep_in
标准 IN 端点(9B)/ Std IN EP (9B)
Definition uac_mic.hpp:267