李京倫
李京倫

Reputation: 23

how to record microphone and piano at same time

I am trying to make a app which can record the piano sound make by AudioKit AKOscillatorBank and vocal from microphone at same time in swift 4. I tried AVFoundation's AVAudioRecorder, but it failed when I playing piano. I have tried using AVAudioSessionCategoryPlayAndRecord but it still seems not work:

  func startRecording() {
        fileName = String(format:"AudioFile%d.m4a",k)
        k = k+1

        let audioFilename = getDocumentsDirectory().appendingPathComponent(fileName)
        address.append("\(audioFilename)")
        print("adress = %s",address[num])
        num = num + 1
        let settings = [
            AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
            AVSampleRateKey: 12000,
            AVNumberOfChannelsKey: 2,
            AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
        ]


        do {
             try session.setCategory(AVAudioSessionCategoryPlayAndRecord, with:AVAudioSessionCategoryOptions.defaultToSpeaker)
            try session.setActive(true)
            SoundRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
            SoundRecorder.isMeteringEnabled = true
            SoundRecorder.prepareToRecord()
            SoundRecorder.delegate = self
            SoundRecorder.record()

        } catch {

        }
    } 

I have no Idea what can I do, is there any way to do it by swift or objective C? any advice and suggestions will be greatly appreciated.

Upvotes: 1

Views: 195

Answers (1)

Aurelius Prochazka
Aurelius Prochazka

Reputation: 4573

To record different AudioKit nodes, use AKNodeRecorder. There is a "Recorder" example project included with AudioKit:

https://github.com/AudioKit/AudioKit/blob/v4-master/Examples/iOS/Recorder/Recorder/ViewController.swift

Upvotes: 2

Related Questions