audio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. return size;
  237. }
  238. static rt_err_t _audio_dev_control(struct rt_device *dev, int cmd, void *args)
  239. {
  240. rt_err_t result = RT_EOK;
  241. struct rt_audio_device *audio;
  242. RT_ASSERT(dev != RT_NULL);
  243. audio = (struct rt_audio_device *) dev;
  244. //dev stat...
  245. switch (cmd)
  246. {
  247. case AUDIO_CTL_GETCAPS:
  248. {
  249. struct rt_audio_caps *caps = (struct rt_audio_caps *) args;
  250. AUDIO_DBG("AUDIO_CTL_GETCAPS: main_type = %d,sub_type = %d\n",caps->main_type,caps->sub_type);
  251. if (audio->ops->getcaps != RT_NULL)
  252. {
  253. result = audio->ops->getcaps(audio, caps);
  254. }
  255. }
  256. break;
  257. case AUDIO_CTL_CONFIGURE:
  258. {
  259. struct rt_audio_caps *caps = (struct rt_audio_caps *) args;
  260. AUDIO_DBG("AUDIO_CTL_CONFIGURE: main_type = %d,sub_type = %d\n",caps->main_type,caps->sub_type);
  261. if (audio->ops->configure != RT_NULL)
  262. {
  263. result = audio->ops->configure(audio, caps);
  264. }
  265. }
  266. break;
  267. case AUDIO_CTL_SHUTDOWN:
  268. {
  269. AUDIO_DBG("AUDIO_CTL_SHUTDOWN\n");
  270. if (audio->ops->shutdown != RT_NULL)
  271. result = audio->ops->shutdown(audio);
  272. //flush replay frame...
  273. _audio_flush_replay_frame(audio);
  274. }
  275. break;
  276. case AUDIO_CTL_START:
  277. {
  278. int stream = *(int *) args;
  279. AUDIO_DBG("AUDIO_CTL_START: stream = %d\n",stream);
  280. if (audio->ops->start != RT_NULL)
  281. result = audio->ops->start(audio, stream);
  282. }
  283. break;
  284. case AUDIO_CTL_STOP:
  285. {
  286. int stream = *(int *) args;
  287. AUDIO_DBG("AUDIO_CTL_STOP: stream = %d\n",stream);
  288. if (audio->ops->start != RT_NULL)
  289. result = audio->ops->stop(audio, stream);
  290. if (stream == AUDIO_STREAM_REPLAY)
  291. {
  292. _audio_flush_replay_frame(audio);
  293. }
  294. }
  295. break;
  296. case AUDIO_CTL_PAUSE:
  297. {
  298. int stream = *(int *) args;
  299. AUDIO_DBG("AUDIO_CTL_PAUSE: stream = %d\n",stream);
  300. if (audio->ops->start != RT_NULL)
  301. result = audio->ops->suspend(audio, stream);
  302. }
  303. break;
  304. case AUDIO_CTL_RESUME:
  305. {
  306. int stream = *(int *) args;
  307. AUDIO_DBG("AUDIO_CTL_RESUME: stream = %d\n",stream);
  308. if (audio->ops->start != RT_NULL)
  309. result = audio->ops->resume(audio, stream);
  310. //resume tx frame...
  311. if (stream == AUDIO_STREAM_REPLAY)
  312. _audio_send_replay_frame(audio);
  313. }
  314. break;
  315. case AUDIO_CTL_ALLOCBUFFER:
  316. {
  317. struct rt_audio_buf_desc *desc = (struct rt_audio_buf_desc *) args;
  318. if (desc)
  319. {
  320. desc->data_size = AUDIO_DEVICE_DECODE_MP_BLOCK_SZ * 2;
  321. desc->data_ptr = rt_mp_alloc(&audio->mp, RT_WAITING_FOREVER);
  322. result = RT_EOK;
  323. }
  324. else result = -RT_EIO;
  325. }
  326. break;
  327. case AUDIO_CTL_FREEBUFFER:
  328. {
  329. rt_uint8_t *data_ptr = (rt_uint8_t *) args;
  330. if (data_ptr)
  331. rt_mp_free(data_ptr);
  332. }
  333. break;
  334. default:
  335. result = audio->ops->control(audio, cmd, args);
  336. break;
  337. }
  338. return result;
  339. }
  340. rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data)
  341. {
  342. struct rt_device *device;
  343. RT_ASSERT(audio != RT_NULL);
  344. device = &(audio->parent);
  345. device->type = RT_Device_Class_Sound;
  346. device->rx_indicate = RT_NULL;
  347. device->tx_complete = RT_NULL;
  348. device->init = _audio_dev_init;
  349. device->open = _audio_dev_open;
  350. device->close = _audio_dev_close;
  351. device->read = _audio_dev_read;
  352. device->write = _audio_dev_write;
  353. device->control = _audio_dev_control;
  354. device->user_data = data;
  355. //init memory pool for replay
  356. {
  357. rt_uint8_t *mempool = rt_malloc(AUDIO_DEVICE_DECODE_MP_SZ);
  358. rt_mp_init(&audio->mp, "adu_mp", mempool, AUDIO_DEVICE_DECODE_MP_SZ,
  359. AUDIO_DEVICE_DECODE_MP_BLOCK_SZ * 2);
  360. }
  361. /* register a character device */
  362. return rt_device_register(device, name, flag | RT_DEVICE_FLAG_REMOVABLE);
  363. }
  364. int rt_audio_samplerate_to_speed(rt_uint32_t bitValue)
  365. {
  366. int speed = 0;
  367. switch (bitValue)
  368. {
  369. case AUDIO_SAMP_RATE_8K:
  370. speed = 8000;
  371. break;
  372. case AUDIO_SAMP_RATE_11K:
  373. speed = 11052;
  374. break;
  375. case AUDIO_SAMP_RATE_16K:
  376. speed = 16000;
  377. break;
  378. case AUDIO_SAMP_RATE_22K:
  379. speed = 22050;
  380. break;
  381. case AUDIO_SAMP_RATE_32K:
  382. speed = 32000;
  383. break;
  384. case AUDIO_SAMP_RATE_44K:
  385. speed = 44100;
  386. break;
  387. case AUDIO_SAMP_RATE_48K:
  388. speed = 48000;
  389. break;
  390. case AUDIO_SAMP_RATE_96K:
  391. speed = 96000;
  392. break;
  393. case AUDIO_SAMP_RATE_128K:
  394. speed = 128000;
  395. break;
  396. case AUDIO_SAMP_RATE_160K:
  397. speed = 160000;
  398. break;
  399. case AUDIO_SAMP_RATE_172K:
  400. speed = 176400;
  401. break;
  402. case AUDIO_SAMP_RATE_192K:
  403. speed = 192000;
  404. break;
  405. default:
  406. break;
  407. }
  408. return speed;
  409. }
  410. rt_uint32_t rt_audio_format_to_bits(rt_uint32_t format)
  411. {
  412. switch (format)
  413. {
  414. case AUDIO_FMT_PCM_U8:
  415. case AUDIO_FMT_PCM_S8:
  416. return 8;
  417. case AUDIO_FMT_PCM_S16_LE:
  418. case AUDIO_FMT_PCM_S16_BE:
  419. case AUDIO_FMT_PCM_U16_LE:
  420. case AUDIO_FMT_PCM_U16_BE:
  421. return 16;
  422. default:
  423. return 32;
  424. };
  425. }
  426. void rt_audio_tx_complete(struct rt_audio_device *audio, rt_uint8_t *pbuf)
  427. {
  428. rt_err_t result;
  429. AUDIO_DBG("audio tx complete ptr=%x...\n",(rt_uint32_t)pbuf);
  430. //try to send all frame
  431. do
  432. {
  433. result = _audio_send_replay_frame(audio);
  434. } while (result == RT_EOK);
  435. /* notify transmitted complete. */
  436. if (audio->parent.tx_complete != RT_NULL)
  437. audio->parent.tx_complete(&audio->parent, (void *) pbuf);
  438. }
  439. void rt_audio_rx_done(struct rt_audio_device *audio, rt_uint8_t *pbuf, rt_size_t len)
  440. {
  441. rt_err_t result = RT_EOK;
  442. //save data to record pipe
  443. rt_device_write(RT_DEVICE(RT_DEVICE(&audio_pipe)), 0, pbuf, len);
  444. /* invoke callback */
  445. if (audio->parent.rx_indicate != RT_NULL)
  446. audio->parent.rx_indicate(&audio->parent, len);
  447. }