6

I've been looking around Swift documentation to save an audio output from AVAudioEngine but I couldn't find any useful tip.
Any suggestion?

Solution I found a way around thanks to matt's answer. Here a sample code of how to save an audio after passing it through an AVAudioEngine (i think that technically it's before)

newAudio = AVAudioFile(forWriting: newAudio.url, settings: nil, error: NSErrorPointer()) 
//Your new file on which you want to save some changed audio, and prepared to be bufferd in some new data...

var audioPlayerNode = AVAudioPlayerNode() //or your Time pitch unit if pitch changed   

//Now install a Tap on the output bus to "record" the transformed file on a our newAudio file.
audioPlayerNode.installTapOnBus(0, bufferSize: (AVAudioFrameCount(audioPlayer.duration)), format: opffb){
        (buffer: AVAudioPCMBuffer!, time: AVAudioTime!)  in 

        if (self.newAudio.length) < (self.audioFile.length){//Let us know when to stop saving the file, otherwise saving infinitely

        self.newAudio.writeFromBuffer(buffer, error: NSErrorPointer())//let's write the buffer result into our file

        }else{
             audioPlayerNode.removeTapOnBus(0)//if we dont remove it, will keep on tapping infinitely
            println("Did you like it? Please, vote up for my question")
        }

    }

Hope this helps !

One issue to solve:

Sometimes, your outputNode is shorter than the input: if you accelerate the time rate by 2, your audio will be 2 times shorter. This is the issue im facing for now since my condition for saving the file is (line 10)

if(newAudio.length) < (self.audioFile.length)//audiofile being the original(long) audio and newAudio being the new changed (shorter) audio.

Any help here?

mlk
  • 388
  • 2
  • 10
  • Here: [Apple doc to export from AV](https://developer.apple.com/library/prerelease/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/05_Export.html#//apple_ref/doc/uid/TP40010188-CH9-SW2) – Eric Aya May 08 '15 at 19:43
  • Thanks @eric D., i am going through the code. Looks like it will do the job. Nevertheless, there are many things i am discovering and many lines I have to understand. Is it ok if I raised questions? For example, how could i load an Asset that is not a AudioFile with URL but an audioEngine or AudioNode? Have you ever gone through this code? – mlk May 09 '15 at 14:17
  • 1
    Personnally, not really, I just thought you may have missed this doc, so I pasted the link. But that's not the problem: the thing is you can't ask too broad questions on SO. And comments are not the place for this anyway. I suggest you create a new question including your current code, showing what you try to achieve and where you struggle, or anything like that. Oh, and *one* topic per post. Welcome to SO, by the way. :) – Eric Aya May 09 '15 at 14:27
  • Thank you, i just signed up and this is my very first question. I It doesnt seem as a broad question: AVAudioEngine to Audiofile? I will post a new question more precise with the code ! Thank you again very much, your doc is very relevant, too bad i cant vote for your answer. – mlk May 09 '15 at 14:36
  • I meant *this* question is broad. :) And that's why I only posted a link to the docs in the comments. The question didn't fit for a proper answer. Anyway, it's all very well described [in the Tour](http://stackoverflow.com/tour) and [the Help Center](http://stackoverflow.com/help). Have fun! – Eric Aya May 09 '15 at 14:42

2 Answers2

5

Yes, it's quite easy. You simply put a tap on a node and save the buffer into a file.

Unfortunately this means you have to play through the node. I was hoping that AVAudioEngine would let me process one sound file into another directly, but apparently that's impossible - you have to play and process in real time.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks man, it helped me a lot. One thing, I dont think you need to play if you install the tap on the audioNode. I havent tried it yet but I actually dont need it since i play the audio anyway before saving it. just like a sanity check. – mlk May 11 '15 at 16:30
  • Well, I'll try it again, but in my testing I couldn't get the file to save without actually playing through the buffers. – matt May 11 '15 at 17:14
  • Can you provide the example to save new audio? – Bhavin Ramani May 31 '19 at 12:52
2

Offline rendering Worked for me using GenericOutput AudioUnit. Please check this link, I have done mixing two,three audios offline and combine it to a single file. Not the same scenario but it may help you for getting some idea. core audio offline rendering GenericOutput

Community
  • 1
  • 1
Abdusha M A
  • 509
  • 5
  • 13