iOS将音频采样率从16 kHz转换为8 kHz

时间:2017-03-23 09:41:45

标签: ios iphone audio core-audio audiotoolbox

我尝试将PCM音频从16kHz转换为8kHz,只是采样率,没有格式变化,流程看起来很简单,但我不断调用kAudioConverterErr_InvalidInputSize AudioConverterFillComplexBuffer(“insz”)。我的输入音频样本大小是320字节,结果应该是160字节,但我的输出缓冲区只有144字节。过去几个小时我一直在脱掉头发。有没有设置错误?

static AudioConverterRef PCM8kTo16kConverterRef;

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initConverter];
    }
    return self;
}

-(void)initConverter{
    AudioStreamBasicDescription PCM8kDescription = {0};
    PCM8kDescription.mSampleRate = 8000.0;
    PCM8kDescription.mFormatID = kAudioFormatLinearPCM;
    PCM8kDescription.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian;
    PCM8kDescription.mBitsPerChannel = 8 * sizeof(SInt16);
    PCM8kDescription.mChannelsPerFrame = 1;
    PCM8kDescription.mBytesPerFrame = sizeof(SInt16) * PCM8kDescription.mChannelsPerFrame;
    PCM8kDescription.mFramesPerPacket = 1;
    PCM8kDescription.mBytesPerPacket = PCM8kDescription.mBytesPerFrame * PCM8kDescription.mFramesPerPacket;

    AudioStreamBasicDescription PCM16kDescription = {0};
    PCM16kDescription.mSampleRate = 16000.0;
    PCM16kDescription.mFormatID = kAudioFormatLinearPCM;
    PCM16kDescription.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagsNativeEndian;
    PCM16kDescription.mBitsPerChannel = 8 * sizeof(SInt16);
    PCM16kDescription.mChannelsPerFrame = 1;
    PCM16kDescription.mBytesPerFrame = sizeof(SInt16) * PCM16kDescription.mChannelsPerFrame;
    PCM16kDescription.mFramesPerPacket = 1;
    PCM16kDescription.mBytesPerPacket = PCM16kDescription.mBytesPerFrame * PCM16kDescription.mFramesPerPacket;

    OSStatus status = AudioConverterNew(&PCM16kDescription, &PCM8kDescription, &converterRef);
}

OSStatus inInputDataProc(AudioConverterRef inAudioConverter, UInt32 *ioNumberDataPackets, AudioBufferList *ioData, AudioStreamPacketDescription **outDataPacketDescription, void *inUserData)
{
    AudioBufferList audioBufferList = *(AudioBufferList *)inUserData;

    ioData->mBuffers[0].mData = audioBufferList.mBuffers[0].mData;
    ioData->mBuffers[0].mDataByteSize = audioBufferList.mBuffers[0].mDataByteSize;

    return  noErr;
}

- (NSData *)testSample:(NSData *)inAudio {

    NSMutableData *ddd = [inAudio mutableCopy];
    AudioBufferList inAudioBufferList = {0};
    inAudioBufferList.mNumberBuffers = 1;
    inAudioBufferList.mBuffers[0].mNumberChannels = 1;
    inAudioBufferList.mBuffers[0].mDataByteSize = (UInt32)[ddd length];
    inAudioBufferList.mBuffers[0].mData = [ddd mutableBytes];

    uint32_t bufferSize = (UInt32)[inAudio length] / 2;
    uint8_t *buffer = (uint8_t *)malloc(bufferSize);
    memset(buffer, 0, bufferSize);
    AudioBufferList outAudioBufferList;
    outAudioBufferList.mNumberBuffers = 1;
    outAudioBufferList.mBuffers[0].mNumberChannels = 1;
    outAudioBufferList.mBuffers[0].mDataByteSize = bufferSize;
    outAudioBufferList.mBuffers[0].mData = buffer;

    UInt32 ioOutputDataPacketSize = bufferSize;

    OSStatus ret = AudioConverterFillComplexBuffer(converterRef, inInputDataProc, &inAudioBufferList, &ioOutputDataPacketSize, &outAudioBufferList, NULL) ;

    NSData *data = [NSData dataWithBytes:outAudioBufferList.mBuffers[0].mData length:outAudioBufferList.mBuffers[0].mDataByteSize];
    free(buffer);
    return data;
}

1 个答案:

答案 0 :(得分:3)

有两个问题:

  1. 您的AudioConverterComplexInputDataProc未设置ioNumberDataPackets

    *ioNumberDataPackets = audioBufferList.mBuffers[0].mDataByteSize/2;
    
  2. ioOutputDataPacketSize应该是数据包/帧中的输出缓冲区容量,而不是字节,所以不应该除以2吗?