getdunne
getdunne

Reputation: 101

Unable to load WAV to AKMidiSampler on MacOSX

I'm trying to work with AKMidiSampler on Mac OSX. I'm unable to load sample data from a file. The following code will illustrate the problem when put into the Development Playground in the AudioKit for macOS project:

import AudioKit

let sampler1 = AKMIDISampler()
sampler1.loadPath("/Users/shane/Documents/Cel1.wav")
AudioKit.output = sampler1
AudioKit.start()
sampler1.play(noteNumber: 64, velocity: 100, channel: 0)
sleep(5)
sampler1.stop(noteNumber: 64, channel: 0)

The error happens right at line 2:

AKSampler.swift:loadPath:114:Error loading audio file at /Users/shane/Documents/samples/Cel1.wav

and all I hear is the default sine tone. I've checked the obvious things, e.g. the file is quite definitely present, permissions OK (actually rwx for everybody, just in case). Earlier experiments trying to load an ESX file indicated permission errors (code -54).

Can anyone verify that AKSampler and/or AKMIDISampler actually work in OSX?

Upvotes: 2

Views: 354

Answers (2)

getdunne
getdunne

Reputation: 101

Update March 20, 2018: The AudioKit team has since made some additions to the AKSampler/AKMIDISampler API to allow loading sample files from arbitrary file paths.

I have been invited to join the AudioKit core team, and have written a new sampler engine from scratch. In the next AudioKit release (expected within a day or two), the name "AKSampler" will refer to this newer code, but users should be aware that it is not a direct replacement for the older AKSampler, which will be renamed "AKAppleSampler" to reflect the fact that it is a wrapper for Apple's built-in "AUSampler" audio unit. The AKMIDISampler class (the one most people actually use) will remain unchanged as a wrapper for AKAppleSampler.

Upvotes: 3

Eric George
Eric George

Reputation: 246

In the AudioKit source code, loadPath(_ :) calls loadInstrument(_ : type:) which looks in the bundle for your file. See a copy of the sources here:

@objc open func loadPath(_ filePath: String) {
    do {
        try samplerUnit.loadInstrument(at: URL(fileURLWithPath: filePath))
    } catch {
        AKLog("Error loading audio file at \(filePath)")
    }
}

internal func loadInstrument(_ file: String, type: String) throws {
    //AKLog("filename is \(file)")
    guard let url = Bundle.main.url(forResource: file, withExtension: type) else {
        fatalError("file not found.")
    }
    do {
        try samplerUnit.loadInstrument(at: url)
    } catch let error as NSError {
        AKLog("Error loading instrument resource \(file)")
        throw error
    }
}

So you need to put your audio file in the app's or playground's bundle for this to call for this to work.

Upvotes: 1

Related Questions