2

Context: I'm trying to make a real-time audio chat.

I've been playing around with AVAudioSinkNode all week so I can record audio, however for some reason whenever I try it, the audioBufferList is always filled with zeros. Right now I'm testing it in Swift Playground. Here is my code:

let engine = AVAudioEngine()

let sinkNode = AVAudioSinkNode { (timestep, frames, audioBufferList) -> OSStatus in
    let ptr = audioBufferList.pointee.mBuffers.mData?.assumingMemoryBound(to: Float.self)
    var monoSamples = [Float]()
    monoSamples.append(contentsOf: UnsafeBufferPointer(start: ptr, count: Int(frames)))
    for frame in 0..<frames {
      print("sink: " + String(monoSamples[Int(frame)]))
    }
    return noErr
}

engine.attach(sinkNode)

engine.connect(engine.inputNode, to: sinkNode, format: nil)

do {
    try engine.start()
    CFRunLoopRunInMode(.defaultMode, CFTimeInterval(duration), false)
    engine.stop()
} catch {
    print("Could not start engine: \(error)")
}

Then this is the resulting output:

...
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
sink: 0.0
Program ended with exit code: 0

I've looked around at some questions that were similar, but few seem to be having my issue. I've looked into using taps but from what I've seen those aren't great for real-time applications like mine.

Could anyone point me in the right direction?

Noah Z
  • 23
  • 6

1 Answers1

1

After playing with your example I can confirm it works as expected. However, that was initially not the case because I had my Dante Virtual Soundcard selected as system-wide audio input.

After setting the audio input to microphone the numbers started to reflect an actual audio signal.

So, try set your audio input to something you know for sure produces audio.

EDIT

As we found out another way this can fail is when the permissions for using the microphone are not set for XCode. These can be set using System Preferences -> Security & Privacy -> Microphone -> XCode

Ruurd Adema
  • 920
  • 7
  • 17
  • Hmm I changed it to use my internal mic and speakers and still no luck. Were you running it in a simulator or in SwiftPlayground? Maybe my issue is that I'm not running it in a simulator. – Noah Z Jun 13 '20 at 19:01
  • 1
    I ran the example in a Playground on macOS Catalina – Ruurd Adema Jun 13 '20 at 19:01
  • Thats so odd I'm running Catalina as well. I've unplugged everything from my laptop so there is only one possible microphone source. I checked my audio settings and the internal microphone is selected. In the settings it is responding to my noise but the playground code is still not working as expected for me – Noah Z Jun 13 '20 at 19:12
  • 1
    What is your output of the commands `xcodebuild -version` and `swift -version`? Mine is `Xcode 11.5 Build version 11E608c` and `Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53) Target: x86_64-apple-darwin19.5.0` – Ruurd Adema Jun 13 '20 at 19:22
  • ```$ xcodebuild -version Xcode 11.5 Build version 11E608c $ swift -version Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53) Target: x86_64-apple-darwin19.5.0``` – Noah Z Jun 13 '20 at 19:24
  • I'm using the 2020 macbook could that be it? I could try it on an older laptop? – Noah Z Jun 13 '20 at 19:26
  • 1
    I'm running this on a 2012 MacBook (!), so definitely give that a try! And if you're able to use another source like an external USB device, please do! – Ruurd Adema Jun 13 '20 at 19:27
  • 1
    I reproduced your case. I did this by disabling microphone access to XCode under System Preferences => Security & Privacy => Microphone => XCode (deselected). Could this be the cause of you problem? Please let me know and I'll update the answer. – Ruurd Adema Jun 13 '20 at 19:34
  • XCode is not even an option in the list under ```System Preferences => Security & Privacy => Microphone``` Very odd. I would expect it to ask me at some point but it never did. This is likely the source of the issue. Not sure how to get the prompt though. – Noah Z Jun 13 '20 at 19:42
  • This did end up being the problem. If you want to go ahead and update your answer I'll go ahead and give it the green check :) – Noah Z Jun 13 '20 at 21:17