开发者

AudioConverterConvertBuffer problem with insz error

开发者 https://www.devze.com 2023-02-03 20:40 出处:网络
I have a problem with the this function AudioConverterConvertBuffer. Basically I want to convert from this format

I have a problem with the this function AudioConverterConvertBuffer. Basically I want to convert from this format

  _

streamFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked |0 ;
  _streamFormat.mBitsPerChannel = 16;
  _streamFormat.mChannelsPerFrame = 2;
  _streamFormat.mBytesPerPacket = 4;
  _streamFormat.mBytesPerFrame = 4;
  _streamFormat.mFramesPerPacket = 1;
  _streamFormat.mSampleRate = 44100;
  _streamFormat.mReserved = 0;

to this format

_streamFormatOutput.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked|0 ;//| kAudioFormatFlagIsNonInterleaved |0;
  _streamFormatOutput.mBitsPerChannel = 16;
  _streamFormatOutput.mChannelsPerFrame = 1;
  _streamFormatOutput.mBytesPerPacket = 2;
  _streamFormatOutput.mBytesPerFrame = 2;
  _streamFormatOutput.mFramesPerPacket = 1;
  _streamFormatOutput.mSampleRate = 44100;
  _streamFormatOutput.mReserved = 0;

and what i want to do is to extract an audio channel(Left channel or right channel) from an LPCM buffer based on the input format to make it mono in the output format. Some logic code to convert is as follows

This is to set the channel map for PCM output file

SInt32 channelMap[1] = {0};
 status = AudioConverterSetProperty(converter, kAudioConverterChannelMap, sizeof(channelMap), channelMap);

and this is to convert the buffer in a while loop

      AudioBufferList  audioBufferList;
  CMBlockBufferRef blockBuffer;
  CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampBuffer, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);
  for (int y=0; y<audioBufferList.mNumberBuffers; y++) {
   AudioBuffer audioBuffer = audioBufferList.mBuffers[y];
   //frames = audioBuffer.mData;
   NSLog(@"the number of channel for buffer number %d is %d",y,audioBuffer.mNumberChannels);
   NSLog(@"The buffer size is %d",audioBuffer.mDataByteSize);
   numBytesIO = audioBuffer.mDataByteSize;

   convertedBuf = malloc(sizeof(char)*numBytesIO);


   status = AudioConverterConvertBuffer(converter, audioBuffer.mDataByteSize, audioBuffer.mData, &numBytesIO, convertedBuf);
   char errchar[10];
   NSLog(@"status audio converter convert %d",status);
   if (status != 0) {
    NSLog(@"Fail conversion");
    assert(0);
   }
   NSLog(@"Bytes converted %d",numBytesIO);

   status = AudioFileWriteBytes(mRecordFile, YES, countByteBuf, &numBytesIO, convertedBuf);
   NSLog(@"status for writebyte %d, bytes written %d",status,numBytesIO);

   free(convertedBuf);

   if (numBytesIO != audioBuffer.mDataByteSize) {
    NSLog(@"Something wrong in writing");
    assert(0);
   }
   countByteBuf = countByteBuf + numBytesIO;

But 开发者_如何学Gothe insz problem is there... so it cant convert. I would appreciate any input

Thanks in advance


First, you cannot use AudioConverterConvertBuffer() to convert anything where input and output byte size is different. You need to use AudioConverterFillComplexBuffer(). This includes performing any kind of sample rate conversions, or adding/removing channels.

See Apple's documentation on AudioConverterConvertBuffer(). This was also discussed on Apple's CoreAudio mailing lists, but I'm afraid I cannot find a reference right now.

Second, even if this could be done (which it can't) you are passing the same number of bytes allocated for output as you had for input, despite actually requiring half of the number of bytes (due to reducing number of channels from 2 to 1).

I'm actually working on using AudioConverterConvertBuffer() right now, and the test files are mono while I need to play stereo. I'm currently stuck with the converter performing conversion only of the first chunk of the data. If I manage to get this to work, I'll try to remember to post the code. If I don't post it, please poke me in comments.

0

精彩评论

暂无评论...
验证码 换一张
取 消