audio.c 15 KB

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