0

我正在为 iPhone 编写一个基于 VOIP 的应用程序。我遇到了一个奇怪的问题,当用户按下屏幕时音频出现故障,当您按下手机本身的音量增大/减小按钮时也会发生这种情况。经过几天的调试,我发现它与我的循环缓冲区有关。我在这里换了一个:

http://atastypixel.com/blog/a-simple-fast-circular-buffer-implementation-for-audio-processing/

这不会导致故障,但延迟比我的要长近 4 倍,我必须有最小的延迟并且无法弄清楚我的应用程序发生了什么。

设置:

我跟着: http: //www.stefanpopp.de/2011/capture-iphone-microphone/有点制作基本应用程序,但我有不同的设置/功能等。我有一个视图控制器,它有这个 audioProcessor 类的属性,这个类有一个用于循环缓冲区的变量。在录制回调中我发送数据,这一切都很好。在 CFSocket 回调中,我将来自网络的数据添加到此缓冲区,然后播放回调从此缓冲区中提取数据并将其传递给系统。

在播放过程中的某个时刻,如果用户按下会触发 UI 事件,这一切都会变得很糟糕,并且会出现这个奇怪的数据。我猜这是某种线程问题,但我在这方面几乎没有经验。我会很感激任何帮助。下面是相关代码:

网络回调 - 将数据添加到缓冲区:

static void addDataToBuffer(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
    AudioUnitCBufferProduce(&audioProcessor->auCBuffer, (uint8_t*)[(__bridge NSData *)data bytes], [(__bridge NSData *)data length]);
}

音频单元播放 - 从缓冲区复制数据并放入指向 ioData 的“targetBuffer”:

static OSStatus playbackCallback(void *inRefCon,
                             AudioUnitRenderActionFlags *ioActionFlags,
                             const AudioTimeStamp *inTimeStamp,
                             UInt32 inBusNumber,
                             UInt32 inNumberFrames,
                             AudioBufferList *ioData)
{

    uint8_t *targetBuffer = (uint8_t*)ioData->mBuffers[0].mData;
    AudioUnitCBufferConsume(&audioProcessor->auCBuffer, targetBuffer, inNumberFrames);

    return noErr;
}

缓冲区初始化:

void AudioUnitCBufferInit(AudioUnitCBuffer *b)
{
  // create array of bytes of length specified, fill with silence
  uint8_t buffer[2048];

  for(int i = 0; i < 2048; i++)
  {
      buffer[i] = 0xd5;
  }

  // init buffer elements
  b->buffer = buffer;
  b->consumer = buffer;
  b->producer = buffer;
  b->length = 2048;
}

缓冲区生产者/消费者:

这是为了让你传入一个指向函数的指针,然后用数据填充这个指针,如果没有数据,指针将用 ALAW 十六进制值填充以保持静音。这使音频单元代码保持较小,因为缓冲区确保它始终向其提供数据。这也比复制到某个临时位置然后 memcpy 将其放入上面链接使用的缓冲区中要快得多,而且对于我的需要来说远远慢。

inline static void AudioUnitCBufferProduce(AudioUnitCBuffer *b, uint8_t *bytes, int32_t len)
{   
//printf("\n\ninside producer: len %i \n\n", len);
while(len--)
{
    // if producer catches up with consumer, skip a byte
    if (b->producer+1 == b->consumer)
    {
        //printf("b->producer+1 == b->consumer == continue \n");
        continue;
    }
    else
    {
        //printf("b->producer+1 != b->consumer == add byte \n");
        *b->producer = *bytes++;
        b->producer++;

        if(b->producer == &b->buffer[b->length-1])
        {
            //printf("\n\nproducer == end, skipping \n\n");
            b->producer = b->buffer;
        }
    }
}
}

inline static void AudioUnitCBufferConsume(AudioUnitCBuffer *b, uint8_t *bytes, int32_t len)
{
while(len--)
{
    // if producer catches up with consumer, skip a byte
    if (b->consumer == b->producer)
    {
        *bytes++ = 0xd5;
    }
    else
    {
        *bytes++ = *b->consumer;
        b->consumer++;

        if(b->consumer == &b->buffer[b->length-1])
        {
            b->consumer = b->buffer;
        }
    }
}
}
4

1 回答 1

2

Ok 写了一种不同风格的 Circular 缓冲区,似乎已经成功了,延迟非常相似,没有故障。我仍然不完全明白为什么这会更好,请有这方面经验的任何人分享。

由于苹果发布的这些东西很少,下面是我的循环缓冲区实现,它适用于我的 VOIP 设置,请随意使用它,欢迎任何建议,如果没有就不要来找我为你工作。这次它是一个objective-c类。

请注意,这是设计用于 ALAW 格式而非线性 PCM,“0xd5”是 ALAW 中的一个静音字节,不确定这在 PCM 中会是什么,但预计会是噪音。

循环缓冲区.h:

//
//  CircularBuffer.h
//  clevercall
//
//  Created by Simon Mcloughlin on 10/1/2013.
//
//

#import <Foundation/Foundation.h>

@interface CircularBuffer : NSObject

-(int) availableBytes;
-(id) initWithLength:(int)length;
-(void) produceToBuffer:(const void*)data ofLength:(int)length;
-(void) consumeBytesTo:(void *)buf OfLength:(int)length;

@end

循环缓冲区.m:

//
//  CircularBuffer.m
//  clevercall
//
//  Created by Simon Mcloughlin on 10/1/2013.
//
//

#import "CircularBuffer.h"

@implementation CircularBuffer
{
    unsigned int gBufferLength;
    unsigned int gAvailableBytes;
    unsigned int gHead;
    unsigned int gTail;
    void *gBuffer;
}

// Init instance with a certain length and alloc the space
-(id)initWithLength:(int)length
{
    self = [super init];

    if (self != nil)
    {
        gBufferLength = length;
        gBuffer = malloc(length);
        memset(gBuffer, 0xd5, length);

        gAvailableBytes = 0;
        gHead = 0;
        gTail = 0;
    }

    return self;
}

// return the number of bytes stored in the buffer
-(int) availableBytes
{
    return gAvailableBytes;
}

-(void) produceToBuffer:(const void*)data ofLength:(int)length
{
    // if the number of bytes to add to the buffer will go past the end.
    // copy enough to fill to the end
    // go back to the start
    // fill the remaining
    if((gHead + length) > gBufferLength-1)
    {
        int remainder = ((gBufferLength-1) - gHead);
        memcpy(gBuffer + gHead, data, remainder);
        gHead = 0;
        memcpy(gBuffer + gHead, data + remainder, (length - remainder));
        gHead += (length - remainder);
        gAvailableBytes += length;
    }
    // if there is room in the buffer for these bytes add them
    else if((gAvailableBytes + length) <= gBufferLength-1)
    {
        memcpy(gBuffer + gHead, data, length);
        gAvailableBytes += length;
        gHead += length;
    }
    else
    {
        //NSLog(@"--- Discarded ---");
    }
}

-(void) consumeBytesTo:(void *)buf OfLength:(int)length
{
    // if the tail is at a point where there is not enough between it and the end to fill the buffer.
    // copy out whats left
    // move back to the start
    // copy out the rest
    if((gTail + length) > gBufferLength-1 && length <= gAvailableBytes)
    {
        int remainder = ((gBufferLength-1) - gTail);
        memcpy(buf, gBuffer + gTail, remainder);
        gTail = 0;
        memcpy(buf + remainder, gBuffer, (length -remainder));
        gAvailableBytes-=length;
        gTail += (length -remainder);
    }
    // if there is enough bytes in the buffer
    else if(length <= gAvailableBytes)
    {
        memcpy(buf, gBuffer + gTail, length);
        gAvailableBytes-=length;
        gTail+=length;
    }
    // else play silence
    else
    {
        memset(buf, 0xd5, length);
    }
}

@end
于 2013-01-10T15:06:09.687 回答