【问题标题】:MacOS Core Audio CAF recording produces all zerosMacOS Core Audio CAF 录音产生全零
【发布时间】:2020-04-08 16:34:55
【问题描述】:

我正在学习 Learning Core Audio 书籍教程,在第 4 章中有一个练习,其中包括创建缓冲区队列、收听默认输入麦克风并将缓冲区保存在 @ 987654321@ 文件。 我多次尝试执行代码,作者也提供了一个版本。但我得到的所有时间都是一个 30kb 的 caf 文件,其中包含前导和尾随魔术 cookie,中间全为零。

xxd output.caf 摘录:

00006fb0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00006fc0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00006fd0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
00006fe0: 0000 0000 0000 0000 0000 0000 0000 0000  ................

我多次尝试重置麦克风隐私首选项,允许在 XCode 和终端的设置中使用麦克风,在调试和发布模式以及不同文件夹中执行 CLI 应用程序,但没有任何改变。我得到的只是一个 30kb caf 文件,无论持续时间长短。

我认为报告所有代码并没有多大用处,因为它似乎对其他人有用。但这里是回调实现:

// Audio Queue callback function, called when an input buffer has been filled.
static void MyAQInputCallback(void *inUserData, AudioQueueRef inQueue,
                              AudioQueueBufferRef inBuffer,
                              const AudioTimeStamp *inStartTime,
                              UInt32 inNumPackets,
                              const AudioStreamPacketDescription *inPacketDesc)
{
    MyRecorder *recorder = (MyRecorder *)inUserData;

    // if inNumPackets is greater then zero, our buffer contains audio data
    // in the format we specified (AAC)
    if (inNumPackets > 0)
    {
        // write packets to file
        CheckError(AudioFileWritePackets(recorder->recordFile, FALSE, inBuffer->mAudioDataByteSize,
                                         inPacketDesc, recorder->recordPacket, &inNumPackets,
                                         inBuffer->mAudioData), "AudioFileWritePackets failed");
        // increment packet index
        recorder->recordPacket += inNumPackets;
    }

    // if we're not stopping, re-enqueue the buffer so that it gets filled again
    if (recorder->running)
        CheckError(AudioQueueEnqueueBuffer(inQueue, inBuffer,
                                           0, NULL), "AudioQueueEnqueueBuffer failed");
}

我尝试打印一些变量,感觉一切都很好。告诉我你是否想要某个变量或其他代码的输出。

我知道你很难用这么少的信息提供帮助,但我真的需要这个功能,我希望你至少可以做出一些假设或指导我完成调试。

谢谢

【问题讨论】:

    标签: objective-c xcode core-audio audio-recording


    【解决方案1】:

    首先:

    我得到的只是一个 30kb 的 caf 文件,无论持续时间长短。

    这可以解决

    CheckError(AudioQueueNewInput(&recordFormat, // ASBD
                                  MyAQInputCallback, // Callback
                                  &recorder, // user data
                                  NULL, // run loop
                                  NULL, // run loop mode
                                  0, // flags (always 0)
                                  &queue),
               "AudioQueueNewInput failed");
    

    运行循环和运行循环模式,均为NULL。


    其次:

    在 iOS 中,无论是设备还是模拟器,

    可以使用AudioQueueNewInput 进行录音。

    在 OS X 终端应用程序中,

    produces all zeros
    

    打印一些变量并不行

    这样打印

     CheckError(AudioFileWritePackets(recorder->recordFile, FALSE, inBuffer->mAudioDataByteSize,
                                         inPacketDesc, recorder->recordPacket, &inNumPackets,
                                         inBuffer->mAudioData), "AudioFileWritePackets failed");
    
     printf("\ninBuffer->mAudioData: %d\n", inBuffer->mAudioData);
    

    在我的 Mbp 中,它会重复

    25556992

    25492480

    所以AudioQueueNewInput 效果不佳


    我也试过 swift 版的

    Learning Core 有声读物教程

    遇到了同样的问题。

    这里是 Swift 版本的回调代码:

    func inputCallback(userData: UnsafeMutableRawPointer?,
                       queue: AudioQueueRef,
                       bufferToEmpty: AudioQueueBufferRef,
                       startTime: UnsafePointer<AudioTimeStamp>,
                       numPackets: UInt32,
                       packetDesc: UnsafePointer<AudioStreamPacketDescription>?) {
    
    
    
    
        // cast the inUserData Void pointer to a Recorder struct pointer
        let recorder = userData!.assumingMemoryBound(to: Recorder.self)
    
        // if inNumPackets is greater then zero, our buffer contains audio data
        // in the format we specified (AAC)
        if numPackets > 0 {
        
             // write packets to file
             var ioNumPackets = numPackets
             let temp = bufferToEmpty.pointee.mAudioData
        
             let val = temp.assumingMemoryBound(to: UInt32.self).pointee
        
             print("\n\n bufferToEmpty.pointee.mAudioData", val)
             print("recorder.recordFile!", recorder.pointee.recordFile!)
             print("bufferToEmpty.pointee.mAudioDataByteSize", bufferToEmpty.pointee.mAudioDataByteSize)
        
             print("Int64(numPackets)", Int64(numPackets))
             Utility.check(error: AudioFileWritePackets(recorder.pointee.recordFile!,               // AudioFileID
                                                   false,                                       // use cache?
                                                   bufferToEmpty.pointee.mAudioDataByteSize,    // number of bytes to be written
                                                   packetDesc,                                  // pointer to an array of PacketDescriptors
                                                   recorder.pointee.recordPacket,              // index of first packet to be written
                                                   &ioNumPackets,                               // number of packets to be written
                                                   bufferToEmpty.pointee.mAudioData),           // buffer of audio to be written
                      operation: "AudioFileWritePackets failed")
        
             // increment packet index
              recorder.pointee.recordPacket = recorder.pointee.recordPacket + Int64(numPackets)
        }
    
    // if we're not stopping, re-enqueue the buffer so that it gets filled again
        if recorder.pointee.running {
               Utility.check(error: AudioQueueEnqueueBuffer(queue,                                     // queue
                                                     bufferToEmpty,                             // buffer to enqueue
                                                     0,                                         // always 0 for recording
                                                     nil),                                      // always nil for recording
                      operation: "AudioQueueEnqueueBuffer failed")
         }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-30
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 2012-06-20
      • 2011-04-14
      • 1970-01-01
      相关资源
      最近更新 更多