audio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-05-09 Urey first version
  9. */
  10. #include <stdio.h>
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include <drivers/audio.h>
  15. #include "audio_pipe.h"
  16. #define AUDIO_DEBUG 0
  17. #if AUDIO_DEBUG
  18. #define AUDIO_DBG(...) printf("[AUDIO]:"),printf(__VA_ARGS__)
  19. #else
  20. #define AUDIO_DBG(...)
  21. #endif
  22. static struct rt_audio_pipe audio_pipe;
  23. static rt_err_t _audio_send_replay_frame(struct rt_audio_device *audio)
  24. {
  25. rt_err_t result = RT_EOK;
  26. rt_base_t level;
  27. struct rt_audio_frame frame;
  28. RT_ASSERT(audio != RT_NULL);
  29. //check repaly queue is empty
  30. if (rt_data_queue_peak(&audio->replay->queue, &frame.data_ptr, &frame.data_size) != RT_EOK)
  31. {
  32. AUDIO_DBG("TX queue is empty\n");
  33. result = -RT_EEMPTY;
  34. level = rt_hw_interrupt_disable();
  35. audio->replay->activated = RT_FALSE;
  36. rt_hw_interrupt_enable(level);
  37. goto _exit;
  38. }
  39. if (audio->ops->transmit != RT_NULL)
  40. {
  41. AUDIO_DBG("audio transmit...\n");
  42. if (audio->ops->transmit(audio, frame.data_ptr, RT_NULL, frame.data_size) != frame.data_size)
  43. {
  44. result = -RT_EBUSY;
  45. goto _exit;
  46. }
  47. }
  48. //pop the head frame...
  49. rt_data_queue_pop(&audio->replay->queue, &frame.data_ptr, &frame.data_size, RT_WAITING_FOREVER);
  50. _exit: return result;
  51. }
  52. static rt_err_t _audio_flush_replay_frame(struct rt_audio_device *audio)
  53. {
  54. struct rt_audio_frame frame;
  55. if (audio->replay == RT_NULL)
  56. return -RT_EIO;
  57. while (rt_data_queue_peak(&audio->replay->queue, &frame.data_ptr, &frame.data_size) == RT_EOK)
  58. {
  59. //pop the head frame...
  60. rt_data_queue_pop(&audio->replay->queue, &frame.data_ptr, &frame.data_size, RT_WAITING_FOREVER);
  61. /* notify transmitted complete. */
  62. if (audio->parent.tx_complete != RT_NULL)
  63. audio->parent.tx_complete(&audio->parent, (void *) frame.data_ptr);
  64. }
  65. return RT_EOK;
  66. }
  67. static rt_err_t _audio_dev_init(struct rt_device *dev)
  68. {
  69. rt_err_t result = RT_EOK;
  70. struct rt_audio_device *audio;
  71. RT_ASSERT(dev != RT_NULL);
  72. audio = (struct rt_audio_device *) dev;
  73. /* initialize replay & record */
  74. audio->replay = RT_NULL;
  75. audio->record = RT_NULL;
  76. /* apply configuration */
  77. if (audio->ops->init)
  78. result = audio->ops->init(audio);
  79. return result;
  80. }
  81. static rt_err_t _audio_dev_open(struct rt_device *dev, rt_uint16_t oflag)
  82. {
  83. struct rt_audio_device *audio;
  84. RT_ASSERT(dev != RT_NULL);
  85. audio = (struct rt_audio_device *) dev;
  86. /* check device flag with the open flag */
  87. if ((oflag & RT_DEVICE_OFLAG_RDONLY) && !(dev->flag & RT_DEVICE_FLAG_RDONLY))
  88. return -RT_EIO;
  89. if ((oflag & RT_DEVICE_OFLAG_WRONLY) && !(dev->flag & RT_DEVICE_FLAG_WRONLY))
  90. return -RT_EIO;
  91. /* get open flags */
  92. dev->open_flag = oflag & 0xff;
  93. /* initialize the Rx/Tx structure according to open flag */
  94. if (oflag & RT_DEVICE_OFLAG_WRONLY)
  95. {
  96. AUDIO_DBG("open audio device ,oflag = %x\n",oflag);
  97. if (audio->replay == RT_NULL)
  98. {
  99. struct rt_audio_replay *replay = (struct rt_audio_replay *) rt_malloc(sizeof(struct rt_audio_replay));
  100. if (replay == RT_NULL)
  101. {
  102. AUDIO_DBG("request memory for replay error\n");
  103. return -RT_ENOMEM;
  104. }
  105. //init queue for audio replay
  106. rt_data_queue_init(&replay->queue, CFG_AUDIO_REPLAY_QUEUE_COUNT, CFG_AUDIO_REPLAY_QUEUE_COUNT / 2, RT_NULL);
  107. replay->activated = RT_FALSE;
  108. audio->replay = replay;
  109. }
  110. dev->open_flag |= RT_DEVICE_OFLAG_WRONLY;
  111. }
  112. if (oflag & RT_DEVICE_OFLAG_RDONLY)
  113. {
  114. if (audio->record == RT_NULL)
  115. {
  116. struct rt_audio_record *record = (struct rt_audio_record *) rt_malloc(sizeof(struct rt_audio_record));
  117. if (record == RT_NULL)
  118. {
  119. AUDIO_DBG("request memory for record error\n");
  120. return -RT_ENOMEM;
  121. }
  122. //init pipe for record
  123. {
  124. rt_uint8_t *buf = rt_malloc(CFG_AUDIO_RECORD_PIPE_SIZE);
  125. if (buf == RT_NULL)
  126. {
  127. rt_free(record);
  128. AUDIO_DBG("request pipe memory error\n");
  129. return -RT_ENOMEM;
  130. }
  131. rt_audio_pipe_init(&audio_pipe, "recpipe", (rt_int32_t)(RT_PIPE_FLAG_FORCE_WR | RT_PIPE_FLAG_BLOCK_RD), buf,
  132. CFG_AUDIO_RECORD_PIPE_SIZE);
  133. }
  134. record->activated = RT_FALSE;
  135. audio->record = record;
  136. }
  137. //open record pipe
  138. if (audio->record != RT_NULL)
  139. {
  140. rt_device_open(RT_DEVICE(&audio_pipe), RT_DEVICE_OFLAG_RDONLY);
  141. }
  142. dev->open_flag |= RT_DEVICE_OFLAG_RDONLY;
  143. }
  144. return RT_EOK;
  145. }
  146. static rt_err_t _audio_dev_close(struct rt_device *dev)
  147. {
  148. struct rt_audio_device *audio;
  149. RT_ASSERT(dev != RT_NULL);
  150. audio = (struct rt_audio_device *) dev;
  151. //shutdown the lower device
  152. if (audio->ops->shutdown != RT_NULL)
  153. audio->ops->shutdown(audio);
  154. if (dev->open_flag & RT_DEVICE_OFLAG_WRONLY)
  155. {
  156. struct rt_audio_frame frame;
  157. //stop replay stream
  158. audio->ops->stop(audio, AUDIO_STREAM_REPLAY);
  159. //flush all frame
  160. while (rt_data_queue_peak(&audio->replay->queue, &frame.data_ptr, &frame.data_size) == RT_EOK)
  161. {
  162. //pop the head frame...
  163. rt_data_queue_pop(&audio->replay->queue, &frame.data_ptr, &frame.data_size, RT_WAITING_FOREVER);
  164. /* notify transmitted complete. */
  165. if (audio->parent.tx_complete != RT_NULL)
  166. audio->parent.tx_complete(&audio->parent, (void *) frame.data_ptr);
  167. }
  168. dev->open_flag &= ~RT_DEVICE_OFLAG_WRONLY;
  169. }
  170. if (dev->open_flag & RT_DEVICE_OFLAG_RDONLY)
  171. {
  172. //stop record stream
  173. audio->ops->stop(audio, AUDIO_STREAM_RECORD);
  174. //close record pipe
  175. if (audio->record != RT_NULL)
  176. rt_device_close(RT_DEVICE(&audio_pipe));
  177. dev->open_flag &= ~RT_DEVICE_OFLAG_RDONLY;
  178. }
  179. return RT_EOK;
  180. }
  181. static rt_size_t _audio_dev_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
  182. {
  183. struct rt_audio_device *audio;
  184. RT_ASSERT(dev != RT_NULL);
  185. audio = (struct rt_audio_device *) dev;
  186. if (!(dev->open_flag & RT_DEVICE_OFLAG_RDONLY) || (audio->record == RT_NULL))
  187. return 0;
  188. return rt_device_read(RT_DEVICE(&audio_pipe), pos, buffer, size);
  189. }
  190. static rt_size_t _audio_dev_write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size)
  191. {
  192. rt_err_t result = RT_EOK;
  193. rt_base_t level;
  194. struct rt_audio_device *audio;
  195. RT_ASSERT(dev != RT_NULL);
  196. audio = (struct rt_audio_device *) dev;
  197. if (!(dev->open_flag & RT_DEVICE_OFLAG_WRONLY) || (audio->replay == RT_NULL))
  198. return 0;
  199. AUDIO_DBG("audio write : pos = %d,buffer = %x,size = %d\n",pos,(rt_uint32_t)buffer,size);
  200. //push a new frame to tx queue
  201. {
  202. result = rt_data_queue_push(&audio->replay->queue, buffer, size,
  203. RT_WAITING_FOREVER);
  204. if (result != RT_EOK)
  205. {
  206. AUDIO_DBG("TX frame queue push error\n");
  207. rt_set_errno(-RT_EFULL);
  208. return 0;
  209. }
  210. }
  211. //check tx state...
  212. level = rt_hw_interrupt_disable();
  213. if (audio->replay->activated != RT_TRUE)
  214. {
  215. audio->replay->activated = RT_TRUE;
  216. rt_hw_interrupt_enable(level);
  217. _audio_send_replay_frame(audio);
  218. }
  219. else
  220. {
  221. rt_hw_interrupt_enable(level);
  222. }
  223. return size;
  224. }
  225. static rt_err_t _audio_dev_control(struct rt_device *dev, int cmd, void *args)
  226. {
  227. rt_err_t result = RT_EOK;
  228. struct rt_audio_device *audio;
  229. RT_ASSERT(dev != RT_NULL);
  230. audio = (struct rt_audio_device *) dev;
  231. //dev stat...
  232. switch (cmd)
  233. {
  234. case AUDIO_CTL_GETCAPS:
  235. {
  236. struct rt_audio_caps *caps = (struct rt_audio_caps *) args;
  237. AUDIO_DBG("AUDIO_CTL_GETCAPS: main_type = %d,sub_type = %d\n",caps->main_type,caps->sub_type);
  238. if (audio->ops->getcaps != RT_NULL)
  239. {
  240. result = audio->ops->getcaps(audio, caps);
  241. }
  242. }
  243. break;
  244. case AUDIO_CTL_CONFIGURE:
  245. {
  246. struct rt_audio_caps *caps = (struct rt_audio_caps *) args;
  247. AUDIO_DBG("AUDIO_CTL_CONFIGURE: main_type = %d,sub_type = %d\n",caps->main_type,caps->sub_type);
  248. if (audio->ops->configure != RT_NULL)
  249. {
  250. result = audio->ops->configure(audio, caps);
  251. }
  252. }
  253. break;
  254. case AUDIO_CTL_SHUTDOWN:
  255. {
  256. AUDIO_DBG("AUDIO_CTL_SHUTDOWN\n");
  257. if (audio->ops->shutdown != RT_NULL)
  258. result = audio->ops->shutdown(audio);
  259. //flush replay frame...
  260. _audio_flush_replay_frame(audio);
  261. }
  262. break;
  263. case AUDIO_CTL_START:
  264. {
  265. int stream = *(int *) args;
  266. AUDIO_DBG("AUDIO_CTL_START: stream = %d\n",stream);
  267. if (audio->ops->start != RT_NULL)
  268. result = audio->ops->start(audio, stream);
  269. }
  270. break;
  271. case AUDIO_CTL_STOP:
  272. {
  273. int stream = *(int *) args;
  274. AUDIO_DBG("AUDIO_CTL_STOP: stream = %d\n",stream);
  275. if (audio->ops->start != RT_NULL)
  276. result = audio->ops->stop(audio, stream);
  277. if (stream == AUDIO_STREAM_REPLAY)
  278. {
  279. _audio_flush_replay_frame(audio);
  280. }
  281. }
  282. break;
  283. case AUDIO_CTL_PAUSE:
  284. {
  285. int stream = *(int *) args;
  286. AUDIO_DBG("AUDIO_CTL_PAUSE: stream = %d\n",stream);
  287. if (audio->ops->start != RT_NULL)
  288. result = audio->ops->suspend(audio, stream);
  289. }
  290. break;
  291. case AUDIO_CTL_RESUME:
  292. {
  293. int stream = *(int *) args;
  294. AUDIO_DBG("AUDIO_CTL_RESUME: stream = %d\n",stream);
  295. if (audio->ops->start != RT_NULL)
  296. result = audio->ops->resume(audio, stream);
  297. //resume tx frame...
  298. if (stream == AUDIO_STREAM_REPLAY)
  299. _audio_send_replay_frame(audio);
  300. }
  301. break;
  302. case AUDIO_CTL_ALLOCBUFFER:
  303. {
  304. struct rt_audio_buf_desc *desc = (struct rt_audio_buf_desc *) args;
  305. if (desc)
  306. {
  307. desc->data_size = AUDIO_DEVICE_DECODE_MP_BLOCK_SZ * 2;
  308. desc->data_ptr = rt_mp_alloc(&audio->mp, RT_WAITING_FOREVER);
  309. result = RT_EOK;
  310. }
  311. else result = -RT_EIO;
  312. }
  313. break;
  314. case AUDIO_CTL_FREEBUFFER:
  315. {
  316. rt_uint8_t *data_ptr = (rt_uint8_t *) args;
  317. if (data_ptr)
  318. rt_mp_free(data_ptr);
  319. }
  320. break;
  321. default:
  322. result = audio->ops->control(audio, cmd, args);
  323. break;
  324. }
  325. return result;
  326. }
  327. #ifdef RT_USING_DEVICE_OPS
  328. const static struct rt_device_ops audio_ops =
  329. {
  330. _audio_dev_init,
  331. _audio_dev_open,
  332. _audio_dev_close,
  333. _audio_dev_read,
  334. _audio_dev_write,
  335. _audio_dev_control
  336. };
  337. #endif
  338. rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data)
  339. {
  340. struct rt_device *device;
  341. RT_ASSERT(audio != RT_NULL);
  342. device = &(audio->parent);
  343. device->type = RT_Device_Class_Sound;
  344. device->rx_indicate = RT_NULL;
  345. device->tx_complete = RT_NULL;
  346. #ifdef RT_USING_DEVICE_OPS
  347. device->ops = &audio_ops;
  348. #else
  349. device->init = _audio_dev_init;
  350. device->open = _audio_dev_open;
  351. device->close = _audio_dev_close;
  352. device->read = _audio_dev_read;
  353. device->write = _audio_dev_write;
  354. device->control = _audio_dev_control;
  355. #endif
  356. device->user_data = data;
  357. //init memory pool for replay
  358. {
  359. rt_uint8_t *mempool = rt_malloc(AUDIO_DEVICE_DECODE_MP_SZ);
  360. rt_mp_init(&audio->mp, "adu_mp", mempool, AUDIO_DEVICE_DECODE_MP_SZ,
  361. AUDIO_DEVICE_DECODE_MP_BLOCK_SZ * 2);
  362. }
  363. /* register a character device */
  364. return rt_device_register(device, name, flag | RT_DEVICE_FLAG_REMOVABLE);
  365. }
  366. int rt_audio_samplerate_to_speed(rt_uint32_t bitValue)
  367. {
  368. int speed = 0;
  369. switch (bitValue)
  370. {
  371. case AUDIO_SAMP_RATE_8K:
  372. speed = 8000;
  373. break;
  374. case AUDIO_SAMP_RATE_11K:
  375. speed = 11052;
  376. break;
  377. case AUDIO_SAMP_RATE_16K:
  378. speed = 16000;
  379. break;
  380. case AUDIO_SAMP_RATE_22K:
  381. speed = 22050;
  382. break;
  383. case AUDIO_SAMP_RATE_32K:
  384. speed = 32000;
  385. break;
  386. case AUDIO_SAMP_RATE_44K:
  387. speed = 44100;
  388. break;
  389. case AUDIO_SAMP_RATE_48K:
  390. speed = 48000;
  391. break;
  392. case AUDIO_SAMP_RATE_96K:
  393. speed = 96000;
  394. break;
  395. case AUDIO_SAMP_RATE_128K:
  396. speed = 128000;
  397. break;
  398. case AUDIO_SAMP_RATE_160K:
  399. speed = 160000;
  400. break;
  401. case AUDIO_SAMP_RATE_172K:
  402. speed = 176400;
  403. break;
  404. case AUDIO_SAMP_RATE_192K:
  405. speed = 192000;
  406. break;
  407. default:
  408. break;
  409. }
  410. return speed;
  411. }
  412. rt_uint32_t rt_audio_format_to_bits(rt_uint32_t format)
  413. {
  414. switch (format)
  415. {
  416. case AUDIO_FMT_PCM_U8:
  417. case AUDIO_FMT_PCM_S8:
  418. return 8;
  419. case AUDIO_FMT_PCM_S16_LE:
  420. case AUDIO_FMT_PCM_S16_BE:
  421. case AUDIO_FMT_PCM_U16_LE:
  422. case AUDIO_FMT_PCM_U16_BE:
  423. return 16;
  424. default:
  425. return 32;
  426. };
  427. }
  428. void rt_audio_tx_complete(struct rt_audio_device *audio, rt_uint8_t *pbuf)
  429. {
  430. rt_err_t result;
  431. AUDIO_DBG("audio tx complete ptr=%x...\n",(rt_uint32_t)pbuf);
  432. //try to send all frame
  433. do
  434. {
  435. result = _audio_send_replay_frame(audio);
  436. } while (result == RT_EOK);
  437. /* notify transmitted complete. */
  438. if (audio->parent.tx_complete != RT_NULL)
  439. audio->parent.tx_complete(&audio->parent, (void *) pbuf);
  440. }
  441. void rt_audio_rx_done(struct rt_audio_device *audio, rt_uint8_t *pbuf, rt_size_t len)
  442. {
  443. //save data to record pipe
  444. rt_device_write(RT_DEVICE(RT_DEVICE(&audio_pipe)), 0, pbuf, len);
  445. /* invoke callback */
  446. if (audio->parent.rx_indicate != RT_NULL)
  447. audio->parent.rx_indicate(&audio->parent, len);
  448. }