I would like to open an audio file on my iOS device and remove long silences. I already have the code for calculating volumes so am not pasting that here.
What I am unsure of "how to do" is: While I believe that I have the proper code to read the file below, I am unsure as to how to read it in proper pieces to I can later get the volume of each piece.
I realize that this might be a situation of calculating the size of frames and whatnot. But I am totally green when it comes to audio.
I would seriously appreciate any guidance.
guard let input = try? AVAudioFile(forReading: url) else {
return nil
}
guard let buffer = AVAudioPCMBuffer(pcmFormat: input.processingFormat, frameCapacity: AVAudioFrameCount(input.length)) else {
return nil
}
do {
try input.read(into: buffer)
} catch {
return nil
}
In the example above, you are reading the entire file into a single PCM buffer. To read a portion of the file, you first specify the framePosition property of the AVAudioFile to perform a seek before a read. You then call read(into:frameCount:) to read a certain number of frames into a PCM buffer, specifying the destination buffer and the number of frames to be read. Please note that a read operation advances the frame position value by the number of frames it reads.