【发布时间】:2020-08-15 10:09:21
【问题描述】:
我正在使用 AVAudioEngine、AVAudioSequencer、AVAudioUnitSampler 播放 MID。 AVAudioUnitSampler 加载 Soundfont 和 AVAudioSequencer 加载 MIDI 文件。 我的初始配置是
engine = AVAudioEngine()
sampler = AVAudioUnitSampler()
speedControl = AVAudioUnitVarispeed()
pitchControl = AVAudioUnitTimePitch()
engine.attach(sampler)
engine.attach(pitchControl)
engine.attach(speedControl)
engine.connect(sampler, to: speedControl, format: nil)
engine.connect(speedControl, to: pitchControl, format: nil)
engine.connect(pitchControl, to: engine.mainMixerNode, format: nil)
这是我的序列如何加载 MIDI 文件
func setupSequencer() {
self.sequencer = AVAudioSequencer(audioEngine: self.engine)
let options = AVMusicSequenceLoadOptions()
let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let introurl = URL(string: songDetails!.intro!)
let midiFileURL = documentsDirectoryURL.appendingPathComponent(introurl!.lastPathComponent)
do {
try sequencer.load(from: midiFileURL, options: options)
print("loaded \(midiFileURL)")
} catch {
print("something screwed up \(error)")
return
}
sequencer.prepareToPlay()
if sequencer.isPlaying == false{
}
}
这是采样器加载 SoundFont 的方式
func loadSF2PresetIntoSampler(_ preset: UInt8,bankURL:URL ) {
do {
try self.sampler.loadSoundBankInstrument(at: bankURL,
program: preset,
bankMSB: UInt8(kAUSampler_DefaultMelodicBankMSB),
bankLSB: UInt8(kAUSampler_DefaultBankLSB))
} catch {
print("error loading sound bank instrument")
}
}
而且它玩得很好,这没有问题。我还有 2 个其他要求,但在这些方面遇到了问题
-
我必须在第一个 MID 结束播放后播放另一个 MIDI 文件,为此我需要从引擎或序列中获取完整/完成 MIDI 文件回调或我如何加载多个 MIDI 文件顺序?我尝试了很多方法,但都没有帮助。
-
我需要显示 MIDI 文件播放的进度,例如当前时间和总时间。为此,我尝试了一种在堆栈答案中找到的方法,即:
var currentPositionInSeconds: TimeInterval { get { guard let offsetTime = offsetTime else { return 0 } guard let lastRenderTime = engine.outputNode.lastRenderTime else { return 0 } let frames = lastRenderTime.sampleTime - offsetTime.sampleTime return Double(frames) / offsetTime.sampleRate }}
这里offsetTime是
offsetTime = engine.outputNode.lastRenderTime
而且它总是返回 nil。
【问题讨论】:
标签: swift midi avaudioengine