简体   繁体   English

线性PCM 16位至8位

[英]Linear PCM 16 bit to 8 bit

I am trying to adopt recording part of the Apple example entitled "SpeakHere" to my purposes. 我试图通过录音来达到目的,录制苹果示例中名为“ SpeakHere”的部分。 Everything seems to fine, but I need to add an option which actually offers 8 bit recording. 一切似乎都很好,但是我需要添加一个实际上提供8位录制的选项。 This is according to the specification not allowed by any audio settings, so I need some kind of conversion from 16 bit. 根据任何音频设置所不允许的规范,因此我需要某种16位转换。 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. 任何想法将不胜感激。

why don't you try to initialize your audio queue with something like that ? 为什么不尝试用类似的方法初始化音频队列?

    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; 

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM