4

I have AVAudioPCMBuffer which I want to copy a portion of to other buffer (cutting some part of the voice from the end of it )

    let samples = Array(UnsafeBufferPointer(start: buffer.floatChannelData![0], count:Int(buffer.frameLength)))
    let newPcmBuffer = AVAudioPCMBuffer(pcmFormat: FileThatContainsTheBuffer.processingFormat, frameCapacity: AVAudioFrameCount(_playFile.length))

    let count = Int(CGFloat(samples.count) * (/* some value between 0 and 1 */))

    for i in 0 ..<  count
    {
        newPcmBuffer.floatChannelData![0][i] = buffer.floatChannelData![0][i]
    }

    if let url = getFileUrl(fileExtension: "caf")
    {
        let newPlayFile = try AVAudioFile(forWriting: url as URL, settings: [:], commonFormat: self.format!.commonFormat, interleaved: false)
        try newPlayFile.write(from: newPcmBuffer)
    }

when I try to play the newPlayFile it plays silence (or doesn't play at all ) I know that the problem is in the for loop , whats the right way to do this ?

user3703910
  • 604
  • 1
  • 5
  • 22

0 Answers0