前提・実現したいこと
SwiftでPlayGroundを使用し、wavファイルをフーリエ変換したいと考えています。
値がうまく取得出来ず、swift でwavファイルの扱いまた、フーリエ変換についてお分かりでしたら教えていただきたいです
発生している問題・エラーメッセージ
Value of optional type 'AVAudioPCMBuffer?' must be unwrapped to a value of type 'AVAudioPCMBuffer' Value of optional type 'AVAudioPCMBuffer?' must be unwrapped to refer to member 'floatChannelData' of wrapped base type 'AVAudioPCMBuffer'
該当のソースコード
import AVFoundation import AudioToolbox import Accelerate var soundURL = Bundle.main.url(forResource: "1", withExtension: "wav") let audioFile = try! AVAudioFile(forReading: soundURL!) let frameCount = UInt32(audioFile.length) let buffer = AVAudioPCMBuffer(pcmFormat: audioFile.processingFormat, frameCapacity: frameCount) do { try audioFile.readIntoBuffer(buffer, frameCount:frameCount) } catch { } let log2n = UInt(round(log2(Double(frameCount)))) let bufferSize = Int(1 << log2n) let fftSetup = vDSP_create_fftsetup(log2n, Int32(kFFTRadix2)) var realp = [Float](repeating: 0,count: bufferSize/2) var imagp = [Float](repeating: 0,count: bufferSize/2) var output = DSPSplitComplex(realp: &realp, imagp: &imagp) vDSP_ctoz(UnsafePointer<DSPComplex>(buffer.floatChannelData.memory), 2, &output, 1, UInt(bufferSize / 2)) vDSP_fft_zrip(fftSetup!, &output, 1, log2n, Int32(FFT_FORWARD)) var fft = [Float](repeating: 0.0,count:Int(bufferSize / 2)) let bufferOver2: vDSP_Length = vDSP_Length(bufferSize / 2) vDSP_zvmags(&output, 1, &fft, 1, bufferOver2)
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー