簡體   English   中英

在IOS中重復播放音頻會損壞錄制

[英]Corrupt recording with repeating audio in IOS

我的應用程序在iPhone上錄制流音頻。 我的問題是一小部分(〜2%)的錄音已損壞。 他們似乎有一些音頻緩沖區重復。

例如,聽這個文件


編輯:令人驚訝的是,使用Audacity仔細查看數據后發現重復部分非常相似,但不完全相同。 由於FLAC(我用於編碼音頻的格式)是一種無損壓縮,因此我認為這不是流/編碼中的錯誤,而是問題出自於來自麥克風的數據!


以下是我用於設置音頻流傳輸的代碼-它有什么問題嗎?

// see functions implementation below
- (void)startRecording
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
               , ^{
        [self setUpRecordQueue];
        [self setUpRecordQueueBuffers];
        [self primeRecordQueueBuffers];
        AudioQueueStart(recordQueue, NULL);
    });
}



// this is called only once before any recording takes place
- (void)setUpAudioFormat
{
    AudioSessionInitialize(
                           NULL,
                           NULL,
                           nil,
                           (__bridge  void *)(self)
                           );

        UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
        AudioSessionSetProperty(
                            kAudioSessionProperty_AudioCategory,
                            sizeof(sessionCategory),
                            &sessionCategory
                            );

        AudioSessionSetActive(true);

    audioFormat.mFormatID         = kAudioFormatLinearPCM;
    audioFormat.mSampleRate       = SAMPLE_RATE;//16000.0;
    audioFormat.mChannelsPerFrame = CHANNELS;//1;
    audioFormat.mBitsPerChannel   = 16;
    audioFormat.mFramesPerPacket  = 1;
    audioFormat.mBytesPerFrame    = audioFormat.mChannelsPerFrame * sizeof(SInt16);
    audioFormat.mBytesPerPacket   = audioFormat.mBytesPerFrame * audioFormat.mFramesPerPacket;
    audioFormat.mFormatFlags      = kLinearPCMFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsPacked;

    bufferNumPackets = 2048;  // must be power of 2 for FFT!
    bufferByteSize = [self byteSizeForNumPackets:bufferNumPackets];
}

// I suspect the duplicate buffers arrive here:
static void recordCallback(
                       void* inUserData,
                       AudioQueueRef inAudioQueue,
                       AudioQueueBufferRef inBuffer,
                       const AudioTimeStamp* inStartTime,
                       UInt32 inNumPackets,
                       const AudioStreamPacketDescription* inPacketDesc)
{
     Recorder* recorder = (__bridge Recorder*) inUserData;
    if (inNumPackets > 0)
    {
        // append the buffer to FLAC encoder
        [recorder recordedBuffer:inBuffer->mAudioData byteSize:inBuffer->mAudioDataByteSize packetsNum:inNumPackets];
    }

    AudioQueueEnqueueBuffer(inAudioQueue, inBuffer, 0, NULL);

}


- (void)setUpRecordQueue
{
     OSStatus errorStatus = AudioQueueNewInput(
                   &audioFormat,
                   recordCallback,
                   (__bridge void *)(self),                // userData
                   CFRunLoopGetMain(),  // run loop
                   NULL,                // run loop mode
                   0,                   // flags
                   &recordQueue);
       UInt32 trueValue = true;
      AudioQueueSetProperty(recordQueue,kAudioQueueProperty_EnableLevelMetering,&trueValue,sizeof (UInt32));
}

- (void)setUpRecordQueueBuffers
{
    for (int t = 0; t < NUMBER_AUDIO_DATA_BUFFERS; ++t)
    {
         OSStatus errorStatus = AudioQueueAllocateBuffer(
                             recordQueue,
                             bufferByteSize,
                             &recordQueueBuffers[t]);
    }
}

- (void)primeRecordQueueBuffers
{
    for (int t = 0; t < NUMBER_AUDIO_DATA_BUFFERS; ++t)
    {
        OSStatus errorStatus = AudioQueueEnqueueBuffer(
                            recordQueue,
                            recordQueueBuffers[t],
                            0,
                            NULL);
    }
}

事實證明,存在一個罕見的錯誤,幾乎可以同時開始多個錄音-因此兩個錄音並行發生,但是將音頻緩沖區發送到同一回調中,從而使編碼錄音中的重復緩沖區失真了...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM