加载声音失败并且“无法找到补丁0 bank 0x0 / 0' 查看完整的结果路径时,声音的URL显示正确。
AVAudioUnitSampler
继承自AVAudioUnitMidiInstrument
AVAudioUnitSampler
没有自己的初始化程序。
AVAudioUnitMidiInstrument
有一个AudioComponentDescription
使用任何一个导致相同的错误。
var error:NSError?
let soundBankUrl:NSURL = NSURL(fileURLWithPath:
NSBundle.mainBundle().pathForResource("FluidR3 GM2-2", ofType: "SF2"))
var audioEngine:AVAudioEngine = AVAudioEngine()
// sampler
var audioCompDesc = AudioComponentDescription(
componentType: OSType(kAudioUnitType_MusicDevice),
componentSubType: OSType(kAudioUnitSubType_Sampler),
componentManufacturer: OSType(kAudioUnitManufacturer_Apple),
componentFlags: 0, componentFlagsMask: 0)
var sampler:AVAudioUnitSampler = AVAudioUnitSampler(audioCompDesc)
// var sampler:AVAudioUnitSampler = AVAudioUnitSampler()
let loaded = sampler.loadSoundBankAtURL(soundBankUrl, error: &error)
结果:
Unable to find patch 0 bank 0x0/0
使用较低级别的API,可以在音频设备上设置属性。
AUSamplerBankPresetData bpdata;
bpdata.bankURL = (__bridge CFURLRef) presetURL;
bpdata.bankMSB = kAUSampler_DefaultMelodicBankMSB;
bpdata.bankLSB = kAUSampler_DefaultBankLSB;
bpdata.presetID = (UInt8) patch;
return [self loadSynthFromPresetURL:(NSURL*)presetURL
withPatch:(int) patch
audioUnit: (AudioUnit) audioUnit
samplerBankPesetData: (AUSamplerBankPresetData) bpdata];
result = AudioUnitSetProperty(audioUnit,
kAUSamplerProperty_LoadPresetFromBank, kAudioUnitScope_Global,
0, &samplerBankPresetData, sizeof(samplerBankPresetData));
答案 0 :(得分:1)
A)Xcode 6 beta的更高版本3版增加了额外的方法,无需桥接。
//
// ViewController.swift
// SwiftAudio
//
// Created by Robert Buskens on 2014-06-06.
// Copyright (c) 2014 Buskens & Associates Inc. All rights reserved.
//
import UIKit
import AVFoundation
import AudioUnit
class ViewController: UIViewController {
var engine: AVAudioEngine
var sampler:AVAudioUnitSampler
init(coder aDecoder: NSCoder!) {
var retVal:Bool = false
var error:NSErrorPointer = nil
engine = AVAudioEngine()
sampler = AVAudioUnitSampler()
engine.attachNode(sampler)
engine.connect(sampler, to: engine.outputNode, format: nil)
var url = NSBundle.mainBundle().URLForResource("Framus_western", withExtension: "sf2")
retVal = engine.startAndReturnError(error)
var r:Bool = sampler.loadSoundBankInstrumentAtURL(url, program: 0, bankMSB: 0x79, bankLSB: 0, error: error)
engine.startAndReturnError(error)
println("engine start returned: \(retVal)")
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func playNote(sender: UIButton) {
sampler.startNote(45, withVelocity: 80, onChannel: 1)
}
}
比没有AVAudioEngine简单得多。