OSStatus error -50 (invalid parameters) AudioQueueNewInput recording audio on iOS

∥☆過路亽.° 提交于 2019-12-11 03:08:43

问题


I've been trawling the internet for ages trying to find the cause of this error but I'm stuck. I've been following the Apple Developer documentation for using Audio Services to record audio and I keep getting this error whatever I do.

I can record audio fine using AVAudioRecorder into any format but my end game is to obtain a normalised array of floats from the input data in order to apply an FFT to it (sorry for the noob phrasing I'm very new to audio programming).

Here's my code:

- (void)beginRecording
{
    // Initialise session
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];

    state.dataFormat.mFormatID = kAudioFormatLinearPCM;
    state.dataFormat.mSampleRate = 8000.0f;
    state.dataFormat.mChannelsPerFrame = 1;
    state.dataFormat.mBitsPerChannel = 16;
    state.dataFormat.mBytesPerPacket = state.dataFormat.mChannelsPerFrame * sizeof(SInt16);
    state.dataFormat.mFramesPerPacket = 1;

    //AudioFileTypeID fileID = kAudioFileAIFFType;

    state.dataFormat.mFormatFlags = kLinearPCMFormatFlagIsBigEndian | kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;

    OSStatus err = AudioQueueNewInput(&state.dataFormat, handleInputBuffer, &state, CFRunLoopGetMain(), kCFRunLoopCommonModes, 0, &state.queue);
    printf("%i", err); // this is always -50 i.e. invalid parameters error

    deriveBufferSize(state.queue, state.dataFormat, 0.5, &state.bufferByteState);

    for (int i = 0; i < kNumberOfBuffers; i++) {
        AudioQueueAllocateBuffer(state.queue, state.bufferByteState, &state.buffers[i]);
        AudioQueueEnqueueBuffer(state.queue, state.buffers[i], 0, NULL);
    }

    state.currentPacket = 0;
    state.isRunning = YES;

    AudioQueueStart(state.queue, NULL);
}

- (void)endRecording
{
    AudioQueueStop(state.queue, YES);
    state.isRunning = NO;

    AudioQueueDispose(state.queue, YES);

    // Close the audio file here...
}

#pragma mark - CoreAudio

// Core Audio Callback Function
static void handleInputBuffer(void *agData, AudioQueueRef inAQ, AudioQueueBufferRef inBuffer, const AudioTimeStamp *inStartTime, UInt32 inNumPackets, const AudioStreamPacketDescription *inPacketDesc) {

    AQRecorderState *state = (AQRecorderState *)agData;

    if (inNumPackets == 0 && state->dataFormat.mBytesPerPacket != 0) {
        inNumPackets = inBuffer->mAudioDataByteSize / state->dataFormat.mBytesPerPacket;
    }

    printf("Called");

    /*
    if (AudioFileWritePackets(state->audioFile, false, inBuffer->mAudioDataByteSize, inPacketDesc, state->currentPacket, &inNumPackets, inBuffer->mAudioData) == noErr) {
        state->currentPacket += inNumPackets;
    }
     */

    if (state->isRunning) {
        AudioQueueEnqueueBuffer(state->queue, inBuffer, 0, NULL);
    }
}

void deriveBufferSize(AudioQueueRef audioQueue, AudioStreamBasicDescription ABSDescription, Float64 secs, UInt32 *outBufferSize) {

    static const int maxBufferSize = 0x50000;

    int maxPacketSize = ABSDescription.mBytesPerPacket;
    if (maxPacketSize == 0) {
        UInt32 maxVBRPacketSize = sizeof(maxPacketSize);
        AudioQueueGetProperty(audioQueue, kAudioConverterPropertyMaximumOutputPacketSize, &maxPacketSize, &maxVBRPacketSize);
    }

    Float64 numBytesForTime = ABSDescription.mSampleRate * maxPacketSize * secs;
    UInt32 x = (numBytesForTime < maxBufferSize ? numBytesForTime : maxBufferSize);
    *outBufferSize = x;
}

If anyone knows what's going on here I'd be very grateful. Here is the apple docs for the error


回答1:


You are getting a -50 (kAudio_ParamError) because you haven't initialised AudioStreamBasicDescription's mBytesPerFrame field:

asbd.mBytesPerFrame = asbd.mFramesPerPacket*asbd.mBytesPerPacket;

where asbd is short for state.dataFormat. In your case mBytesPerFrame = 2.

I also wouldn't specify the kLinearPCMFormatFlagIsBigEndian, let the recorder return you native byte order samples.



来源:https://stackoverflow.com/questions/30413180/osstatus-error-50-invalid-parameters-audioqueuenewinput-recording-audio-on-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!