Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions HaishinKit/Sources/Mixer/AudioRingBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ final class AudioRingBuffer {

@inline(__always)
private func append(_ audioPCMBuffer: AVAudioPCMBuffer, offset: Int = 0) {
guard offset >= 0, offset < Int(audioPCMBuffer.frameLength),
head >= 0, head < Int(outputBuffer.frameLength) else {
return
}
let numSamples = min(Int(audioPCMBuffer.frameLength) - offset, Int(outputBuffer.frameLength) - head)
if inputFormat.isInterleaved {
let channelCount = Int(inputFormat.channelCount)
Expand Down Expand Up @@ -217,8 +221,9 @@ final class AudioRingBuffer {
sampleTime += Int64(numSamples)
if head == outputBuffer.frameLength {
head = 0
if 0 < Int(audioPCMBuffer.frameLength) - numSamples {
append(audioPCMBuffer, offset: numSamples)
let nextOffset = offset + numSamples
if nextOffset < Int(audioPCMBuffer.frameLength) {
append(audioPCMBuffer, offset: nextOffset)
}
}
}
Expand Down