0

我正在使用AudioToolBox.AudioConverter库转换音频。

AudioStreamBasicDescription inDescription;
inDescription.mSampleRate = 44100;
inDescription.mFormatID = kAudioFormatMPEGLayer3;
inDescription.mFormatFlags = 0;
inDescription.mBytesPerPacket = 0;
inDescription.mFramesPerPacket = 1152;
inDescription.mBytesPerFrame = 0;
inDescription.mChannelsPerFrame = 2;
inDescription.mBitsPerChannel = 0;
inDescription.mReserved = 0;

AudioStreamBasicDescription outDescription;
audioDescription.mFormatID = kAudioFormatLinearPCM
audioDescription.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian;
audioDescription.mChannelsPerFrame = 2;
audioDescription.mBytesPerPacket = sizeof(SInt16)*audioDescription.mChannelsPerFrame;
audioDescription.mFramesPerPacket = 1;
audioDescription.mBytesPerFrame = sizeof(SInt16)*audioDescription.mChannelsPerFrame;
audioDescription.mBitsPerChannel = 8 * sizeof(SInt16);
audioDescription.mSampleRate = 44100.0;

这是 mp3AudioData 的转换部分...

AudioBufferList *mp3Audio = (AudioBufferList *)malloc(sizeof(AudioBufferList) + sizeof(AudioBuffer));

mp3Audio->mNumberBuffers = 1;
mp3Audio->mBuffers[0].mNumberChannels  = 2;
mp3Audio->mBuffers[0].mDataByteSize = chunkLen;
mp3Audio->mBuffers[0].mData = calloc(chunkLen, sizeof(uint8_t));
memcpy(mp3Audio->mBuffers[0].mData, chunkData, chunkLen);

AudioStreamPacketDescription *packetDescription =
(AudioStreamPacketDescription*)malloc(sizeof(AudioStreamPacketDescription) * frames);
packetDescription->mDataByteSize = chunkLen;
packetDescription->mStartOffset = 0;
packetDescription->mVariableFramesInPacket = 1;

OSStatus result = AudioConverterFillComplexBuffer(audioConverter,
                                                  fillComplexBufferInputProc,
                                                  &(struct fillComplexBufferInputProc_t) { .bufferList =mp3Audio, .frames = frames, .packetDescriptions = packetDescription },
                                                  &frames,
                                                  pcmAudio,
                                                  NULL);

free(mp3Audio->mBuffers[0].mData);
free(mp3Audio);
free(packetDescription);

第一次运行没问题,AudioConverterFillComplexBuffer但是当我第二次运行该函数时,无限循环发生在 cpuload 100% 的情况下。

这段代码在 IOS 7 设备上是可以的,但是当我在 IOS 8 设备上运行它时,就会发生无限循环。

谁能告诉我为什么会在 IOS 8 上发生这种情况?

fillComplexBufferInputProc功能:

static OSStatus fillComplexBufferInputProc(AudioConverterRef             inAudioConverter,
                                           UInt32                        *ioNumberDataPackets,
                                           AudioBufferList               *ioData,
                                           AudioStreamPacketDescription  **outDataPacketDescription,
                                           void                          *inUserData) {
    struct fillComplexBufferInputProc_t *arg = inUserData;

    for (int i = 0; i < ioData->mNumberBuffers; i++) {
        ioData->mBuffers[i].mData = arg->bufferList->mBuffers[i].mData;
        ioData->mBuffers[i].mDataByteSize = arg->bufferList->mBuffers[i].mDataByteSize;
    }

    if (NULL == *outDataPacketDescription) {
        *outDataPacketDescription = arg->packetDescriptions;
    }

    *ioNumberDataPackets = arg->frames;

    return noErr;
}
4

1 回答 1

1

因为您的代码中似乎没有无限循环(fillComplexBufferInputProc函数中的 for-loop 除外),我建议您使用 Instruments (Time Profiler) 来查看使用大部分 CPU 时间的方法。检查堆栈跟踪以获取更多信息,所以也许我们可以提供帮助。是时间分析器的教程。

于 2015-05-28T09:36:36.323 回答