2

I changed the code in AudioController.swift to:

...
// Set format for mic input (bus 1) on RemoteIO's output scope
var asbd = AudioStreamBasicDescription()
...
asbd.mFormatID = kAudioFormatFLAC
asbd.mFormatFlags = kAppleLosslessFormatFlag_16BitSourceData
...

and SpeechRecognitionService.swift to

...
// send an initial request message to configure the service
let recognitionConfig = RecognitionConfig()
recognitionConfig.encoding =  .flac
...

I tried tinkering around with AudioStreamBasicDescription.mFormatFlags but cannot find the right flags for Google Speech API to recognize the format.

How do I make iOS to record FLAC using the given iOS API?

Raviteja
  • 101
  • 1
  • 8

1 Answers1

1

Try This

/// Common Struct 
struct Manager
{    
    //Required Objects - AVFoundation
    ///AVAudio Session
    static var recordingSession: AVAudioSession!

    ///AVAudio Recorder
    static var recorder: AVAudioRecorder?
}

Permissions

func CheckForPermission() {
    Manager.recordingSession = AVAudioSession.sharedInstance()
    do {
        try Manager.recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: .defaultToSpeaker)
        Manager.recordingSession.requestRecordPermission({ (allowed) in
            if allowed {
                Manager.micAuthorised = true
            }
            else {
                Manager.micAuthorised = false
            }
        })
        DispatchQueue.main.async  {
            //Reload Label text in Home screen
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "updateLabel"), object: nil)
        }
    }
    catch {
        print("Failed to set Category", error.localizedDescription)
    }
}

Recorder Setting & Recording with flac

func startMainRecording() {
    print("Main Recording session is created")

    //Get unique File Name
    Manager.audioName = getUniqueName()
    let AudioFileName = getDocumentsDirectory().appendingPathComponent("\(Manager.audioName).flac")
    print("New Path: \(AudioFileName)")

    //Recorder Settings
    let settings: [String: Any] = [
        /// Format Flac
        AVFormatIDKey: Int(kAudioFormatFLAC),
        AVSampleRateKey: 16000,
        AVNumberOfChannelsKey: 1,
        AVLinearPCMBitDepthKey: 16,
        AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue,
        AVLinearPCMIsBigEndianKey: false,
        AVLinearPCMIsFloatKey: false,
        ]

    //Handler
    do {
        //Activate session when required

        //try Manager.recordingSession.setPreferredSampleRate(44000)
        try Manager.recordingSession.setActive(true)

        //Start Recording With Audio File name
        Manager.recorder = try AVAudioRecorder(url: AudioFileName, settings: settings)
        Manager.recorder?.delegate = self
        Manager.recorder?.isMeteringEnabled = true
    }
    catch
    {
        //Finish Recording with a Error
        print("Error Handling: \(error.localizedDescription)")
        /// Saved here if any error occur while recording Just case
        self.finishRecording(success: false)
    }
}
iOS Geek
  • 4,825
  • 1
  • 9
  • 30