c++iosobjective-caudioaudioqueue

Linear PCM 16 bit to 8 bit


I am trying to adopt recording part of the Apple example entitled "SpeakHere" to my purposes. Everything seems to fine, but I need to add an option which actually offers 8 bit recording. This is according to the specification not allowed by any audio settings, so I need some kind of conversion from 16 bit. I think I need to place it in the callback function.

// ____________________________________________________________________________________
// AudioQueue callback function, called when an input buffers has been filled.
void AQRecorder::MyInputBufferHandler(  void *                              inUserData,
                                        AudioQueueRef                       inAQ,
                                        AudioQueueBufferRef                 inBuffer,
                                        const AudioTimeStamp *              inStartTime,
                                        UInt32                              inNumPackets,
                                        const AudioStreamPacketDescription* inPacketDesc)
{
    AQRecorder *aqr = (AQRecorder *)inUserData;
    try {
        if (inNumPackets > 0) {
            // write packets to file
            XThrowIfError(AudioFileWritePackets(aqr->mRecordFile, FALSE, inBuffer->mAudioDataByteSize,
                                             inPacketDesc, aqr->mRecordPacket, &inNumPackets, inBuffer->mAudioData),
                       "AudioFileWritePackets failed");
            aqr->mRecordPacket += inNumPackets;
        }

        // if we're not stopping, re-enqueue the buffe so that it gets filled again
        if (aqr->IsRunning())
            XThrowIfError(AudioQueueEnqueueBuffer(inAQ, inBuffer, 0, NULL), "AudioQueueEnqueueBuffer failed");
    } catch (CAXException e) {
        char buf[256];
        fprintf(stderr, "Error: %s (%s)\n", e.mOperation, e.FormatError(buf));
    }
}

but to be honest don't know how. Any idea will be appreciated.


Solution

  • After quite of investigation and trying things, I have found out, that I do not need conversion, but just have to set different format flags.

    mRecordFormat.mFormatFlags      = kLinearPCMFormatFlagIsBigEndian;
    mRecordFormat.mBitsPerChannel   = 8;