简体   繁体   中英

iOS convert audio sample rate from 16 kHz to 8 kHz

I tried to convert PCM audio from 16kHz to 8kHz, just sample rate, no format change, the flow looks simple but I kept getting kAudioConverterErr_InvalidInputSize ("insz") from calling AudioConverterFillComplexBuffer . My input audio sample size is 320 bytes, the result is supposed to be 160 bytes but I just got 144 bytes in my output buffer. have been pulling my hair off for the last couple hours. Is there any setting wrong?

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;
}

There are two problems:

  1. your AudioConverterComplexInputDataProc isn't setting ioNumberDataPackets :

     *ioNumberDataPackets = audioBufferList.mBuffers[0].mDataByteSize/2; 
  2. ioOutputDataPacketSize is supposed to be the output buffer capacity in packets/frames , not bytes, so shouldn't you divide by 2?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM