1
votes

this can be a very stupid question as I am really new to AudioKit and IOS audio in general. What I am trying to achieve is to record a clip (or just a simple record to file) from my microphone.

I don't want my app to send the input (from microphone) directly to output, I mean I want the microphone be muted when recording or using my app (mostly because I get feedback when debugging, I want to unmute it when user is with headphones for example).

Tried to play with AKSettings.audioInputEnabled and AKSettings.defaultToSpeaker in my AppDelegate.swift -> application function (is this the right place to do so?) but couldn't make it work as I want. Tried also to connect the microphone via a booster and set gain to 0, but then it recorded empty file.

Maybe I am not using the right terms so I will be happy if you correct me.

Here is my piece of code

AppDelegate.swift

AKSettings.defaultToSpeaker = true
AKSettings.audioInputEnabled = false
try! AKSettings.setSession(category: .playAndRecord)

record code (using clipRecorder but I guess this question is also for regular recorder):

var mixer: AKMixer?
let length = 4.0
let microphone = AKMicrophone()
/* ... */
let booster = AKBooster(microphone)
booster.gain = 0
self.mixer = AKMixer(booster)
AudioKit.output = self.mixer
AudioKit.start()
/* ... */
self.clipRecorder = AKClipRecorder(node: mixer)
try clipRecorder.recordClip(time: clipRecorder.currentTime, duration: length, tap: nil, completion: { url, something, err in
    /* ... */
} catch {
    print("error")
}

Thank you very much AudioKit guys, I would appreciate also more explanation on what and why I did wrong please if it is not a bother.

EDIT: providing the current code as Aurelius asked, the problem is if i set gain to 0, i don't hear the mic feedback indeed but all is recorded is complete silence.

AppDelegate.swift

AKSettings.defaultToSpeaker = true
AKSettings.audioInputEnabled = true
try! AKSettings.setSession(category: .playAndRecord)

ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()
    silence = AKBooster(mic, gain: 0)
    //mixer = AKMixer(silence)
    AudioKit.output = silence
    AudioKit.start()
    clipRecorder = AKClipRecorder(node: silence!)
    clipRecorder?.play()
}

@IBAction func recordButtonPressed(_ sender: Any) {
    print("perssed")
    do {
        try clipRecorder?.recordClip(time: (clipRecorder?.currentTime)!, duration: 3.0, tap: nil, completion: {url,_,_ in
            print("done")
        })
    } catch {
        print("error")
    }
}

Edit 2: ok, after few hours of fighting with myself I figured out what was wrong, and indeed Aurelius was right, it was just the order of things. The recorder of course needs to be provided with the booster before silenced (gain=0), the recorder gets the correct values which silenced when going to speakers. for the example i've provided above the fix is pretty easy - ViewController.swift

override func viewDidLoad() {
    super.viewDidLoad()
    silence = AKBooster(mic)
    clipRecorder = AKClipRecorder(node: silence!)
    silence.gain = 0
    AudioKit.output = silence
    AudioKit.start()

    clipRecorder?.play()
}

@IBAction func recordButtonPressed(_ sender: Any) {
    print("perssed")
    do {
        try clipRecorder?.recordClip(time: (clipRecorder?.currentTime)!, duration: 3.0, tap: nil, completion: {url,_,_ in
            print("done")
        })
    } catch {
        print("error")
    }
}
1

1 Answers

1
votes

I think you need put the booster after the node the you are recording. You want to record the regular levels, then after that send it through a booster whose gain is set to zero.