audio.c 15 KB

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