Play segment of AVAudioPCMBuffer
Asked Answered
H

1

6

I'm creating this simple audio recorder and editor interface for an iOS app:

Screenshot of the recorder/editor interface

The audio is recorded into a float array that is used to create the waveform. After recording I copy the float data into an AVAudioPCMBuffer for playing with AVAudioPlayerNode. I'm able to play the buffer from the start, but I can not figure out how I can play just a segment of the buffer. The scheduleSegment function only works for files.

Hoarse answered 5/11, 2018 at 21:8 Comment(0)
E
8

You can create a new buffer that's a segment of the original. AVAudioPCMBuffer and AudioBufferlist are kind of a pain in Swift. There are a few ways to do this, if you are using floats you can access AVAUdioPCMBuffer.floatChannelData, but here's a method that works for both float and int samples.

func segment(of buffer: AVAudioPCMBuffer, from startFrame: AVAudioFramePosition, to endFrame: AVAudioFramePosition) -> AVAudioPCMBuffer? {
    let framesToCopy = AVAudioFrameCount(endFrame - startFrame)
    guard let segment = AVAudioPCMBuffer(pcmFormat: buffer.format, frameCapacity: framesToCopy) else { return nil }

    let sampleSize = buffer.format.streamDescription.pointee.mBytesPerFrame

    let srcPtr = UnsafeMutableAudioBufferListPointer(buffer.mutableAudioBufferList)
    let dstPtr = UnsafeMutableAudioBufferListPointer(segment.mutableAudioBufferList)
    for (src, dst) in zip(srcPtr, dstPtr) {
        memcpy(dst.mData, src.mData?.advanced(by: Int(startFrame) * Int(sampleSize)), Int(framesToCopy) * Int(sampleSize))
    }

    segment.frameLength = framesToCopy
    return segment
}
Elene answered 6/11, 2018 at 1:48 Comment(1)
This beats chat GPT (by far!) in 2023. Thanks!!Hutchens

© 2022 - 2024 — McMap. All rights reserved.