从未调用过CoreAudio AudioQueue回调函数,没有报告错误

时间:2013-10-17 18:26:26

标签: objective-c macos callback core-audio audioqueue

我正在尝试从文件功能进行简单的播放,看起来我的回调函数从未被调用过。它没有多大意义,因为所有的OSStatus都返回0,其他数字也都显示正确(就像输出数据包从AudioFileReadPackets中读取指针一样)。

以下是设置:

OSStatus stat;

stat = AudioFileOpenURL(
    (CFURLRef)urlpath, kAudioFileReadPermission, 0, &aStreamData->aFile
);

UInt32 dsze = 0;
stat = AudioFileGetPropertyInfo(
    aStreamData->aFile, kAudioFilePropertyDataFormat, &dsze, 0
);

stat = AudioFileGetProperty(
    aStreamData->aFile, kAudioFilePropertyDataFormat, &dsze, &aStreamData->aDescription
);

stat = AudioQueueNewOutput(
    &aStreamData->aDescription, bufferCallback, aStreamData, NULL, NULL, 0, &aStreamData->aQueue
);

aStreamData->pOffset = 0;

for(int i = 0; i < NUM_BUFFERS; i++) {
    stat = AudioQueueAllocateBuffer(
        aStreamData->aQueue, aStreamData->aDescription.mBytesPerPacket, &aStreamData->aBuffer[i]
    );

    bufferCallback(aStreamData, aStreamData->aQueue, aStreamData->aBuffer[i]);
}

stat = AudioQueuePrime(aStreamData->aQueue, 0, NULL);
stat = AudioQueueStart(aStreamData->aQueue, NULL);

(未显示我正在检查函数之间的stat的值,它只是恢复正常。)

回调函数:

void bufferCallback(void *uData, AudioQueueRef queue, AudioQueueBufferRef buffer) {
    UInt32 bread = 0;
    UInt32 pread = buffer->mAudioDataBytesCapacity / player->aStreamData->aDescription.mBytesPerPacket;

    OSStatus stat;

    stat = AudioFileReadPackets(
        player->aStreamData->aFile, false, &bread, NULL, player->aStreamData->pOffset, &pread, buffer->mAudioData
    );

    buffer->mAudioDataByteSize = bread;

    stat = AudioQueueEnqueueBuffer(queue, buffer, 0, NULL);

    player->aStreamData->pOffset += pread;
}

其中aStreamData是我的用户数据结构(typedefed所以我可以将它用作类属性)而player是控制Objective-C类的静态实例。如果需要任何其他代码,请告诉我。我有点在我的智慧结束。打印此处涉及的任何数字都会产生正确的结果,包括我在allocate循环中自己调用bufferCallback时的函数。它之后永远不会被调用。启动方法返回,没有任何反应。

另外有趣的是,我正在使用外围设备(MBox Pro 3)来播放CoreAudio即将输出时才启动的声音。 IE如果我启动iTunes或其他东西,扬声器会微弱地弹出,并且有一个LED从闪烁变为稳定。设备启动就像它一样,所以CA肯定在做某事。 (当然,我当然尝试使用板载Macbook声音来播放设备。)

我已经阅读了其他听起来类似的问题的解决方案,但它们无法正常工作。就像使用我现在正在做的多个缓冲区一样,似乎没有任何区别。

我基本上假设我在某种程度上做了明显错误的事情,但不确定它可能是什么。我已经阅读了相关文档,查看了可用的代码示例,并在网上搜索了一些答案,看来这就是我需要做的所有事情,它应该只是去。

至少,我还有什么可以做的调查吗?

2 个答案:

答案 0 :(得分:2)

我的第一个答案还不够好,所以我编写了一个最小的例子来播放2通道,16位波形文件。

与代码的主要区别在于我让一个属性监听器监听播放开始和停止事件。

至于你的代码,乍一看似乎合法。不过我要指出两件事: 1.似乎您正在使用TOO SMALL缓冲区大小分配缓冲区。我注意到如果缓冲区太小,AudioQueues将无法播放,这似乎适合您的问题。 2.您是否验证了退回的物业?

回到我的代码示例:

所有东西都是硬编码的,所以它不是很好的编码练习,但它显示了你如何做到这一点。

AudioStreamTest.h

#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>

uint32_t bufferSizeInSamples;
AudioFileID file;
UInt32 currentPacket;
AudioQueueRef audioQueue;
AudioQueueBufferRef buffer[3];
AudioStreamBasicDescription audioStreamBasicDescription;

@interface AudioStreamTest : NSObject

- (void)start;
- (void)stop;

@end

AudioStreamTest.m

#import "AudioStreamTest.h"

@implementation AudioStreamTest

- (id)init
{
    self = [super init];
    if (self) {
        bufferSizeInSamples = 441;

        file = NULL;
        currentPacket = 0;

        audioStreamBasicDescription.mBitsPerChannel = 16;
        audioStreamBasicDescription.mBytesPerFrame = 4;
        audioStreamBasicDescription.mBytesPerPacket = 4;
        audioStreamBasicDescription.mChannelsPerFrame = 2;
        audioStreamBasicDescription.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
        audioStreamBasicDescription.mFormatID = kAudioFormatLinearPCM;
        audioStreamBasicDescription.mFramesPerPacket = 1;
        audioStreamBasicDescription.mReserved = 0;
        audioStreamBasicDescription.mSampleRate = 44100;
    }

    return self;
}

- (void)start {
    AudioQueueNewOutput(&audioStreamBasicDescription, AudioEngineOutputBufferCallback, (__bridge void *)(self), NULL, NULL, 0, &audioQueue);

    AudioQueueAddPropertyListener(audioQueue, kAudioQueueProperty_IsRunning, AudioEnginePropertyListenerProc, NULL);

    AudioQueueStart(audioQueue, NULL);
}

- (void)stop {
    AudioQueueStop(audioQueue, YES);
    AudioQueueRemovePropertyListener(audioQueue, kAudioQueueProperty_IsRunning, AudioEnginePropertyListenerProc, NULL);
}

void AudioEngineOutputBufferCallback(void *inUserData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer) {
    if (file == NULL) return;

    UInt32 bytesRead = bufferSizeInSamples * 4;
    UInt32 packetsRead = bufferSizeInSamples;
    AudioFileReadPacketData(file, false, &bytesRead, NULL, currentPacket, &packetsRead, inBuffer->mAudioData);
    inBuffer->mAudioDataByteSize = bytesRead;
    currentPacket += packetsRead;

    if (bytesRead == 0) {
        AudioQueueStop(inAQ, false);
    }
    else {
        AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL);
    }
}

void AudioEnginePropertyListenerProc (void *inUserData, AudioQueueRef inAQ, AudioQueuePropertyID inID) {
    //We are only interested in the property kAudioQueueProperty_IsRunning
    if (inID != kAudioQueueProperty_IsRunning) return;

    //Get the status of the property
    UInt32 isRunning = false;
    UInt32 size = sizeof(isRunning);
    AudioQueueGetProperty(inAQ, kAudioQueueProperty_IsRunning, &isRunning, &size);

    if (isRunning) {
        currentPacket = 0;

        NSString *fileName = @"/Users/roy/Documents/XCodeProjectsData/FUZZ/03.wav";
        NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: fileName];
        AudioFileOpenURL((__bridge CFURLRef) fileURL, kAudioFileReadPermission, 0, &file);

        for (int i = 0; i < 3; i++){
            AudioQueueAllocateBuffer(audioQueue, bufferSizeInSamples * 4, &buffer[i]);

            UInt32 bytesRead = bufferSizeInSamples * 4;
            UInt32 packetsRead = bufferSizeInSamples;
            AudioFileReadPacketData(file, false, &bytesRead, NULL, currentPacket, &packetsRead, buffer[i]->mAudioData);
            buffer[i]->mAudioDataByteSize = bytesRead;
            currentPacket += packetsRead;

            AudioQueueEnqueueBuffer(audioQueue, buffer[i], 0, NULL);
        }
    }

    else {
        if (file != NULL) {
            AudioFileClose(file);
            file = NULL;

            for (int i = 0; i < 3; i++) {
                AudioQueueFreeBuffer(audioQueue, buffer[i]);
                buffer[i] = NULL;
            }
        }
    }
}

-(void)dealloc {
    [super dealloc];
    AudioQueueDispose(audioQueue, true);
    audioQueue = NULL;
}

@end

最后,我想包括我今天所做的一些研究,以测试AudioQueues的稳健性。

我注意到如果你制作的AudioQueue缓冲区太小,它根本就不会播放。这让我玩了一下,看看它为什么不玩。

如果我尝试只能容纳150个样本的缓冲区大小,我根本就没有声音。

如果我尝试可以容纳175个样本的缓冲区大小,它会播放整首歌曲,但会出现很多失真。 175相当于小于4毫秒的音频。

只要您继续提供缓冲区,AudioQueue就会不断要求新的缓冲区。无论AudioQueue实际上是否正在播放缓冲区,都是如此。

如果提供大小为0的缓冲区,缓冲区将丢失,并为该队列入队请求返回错误kAudioQueueErr_BufferEmpty。你永远不会看到AudioQueue要求你再次填充缓冲区。如果发生在您发布的最后一个队列中,AudioQueue将停止要求您填充更多缓冲区。在这种情况下,您将不会再听到该会话的音频。

为了了解为什么AudioQueues没有播放缓冲区大小较小的东西,我做了一个测试,看看即使没有声音我的回调是否完全被调用。答案是只要AudioQueues正在播放并且需要数据,就会一直调用缓冲区。

因此,如果您继续向队列提供缓冲区,则不会丢失任何缓冲区。它不会发生。当然,除非有错误。

为什么没有声音播放?

我测试了'AudioQueueEnqueueBuffer()'是否返回任何错误。它没。我的游戏例程中也没有其他错误。从文件中读取的数据也很好。

一切正常,缓冲区很好,重新排队的数据很好,没有声音。

所以我的最后一次测试是慢慢增加缓冲区大小,直到我听到任何声音。我终于听到了微弱和零星的失真。

然后它来找我......

似乎问题在于系统试图让流与时间保持同步,因此如果您将音频排队,并且您想要播放的音频的时间已经过去,它将跳过该部分缓冲区。如果缓冲区大小太小,则会丢弃或跳过越来越多的数据,直到音频系统再次同步为止。如果缓冲区大小太小,则永远不会出现这种情况。 (如果选择的缓冲区大小不足以支持连续播放,则可以将其视为失真。)

如果您考虑一下,这是音频队列可以工作的唯一方式,但是当您像我一样无能为力并“发现”它如何运作时,这是一个很好的实现。

答案 1 :(得分:2)

我决定再次看一下这个,并且能够通过扩大缓冲区来解决这个问题。我已经接受了@RoyGal的答案,因为这是他们的建议,但我想提供实际的代码,因为我猜其他人也有同样的问题(问题有一些不是我的最爱)。 / p>

我尝试过的一件事就是让数据包大小更大:

aData->aDescription.mFramesPerPacket = 512; // or some other number
aData->aDescription.mBytesPerPacket = (
    aData->aDescription.mFramesPerPacket * aData->aDescription.mBytesPerFrame
);

这不起作用:它会导致AudioQueuePrime失败并显示AudioConverterNew returned -50消息。我想它希望mFramesPerPacket为1。

(我也尝试设置kAudioQueueProperty_DecodeBufferSizeFrames属性似乎没有做任何事情。不确定它是什么。)

解决方案似乎只是分配具有指定大小的缓冲区:

AudioQueueAllocateBuffer(
    aData->aQueue,
    aData->aDescription.mBytesPerPacket * N_BUFFER_PACKETS / N_BUFFERS,
    &aData->aBuffer[i]
);

尺寸必须足够大。我发现神奇的数字是:

mBytesPerPacket * 1024 / N_BUFFERS

(其中N_BUFFERS是缓冲区的数量,应为> 1或播放不稳定。)

以下是MCVE演示问题和解决方案:

#import <Foundation/Foundation.h>

#import <AudioToolbox/AudioToolbox.h>
#import <AudioToolbox/AudioQueue.h>
#import <AudioToolbox/AudioFile.h>

#define N_BUFFERS 2
#define N_BUFFER_PACKETS 1024

typedef struct AStreamData {
    AudioFileID aFile;
    AudioQueueRef aQueue;
    AudioQueueBufferRef aBuffer[N_BUFFERS];
    AudioStreamBasicDescription aDescription;
    SInt64 pOffset;
    volatile BOOL isRunning;
} AStreamData;

void printASBD(AudioStreamBasicDescription* desc) {
    printf("mSampleRate = %d\n", (int)desc->mSampleRate);
    printf("mBytesPerPacket = %d\n", desc->mBytesPerPacket);
    printf("mFramesPerPacket = %d\n", desc->mFramesPerPacket);
    printf("mBytesPerFrame = %d\n", desc->mBytesPerFrame);
    printf("mChannelsPerFrame = %d\n", desc->mChannelsPerFrame);
    printf("mBitsPerChannel = %d\n", desc->mBitsPerChannel);
}

void bufferCallback(
    void *vData, AudioQueueRef aQueue, AudioQueueBufferRef aBuffer
) {
    AStreamData* aData = (AStreamData*)vData;

    UInt32 bRead = 0;
    UInt32 pRead = (
        aBuffer->mAudioDataBytesCapacity / aData->aDescription.mBytesPerPacket
    );

    OSStatus stat;

    stat = AudioFileReadPackets(
        aData->aFile, false, &bRead, NULL, aData->pOffset, &pRead, aBuffer->mAudioData
    );
    if(stat != 0) {
        printf("AudioFileReadPackets returned %d\n", stat);
    }

    if(pRead == 0) {
        aData->isRunning = NO;
        return;
    }

    aBuffer->mAudioDataByteSize = bRead;

    stat = AudioQueueEnqueueBuffer(aQueue, aBuffer, 0, NULL);
    if(stat != 0) {
        printf("AudioQueueEnqueueBuffer returned %d\n", stat);
    }

    aData->pOffset += pRead;
}

AStreamData* beginPlayback(NSURL* path) {
    static AStreamData* aData;
    aData = malloc(sizeof(AStreamData));

    OSStatus stat;

    stat = AudioFileOpenURL(
        (CFURLRef)path, kAudioFileReadPermission, 0, &aData->aFile
    );
    printf("AudioFileOpenURL returned %d\n", stat);

    UInt32 dSize = 0;

    stat = AudioFileGetPropertyInfo(
        aData->aFile, kAudioFilePropertyDataFormat, &dSize, 0
    );
    printf("AudioFileGetPropertyInfo returned %d\n", stat);

    stat = AudioFileGetProperty(
        aData->aFile, kAudioFilePropertyDataFormat, &dSize, &aData->aDescription
    );
    printf("AudioFileGetProperty returned %d\n", stat);

    printASBD(&aData->aDescription);

    stat = AudioQueueNewOutput(
        &aData->aDescription, bufferCallback, aData, NULL, NULL, 0, &aData->aQueue
    );
    printf("AudioQueueNewOutput returned %d\n", stat);

    aData->pOffset = 0;

    for(int i = 0; i < N_BUFFERS; i++) {
        // change YES to NO for stale playback
        if(YES) {
            stat = AudioQueueAllocateBuffer(
                aData->aQueue,
                aData->aDescription.mBytesPerPacket * N_BUFFER_PACKETS / N_BUFFERS,
                &aData->aBuffer[i]
            );
        } else {
            stat = AudioQueueAllocateBuffer(
                aData->aQueue,
                aData->aDescription.mBytesPerPacket,
                &aData->aBuffer[i]
            );
        }

        printf(
            "AudioQueueAllocateBuffer returned %d for aBuffer[%d] with capacity %d\n",
            stat, i, aData->aBuffer[i]->mAudioDataBytesCapacity
        );

        bufferCallback(aData, aData->aQueue, aData->aBuffer[i]);
    }

    UInt32 numFramesPrepared = 0;
    stat = AudioQueuePrime(aData->aQueue, 0, &numFramesPrepared);
    printf("AudioQueuePrime returned %d with %d frames prepared\n", stat, numFramesPrepared);

    stat = AudioQueueStart(aData->aQueue, NULL);
    printf("AudioQueueStart returned %d\n", stat);

    UInt32 pSize = sizeof(UInt32);
    UInt32 isRunning;
    stat = AudioQueueGetProperty(
        aData->aQueue, kAudioQueueProperty_IsRunning, &isRunning, &pSize
    );
    printf("AudioQueueGetProperty returned %d\n", stat);

    aData->isRunning = !!isRunning;
    return aData;
}

void endPlayback(AStreamData* aData) {
    OSStatus stat = AudioQueueStop(aData->aQueue, NO);
    printf("AudioQueueStop returned %d\n", stat);
}

NSString* getPath() {
    // change NO to YES and enter path to hard code
    if(NO) {
        return @"";
    }

    char input[512];
    printf("Enter file path: ");
    scanf("%[^\n]", input);

    return [[NSString alloc] initWithCString:input encoding:NSASCIIStringEncoding];
}

int main(int argc, const char* argv[]) {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    NSURL* path = [NSURL fileURLWithPath:getPath()];
    AStreamData* aData = beginPlayback(path);

    if(aData->isRunning) {
        do {
            printf("Queue is running...\n");
            [NSThread sleepForTimeInterval:1.0];
        } while(aData->isRunning);
        endPlayback(aData);
    } else {
        printf("Playback did not start\n");
    }

    [pool drain];
    return 0;
}