9浏览
查看: 9|回复: 0

[ESP8266/ESP32] DFRobot 实现 ESP32 P4 USB音频设备的框架

[复制链接]
ESP32 官方的 USB 框架是 TinyUSB, 这次带来的是使用 TinyUSB 在 IDF 下实现的虚拟USB麦克风。
代码内置了一个正弦波的采样数据,录音机打开之后会送出:

  1. /*
  2. * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include "sdkconfig.h"
  9. #include "esp_err.h"
  10. #include "esp_log.h"
  11. #include "driver/uart.h"
  12. #include "usb_device_uac.h"
  13. #include "sine.h"
  14. static const char *TAG = "usb_uac_main";
  15. int audiobuffer=0;
  16. static esp_err_t uac_device_output_cb(uint8_t *buf, size_t len, void *arg)
  17. {
  18.     //size_t bytes_written = 0;
  19.     //bsp_extra_i2s_write(buf, len, &bytes_written, 0);
  20.         ESP_LOGE(TAG, "ocb");
  21.     return ESP_OK;
  22. }
  23. static esp_err_t uac_device_input_cb(uint8_t *buf, size_t len, size_t *bytes_read, void *arg)
  24. {
  25.        
  26.         for (int i=0;i<len;i++) {
  27.                 buf[i]=AudioData[(audiobuffer+i)%AUDIOLEN];
  28.         }
  29.         audiobuffer=(audiobuffer+len)%AUDIOLEN;
  30. ESP_LOGE(TAG, "icb");
  31. *bytes_read=len;
  32.     return ESP_OK;
  33. }
  34. static void uac_device_set_mute_cb(uint32_t mute, void *arg)
  35. {
  36.     ESP_LOGI(TAG, "uac_device_set_mute_cb: %"PRIu32"", mute);
  37.     //bsp_extra_codec_mute_set(mute);
  38. }
  39. static void uac_device_set_volume_cb(uint32_t volume, void *arg)
  40. {
  41.     ESP_LOGI(TAG, "uac_device_set_volume_cb: %"PRIu32"", volume);
  42.     //bsp_extra_codec_volume_set(volume, NULL);
  43. }
  44. void app_main(void)
  45. {
  46. uart_config_t uart_config = {
  47.         .baud_rate = 2000000,
  48.         .data_bits = UART_DATA_8_BITS,
  49.         .parity= UART_PARITY_DISABLE,
  50.         .stop_bits=UART_STOP_BITS_1
  51. };
  52. uart_param_config(UART_NUM_0,&uart_config);
  53.     uac_device_config_t config = {
  54.         .output_cb = uac_device_output_cb,
  55.         .input_cb = uac_device_input_cb,
  56.         .set_mute_cb = uac_device_set_mute_cb,
  57.         .set_volume_cb = uac_device_set_volume_cb,
  58.         .cb_ctx = NULL,
  59.     };
  60.     uac_device_init(&config);
  61. }
复制代码


完整的代码:
下载附件simpleuac.zip


测试的视频:


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail