audio.c 15 KB

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