Ben Spector
Ben Spector

Reputation: 329

Can't load .wav file to the new AKSampler

I'm trying to replace AKMidiSampler and AKPlayer with the new AKSampler, but for some reason the new sampler doesn't play the AKAudioFiles that the old ones did.

Here is a small code example that uses all three samplers: AKMidiSampler, AKPlayer, and the new AKSampler. All loaded with the same AKAudioFile. If ran the way it is, no sound is played.

When uncommenting

player.play()

or the

try auSampler.play(noteNumber: 60, velocity: 127, channel: 0)

There will be sound.

My guess is that I'm not using the AKSampleDescriptor properly, I use it as the example here.

import Foundation
import AudioKit

class Conductor {
    var sampler   = AKSampler()
    var auSampler = AKMIDISampler()
    var player    = AKPlayer()
    var mixer     = AKMixer()

    init() {
        do {
            let akfile = try AKAudioFile(readFileName: "Drums.wav")



            let descriptor = AKSampleDescriptor(noteNumber: 60,
                                                noteFrequency: Float(AKPolyphonicNode.tuningTable.frequency(forNoteNumber: 60)),
                                                minimumNoteNumber: 0,
                                                maximumNoteNumber: 127,
                                                minimumVelocity: 0,
                                                maximumVelocity: 127,
                                                isLooping: true,
                                                loopStartPoint: 0.0,
                                                loopEndPoint: 1.0,
                                                startPoint: 0.0,
                                                endPoint: 0.0)

            sampler.loadAKAudioFile(from: descriptor, file: akfile)
            try auSampler.loadAudioFile(akfile)
            player = akfile.player

            [player, sampler, auSampler] >>> mixer
            AudioKit.output = mixer
            try AudioKit.start()


//            player.play()
//            try auSampler.play(noteNumber: 60, velocity: 127, channel: 0)
            sampler.play(noteNumber: 60, velocity: 127)



        }
        catch {
            print("ehhhh...")
        }

    }


}

Upvotes: 2

Views: 252

Answers (1)

Ben Spector
Ben Spector

Reputation: 329

The solution was simple, I just had to add the line:

sampler.buildKeyMap()

after loading the samples. Now everything is working as expected.

Upvotes: 3

Related Questions