audio_device.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * File : audio_device.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 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. * 2018-05-26 RT-Thread the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rthw.h>
  26. #include <rtdevice.h>
  27. #include <string.h>
  28. #include "drv_pl041.h"
  29. #include "drv_ac97.h"
  30. #include "audio_device.h"
  31. struct audio_device
  32. {
  33. struct rt_device *snd;
  34. struct rt_mempool mp;
  35. int state;
  36. void (*evt_handler)(void *parameter, int state);
  37. void *parameter;
  38. };
  39. static struct audio_device *_audio_device = NULL;
  40. void *audio_device_get_buffer(int *bufsz)
  41. {
  42. if (bufsz)
  43. {
  44. *bufsz = AUDIO_DEVICE_DECODE_MP_BLOCK_SZ * 2;
  45. }
  46. return rt_mp_alloc(&(_audio_device->mp), RT_WAITING_FOREVER);
  47. }
  48. void audio_device_put_buffer(void *ptr)
  49. {
  50. if (ptr) rt_mp_free(ptr);
  51. return ;
  52. }
  53. static rt_err_t audio_device_write_done(struct rt_device *device, void *ptr)
  54. {
  55. if (!ptr)
  56. {
  57. rt_kprintf("device buf_release NULL\n");
  58. return -RT_ERROR;
  59. }
  60. rt_mp_free(ptr);
  61. return RT_EOK;
  62. }
  63. void audio_device_write(void *buffer, int size)
  64. {
  65. if (_audio_device->snd && size != 0)
  66. {
  67. if (_audio_device->state == AUDIO_DEVICE_IDLE)
  68. {
  69. if (_audio_device->evt_handler)
  70. _audio_device->evt_handler(_audio_device->parameter, AUDIO_DEVICE_PLAYBACK);
  71. /* change audio device state */
  72. _audio_device->state = AUDIO_DEVICE_PLAYBACK;
  73. }
  74. rt_device_write(_audio_device->snd, 0, buffer, size);
  75. }
  76. else
  77. {
  78. /* release buffer directly */
  79. rt_mp_free(buffer);
  80. }
  81. return ;
  82. }
  83. int audio_device_init(void)
  84. {
  85. uint8_t *mempool_ptr;
  86. if (!_audio_device)
  87. {
  88. _audio_device = (struct audio_device *) rt_malloc(sizeof(struct audio_device) + AUDIO_DEVICE_DECODE_MP_SZ);
  89. if (_audio_device == NULL)
  90. {
  91. rt_kprintf("malloc memeory for _audio_device failed! \n");
  92. return -RT_ERROR;
  93. }
  94. _audio_device->evt_handler = NULL;
  95. _audio_device->parameter = NULL;
  96. mempool_ptr = (uint8_t *)(_audio_device + 1);
  97. rt_mp_init(&(_audio_device->mp), "adbuf", mempool_ptr, AUDIO_DEVICE_DECODE_MP_SZ, AUDIO_DEVICE_DECODE_MP_BLOCK_SZ * 2);
  98. /* find snd device */
  99. _audio_device->snd = rt_device_find("sound");
  100. if (_audio_device->snd == NULL)
  101. {
  102. rt_kprintf("sound device not found \n");
  103. return -1;
  104. }
  105. /* set tx complete call back function */
  106. rt_device_set_tx_complete(_audio_device->snd, audio_device_write_done);
  107. }
  108. return RT_EOK;
  109. }
  110. int audio_device_set_evt_handler(void (*handler)(void *parameter, int state), void *parameter)
  111. {
  112. if (_audio_device)
  113. {
  114. _audio_device->evt_handler = handler;
  115. _audio_device->parameter = parameter;
  116. }
  117. return 0;
  118. }
  119. void audio_device_set_rate(int sample_rate)
  120. {
  121. if (_audio_device->snd)
  122. {
  123. int rate = sample_rate;
  124. rt_device_control(_audio_device->snd, CODEC_CMD_SAMPLERATE, &rate);
  125. }
  126. }
  127. void audio_device_set_volume(int value)
  128. {
  129. if (_audio_device->snd)
  130. {
  131. rt_device_control(_audio_device->snd, CODEC_CMD_SET_VOLUME, &value);
  132. }
  133. }
  134. int audio_device_get_volume(void)
  135. {
  136. int value = 0;
  137. if (_audio_device->snd)
  138. {
  139. rt_device_control(_audio_device->snd, CODEC_CMD_GET_VOLUME, &value);
  140. }
  141. return value;
  142. }
  143. void audio_device_open(void)
  144. {
  145. _audio_device->state = AUDIO_DEVICE_IDLE;
  146. rt_device_open(_audio_device->snd, RT_DEVICE_OFLAG_WRONLY);
  147. }
  148. void audio_device_close(void)
  149. {
  150. rt_device_close(_audio_device->snd);
  151. if (_audio_device->state == AUDIO_DEVICE_PLAYBACK)
  152. {
  153. if (_audio_device->evt_handler)
  154. _audio_device->evt_handler(_audio_device->parameter, AUDIO_DEVICE_CLOSE);
  155. }
  156. /* set to idle */
  157. _audio_device->state = AUDIO_DEVICE_CLOSE;
  158. }