Format error setting up AudioQueue for recording, but only happens once in a while - ios

So oddly, this error happens only once in a while, when we are setting up the audio queue (even though I'm doing everything the same way). Device iPhone 5, iOS8.3:
mediaserverd[37] <Error>: 15:14:24.594 ERROR: [0x2883000] >aq> 323: AudioConverterNew from AudioQueueNew returned 'fmt?'
io: 0 ch, 44100 Hz, 'lpcm' (0x00000029) 32-bit little-endian float, deinterleaved
client: 0 ch, 44100 Hz, 'lpcm' (0x0000000C) 16-bit signed integer
Here's the code that triggers it.
SetupAudioFormat(kAudioFormatLinearPCM);
// create the queue
XThrowIfError(AudioQueueNewInput(
&mRecordFormat,
MyInputBufferHandler,
this /* userData */,
NULL /* run loop */, NULL /* run loop mode */,
0 /* flags */, &mQueue), "AudioQueueNewInput failed");
where mRecordFormat is setup like:
void AQRecorder::SetupAudioFormat(UInt32 inFormatID)
{
memset(&mRecordFormat, 0, sizeof(mRecordFormat));
UInt32 size = sizeof(mRecordFormat.mSampleRate);
mRecordFormat.mSampleRate=[AVAudioSession sharedInstance].sampleRate;
size = sizeof(mRecordFormat.mChannelsPerFrame);
mRecordFormat.mChannelsPerFrame=(UInt32)[AVAudioSession sharedInstance].inputNumberOfChannels;
mRecordFormat.mFormatID = inFormatID;
mRecordFormat.mBytesPerFrame =mRecordFormat.mChannelsPerFrame * sizeof (SInt16);
if (inFormatID == kAudioFormatLinearPCM)
{
// if we want pcm, default to signed 16-bit little-endian
mRecordFormat.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
mRecordFormat.mBitsPerChannel = 16;
mRecordFormat.mBytesPerPacket = mRecordFormat.mBytesPerFrame = (mRecordFormat.mBitsPerChannel / 8) * mRecordFormat.mChannelsPerFrame;
mRecordFormat.mFramesPerPacket = 1;
} else if(inFormatID==kAudioFileAIFFType) {
mRecordFormat.mFramesPerPacket = 1;
mRecordFormat.mFormatFlags =
kLinearPCMFormatFlagIsBigEndian
| kLinearPCMFormatFlagIsSignedInteger
| kLinearPCMFormatFlagIsPacked;
}
}
My interpretation of the error is that the phone is recording as 32-bit little-endian float, deinterleaved and I'm trying to setup a queue with a format that is 16-bit signed integer. But why don't I get the error everytime? How to fix it?

AudioStreamBasicDescriptions are really annoying. Here is what I use. I have typedefed AudioStreamBasicDescription to ASBD
ASBD asbdWithInfo(Boolean isFloat,int numberOfChannels,Boolean interleavedIfStereo){
ASBD asbd = {0};
int sampleSize = isFloat ? sizeof(float) : sizeof(SInt16);
asbd.mChannelsPerFrame = (numberOfChannels == 1) ? 1 : 2;
asbd.mBitsPerChannel = 8 * sampleSize;
asbd.mFramesPerPacket = 1;
asbd.mSampleRate = 44100.0;
asbd.mBytesPerFrame = interleavedIfStereo ? sampleSize * asbd.mChannelsPerFrame : sampleSize;
asbd.mBytesPerPacket = asbd.mBytesPerFrame;
asbd.mReserved = 0;
asbd.mFormatID = kAudioFormatLinearPCM;
if (isFloat) {
asbd.mFormatFlags = kAudioFormatFlagIsFloat;
if (interleavedIfStereo) {
if (numberOfChannels == 1) {
asbd.mFormatFlags = asbd.mFormatFlags | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved;
}
}
else{
asbd.mFormatFlags = asbd.mFormatFlags | kAudioFormatFlagIsNonInterleaved | kAudioFormatFlagIsPacked ;
}
}
else{
asbd.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked;
if (!interleavedIfStereo) {
if (numberOfChannels > 1) {
asbd.mFormatFlags = asbd.mFormatFlags | kAudioFormatFlagIsNonInterleaved;
}
}
}
return asbd;
}

Related

Using CMSampleTimingInfo, CMSampleBuffer and AudioBufferList from raw PCM 16000 sample rate stream

I recevie audio data and size from outside, the audio appears to be linear PCM, signed int16, but when recording this using an AssetWriter it saves to the audio file highly distorted and higher pitch.
#define kSamplingRate 16000
#define kNumberChannels 1
UInt32 framesAlreadyWritten = 0;
-(AudioStreamBasicDescription) getAudioFormat {
AudioStreamBasicDescription format;
format.mSampleRate = kSamplingRate;
format.mFormatID = kAudioFormatLinearPCM;
format.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;
format.mChannelsPerFrame = 1; // mono
format.mBitsPerChannel = 16;
format.mBytesPerFrame = sizeof(SInt16);
format.mFramesPerPacket = 1;
format.mBytesPerPacket = format.mBytesPerFrame * format.mFramesPerPacket;
format.mReserved = 0;
return format;
}
- (CMSampleBufferRef)createAudioSample:(const void *)audioData frames: (UInt32)len {
AudioStreamBasicDescription asbd = [self getAudioFormat];
CMSampleBufferRef buff = NULL;
static CMFormatDescriptionRef format = NULL;
OSStatus error = 0;
if(format == NULL) {
AudioChannelLayout acl;
bzero(&acl, sizeof(acl));
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;
error = CMAudioFormatDescriptionCreate(kCFAllocatorDefault, &asbd, sizeof(acl), &acl, 0, NULL, NULL, &format);
}
CMTime duration = CMTimeMake(1, kSamplingRate);
CMTime pts = CMTimeMake(framesAlreadyWritten, kSamplingRate);
NSLog(#"-----------pts");
CMTimeShow(pts);
CMSampleTimingInfo timing = {duration , pts, kCMTimeInvalid };
error = CMSampleBufferCreate(kCFAllocatorDefault, NULL, false, NULL, NULL, format, len, 1, &timing, 0, NULL, &buff);
framesAlreadyWritten += len;
if (error) {
NSLog(#"CMSampleBufferCreate returned error: %ld", (long)error);
return NULL;
}
AudioBufferList audioBufferList;
audioBufferList.mNumberBuffers = 1;
audioBufferList.mBuffers[0].mNumberChannels = asbd.mChannelsPerFrame;
audioBufferList.mBuffers[0].mDataByteSize = (UInt32)(number_of_frames * audioFormat.mBytesPerFrame);
audioBufferList.mBuffers[0].mData = audioData;
error = CMSampleBufferSetDataBufferFromAudioBufferList(buff, kCFAllocatorDefault, kCFAllocatorDefault, 0, &audioBufferList);
if(error) {
NSLog(#"CMSampleBufferSetDataBufferFromAudioBufferList returned error: %ld", (long)error);
return NULL;
}
return buff;
}
Not sure why you're dividing len by two, but your time should progress instead of being constant, something like
CMTime time = CMTimeMake(framesAlreadyWritten , kSamplingRate);

Using AudioQueueNewInput to record stereo

I would like to use AudioQueueNewInput to create a stereo recording. I configured it as follows:
audioFormat.mFormatID = kAudioFormatLinearPCM;
hardwareChannels = 2;
audioFormat.mChannelsPerFrame = hardwareChannels;
audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked | kAudioFormatFlagIsBigEndian;
audioFormat.mFramesPerPacket = 1;
audioFormat.mBitsPerChannel = 16;
audioFormat.mBytesPerPacket = (audioFormat.mBitsPerChannel / 8) * hardwareChannels;
audioFormat.mBytesPerFrame = audioFormat.mBytesPerPacket;
OSStatus result = AudioQueueNewInput(
&audioFormat,
recordingCallback,
(__bridge void *)(self), // userData
NULL, // run loop
NULL, // run loop mode
0, // flags
&queueObject
);
AudioQueueStart (
queueObject,
NULL // start time. NULL means as soon as possible.
);
I tested this code on an iPhone 6s plus with an external stereo microphone. It does not seem to record stereo. Both the left and right channels get identical streams of data. What else do I need to do to record stereo?

Swift Error: Struct 'XX' must be completely initialized before a member is stored to

I am trying to define AudioStreamBasicDescription in Swift.
In Objective-C, I used something like the following code.
AudioStreamBasicDescription ASBD;
ASBD.mSampleRate = 8000;
ASBD.mFormatID = kAudioFormatLinearPCM;
ASBD.mFormatFlags = kAudioFormatFlagsCanonical | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
ASBD.mFramesPerPacket = 1;
ASBD.mChannelsPerFrame = 1;
ASBD.mBitsPerChannel = 16;
ASBD.mBytesPerPacket = 2;
ASBD.mBytesPerFrame = 2;
And my converted Swift code is bellow
var ASBD: AudioStreamBasicDescription
ASBD.mSampleRate = 8000 // ERROR here
ASBD.mFormatID = kAudioFormatLinearPCM
ASBD.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked
ASBD.mFramesPerPacket = 1
ASBD.mChannelsPerFrame = 1
ASBD.mBitsPerChannel = 16
ASBD.mBytesPerPacket = 2
ASBD.mBytesPerFrame = 2
But the second line of this swift code is throwing this error. I don't know why i am getting this error. Can anyone please help me with this ?
This means that a structure needs to be completely initialized before using it. AudioStreamBasicDescription is a structure, so you need to initialize it before using it. The right code would be the following:
var ASBD: AudioStreamBasicDescription! = AudioStreamBasicDescription()
ASBD.mSampleRate = 8000
ASBD.mFormatID = kAudioFormatLinearPCM
ASBD.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked
ASBD.mFramesPerPacket = 1
ASBD.mChannelsPerFrame = 1
ASBD.mBitsPerChannel = 16
ASBD.mBytesPerPacket = 2
ASBD.mBytesPerFrame = 2

AudioStreamBasicDescription setting values for wav

I am trying to play a simple PCM file on iOS but couldn't wrap my head around AudioStreamBasicDescription and this link does not provide enough information.
I get this values from terminal
afinfo BlameItOnTheNight.wav
File: BlameItOnTheNight.wav
File type ID: WAVE
Num Tracks: 1
----
Data format: 2 ch, 44100 Hz, 'lpcm' (0x0000000C) 16-bit little-endian signed integer
no channel layout.
estimated duration: 9.938141 sec
audio bytes: 1753088
audio packets: 438272
bit rate: 1411200 bits per second
packet size upper bound: 4
maximum packet size: 4
audio data file offset: 44
optimized
source bit depth: I16
----
Then I choose values in code
- (void)setupAudioFormat:(AudioStreamBasicDescription*)format
{
format->mSampleRate = 44100.0;
format->mFormatID = kAudioFormatLinearPCM;
format->mFramesPerPacket = 1;
format->mChannelsPerFrame = 2;
format->mBytesPerFrame = format->mChannelsPerFrame * sizeof(Float32);
format->mBytesPerPacket = format->mFramesPerPacket * format->mBytesPerFrame;
format->mBitsPerChannel = sizeof(Float32) * 8;
format->mReserved = 0;
format->mFormatFlags = kAudioFormatFlagIsSignedInteger |
kAudioFormatFlagsNativeEndian |
kAudioFormatFlagIsPacked;
}
Audio plays really fast.
Whats the correct way the calculate this values based on actual audio file?
when I changed the values I was getting following error.
error for object 0x7fba72c50db8: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
then finally I figured out that my AudioStreamBasicDescription bitsperchannel values was not correct also the buffer size was not enough.
So first I have changed the values to
- (void)setupAudioFormat:(AudioStreamBasicDescription*)format
{
format->mSampleRate = 44100.0;
format->mFormatID = kAudioFormatLinearPCM;
format->mFramesPerPacket = 1; //For uncompressed audio, the value is 1. For variable bit-rate formats, the value is a larger fixed number, such as 1024 for AAC
format->mChannelsPerFrame = 2;
format->mBytesPerFrame = format->mChannelsPerFrame * 2;
format->mBytesPerPacket = format->mFramesPerPacket * format->mBytesPerFrame;
format->mBitsPerChannel = 16;
format->mReserved = 0;
format->mFormatFlags = kAudioFormatFlagIsSignedInteger |
kAudioFormatFlagsNativeEndian |
kLinearPCMFormatFlagIsPacked;
}
then when I allocate buffer I increased the size
// Allocate and prime playback buffers
playState.playing = true;
for (int i = 0; i < NUM_BUFFERS && playState.playing; i++)
{
AudioQueueAllocateBuffer(playState.queue, 32000, &playState.buffers[i]);
AudioOutputCallback(&playState, playState.queue, playState.buffers[i]);
}
In my original code it was set to 8000, now changing it to 32000 solves the problem.

Audio Queue Converting sample rate iOS

ok so, noob to iOS. I am using the Audio Queue Buffer to record audio. The Linear PCM format defaults to 44100 Hz, 1 channel, 16bit, little endian. Is there a way I can force a format of 8000 hz, 1 channel, 32bit floating point, little endian?
You can specify the format you want at initialization:
AudioStreamBasicDescription asbd;
asbd.mSampleRate = 8000;
asbd.mFormatID = kAudioFormatLinearPCM;
asbd.mFormatFlags = kLinearPCMFormatFlagIsFloat;
asbd.mBytesPerPacket = sizeof(float);
asbd.mFramesPerPacket = 1;
asbd.mBytesPerFrame = sizeof(float);
asbd.mChannelsPerFrame = 1;
asbd.mBitsPerChannel = sizeof(float) * CHAR_BIT;
asbd.mReserved = 0;
OSStatus e = AudioQueueNewInput(&asbd, ...............

Resources