AudioKit - Does AKSampler pitch or do I need to add multiple sample files?

Viewed 496

I am just getting started with AudioKit. I want to keep it very simple. I want to make a few UIButtons (C,D,E,F,...) and then have them play the corresponding piano sample. However I don't understand how to correctly prepare the sample file(s).

I found this example:

let sampler = AKSampler()
sampler.loadWav("Sounds/fmpia1")
let ampedSampler = AKBooster(sampler, gain: 3.0)
var delay  = AKDelay(ampedSampler)
delay.time = pulse * 1.5
delay.dryWetMix = 0.0
delay.feedback = 0.0

let cMajor = [72, 74, 76, 77, 79, 81, 83, 84]

var mix = AKMixer(delay)
var reverb = AKReverb(mix)

AudioKit.output = reverb
AudioKit.start()

for note in cMajor {
    sampler.playNote(note)
    sleep(1)
}

What I understand: Loading the sampler and the numbers (72, 74, ...) are the MIDI signals for the notes.

However: How does the sampler know what to play? Does the sample "fmpia1" contain all notes? Is it just one sample, but the AKSampler pitches them automatically? But then how does AKSampler know what note the sample is? Shouldn't AKSampler be informed that the sample in the file is, let's say a F# ? So he can pitch accordingly?

I am very confused about this. I hope you can understand what my problem is.

Thanks in advance for any help!

1 Answers

AKSampler (and AKMIDISampler) use Apple's AVAudioUnitSampler internally. It is AVAudioUnitSampler that is doing the playback and pitching your root note. If you look at the documentation for AVAudioUnitSampler loadAudioFiles(at:) (https://developer.apple.com/documentation/avfoundation/avaudiounitsampler/1388631-loadaudiofiles), you will see that it creates a new zone for each audio file and uses the metadata in the audio file to try and map it correctly. It can also take a shortcut if the root note is in the file name (ie - ViolinC4).

So, in direct response to your questions:

  1. fmpia1 is a single audio file (pitch). It gets mapped internally to a root note (maybe C4 if not specified - needs verified).
  2. when you send in a midi event with a specific note number, the sampler will pitch your audio file to that note and play it back. (Here is a handy map of midi to notes: https://medium.com/@gmcerveny/midi-note-number-chart-for-ios-music-apps-b3c01df3cb19)
  3. Yes, if you know the root note (pitch of the file), specifying as I said above will result in accurate playback.
Related