Linux如何在RAM缓冲区中录制声音并以自定义延迟播放音频

前端之家收集整理的这篇文章主要介绍了Linux如何在RAM缓冲区中录制声音并以自定义延迟播放音频前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要使用嵌入式 Linux系统将音频从收音机发送到辅助系统.

辅助系统需要建立一个需要几秒钟的通信信道.

因此,如果我不想失去音频的开头,我需要一种方法来录制声音并以自定义延迟播放(最多几秒).

应该可以启动arecord将音频记录在tmpfs文件系统的文件中,并且当传入通信时,启动aplay.
但在这种情况下,开始仍然会丢失,因为要记录的信号来得太晚了.

Linux上是否有一个程序可以在RAM中的环形缓冲区中连续录制声音,并且能够根据需要自定义延迟播放?

如果没有,在嵌入式系统上编写这样一个程序的最佳库是什么? alsa还是其他什么?

解决方法

这是一个简单的C程序,它将在管道输入和输出之间保持循环缓冲区.在|中使用buffer_program |出.错误检查省略.坚固性不保证.给出了一般的想法.

测试脚本(但实际上,因为它的循环缓冲区需要管道中的数据,以便它只需要流中的任何块.或者只是使缓冲区大于数据):

  1. cat some.wav | ./circular_buffer 100000 | (sleep 1 && aplay)

circular_buffer.c:

  1. /**
  2. * This program simply maintains a circular buffer of a given size indefinitely.
  3. */
  4. #include <stdio.h>
  5. #include <stddef.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <stdbool.h> /* C99 only */
  9. #include <sys/select.h>
  10. #include <errno.h>
  11. #include <fcntl.h>
  12.  
  13. int c_read(int fd,char * buf,unsigned int size,unsigned int * head_in,unsigned int * tail_in);
  14. int c_write(int fd,unsigned int * tail_in);
  15. bool empty_buf(unsigned int head,unsigned int tail);
  16. bool setblock(int fd,bool block);
  17. #define FD_SET_SET(set,fd,max) FD_SET(fd,&set); max = ((fd > max) ? fd : max);
  18. #define FD_SET_UNSET(set,max) FD_CLR(fd,&set); max = ((fd == max) ? max - 1 : max); //not ideal. Do while ISFDSET...
  19.  
  20. int main(int argc,char **argv)
  21. {
  22. char * buf;
  23. unsigned int buf_size = 0;
  24. unsigned int buf_head = 0;
  25. unsigned int buf_tail = 0;
  26.  
  27. // Check args.
  28. if(argc != 2) {
  29. fprintf(stderr,"Usage: %s <buffer size in bytes>\n",__FILE__);
  30. exit(EXIT_FAILURE);
  31. }
  32. sscanf(argv[1],"%d",&buf_size);
  33. buf_size = ( buf_size < 2 ) ? 2 : buf_size;
  34.  
  35. // Note the usable buffer space is buf_size-1.
  36. fprintf(stderr,"Allocating %d\n",buf_size);
  37. buf = (char*)malloc(buf_size);
  38.  
  39. bool done_reading = false;
  40. int maxfd = 0;
  41. fd_set r_set,w_set,r_tempset,w_tempset;
  42. setblock(STDIN_FILENO,false);
  43. setblock(STDOUT_FILENO,false);
  44. FD_ZERO(&r_set);
  45. FD_ZERO(&w_set);
  46. FD_ZERO(&r_tempset);
  47. FD_ZERO(&w_tempset);
  48. FD_SET_SET(r_tempset,STDIN_FILENO,maxfd);
  49. FD_SET_SET(w_tempset,STDOUT_FILENO,maxfd);
  50. r_set = r_tempset;
  51. while(true) {
  52. select((maxfd + 1),&r_set,&w_set,NULL,NULL);
  53. if(FD_ISSET(STDIN_FILENO,&r_set)) {
  54. int c = c_read(STDIN_FILENO,buf,buf_size,&buf_head,&buf_tail);
  55. if(c == -1) { // EOF,disable select on the input.
  56. fprintf(stderr,"No more bytes to read\n");
  57. done_reading = true;
  58. FD_ZERO(&r_set);
  59. }
  60. }
  61. if(!done_reading) {
  62. r_set = r_tempset;
  63. }
  64. if(FD_ISSET(STDOUT_FILENO,&w_set)) {
  65. c_write(STDOUT_FILENO,&buf_tail);
  66. }
  67. if(!empty_buf(buf_head,buf_tail)) { // Enable select on write whenever there is bytes.
  68. w_set = w_tempset;
  69. }
  70. else {
  71. FD_ZERO(&w_set);
  72. if(done_reading) { // Finish.
  73. fprintf(stderr,"No more bytes to write\n");
  74. break;
  75. }
  76. }
  77. }
  78. fflush(stderr);
  79. return 0;
  80. }
  81.  
  82. bool empty_buf(unsigned int head,unsigned int tail) {
  83. return head == tail;
  84. }
  85.  
  86. /**
  87. * Keep reading until we can read no more. Keep on pushing the tail forward as we overflow.
  88. * Expects fd to be non blocking.
  89. * @returns number of byte read,0 on non stopping error,or -1 on error or EOF.
  90. */
  91. int c_read(int fd,unsigned int * tail_in) {
  92. fprintf(stderr,"In c_read()\n");
  93. unsigned int head = *head_in;
  94. unsigned int tail = *tail_in;
  95. bool more_bytes = true;
  96. int n = 0;
  97. int c = 0;
  98.  
  99. while(more_bytes) {
  100. bool in_front = tail > head;
  101. fprintf(stderr,"Read %d %d %d\n",size,head,tail);
  102.  
  103. n = read(fd,buf+head,size - head);
  104. if(n == -1) {
  105. more_bytes = false;
  106. if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) { // Not EOF but the read would block.
  107. c = 0;
  108. }
  109. else {
  110. c = -1;
  111. }
  112. }
  113. else if(n == 0) { // EOF. No more bytes possible.
  114. more_bytes = false;
  115. c = -1;
  116. }
  117. else if(n != (size - head)) { // if not full read adjust pointers and break.
  118. more_bytes = false;
  119. c += n;
  120. head = (head+n)%size;
  121. if(in_front && (head >= tail || head == 0)) {
  122. tail = (head+1)%size;
  123. }
  124. }
  125. else {
  126. c = 0;
  127. head = 0;
  128. tail = (tail == 0) ? 1 : tail;
  129. }
  130. }
  131. *head_in = head;
  132. *tail_in = tail;
  133. return c;
  134. }
  135.  
  136. /**
  137. * Try flush the buffer to fd. fd should be non blocking.
  138. */
  139. int c_write(int fd,"In c_write()\n");
  140. unsigned int head = *head_in;
  141. unsigned int tail = *tail_in;
  142. int n = 0;
  143. fprintf(stderr,"Write %d %d %d\n",tail);
  144.  
  145. if(tail < head) {
  146. n = write(fd,buf+tail,head-tail);
  147. tail += n;
  148. }
  149. else if(head < tail) {
  150. n = write(fd,size-tail);
  151. if(n == size-tail) {
  152. n = write(fd,head);
  153. tail = n;
  154. }
  155. }
  156. *head_in = head;
  157. *tail_in = tail;
  158. return n;
  159. }
  160.  
  161. bool setblock(int fd,bool block)
  162. {
  163. int flags;
  164. flags = fcntl(fd,F_GETFL);
  165. if (block)
  166. flags &= ~O_NONBLOCK;
  167. else
  168. flags |= O_NONBLOCK;
  169. fcntl(fd,F_SETFL,flags);
  170. return true;
  171. }

猜你在找的Linux相关文章