-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use URL in AppleSampler instead of file paths
- Loading branch information
Showing
2 changed files
with
186 additions
and
178 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
Sources/AudioKit/Nodes/Playback/Apple Sampler/AppleSampler+SoundFonts.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.