Skip to content

Commit

Permalink
Use URL in AppleSampler instead of file paths
Browse files Browse the repository at this point in the history
  • Loading branch information
aure committed Apr 19, 2022
1 parent ea59de8 commit 110d2df
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 178 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import AVFoundation

// SoundFont Support
extension AppleSampler {

internal func loadSoundFont(_ file: String, preset: Int, type: Int, in bundle: Bundle = .main) throws {
guard let url = findFileURL(file, withExtension: ["sf2", "dls"], in: bundle) else {
Log("SoundFont file not found: \(file)")
throw NSError(domain: NSURLErrorDomain, code: NSFileReadUnknownError, userInfo: nil)
}
do {
try samplerUnit.loadSoundBankInstrument(
at: url,
program: MIDIByte(preset),
bankMSB: MIDIByte(type),
bankLSB: MIDIByte(kAUSampler_DefaultBankLSB))
samplerUnit.reset()
} catch let error as NSError {
Log("Error loading SoundFont \(file)")
throw error
}
}

internal func loadSoundFont(url: URL, preset: Int, type: Int, in bundle: Bundle = .main) throws {
do {
try samplerUnit.loadSoundBankInstrument(
at: url,
program: MIDIByte(preset),
bankMSB: MIDIByte(type),
bankLSB: MIDIByte(kAUSampler_DefaultBankLSB))
samplerUnit.reset()
} catch let error as NSError {
Log("Error loading SoundFont \(url)")
throw error
}
}

/// Load a Bank from a SoundFont SF2 sample data file or a DLS file
///
/// - Parameters:
/// - file: Name of the SoundFont SF2 or the DLS file without the .sf2 / .dls extension
/// - preset: Number of the program to use
/// - bank: Number of the bank to use
/// - bundle: The bundle from which to load the file. Defaults to main bundle.
///
public func loadSoundFont(_ file: String, preset: Int, bank: Int, in bundle: Bundle = .main) throws {
guard let url = findFileURL(file, withExtension: ["sf2", "dls"], in: bundle) else {
Log("Soundfont file not found: \(file)")
throw NSError(domain: NSURLErrorDomain, code: NSFileReadUnknownError, userInfo: nil)
}
do {
var bMSB: Int
if bank <= 127 {
bMSB = kAUSampler_DefaultMelodicBankMSB
} else {
bMSB = kAUSampler_DefaultPercussionBankMSB
}
let bLSB: Int = bank % 128
try samplerUnit.loadSoundBankInstrument(
at: url,
program: MIDIByte(preset),
bankMSB: MIDIByte(bMSB),
bankLSB: MIDIByte(bLSB))
samplerUnit.reset()
} catch let error as NSError {
Log("Error loading SoundFont \(file)")
throw error
}
}

/// Load a Melodic SoundFont SF2 sample data file or a DLS file
/// - Parameters:
/// - url: Location of the file
/// - preset: Number of the program to use
/// - bundle: The bundle from which to load the file. Defaults to main bundle.
public func loadMelodicSoundFont(url: URL, preset: Int, in bundle: Bundle = .main) throws {
try loadSoundFont(url: url, preset: preset, type: kAUSampler_DefaultMelodicBankMSB, in: bundle)
}

/// Load a Percussive SoundFont SF2 sample data file or a DLS file
///
/// - Parameters:
/// - file: Name of the SoundFont SF2 or the DLS file without the .sf2 / .dls extension
/// - preset: Number of the program to use
/// - bundle: The bundle from which to load the file. Defaults to main bundle.
///
public func loadPercussiveSoundFont(_ file: String, preset: Int = 0, in bundle: Bundle = .main) throws {
try loadSoundFont(file, preset: preset, type: kAUSampler_DefaultPercussionBankMSB, in: bundle)
}
}

0 comments on commit 110d2df

Please sign in to comment.