-1

I want to convert an array of floats (one dimension) to an audio file -- then I want to send this audio file to a server with a put request, thus I need to convert it to Data.

How could I do this?? I have seen stack overflow posts about how to convert an array of floats to a wav file like this: Write array of floats to a wav audio file in swift

But I dont want to store anything, just convert it to a wav file, convert that to Data and send that to my db with a Put request. How can I do this? Thanks so much for your help!!

1 Answers1

0

I recommend creating a temporary AVAudioFile to send to the server, and delete it when all data has been sent. This will prevent having to recreate the file in case the connection is dropped or has some other problem. It is also a high-level API that's easy to use.

If you want to avoid writing to a file, it can be done but will be much lower-level. You can create an AudioFileID with custom Data-based callbacks using AudioFileInitializeWithCallbacks, wrap that with ExtAudioFileWrapAudioFileID, and then write the floats using ExtAudioFileWrite after setting the client data format.

sbooth
  • 16,646
  • 2
  • 55
  • 81
  • Would you mind providing some code showing how that first version works? I can't seem to find too much online about how I can actually send that AVAudioFile to a server as I need to convert it to Data. Thanks! –  Jun 04 '20 at 23:24
  • Once the file is written you can read it as data using `Data.init(contentsOf:)` and send it to your server normally. – sbooth Jun 04 '20 at 23:30