AudioConverterConvertBuffer problem with insz error

Posted by Samuel on Stack Overflow See other posts from Stack Overflow or by Samuel
Published on 2011-01-09T21:15:58Z Indexed on 2011/01/10 7:53 UTC
Read the original article Hit count: 246

Filed under:
|
|
|

Hi Codegurus,

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 the insz problem is there... so it cant convert. I would appreciate any input

Thanks in advance

© Stack Overflow or respective owner

Related posts about iphone

Related posts about audio