audio_play.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. extern void local_sdk_speaker_set_pa_mode(int mode);
  7. extern void local_sdk_speaker_clean_buf_data();
  8. extern void local_sdk_speaker_set_volume(int volume);
  9. extern int local_sdk_speaker_feed_pcm_data(unsigned char *buf, int size);
  10. extern void local_sdk_speaker_finish_buf_data();
  11. extern void CommandResponse(int fd, const char *res);
  12. static pthread_mutex_t AudioPlayMutex = PTHREAD_MUTEX_INITIALIZER;
  13. static int AudioPlayFd = -1;
  14. static char waveFile[256];
  15. static int Volume = 0;
  16. int PlayPCM(char *file, int vol) {
  17. static const int waveHeaderLength = 44;
  18. static const int bufLength = 640;
  19. unsigned char buf[bufLength];
  20. const unsigned char cmp[] = {
  21. 0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x41, 0x56, 0x45, 0x66, 0x6d, 0x74, 0x20,
  22. 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x40, 0x1f, 0x00, 0x00, 0x80, 0x3e, 0x00, 0x00,
  23. 0x02, 0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61
  24. };
  25. fprintf(stderr, "[command] aplay: file:%s\n", file);
  26. FILE *fp = fopen(file, "rb");
  27. if(fp == NULL) {
  28. fprintf(stderr, "[command] aplay err: fopen %s failed!\n", file);
  29. return -1;
  30. } else {
  31. size_t size = fread(buf, 1, waveHeaderLength, fp);
  32. if(size != waveHeaderLength) {
  33. fprintf(stderr, "[command] aplay err: header size error\n");
  34. }
  35. buf[4] = buf[5] = buf[6] = buf[7] = 0;
  36. if(memcmp(buf, cmp, waveHeaderLength - 4)) {
  37. fprintf(stderr, "[command] aplay err: header error\n");
  38. }
  39. local_sdk_speaker_clean_buf_data();
  40. local_sdk_speaker_set_volume(vol);
  41. local_sdk_speaker_set_pa_mode(3);
  42. while(!feof(fp)) {
  43. size = fread(buf, 1, bufLength, fp);
  44. if (size <= 0) break;
  45. while(local_sdk_speaker_feed_pcm_data(buf, size)) usleep(100 * 1000);
  46. }
  47. fclose(fp);
  48. usleep(2 * 1000 * 1000);
  49. local_sdk_speaker_finish_buf_data();
  50. local_sdk_speaker_set_volume(0);
  51. local_sdk_speaker_set_pa_mode(0);
  52. }
  53. fprintf(stderr, "[command] aplay: finish\n");
  54. return 0;
  55. }
  56. static void *AudioPlayThread() {
  57. while(1) {
  58. pthread_mutex_lock(&AudioPlayMutex);
  59. if(AudioPlayFd >= 0) {
  60. int res = PlayPCM(waveFile, Volume);
  61. CommandResponse(AudioPlayFd, res ? "error" : "ok");
  62. }
  63. AudioPlayFd = -1;
  64. }
  65. }
  66. char *AudioPlay(int fd, char *tokenPtr) {
  67. if(AudioPlayFd >= 0) {
  68. fprintf(stderr, "[command] aplay err: Previous file is still playing. %d %d\n", AudioPlayFd, fd);
  69. return "error";
  70. }
  71. char *p = strtok_r(NULL, " \t\r\n", &tokenPtr);
  72. if(!p) {
  73. fprintf(stderr, "[command] aplay err: usage : aplay <wave file> [<volume>]\n");
  74. return "error";
  75. }
  76. strncpy(waveFile, p, 255);
  77. p = strtok_r(NULL, " \t\r\n", &tokenPtr);
  78. Volume = 40;
  79. if(p) Volume = atoi(p);
  80. AudioPlayFd = fd;
  81. pthread_mutex_unlock(&AudioPlayMutex);
  82. return NULL;
  83. }
  84. static void __attribute ((constructor)) AudioPlayInit(void) {
  85. pthread_mutex_lock(&AudioPlayMutex);
  86. pthread_t thread;
  87. if(pthread_create(&thread, NULL, AudioPlayThread, NULL)) {
  88. fprintf(stderr, "pthread_create error\n");
  89. pthread_mutex_unlock(&AudioPlayMutex);
  90. return;
  91. }
  92. }