Files
OSGKeyboard/OSGKeyboardHostSupport/Models/AudioBufferSnapshot+AVFoundation.swift
T
Rocky 31f5937a7f feat(keyboard): improve typing, voice flow, and polish reliability
Reduce extension memory pressure and delivery races while adding richer candidates, tactile feedback, and safer two-level creative polishing.
2026-08-05 21:39:31 +08:00

26 lines
890 B
Swift

// AudioBufferSnapshot+AVFoundation.swift
// OSGKeyboard · HostSupport
//
// AVAudioPCMBuffer → AudioBufferSnapshot copy helper. Kept out of Shared so
// the keyboard extension does not link AVFoundation for this type alone.
import AVFoundation
import Foundation
#if canImport(OSGKeyboardShared)
import OSGKeyboardShared
#endif
extension AudioBufferSnapshot {
/// Construct from an `AVAudioPCMBuffer` by copying out the channel data.
public init(buffer: AVAudioPCMBuffer) {
guard let channelData = buffer.floatChannelData else {
self.init(samples: [], sampleRate: buffer.format.sampleRate)
return
}
let n = Int(buffer.frameLength)
var copy = [Float](repeating: 0, count: n)
memcpy(&copy, channelData[0], n * MemoryLayout<Float>.size)
self.init(samples: copy, sampleRate: buffer.format.sampleRate)
}
}