AudioKit / Cookbook

Canonical Examples for Using the AudioKit Framework
MIT License
633 stars 103 forks source link

Recorder is not recording clear audio #164

Closed RizwanaDesai closed 1 week ago

RizwanaDesai commented 1 month ago

macOS Version(s) Used to Build

macOS 13 Ventura

Xcode Version(s)

Xcode 14

Description

Recorder is not recording clear audio,

We are using AudioKit in our app and from iOS 18 we are facing difficulty with audio quality, tried out Recorder in this demo app too, and demo has the same issue with audio quality.

Crash Logs, Screenshots or Other Attachments (if applicable)

Steps to reproduce in Demo App:

MacOS:14.2 Xcode: 15.1 Issue reproducible in iOS18 beta

NickCulbertson commented 1 month ago

This was mentioned on the AudioKit Discord. I don't think there is an official solution yet, but @Matt54 said Adding Settings.sampleRate = 48_000 during my audio session config fixed it.

RizwanaDesai commented 1 month ago

Yeah, I was about to try the same, as when i tried directly with audio engine it worked with 48000 sample Rate. Thanks @NickCulbertson!

RizwanaDesai commented 1 week ago

Adding Settings.sampleRate = 48_000 resolves the issue; however, the first recording attempt results in a blank audio file. After navigating back to Mini Apps and attempting to record again, it consistently works from the second attempt onward. @NickCulbertson

NickCulbertson commented 1 week ago

Thanks for the info. I'll download the beta this week or next to see what is going on. One thing to note is that if I add Settings.sampleRate = 48_000 in my current version of the macOS/iOS using Xcode 15 it creates pops. So I would check for backwards compatibility. There might be some way to check the sample rate of the input node directly, but I have not had a chance to try that yet.

I was not able to recreate the issue of the first recording not working. That might have to do with where you are calling Settings.sampleRate = 48_000.

RizwanaDesai commented 1 week ago

You can check the sample rate for the inputNode with this code: engine.avEngine.inputNode.inputFormat(forBus: 0).sampleRate I think I saw a static sample rate of 44100 Hz in AudioKit before, but I'm not completely sure. This might be causing the issue if there's a difference in sample rates.

And I have setup sampleRate in init() in Demo Project.

Screenshot 2024-09-03 at 6 57 34 PM
RizwanaDesai commented 1 week ago

nvm fixed it. Will have to configure setting before audio engine initialize

NickCulbertson commented 1 week ago

Actually it looks like this might work In CookbookApp:

init() {
        #if os(iOS)
            do {
                Settings.bufferLength = .short
                Settings.sampleRate = AVAudioSession.sharedInstance().sampleRate

                try AVAudioSession.sharedInstance().setPreferredIOBufferDuration(Settings.bufferLength.duration)
                try AVAudioSession.sharedInstance().setCategory(.playAndRecord,
                                                                options: [.defaultToSpeaker, .mixWithOthers, .allowBluetoothA2DP])
                try AVAudioSession.sharedInstance().setActive(true)
            } catch let err {
                print(err)
            }
        #endif
    }
NickCulbertson commented 1 week ago

Thanks for the heads up! This is now fixed here: https://github.com/AudioKit/Cookbook/pull/165