我目前正在编写一个用于音频流的嵌入式应用程序。嵌入式应用程序将接收通过 wifi 发送的音频数据包,缓冲数据包,然后将音频数据发送到解码器芯片。我编写了一个环形缓冲区实现(在 stackoverflow 上的一篇优秀帖子的帮助下),但有时会出现一些奇怪的行为。在音频方面,我在播放过程中听到歌曲的某些部分重复。我发现这是由于尾指针被两次设置到缓冲区的开头。
(在我的实现中,头指针标记有效数据的结尾,而尾指针标记有效数据的开头)
例如,我看到:
- 头指针复位到缓冲区的开始
- 尾指针复位到缓冲区的开始
- 尾指针重置为缓冲区的开头<-这是我听到音频重复的地方
- 头指针复位到缓冲区的开始
这是环形缓冲区的实现:
typedef struct ring_buffer
{
UINT8 *buffer; /* data buffer */
UINT8 *buffer_end; /* end of data buffer */
size_t capacity; /* maximum number of mp3Bytes in the buffer */
size_t count; /* number of mp3Bytes in the buffer */
size_t typesize; /* size of each mp3Byte in the buffer */
UINT8 *head; /* ring buffer head pointer */
UINT8 *tail; /* ring buffer tail pointer */
} ring_buffer;
PUBLIC UINT8
AppAudioStream_RingBufInit(ring_buffer *rb, size_t capacity, size_t typesize)
{
/* alloc buffer of size capacity * typesize */
rb->buffer = malloc(capacity * typesize);
if(rb->buffer == NULL)
{
printf("ring buffer init fail\r\n");
return RING_BUF_INIT_FAIL;
}
/* init rb buffer to 0 */
memset(rb->buffer, 0, capacity * typesize);
/* rb struct element init */
rb->capacity = capacity;
rb->buffer_end = rb->buffer + capacity * typesize;
rb->count = 0;
rb->typesize = typesize;
rb->head = rb->buffer;
rb->tail = rb->buffer;
return RING_BUF_INIT_DONE;
}
PUBLIC VOID
AppAudioStream_RingBufWrite(ring_buffer *rb, UINT8 *mp3Byte)
{
/* default: allow overwriting if ring buffer is full */
memcpy(rb->head, mp3Byte, rb->typesize);
rb->head = rb->head + rb->typesize;
if(rb->head == rb->buffer_end) {
printf("head back to start\r\n");
rb->head = rb->buffer;
}
if(rb->count == rb->capacity) {
printf("buffer full\r\n");
if (rb->head > rb->tail)
rb->tail = rb->tail + rb->typesize;
} else {
rb->count++;
}
}
PUBLIC VOID
AppAudioStream_RingBufRead(ring_buffer *rb, UINT8 *mp3Byte)
{
/* insert 'comfort noise' if the ring buffer is empty */
if(rb->count == 0){
printf("buffer empty\r\n");
*mp3Byte = NOISE_BYTE;
} else {
/* copy data to mp3Byte and increase tail pointer */
memcpy(mp3Byte, rb->tail, rb->typesize);
rb->tail = rb->tail + rb->typesize;
if(rb->tail == rb->buffer_end) {
printf("TAIL back to start\r\n");
printf("Tbuffer count: %i\r\n", rb->count);
rb->tail = rb->buffer;
}
rb->count--;
}
}
以下是调用环形缓冲区写入函数的方式:
while (1)
{
AppAudioStream_BufRecv(sd, dataLen, &addr);
}
PUBLIC VOID
AppAudioStream_BufRecv(int sd, INT32 dataLen, struct sockaddr_in *addr)
{
INT32 addrlen = sizeof(struct sockaddr_in);
UINT8 j, i = 0;
UINT8 *audioByte;
/* listen to incoming audio data packets */
dataLen = recvfrom(sd, (char *) appRxBuf, sizeof(appRxBuf), 0,
(struct sockaddr *)&addr, &addrlen);
/* set pointer to first element in recieve buffer */
audioByte = appRxBuf;
/* buffer received packets into FIFO */
while (dataLen > 0)
{
/* write 1 byte into audio FIFO */
AppAudioStream_RingBufWrite(&audioFIFO, audioByte);
/* increase pointer index and update # of bytes left to write */
audioByte++;
dataLen--;
}
/* wait until buffer is 2/3 full to start decoding */
if (audioFIFO.count >= FIFO_TWO_THIRD_FULL
&& audioStreamStatus == GSN_STREAM_BUFFERING) {
audioStreamStatus = GSN_STREAM_START;
//printf("stream start\r\n");
}
}
环形缓冲区读取函数在每 2 毫秒发生一次的回调中调用(这基本上是一个 ISR):
PRIVATE VOID
AppAudioStream_DecoderCb(UINT32* pDummy, UINT32 TimerHandle)
{
UINT8 spiWriteCount = 0;
UINT8 mp3Byte;
int i = 0;
GSN_SPI_NUM_T spiPortNumber = GSN_SPI_NUM_0;
/* read 32 bytes of data from FIFO and write to SPI */
while (spiWriteCount < DATA_WRITE_AMT)
{
/* set stream status to decoding */
audioStreamStatus = GSN_STREAM_DECODING;
/* read 1 byte of audio data from FIFO */
AppAudioStream_RingBufRead(&audioFIFO, &mp3Byte);
/* write 1 byte of audio data out to VS1053 */
AppSpi_SdiByteWrite(spiPortNumber, &mp3Byte);
/* increase byte written count */
spiWriteCount++;
}
}
非常感谢任何帮助/见解。我确信我现在只是忽略了一些非常明显的东西。
谢谢!