0

I'm a swift learner and currently working on an Xcode Playground project. Now I'm facing a problem and really need some help here. Tracking the real-time frequency and the amplitude of selected music is the core part of my Playground.

Is there any built-in framework in Xcode Playground that can track the frequency and amplitude of mp3 files? Considering AudioKit cannot import into Playground individually, I wonder which framework I should use (CoreAudio, AVFoundation, AudioUnit or something else?).

I only want to get the real-time numerical value of frequency and amplitude in Swift Playground, nothing more.

Appreciate every answer, it would be awesome if anyone can give me some sample code.

Jack Stark
  • 57
  • 7
  • There's nothing built in. You need an FFT to do what you're trying to do, and while there are FFT functions in vDSP (part of Accelerate), you kind of need to know what you're doing to use those. I'd poke around in Cocoapods or GitHub. You're not going to find what you want in AVFoundation (or the others, but as a beginner, you should mostly avoid things lower than AVFoundation). If you do want to dive into the FFT world, see https://developer.apple.com/documentation/accelerate/vdsp/fast_fourier_transforms – Rob Napier Feb 23 '19 at 14:14
  • so there is no instant method existed to get the frequency of music? – Jack Stark Feb 23 '19 at 16:57
  • 1
    I don't know what "instant" here means (or even what "the frequency of music" means; audio has a frequency *distribution* that evolves over time, not a "real-time frequency"). But there are no built-in audio analysis functions. There are vector-math functions (such as the FFT functions), from which you can build audio analysis. – Rob Napier Feb 23 '19 at 17:07
  • No. Absolutely there isn't. In fact even the concept of that is deeply ambiguous. – marko Feb 23 '19 at 23:42
  • Appreciate your suggestion. But I wonder if there are any other Musical characteristics I could track in an easier way, using swift in playground? – Jack Stark Feb 25 '19 at 04:27

1 Answers1

1

Have a look at this article from Apple.

Aside from the collection of audio samples, you can use the Acceleratre framework.

At a high level, you're looking at applying a Fourier transform to a time series and then geting the ampliude.

import Accelerate

var timeDomainBuffer = [Float](repeating: 0, count: sampleCount)
var frequencyDomainBuffer = [Float](repeating: 0, count: sampleCount)

var fft = let forwardDCT = vDSP.DCT(count: 1024, transformType: .II)!
fft(timeDomainBuffer, result: &frequencyDomainBuffer)

vDSP.absolute(frequencyDomainBuffer, result: &frequencyDomainBuffer)
Scott McKenzie
  • 16,052
  • 8
  • 45
  • 70