1 year ago

#213209

test-img

Alfab

AudioKit 5.2 Migration: AKSampler() to Sampler() latency when loading audioFiles

I am migrating my AudioKit-Code to AudioKit 5.2 and I am having problems with the AK5 Sampler() which I can not solve by myself. In AK previously it was AKSampler() and it had a function called:

loadAKAudioFile(from: AKSampleDescriptor, file: AKAudioFile)

Since Sampler() does not have that function, I took a piece of code from the source Sampler.swift which does the work, but it is extremely slow, it takes ages to load and map the 12 different small samples that I map on the note numbers from 60 to 71:

internal func loadAudioFile(from sampleDescriptor: SampleDescriptor, file: AVAudioFile) {
    guard let floatChannelData = file.toFloatChannelData() else { return }

    let sampleRate = Float(file.fileFormat.sampleRate)
    let sampleCount = Int32(file.length)
    let channelCount = Int32(file.fileFormat.channelCount)
    var flattened = Array(floatChannelData.joined())

    flattened.withUnsafeMutableBufferPointer { data in

        var descriptor = SampleDataDescriptor(sampleDescriptor: sampleDescriptor,
                                              sampleRate: sampleRate,
                                              isInterleaved: false,
                                              channelCount: channelCount,
                                              sampleCount: sampleCount,
                                              data: data.baseAddress)

        akSamplerLoadData(au.dsp, &descriptor)
    }
}

The AKSampler() did not have any noticable latency when doing the work but with Sampler() it takes more than a second to load a sample. Obviously AKSampler() worked asynchronously. I am new to swift and audio so I have no idea how to make Sampler() work asychronously.

Would be great to get some bits of code that could help it, thank's

audiokit

0 Answers

Your Answer

Accepted video resources