wav.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include <finsh.h>
  2. #include <dfs_posix.h>
  3. #include "stm32f10x_lib.h"
  4. rt_uint32_t wav_length;
  5. rt_uint8_t* wav_buffer;
  6. #define WAV_MP_NUM 2
  7. #define WAV_MP_BUFSZ (1024 * 1)
  8. #define STATIC_MEMPOOL
  9. #ifdef STATIC_MEMPOOL
  10. static rt_uint8_t mempool[(WAV_MP_BUFSZ + 4)* WAV_MP_NUM]; // 5k x 2
  11. static struct rt_mempool _mp;
  12. #endif
  13. static rt_mp_t mp;
  14. static rt_err_t wav_tx_done(rt_device_t dev, void *buffer)
  15. {
  16. /* release memory block to memory pool */
  17. rt_mp_free(buffer);
  18. return RT_EOK;
  19. }
  20. void wav(char* filename)
  21. {
  22. int fd;
  23. #ifdef STATIC_MEMPOOL
  24. rt_mp_init(&_mp, "wav", &mempool[0], sizeof(mempool), WAV_MP_BUFSZ);
  25. mp = &_mp;
  26. #else
  27. mp = rt_mp_create("wav", WAV_MP_NUM, WAV_MP_BUFSZ);
  28. #endif
  29. fd = open(filename, O_RDONLY, 0);
  30. if (fd >= 0)
  31. {
  32. rt_uint8_t* buf;
  33. rt_size_t len;
  34. rt_device_t device;
  35. /* open audio device and set tx done call back */
  36. device = rt_device_find("snd");
  37. rt_device_set_tx_complete(device, wav_tx_done);
  38. rt_device_open(device, RT_DEVICE_OFLAG_WRONLY);
  39. // buf = rt_mp_alloc(mp, RT_WAITING_FOREVER);
  40. // len = read(fd, (char*)buf, WAV_MP_BUFSZ);
  41. do {
  42. buf = rt_mp_alloc(mp, RT_WAITING_FOREVER);
  43. rt_kprintf("read file");
  44. len = read(fd, (char*)buf, WAV_MP_BUFSZ);
  45. rt_kprintf("...done!\n");
  46. if (len > 0) rt_device_write(device, 0, buf, len);
  47. } while (len != 0);
  48. /* close device and file */
  49. rt_device_close(device);
  50. close(fd);
  51. /* delete memory pool */
  52. #ifdef STATIC_MEMPOOL
  53. rt_mp_detach(mp);
  54. #else
  55. rt_mp_delete(mp);
  56. #endif
  57. }
  58. }
  59. FINSH_FUNCTION_EXPORT(wav, wav test)