jscalo / tempi-fft

Real-time audio input and FFT written entirely in Swift for iOS.
253 stars 47 forks source link

TempiFFT fix #7

Open slmille4 opened 7 years ago

slmille4 commented 7 years ago

On line 166 in TempiFFT.swift, the call should be

analysisBuffer.withUnsafeBufferPointer { $0.baseAddress!.withMemoryRebound(to: DSPComplex.self, capacity: analysisBuffer.count) { vDSP_ctoz($0, 2, &complexBuffer, 1, UInt(halfSize)) } }

that should help get vDSP_ctoz working again

jscalo commented 7 years ago

Thanks! But I still get an error with that code:

Cannot convert value of type 'UnsafePointer<_>' to expected argument type 'UnsafePointer'

slmille4 commented 7 years ago

It looks like the complexBuffer parameter needs to be DSPSplitComplex instead of DSPSplitComplex!

jscalo commented 7 years ago

complexBuffer is a class property that can't be initialized (sensibly) at init time, so it must be either DSPSplitComplex? or DSPSplitComplex!.

Crittar commented 7 years ago

Cannot convert value of type 'UnsafePointer<_>' to expected argument type 'UnsafePointer'

The error goes away if you put the reference to 'complexBuffer' in parenthesis.

analysisBuffer.withUnsafeBufferPointer { $0.baseAddress!.withMemoryRebound(to: DSPComplex.self, capacity: analysisBuffer.count) { vDSP_ctoz($0, 2, &(complexBuffer!), 1, UInt(halfSize)) } }

Unfortunately, after trying that, I was getting runtime crashes after a few cycles through audio rendering.

Through a bit of trial and error, I found out the runtime errors were occurring from instantiating DSPSplitComplex with locally instantiated Float arrays at the init call. I was able to make it work by declaring the empty real and imaginary Float arrays as private variables in the class instance. Since that all happens at the init call, I also changed to the 'complexBuffer' from 'DSPSplitComplex!' to 'DSPSplitComplex'.

Tested on Swift 4.0, XCode 9.0 beta 4

javadba commented 4 years ago

I do not see a historical PR for this - can you create one for the benefit of other users of this project? Thx!