在 iOS / Swift 中播放来自 gs_instruments 的单个 MIDI 音符

Playing a single midi note from gs_instruments in iOS / Swift

我希望使用 Swift 在 iOS 应用程序上播放单个 MIDI 音符。

这是我目前所拥有的,但不幸的是,这会因错误而崩溃。

func initAudio(){

    let engine = AVAudioEngine()
    self.sampler = AVAudioUnitSampler()
    engine.attachNode(self.sampler!)
    engine.connect(self.sampler!, to: engine.outputNode, format: nil)

    guard let soundbank = NSBundle.mainBundle().URLForResource("gs_instruments", withExtension: "dls") else {

        print("Could not initalize soundbank.")
        return
    }

    let melodicBank:UInt8 = UInt8(kAUSampler_DefaultMelodicBankMSB)
    let gmHarpsichord:UInt8 = 6
    do {

        try self.sampler!.loadSoundBankInstrumentAtURL(soundbank, program: gmHarpsichord, bankMSB: melodicBank, bankLSB: 0)

    }catch {
        print("An error occurred \(error)")
        return
    }

    self.sampler!.startNote(60, withVelocity: 64, onChannel: 0)
}

错误信息如下:

Unable to start playing the low note. Error code: -10867 'ç’ˇˇ'
2015-11-25 15:10:07.419 Demo[774:139850] 15:10:07.418 ERROR:    [0x19eaf4000] AVAudioUnitMIDIInstrument.mm:103: -[AVAudioUnitMIDIInstrument startNote:withVelocity:onChannel:]: error -10867
2015-11-25 15:10:07.419 Demo[774:139850] *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'error -10867'

解决方案

AVAudioEngine需要启动才能使用(error -10867表示"not initialized")。

在 Do-Catch 中添加 try engine.start()

do {
    try engine.start()
    try self.sampler!.loadSoundBankInstrumentAtURL(soundbank, program: gmHarpsichord, bankMSB: melodicBank, bankLSB: 0)
}catch {
    print("An error occurred \(error)")
    return
}

关于错误代码

在您的错误消息中有这一行:

2015-11-25 15:10:07.419 Demo[774:139850] 15:10:07.418 ERROR: [0x19eaf4000] AVAudioUnitMIDIInstrument.mm:103: -[AVAudioUnitMIDIInstrument startNote:withVelocity:onChannel:]: error -10867

我们看到错误代码的发射器是AVAudioUnitMIDIInstrument,它继承自AVAudioUnit:这些东西来自位于OS中的"AudioUnit"框架X 的系统文件夹。

AudioUnit 的错误代码在此头文件中定义:

/System/Library/Frameworks/AudioUnit.framework/Versions/A/Headers/AUComponent.h

与我们相关的部分:

CF_ENUM(OSStatus) {
    kAudioUnitErr_InvalidProperty           = -10879,
    kAudioUnitErr_InvalidParameter          = -10878,
    kAudioUnitErr_InvalidElement            = -10877,
    kAudioUnitErr_NoConnection              = -10876,
    kAudioUnitErr_FailedInitialization      = -10875,
    kAudioUnitErr_TooManyFramesToProcess    = -10874,
    kAudioUnitErr_InvalidFile               = -10871,
    kAudioUnitErr_UnknownFileType           = -10870,
    kAudioUnitErr_FileNotSpecified          = -10869,
    kAudioUnitErr_FormatNotSupported        = -10868,
    kAudioUnitErr_Uninitialized             = -10867,
    kAudioUnitErr_InvalidScope              = -10866,
    kAudioUnitErr_PropertyNotWritable       = -10865,
    kAudioUnitErr_CannotDoInCurrentContext  = -10863,
    kAudioUnitErr_InvalidPropertyValue      = -10851,
    kAudioUnitErr_PropertyNotInUse          = -10850,
    kAudioUnitErr_Initialized               = -10849,
    kAudioUnitErr_InvalidOfflineRender      = -10848,
    kAudioUnitErr_Unauthorized              = -10847,
    kAudioComponentErr_InstanceInvalidated  = -66749,
};

你仍然应该通过将采样器连接到 AVAudioEngine 图来让它工作,即调用 AVAudioEngine.attachNode。

具体失败的原因是采样器中包含的引擎引用尚未初始化。调用 attachNode 可以做到这一点,因此可以稍后调用 startEngine。