以下代码配置 iOS 音频引擎以创建使用一种声音字体设置的采样器(为了讨论简单)。然后我使用这些样本来播放 MIDI 音符,一切都很好,直到我尝试引入一系列标准效果(失真 -> 延迟 -> 混响)。
如果采样器的输出只是直接连接到音频引擎 MainMixerNode 它就可以工作。如果我将采样器连接到失真效果(链中的第一个效果),那么一旦我尝试播放第一个 MIDI 音符,我就会收到错误“AVAudioUnitMIDIInstrument.mm:103: -[AVAudioUnitMIDIInstrument startNote:withVelocity:onChannel:]:错误-10867"
注意:我在 Mac 上使用 Xamarin 进行编码,因此以下内容可能看起来不熟悉,但我希望代码可读性足以帮助您。
问题只是我在初始化我的效果/或其链时哪里出错了?
在此先感谢,克里斯
private void InitAudioEngine(NSUrl sampleFileUrl)
{
AVAudioSession.SharedInstance().Init ();
NSError sessionErrorCode;
sessionErrorCode = AVAudioSession.SharedInstance ().SetCategory (AVAudioSessionCategory.Playback);
if(sessionErrorCode != null)
Logger.Write ("Failed to set AudioSession category");
sessionErrorCode = AVAudioSession.SharedInstance ().SetActive (true);
if(sessionErrorCode != null)
Logger.Write ("Failed to activate AudioSession");
Logger.Write ("Instantiate Audio Engine");
_audioEngine = new AVAudioEngine ();
_samplers = new List<AVAudioUnitSampler> ();
var distortion = new AVAudioUnitDistortion ();
var delay = new AVAudioUnitDelay ();
var reverb = new AVAudioUnitReverb ();
distortion.Init ();
delay.Init ();
reverb.Init ();
distortion.LoadFactoryPreset (AVAudioUnitDistortionPreset.SpeechGoldenPi);
reverb.LoadFactoryPreset (AVAudioUnitReverbPreset.LargeHall2);
delay.DelayTime = 300;
delay.WetDryMix = 30;
delay.Feedback = 30;
_audioEngine.AttachNode (distortion);
_audioEngine.AttachNode (delay);
_audioEngine.AttachNode (reverb);
_audioEngine.Connect (distortion, delay, delay.GetBusOutputFormat (0));
_audioEngine.Connect (delay, reverb, reverb.GetBusOutputFormat (0));
_audioEngine.Connect (reverb, _audioEngine.MainMixerNode, _audioEngine.MainMixerNode.GetBusOutputFormat (0));
for (int index = 0; index < 15; index++)
{
var sampler = new AVAudioUnitSampler ();
sampler.Init ();
_samplers.Add (sampler);
_audioEngine.AttachNode (sampler);
_audioEngine.Connect (sampler, distortion, distortion.GetBusOutputFormat(0));
}
// Connect all the samplers to a defined SoundFont
ConnectSoundbank (sampleFileUrl);
NSError engineErrorCode;
_audioEngine.StartAndReturnError (out engineErrorCode);
if(engineErrorCode != null)
Logger.Write ("Failed to start AudioEngine after samplers attached");
}
// ...
// Later code
// Play a MIDI note on one of the samplers configured above
//
_samplers[0].StartNote(58,127,0); // Crashes with error -10867 (uninitialised)
//...etc...