1

AUdioQueueを使用して内蔵マイクからのオーディオを録音し、ソケットを介して送信しています。AudioQueueバッファーを、1回のショットでたとえば30秒のバッファーを録音するように設定し、bufferSizeを適切に割り当てています。これは、 AudioDataFormat。

AudioStreamBasicDescription sRecordFormat;
 FillOutASBDForLPCM (sRecordFormat,
                    16000,
                    1,
                    16,
                    16,
                    false,
                    false
                    );

次のコードは、オーディオをキャプチャするために割り当てる必要があるbufferSizeを計算します。

int AQRecorder::ComputeRecordBufferSize(const AudioStreamBasicDescription *format, float seconds)
{
    int packets, frames, bytes = 0;
    try {
        frames = (int)ceil(seconds * format->mSampleRate);

        if (format->mBytesPerFrame > 0)
            bytes = frames * format->mBytesPerFrame;
        else {
            UInt32 maxPacketSize;
            if (format->mBytesPerPacket > 0)
                maxPacketSize = format->mBytesPerPacket;    // constant packet size
            else {
                UInt32 propertySize = sizeof(maxPacketSize);
                XThrowIfError(AudioQueueGetProperty(mQueue, kAudioQueueProperty_MaximumOutputPacketSize, &maxPacketSize,
                                                 &propertySize), "couldn't get queue's maximum output packet size");
            }
            if (format->mFramesPerPacket > 0)
                packets = frames / format->mFramesPerPacket;
            else
                packets = frames;   // worst-case scenario: 1 frame in a packet
            if (packets == 0)       // sanity check
                packets = 1;
            bytes = packets * maxPacketSize;
        }
    } catch (CAXException e) {
        char buf[256];
        gLog<<[[NSString stringWithFormat:@"Error:%s (%s)\n",e.mOperation,e.FormatError(buf)] UTF8String]<<endl;
        return 0;
    }   
    return bytes;
}

バッファを割り当てるための次のコードサンプル、

    // allocate and enqueue buffers
    bufferByteSize = ComputeRecordBufferSize(&mRecordFormat, kBufferDurationSeconds);   // enough bytes for 20 ms
    for (i = 0; i < kNumberRecordBuffers; ++i) {
        XThrowIfError(AudioQueueAllocateBuffer(mQueue, bufferByteSize, &mBuffers[i]),
                   "AudioQueueAllocateBuffer failed");
        XThrowIfError(AudioQueueEnqueueBuffer(mQueue, mBuffers[i], 0, NULL),
                   "AudioQueueEnqueueBuffer failed");
    }

はい、あなたはそれを正しく推測しました、SpeakHereの例から参照されたコードのほとんど、AudioCallbackに関しては、私はバッファをキャプチャし、ソケットを介して他のマシンに送信する必要があります、

// ____________________________________________________________________________________
// 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 {
           NSLog([NSString stringWithFormat:"Inside AudioBufferCallback no of packet [%d]",inMumPackets]);
        if (inNumPackets > 0) {
            // write packets to file 
                    // This is only for the test 
            XThrowIfError(AudioFileWritePackets(aqr->mRecordFile, FALSE, inBuffer->mAudioDataByteSize,
                                              inPacketDesc, aqr->mRecordPacket, &inNumPackets, inBuffer->mAudioData),
                       "AudioFileWritePackets failed");
            aqr->mRecordPacket += inNumPackets;

            if(aqr->pInputListener){
                aqr->pInputListener(aqr->pClientUserData,inBuffer->mAudioData,(int)inBuffer->mAudioDataByteSize);
            }
        }

        // 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");
    } 
}

今、ログを見ると、データが来ていますが、そのようなパケットは256、320ではないと表示されます。もう一方の端でデータを渡すと、聞こえません。誰か教えてもらえますか、私は何をする必要がありますかpacketSize、私はbufferSizeがデータを送信するのに十分であるという印象を受けましたが、私は推測します、何かがパケットの数にも関係しています。

4

1 に答える 1

0

バッファ管理に問題があります。このアプリケーションは 20 ミリ秒 +- 5 ミリ秒でデータを送信する必要があります。私の場合、この特定のケースは処理されませんでした。

于 2011-06-08T10:56:04.333 に答える