Storing microphone input as Byte Array in Swift

Hi there,


I am wanting to access the iPhone device's microphone and record audio as FLAC and save it as a byte array, rather than save it as a file. Unfortunately, all the tutorials etc. I have followed have used AVAudioRecorder which seems to require a URL. Could someone please give me some code to record audio and save it as a byte array? I know it will require AVFoundation, but I am still fresh out of Windows/Visual Studio (bought first Mac a few days ago) and I am very unfamiliar with Swift so I apologise for my ignorance...


Cheers

save it as a byte array, rather than save it as a file

In modern architecture systems including iOS and macOS, a file content is a byte sequence.

So, I cannot find any difference between save it as a byte array and save it as a file.

Do you mean you want raw sample data?


Without clarifying what do you mean by save it as a byte array, you may need long, long time to get useful responses.

Thanks for the reply, I want to save the audio as a byte array so I can convert it to a Base64 string. For my purposes, I am not concerned with the filename or its directory, just its content. And yes, I want the raw data. I understand that a file is a byte sequence, however, I would rather not have to save the file, just instantly access the byte sequence and convert it to a Base64 string. If I do save the raw audio as a file in a lossless format, would it be possible to just access it and convert it to a Base64 string? If so, how?

Thanks for the help!

If I do save the raw audio as a file in a lossless format, would it be possible to just access it and convert it to a Base64 string? If so, how?


Assuming you already have an audio file in a desired format, it's not difficult to convert it to base64 String:

        let documentDirUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let audioFileUrl = documentDirUrl.appendingPathComponent("audio.dat") /
        do {
            let audioData = try Data(contentsOf: audioFileUrl)
            let base64audio = audioData.base64EncodedString()
            //...
        } catch {
            print(error)
        }

(Also assuming that the size of the audio file is not so huge that exceeds the app's available memory size.)


But I still do not understand why you need the base64 String and what should be the input binary in converting to base64.

Do you want to send it to some sort of APIs?

Thank you for the code! Yes I am looking at sending it to the Google Speech API, as I have found it to be insanely accurate and the language support is phenomenal. I have to send the audio as a POST http parameter as JSON, therefore, I need the string not the audio file. Thanks again

I understand that a file is a byte sequence, however, I would rather not have to save the file …

It is possible to get audio data without going through a file but it requires use of low-level audio APIs. If you want to continue with that part of this question I recommend that you ask over in Media > AVFoundation (Audio).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Storing microphone input as Byte Array in Swift
 
 
Q