質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1251閲覧

Value of type '' has no member ''のエラーが発生します

ICEr

総合スコア11

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/02/25 17:59

前提・実現したいこと

swiftでFFT解析と処理を行うプログラムをつくって居たのですが、エラーが発生し、どこを修正すれば良いのかわからないです

発生している問題・エラーメッセージ

Value of type 'UnsafePointer<Float>' has no member 'bindMemory'

該当のソースコード

Swift4

1 2import Accelerate 3 4import Foundation 5 6import AVFoundation 7 8import AudioUnit 9 10 11class MyAudioRecorder: NSObject { 12 13 var level: Float = 0.0 14 15 var frameCount: UInt32 = 0 16 17 18 private var _audioUnit: AudioUnit? 19 20 private var _abl: AudioBufferList? 21 22 private let kInputBus: UInt32 = 1 23 24 private let kNumberOfChannels: Int = 1 25 26 27 func start() { 28 29 // AudioSession セットアップ 30 31 do { 32 33 let audioSession = AVAudioSession.sharedInstance() 34 35 try audioSession.setCategory(.record, mode: .default) 36 37 try audioSession.setActive(true) 38 39 } catch { 40 41 } 42 43 // CoreAudio セットアップ 44 45 46 var componentDesc: AudioComponentDescription 47 48 = AudioComponentDescription( 49 50 componentType: OSType(kAudioUnitType_Output), 51 52 componentSubType: OSType(kAudioUnitSubType_RemoteIO), 53 54 componentManufacturer: OSType(kAudioUnitManufacturer_Apple), 55 56 componentFlags: UInt32(0), 57 58 componentFlagsMask: UInt32(0) ) 59 60 61 let component: AudioComponent! = AudioComponentFindNext(nil, &componentDesc) 62 63 var tau: AudioUnit? 64 65 AudioComponentInstanceNew(component, &tau) 66 67 _audioUnit = tau 68 69 70 guard let au = _audioUnit else { 71 72 return 73 74 } 75 76 // RemoteIO のマイクを有効にする 77 78 var enable: UInt32 = 1 79 80 AudioUnitSetProperty(au, 81 82 kAudioOutputUnitProperty_EnableIO, 83 84 kAudioUnitScope_Input, 85 86 kInputBus, 87 88 &enable, 89 90 UInt32(MemoryLayout<UInt32>.size)) 91 92 93 // マイクから取り出すデータフォーマット 94 95 // 32bit float, linear PCM 96 97 guard let fmt = AVAudioFormat(standardFormatWithSampleRate: 44100, 98 99 channels: UInt32(kNumberOfChannels)) else { 100 101 return 102 103 } 104 105 106 // RemoteIO のマイクバスから取り出すフォーマットを設定 107 108 AudioUnitSetProperty(au, 109 110 kAudioUnitProperty_StreamFormat, 111 112 kAudioUnitScope_Output, 113 114 kInputBus, 115 116 fmt.streamDescription, 117 118 UInt32(MemoryLayout<AudioStreamBasicDescription>.size)) 119 120 121 // AudioUnit に録音コールバックを設定 122 123 var inputCallbackStruct 124 125 = AURenderCallbackStruct(inputProc: recordingCallback, 126 127 inputProcRefCon: 128 129 UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())) 130 131 AudioUnitSetProperty(au, 132 133 AudioUnitPropertyID(kAudioOutputUnitProperty_SetInputCallback), 134 135 AudioUnitScope(kAudioUnitScope_Global), 136 137 kInputBus, 138 139 &inputCallbackStruct, 140 141 UInt32(MemoryLayout<AURenderCallbackStruct>.size)) 142 143 144 // データ取り出し時に使う AudioBufferListの設定 145 146 _abl = AudioBufferList( 147 148 mNumberBuffers: 1, 149 150 mBuffers: AudioBuffer( 151 152 mNumberChannels: fmt.channelCount, 153 154 mDataByteSize: fmt.streamDescription.pointee.mBytesPerFrame, 155 156 mData: nil)) 157 158 159 160 AudioUnitInitialize(au) 161 162 AudioOutputUnitStart(au) 163 164 } 165 166 167 let recordingCallback: AURenderCallback = { ( 168 169 inRefCon, 170 171 ioActionFlags, 172 173 inTimeStamp, 174 175 inBusNumber, 176 177 frameCount, 178 179 ioData ) -> OSStatus in 180 181 182 let audioObject = unsafeBitCast(inRefCon, to: MyAudioRecorder.self) 183 184 185 186 if let au = audioObject._audioUnit { 187 188 // マイクから取得したデータを取り出す 189 190 AudioUnitRender(audioObject._audioUnit!, 191 192 ioActionFlags, 193 194 inTimeStamp, 195 196 inBusNumber, 197 198 frameCount, 199 200 &audioObject._abl!) 201 202 } 203 204 audioObject.frameCount = frameCount 205 206 let frameNumber: Int = Int(frameCount) 207 208 let log2n : vDSP_Length=vDSP_Length(log2(Double(frameCount))) 209 210 let inputDataPtr = UnsafeMutableAudioBufferListPointer(&audioObject._abl!) 211 212 let mBuffers: AudioBuffer = inputDataPtr[0] 213 214 let bufferPointer = UnsafeMutableRawPointer(mBuffers.mData) 215 216 if let bptr = bufferPointer { 217 218 let dataArray = bptr.assumingMemoryBound(to: Float.self) 219 220 var sum:Float = 0.0 221 222 if frameCount > 0 { 223 224 for i in 0 ..< Int(frameCount) { 225 226 sum += (dataArray[i]) 227 228 print(vDSP_create_fftsetup(log2n, Int32(kFFTRadix2))) 229 230 } 231 232 233 var inputData = [Float](repeating: 0.0, count: frameNumber) 234 235 var fftObj : FFTSetup = vDSP_create_fftsetup(log2n, Int32(kFFTRadix2))! 236 237 // 窓関数 238 239 var windowData = [Float](repeating: 0.0,count: frameNumber) 240 241 var windowOutput = [Float](repeating: 0.0,count: frameNumber) 242 243 vDSP_hann_window(&windowData, vDSP_Length(frameNumber), Int32(0)) 244 vDSP_vmul(&inputData, 1, &windowData, 1, &windowOutput, 1, vDSP_Length(frameNumber)) 245 246 // Complex 247 var imaginaryData = [Float](repeating: 0.0,count: frameNumber) 248 var dspSplit = DSPSplitComplex(realp: &windowOutput, imagp: &imaginaryData) 249 var samples: [Float] 250 var ctozinput = UnsafePointer(inputData).bindMemory(to: DSPComplex.self, capacity: frameNumber) 251 vDSP_ctoz(ctozinput, 2, &dspSplit, 1, vDSP_Length(frameNumber / 2)) 252 253 // FFT解析 254 vDSP_fft_zrip(fftObj, &dspSplit, 1, log2n, Int32(FFT_FORWARD)) 255 vDSP_destroy_fftsetup(fftObj) 256 257 // 結果出力 258 for i in 0 ..< Int(frameNumber) / 2 { 259 var real = dspSplit.realp[i]; 260 var imag = dspSplit.imagp[i]; 261 var distance = sqrt(pow(real, 2) + pow(imag, 2)) 262 263 print("[(i)], (distance)") 264 } 265 266 audioObject.level = 0.5+sum / Float(frameCount)*10 267 } 268 } 269 return 0 270 } 271 272} 273`` 274 275### 276 277### 補足情報(FW/ツールのバージョンなど) 278Swift4

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

UnsafePointerを、UnsafeRawPointerになおしたらエラー消えました

投稿2019/02/25 18:10

ICEr

総合スコア11

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問