Post

Replies

Boosts

Views

Activity

simd_quatd from to. Difference between vectors by quaternion
let dir = Vector3D(x: 0.21074744760506547, y: 0.38871720406617527, z: -1.0819520200824684).uniformlyScaled(by: 1) let vd = Vector3D(vector: [0,0,-1]) let rot = Rotation3D(position: Point3D(x: 0, y: 0, z: 0),target: Point3D(dir)) print(vd.rotated(by: simd_quatd(from: vd.normalized.vector , to: dir.normalized.vector))) let r = simd.simd_quaternion(simd_float3(vd), simd_float3(dir)) print(vd.rotated(by: Rotation3D(simd_quatd(from: vd.normalized.vector , to: dir.normalized.vector)))) result (x: 0.18030816736048502, y: 0.33257288514358785, z: -0.9256804204747842) (x: 0.18030816736048502, y: 0.33257288514358785, z: -0.9256804204747842) (x: 0.1439359315016178, y: 0.2654854115377888, z: -0.953309993592521) (x: 0.0, y: 0.0, z: 1.0) Expect (x: 0.21074744760506547, y: 0.38871720406617527, z: -1.0819520200824684) Why it is not follow expect value?
0
0
527
Nov ’23
How to Playing A Generated Sine Wave in Swift
AVFoundation or AVAudioPlayer I use this code but does not work. Compile is ok, not throw error, manner mode off, but it does not play tone import Foundation import AudioUnit import AVFoundation final class ToneOutputUnit: NSObject { var auAudioUnit: AUAudioUnit! = nil // placeholder for RemoteIO Audio Unit var avActive = false // AVAudioSession active flag var audioRunning = false // RemoteIO Audio Unit running flag var sampleRate : Double = 44100.0 // typical audio sample rate var f0 = 880.0 // default frequency of tone: 'A' above Concert A var v0 = 16383.0 // default volume of tone: half full scale var toneCount : Int32 = 1 // number of samples of tone to play. 0 for silence private var phY = 0.0 // save phase of sine wave to prevent clicking private var interrupted = false // for restart from audio interruption notification func setFrequency(freq : Double) { // audio frequencies below 500 Hz may be f0 = freq // hard to hear from a tiny iPhone speaker. } func setToneVolume(vol : Double) { // 0.0 to 1.0 v0 = vol * 32766.0 } func setToneTime(t : Double) { toneCount = Int32(t * sampleRate); } func enableSpeaker() { if audioRunning { print("returned") return } // return if RemoteIO is already running do { // not running, so start hardware let audioComponentDescription = AudioComponentDescription( componentType: kAudioUnitType_Output, componentSubType: kAudioUnitSubType_RemoteIO, componentManufacturer: kAudioUnitManufacturer_Apple, componentFlags: 0, componentFlagsMask: 0 ) if (auAudioUnit == nil) { try auAudioUnit = AUAudioUnit(componentDescription: audioComponentDescription) let bus0 = auAudioUnit.inputBusses[0] let audioFormat = AVAudioFormat( commonFormat: AVAudioCommonFormat.pcmFormatInt16, // short int samples sampleRate: Double(sampleRate), channels:1, interleaved: false ) // interleaved stereo try bus0.setFormat(audioFormat ?? AVAudioFormat()) // for speaker bus auAudioUnit.outputProvider = { ( // AURenderPullInputBlock? actionFlags, timestamp, frameCount, inputBusNumber, inputDataList ) -> AUAudioUnitStatus in self.fillSpeakerBuffer(inputDataList: inputDataList, frameCount: frameCount) return(0) } } auAudioUnit.isOutputEnabled = true toneCount = 0 try auAudioUnit.allocateRenderResources() // v2 AudioUnitInitialize() try auAudioUnit.startHardware() // v2 AudioOutputUnitStart() audioRunning = true } catch /* let error as NSError */ { print("error 2 \(error)") } } // helper functions private func fillSpeakerBuffer( // process RemoteIO Buffer for output inputDataList : UnsafeMutablePointer<AudioBufferList>, frameCount : UInt32 ) { let inputDataPtr = UnsafeMutableAudioBufferListPointer(inputDataList) let nBuffers = inputDataPtr.count if (nBuffers > 0) { let mBuffers : AudioBuffer = inputDataPtr[0] let count = Int(frameCount) // Speaker Output == play tone at frequency f0 if ( self.v0 > 0) && (self.toneCount > 0 ) { // audioStalled = false var v = self.v0 ; if v > 32767 { v = 32767 } let sz = Int(mBuffers.mDataByteSize) var a = self.phY // capture from object for use inside block let d = 2.0 * Double.pi * self.f0 / self.sampleRate // phase delta let bufferPointer = UnsafeMutableRawPointer(mBuffers.mData) if var bptr = bufferPointer { for i in 0..<(count) { let u = sin(a) // create a sinewave a += d ; if (a > 2.0 * Double.pi) { a -= 2.0 * Double.pi } let x = Int16(v * u + 0.5) // scale & round if (i < (sz / 2)) { bptr.assumingMemoryBound(to: Int16.self).pointee = x bptr += 2 // increment by 2 bytes for next Int16 item bptr.assumingMemoryBound(to: Int16.self).pointee = x bptr += 2 // stereo, so fill both Left & Right channels } } } self.phY = a // save sinewave phase self.toneCount -= Int32(frameCount) // decrement time remaining } else { // audioStalled = true memset(mBuffers.mData, 0, Int(mBuffers.mDataByteSize)) // silence } } } func stop() { if (audioRunning) { auAudioUnit.stopHardware() audioRunning = false } } }
0
0
1.5k
Dec ’21
simd_quatd from to. Difference between vectors by quaternion
let dir = Vector3D(x: 0.21074744760506547, y: 0.38871720406617527, z: -1.0819520200824684).uniformlyScaled(by: 1) let vd = Vector3D(vector: [0,0,-1]) let rot = Rotation3D(position: Point3D(x: 0, y: 0, z: 0),target: Point3D(dir)) print(vd.rotated(by: simd_quatd(from: vd.normalized.vector , to: dir.normalized.vector))) let r = simd.simd_quaternion(simd_float3(vd), simd_float3(dir)) print(vd.rotated(by: Rotation3D(simd_quatd(from: vd.normalized.vector , to: dir.normalized.vector)))) result (x: 0.18030816736048502, y: 0.33257288514358785, z: -0.9256804204747842) (x: 0.18030816736048502, y: 0.33257288514358785, z: -0.9256804204747842) (x: 0.1439359315016178, y: 0.2654854115377888, z: -0.953309993592521) (x: 0.0, y: 0.0, z: 1.0) Expect (x: 0.21074744760506547, y: 0.38871720406617527, z: -1.0819520200824684) Why it is not follow expect value?
Replies
0
Boosts
0
Views
527
Activity
Nov ’23
How to Playing A Generated Sine Wave in Swift
AVFoundation or AVAudioPlayer I use this code but does not work. Compile is ok, not throw error, manner mode off, but it does not play tone import Foundation import AudioUnit import AVFoundation final class ToneOutputUnit: NSObject { var auAudioUnit: AUAudioUnit! = nil // placeholder for RemoteIO Audio Unit var avActive = false // AVAudioSession active flag var audioRunning = false // RemoteIO Audio Unit running flag var sampleRate : Double = 44100.0 // typical audio sample rate var f0 = 880.0 // default frequency of tone: 'A' above Concert A var v0 = 16383.0 // default volume of tone: half full scale var toneCount : Int32 = 1 // number of samples of tone to play. 0 for silence private var phY = 0.0 // save phase of sine wave to prevent clicking private var interrupted = false // for restart from audio interruption notification func setFrequency(freq : Double) { // audio frequencies below 500 Hz may be f0 = freq // hard to hear from a tiny iPhone speaker. } func setToneVolume(vol : Double) { // 0.0 to 1.0 v0 = vol * 32766.0 } func setToneTime(t : Double) { toneCount = Int32(t * sampleRate); } func enableSpeaker() { if audioRunning { print("returned") return } // return if RemoteIO is already running do { // not running, so start hardware let audioComponentDescription = AudioComponentDescription( componentType: kAudioUnitType_Output, componentSubType: kAudioUnitSubType_RemoteIO, componentManufacturer: kAudioUnitManufacturer_Apple, componentFlags: 0, componentFlagsMask: 0 ) if (auAudioUnit == nil) { try auAudioUnit = AUAudioUnit(componentDescription: audioComponentDescription) let bus0 = auAudioUnit.inputBusses[0] let audioFormat = AVAudioFormat( commonFormat: AVAudioCommonFormat.pcmFormatInt16, // short int samples sampleRate: Double(sampleRate), channels:1, interleaved: false ) // interleaved stereo try bus0.setFormat(audioFormat ?? AVAudioFormat()) // for speaker bus auAudioUnit.outputProvider = { ( // AURenderPullInputBlock? actionFlags, timestamp, frameCount, inputBusNumber, inputDataList ) -> AUAudioUnitStatus in self.fillSpeakerBuffer(inputDataList: inputDataList, frameCount: frameCount) return(0) } } auAudioUnit.isOutputEnabled = true toneCount = 0 try auAudioUnit.allocateRenderResources() // v2 AudioUnitInitialize() try auAudioUnit.startHardware() // v2 AudioOutputUnitStart() audioRunning = true } catch /* let error as NSError */ { print("error 2 \(error)") } } // helper functions private func fillSpeakerBuffer( // process RemoteIO Buffer for output inputDataList : UnsafeMutablePointer<AudioBufferList>, frameCount : UInt32 ) { let inputDataPtr = UnsafeMutableAudioBufferListPointer(inputDataList) let nBuffers = inputDataPtr.count if (nBuffers > 0) { let mBuffers : AudioBuffer = inputDataPtr[0] let count = Int(frameCount) // Speaker Output == play tone at frequency f0 if ( self.v0 > 0) && (self.toneCount > 0 ) { // audioStalled = false var v = self.v0 ; if v > 32767 { v = 32767 } let sz = Int(mBuffers.mDataByteSize) var a = self.phY // capture from object for use inside block let d = 2.0 * Double.pi * self.f0 / self.sampleRate // phase delta let bufferPointer = UnsafeMutableRawPointer(mBuffers.mData) if var bptr = bufferPointer { for i in 0..<(count) { let u = sin(a) // create a sinewave a += d ; if (a > 2.0 * Double.pi) { a -= 2.0 * Double.pi } let x = Int16(v * u + 0.5) // scale & round if (i < (sz / 2)) { bptr.assumingMemoryBound(to: Int16.self).pointee = x bptr += 2 // increment by 2 bytes for next Int16 item bptr.assumingMemoryBound(to: Int16.self).pointee = x bptr += 2 // stereo, so fill both Left & Right channels } } } self.phY = a // save sinewave phase self.toneCount -= Int32(frameCount) // decrement time remaining } else { // audioStalled = true memset(mBuffers.mData, 0, Int(mBuffers.mDataByteSize)) // silence } } } func stop() { if (audioRunning) { auAudioUnit.stopHardware() audioRunning = false } } }
Replies
0
Boosts
0
Views
1.5k
Activity
Dec ’21
How to keep cmmotionactivity confidence high
https://stackoverflow.com/questions/46661363/driving-activity-confidence-is-low-all-time-in-ios-11 I want to keep high confidence long time interval for deviceMotionUpdateInterval Makes high confidence?
Replies
0
Boosts
0
Views
815
Activity
Nov ’21