线性PCM 16位到8位

时间:2013-08-20 14:14:52

标签: c++ ios objective-c audio audioqueue

我正在尝试采用名为“SpeakHere”的Apple示例录制部分来实现我的目的。一切似乎都很好,但我需要添加一个实际提供8位录音的选项。 这是根据任何音频设置不允许的规范,所以我需要从16位进行某种转换。 我想我需要把它放在回调函数中。

// ____________________________________________________________________________________
// 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));
    }
}
但说实话不知道怎么做。任何想法将不胜感激。

2 个答案:

答案 0 :(得分:0)

为什么不尝试使用类似的东西初始化音频队列?

    aqData.mDataFormat.mFormatID = kAudioFormatLinearPCM;        // 2
    aqData.mDataFormat.mSampleRate = 44100.0;                    // 3
    aqData.mDataFormat.mChannelsPerFrame = 1;                    // 4
    aqData.mDataFormat.mBitsPerChannel = 8;                     // 5
    aqData.mDataFormat.mBytesPerPacket =                         // 6
    aqData.mDataFormat.mBytesPerFrame =
    aqData.mDataFormat.mChannelsPerFrame * sizeof (SInt8);
    aqData.mDataFormat.mFramesPerPacket = 1;                     // 7

    AudioFileTypeID fileType = kAudioFileAIFFType;               // 8
    aqData.mDataFormat.mFormatFlags =                            // 9
    kLinearPCMFormatFlagIsBigEndian
    | kLinearPCMFormatFlagIsSignedInteger
    | kLinearPCMFormatFlagIsPacked; 

答案 1 :(得分:0)

经过大量的调查和尝试后,我发现,我不需要转换,但只需要设置不同的格式标记。

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