简体   繁体   English

在IOS中重复播放音频会损坏录制

[英]Corrupt recording with repeating audio in IOS

My application records streaming audio on iPhone. 我的应用程序在iPhone上录制流音频。 My problem is that a small percent (~2%) of the recordings are corrupted. 我的问题是一小部分(〜2%)的录音已损坏。 They appear to have some audio buffers duplicated. 他们似乎有一些音频缓冲区重复。

For example listen to this file . 例如,听这个文件


Edit: A surprising thing is that looking closely at the data using Audacity shows the repeating parts are very very similar but not identical. 编辑:令人惊讶的是,使用Audacity仔细查看数据后发现重复部分非常相似,但不完全相同。 Since FLAC (the format I use for encoding the audio) is a loss-less compression, I guess this is not a bug in the streaming/encoding but the problem originates at the data that comes from the microphone! 由于FLAC(我用于编码音频的格式)是一种无损压缩,因此我认为这不是流/编码中的错误,而是问题出自于来自麦克风的数据!


Below is the code I use to setup the audio recording streaming - is there anything wrong with it? 以下是我用于设置音频流传输的代码-它有什么问题吗?

// 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