diff --git a/Sources/IO/IOAudioResampler.swift b/Sources/IO/IOAudioResampler.swift index e7ba18548..8727b8085 100644 --- a/Sources/IO/IOAudioResampler.swift +++ b/Sources/IO/IOAudioResampler.swift @@ -35,7 +35,7 @@ struct IOAudioResamplerSettings { if converter.downmix != downmix { converter.downmix = downmix } - if let channelMap { + if let channelMap = validatedChannelMap(converter) { converter.channelMap = channelMap } else { switch converter.outputFormat.channelCount { @@ -60,6 +60,18 @@ struct IOAudioResamplerSettings { interleaved: inputFormat.isInterleaved ) } + + private func validatedChannelMap(_ converter: AVAudioConverter) -> [NSNumber]? { + guard let channelMap, channelMap.count == converter.outputFormat.channelCount else { + return nil + } + for inputChannel in channelMap { + if inputChannel.intValue >= converter.inputFormat.channelCount { + return nil + } + } + return channelMap + } } final class IOAudioResampler {