谷动谷力

 找回密码
 立即注册
查看: 1830|回复: 0
打印 上一主题 下一主题
收起左侧

RT-Thread Audio - 1. 音频框架的分析

[复制链接]
跳转到指定楼层
楼主
发表于 2021-12-9 13:53:44 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 sunsili 于 2021-12-9 13:59 编辑

RT-Thread Audio - 1. 音频框架的分析

Github 上 RT-Thread 源码最新更新了关于 Audio 驱动相关的部分,让上层和底层都开发和编程简单了,不过对于大部分的开发者来说还不太熟悉这套框架的,那么笔者就这次编写音频驱动给大家分析下代码框架。

这里会先贴一张框架图,整个帖子都是笔者在编写开发过程中写的,可能有格式和错误,最终全部更新完毕后会整理整个系列的帖子。

1. RT-Thread 音频架构图



2. RT-Thread 音频框架的调用逻辑关系图(建议右键大图)



3. RT-Thread 音频应用代码模板(以wavplay为例)

https://github.com/RT-Thread-packages/wavplayer

4. RT-Thread 音频驱动代码模板

  1. #include "drv_sound.h"
  2. #include "drv_tina.h"
  3. #include "drivers/audio.h"

  4. #define DBG_TAG "drv_sound"
  5. #define DBG_LVL DBG_LOG
  6. #define DBG_COLOR
  7. #include

  8. #define TX_DMA_FIFO_SIZE (2048)

  9. struct temp_sound
  10. {
  11. struct rt_audio_device device;
  12. struct rt_audio_configure replay_config;
  13. int volume;
  14. rt_uint8_t *tx_fifo;
  15. };

  16. static rt_err_t getcaps(struct rt_audio_device *audio, struct rt_audio_caps *caps)
  17. {
  18. struct temp_sound *sound = RT_NULL;

  19. RT_ASSERT(audio != RT_NULL);
  20. sound = (struct temp_sound *)audio->parent.user_data; (void)sound;

  21. return RT_EOK;
  22. }

  23. static rt_err_t configure(struct rt_audio_device *audio, struct rt_audio_caps *caps)
  24. {
  25. struct temp_sound *sound = RT_NULL;

  26. RT_ASSERT(audio != RT_NULL);
  27. sound = (struct temp_sound *)audio->parent.user_data; (void)sound;

  28. return RT_EOK;
  29. }

  30. static rt_err_t init(struct rt_audio_device *audio)
  31. {
  32. struct temp_sound *sound = RT_NULL;

  33. RT_ASSERT(audio != RT_NULL);
  34. sound = (struct temp_sound *)audio->parent.user_data; (void)sound;

  35. return RT_EOK;
  36. }

  37. static rt_err_t start(struct rt_audio_device *audio, int stream)
  38. {
  39. struct temp_sound *sound = RT_NULL;

  40. RT_ASSERT(audio != RT_NULL);
  41. sound = (struct temp_sound *)audio->parent.user_data; (void)sound;

  42. return RT_EOK;
  43. }

  44. static rt_err_t stop(struct rt_audio_device *audio, int stream)
  45. {
  46. struct temp_sound *sound = RT_NULL;

  47. RT_ASSERT(audio != RT_NULL);
  48. sound = (struct temp_sound *)audio->parent.user_data; (void)sound;

  49. return RT_EOK;
  50. }

  51. rt_size_t transmit(struct rt_audio_device *audio, const void *writeBuf, void *readBuf, rt_size_t size)
  52. {
  53. struct temp_sound *sound = RT_NULL;

  54. RT_ASSERT(audio != RT_NULL);
  55. sound = (struct temp_sound *)audio->parent.user_data; (void)sound;

  56. return size;
  57. }

  58. static void buffer_info(struct rt_audio_device *audio, struct rt_audio_buf_info *info)
  59. {
  60. struct temp_sound *sound = RT_NULL;

  61. RT_ASSERT(audio != RT_NULL);
  62. sound = (struct temp_sound *)audio->parent.user_data;

  63. /**
  64. * TX_FIFO
  65. * +----------------+----------------+
  66. * | block1 | block2 |
  67. * +----------------+----------------+
  68. * \ block_size /
  69. */
  70. info->buffer = sound->tx_fifo;
  71. info->total_size = TX_DMA_FIFO_SIZE;
  72. info->block_size = TX_DMA_FIFO_SIZE / 2;
  73. info->block_count = 2;
  74. }

  75. static struct rt_audio_ops ops =
  76. {
  77. .getcaps = getcaps,
  78. .configure = configure,
  79. .init = init,
  80. .start = start,
  81. .stop = stop,
  82. .transmit = transmit,
  83. .buffer_info = buffer_info,
  84. };

  85. static int rt_hw_sound_init(void)
  86. {
  87. rt_uint8_t *tx_fifo = RT_NULL;
  88. static struct temp_sound sound = {0};

  89. /* 分配 DMA 搬运 buffer */
  90. tx_fifo = rt_calloc(1, TX_DMA_FIFO_SIZE);
  91. if(tx_fifo == RT_NULL)
  92. {
  93. return -RT_ENOMEM;
  94. }

  95. sound.tx_fifo = tx_fifo;

  96. /* 注册声卡放音驱动 */
  97. sound.device.ops = &ops;
  98. rt_audio_register(&sound.device, "sound0", RT_DEVICE_FLAG_WRONLY, &sound);

  99. return RT_EOK;
  100. }
  101. INIT_DEVICE_EXPORT(rt_hw_sound_init);
复制代码



5. 代码分析

待后续补充。。。

下载附件[sound_temp.7z]


+10
回复

使用道具 举报

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

本版积分规则

QQ|Archiver|手机版|深圳市光明谷科技有限公司|光明谷商城|Sunshine Silicon Corpporation ( 粤ICP备14060730号|Sitemap

GMT+8, 2024-5-17 13:39 , Processed in 0.083641 second(s), 40 queries .

Powered by Discuz! X3.2 Licensed

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表