forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBleAudioProcessor.swift
More file actions
411 lines (344 loc) · 12.8 KB
/
Copy pathBleAudioProcessor.swift
File metadata and controls
411 lines (344 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import Combine
import Foundation
import os.log
// MARK: - BLE Audio Processor
/// Processes audio data from BLE devices
/// Handles frame reassembly, codec decoding, and integration with transcription
/// Ported from: omi/app/lib/utils/audio/wav_bytes.dart (WavBytesUtil)
final class BleAudioProcessor {
// MARK: - Types
/// Audio frame with metadata
struct AudioFrame {
let pcmSamples: [Int16]
let timestamp: Date
let frameIndex: Int
}
/// Delegate for receiving decoded audio
protocol Delegate: AnyObject {
/// Called when PCM audio samples are ready
func bleAudioProcessor(_ processor: BleAudioProcessor, didDecodeSamples samples: [Int16])
/// Called when audio decoding fails
func bleAudioProcessor(_ processor: BleAudioProcessor, didFailWithError error: Error)
}
// MARK: - Properties
weak var delegate: Delegate?
/// Publisher for decoded PCM samples (alternative to delegate)
var pcmSamplesPublisher: AnyPublisher<[Int16], Never> {
pcmSamplesSubject.eraseToAnyPublisher()
}
/// Publisher for raw PCM data (as bytes, for TranscriptionService)
var pcmDataPublisher: AnyPublisher<Data, Never> {
pcmSamplesSubject
.map { samples in
// Convert Int16 samples to Data (little-endian)
var data = Data(capacity: samples.count * 2)
for sample in samples {
var s = sample
data.append(Data(bytes: &s, count: 2))
}
return data
}
.eraseToAnyPublisher()
}
private let logger = Logger(subsystem: "me.omi.desktop", category: "BleAudioProcessor")
private let pcmSamplesSubject = PassthroughSubject<[Int16], Never>()
// Codec and decoder
private var codec: BleAudioCodec
private var decoder: AudioCodecDecoder?
// Frame reassembly state (for multi-packet frames)
private var lastPacketIndex: Int = -1
private var lastFrameId: Int = -1
private var pendingFrame: [UInt8] = []
// Statistics
private var totalFramesProcessed: Int = 0
private var totalBytesProcessed: Int = 0
private var lostPackets: Int = 0
// MARK: - Initialization
init(codec: BleAudioCodec) {
self.codec = codec
self.decoder = AudioDecoderFactory.createDecoder(for: codec)
if decoder == nil && codec != .unknown {
logger.warning("No decoder available for codec: \(codec.name)")
} else {
logger.info("BleAudioProcessor initialized for codec: \(codec.name)")
}
}
// MARK: - Public Methods
/// Update the codec (e.g., after reading from device)
func updateCodec(_ newCodec: BleAudioCodec) {
guard newCodec != codec else { return }
codec = newCodec
decoder = AudioDecoderFactory.createDecoder(for: newCodec)
reset()
logger.info("Codec updated to: \(newCodec.name)")
}
/// Process raw audio bytes from BLE device
/// Call this with each BLE notification data
/// - Parameter data: Raw audio data from BLE characteristic
func processAudioData(_ data: Data) {
guard !data.isEmpty else { return }
totalBytesProcessed += data.count
// For devices that send pre-framed data (Fieldy, Friend Pendant),
// process directly without reassembly
if codec == .opusFS320 || codec == .lc3FS1030 {
processFramedData(data)
return
}
// For devices with packet framing (Omi/OpenGlass), use reassembly
processPacketData(data)
}
/// Process a complete audio frame (already extracted from BLE packets)
/// Use this for devices that pre-extract frames (Fieldy, Limitless)
func processFrame(_ frame: Data) {
guard !frame.isEmpty else { return }
totalFramesProcessed += 1
if let decoder = decoder {
if let samples = decoder.decode(frame) {
deliverSamples(samples)
decodeFailures = 0 // Reset failure counter on success
} else {
handleDecodeFailure(frame)
}
} else {
// No decoder - try to pass through as PCM
if codec.isPCM {
if let passthrough = PCMPassthroughDecoder(codec: codec).decode(frame) {
deliverSamples(passthrough)
}
} else {
logger.warning("No decoder available for codec: \(self.codec.name)")
}
}
}
/// Track consecutive decode failures
private var decodeFailures = 0
private let maxDecodeFailures = 10
/// Handle decode failure with logging and fallback
private func handleDecodeFailure(_ frame: Data) {
decodeFailures += 1
if decodeFailures == 1 {
// Log first failure with frame details
let preview = frame.prefix(16).map { String(format: "%02x", $0) }.joined(separator: " ")
logger.warning("Decode failed. Frame size: \(frame.count), preview: \(preview)")
} else if decodeFailures == maxDecodeFailures {
let error = NSError(
domain: "BleAudioProcessor",
code: 1,
userInfo: [NSLocalizedDescriptionKey: "Too many consecutive decode failures"])
logger.error("Too many consecutive decode failures (\(self.decodeFailures)). Check codec compatibility.")
log(
"BleAudioProcessor: decode degraded "
+ "(failure_class=ble_decode_degraded recovery_action=continue_raw_capture recovery_result=degraded)")
logError("BleAudioProcessor: decode degraded", error: error)
DesktopDiagnosticsManager.shared.recordBleDecodeDegraded(
codec: codec.name,
failures: decodeFailures
)
delegate?.bleAudioProcessor(self, didFailWithError: error)
}
// For Opus, validate TOC byte
if codec.isOpus && !frame.isEmpty {
let toc = frame[0]
if !isValidOpusToc(toc) {
logger.debug("Invalid Opus TOC: 0x\(String(format: "%02x", toc))")
}
}
}
/// Check if byte is a valid Opus TOC byte
private func isValidOpusToc(_ byte: UInt8) -> Bool {
[0x78, 0xb8, 0xf8, 0x70, 0xb0, 0xf0].contains(byte)
}
/// Process multiple frames at once
func processFrames(_ frames: [Data]) {
for frame in frames {
processFrame(frame)
}
}
/// Reset processor state (call when starting new recording session)
func reset() {
lastPacketIndex = -1
lastFrameId = -1
pendingFrame = []
decoder?.reset()
logger.debug("Processor reset")
}
/// Get processing statistics
func getStatistics() -> (frames: Int, bytes: Int, lostPackets: Int) {
(totalFramesProcessed, totalBytesProcessed, lostPackets)
}
// MARK: - Private Methods
/// Process pre-framed data (Fieldy uses 40-byte Opus frames, Friend Pendant uses 30-byte LC3 frames)
private func processFramedData(_ data: Data) {
// Determine frame size based on codec
let frameSize: Int
switch codec {
case .opusFS320:
frameSize = 40 // Fieldy Opus frames
case .lc3FS1030:
frameSize = 30 // Friend Pendant LC3 frames
default:
frameSize = data.count // Treat as single frame
}
// Extract frames from data
var offset = 0
while offset + frameSize <= data.count {
let frameData = data.subdata(in: offset..<(offset + frameSize))
processFrame(frameData)
offset += frameSize
}
// Handle remaining bytes (partial frame)
if offset < data.count {
logger.debug("Partial frame: \(data.count - offset) bytes remaining")
}
}
/// Process packet data with frame reassembly
/// Ported from Flutter's WavBytesUtil.storeFramePacket()
private func processPacketData(_ data: Data) {
// Minimum packet structure: 2 bytes index + 1 byte frame ID + content
guard data.count >= 3 else {
logger.debug("Packet too small: \(data.count) bytes")
return
}
// Parse packet header
// Format: [index_low, index_high, frame_id, ...content...]
let packetIndex = Int(data[0]) | (Int(data[1]) << 8)
let frameId = Int(data[2])
let content = Array(data.dropFirst(3))
// Detect lost packets
if lastPacketIndex >= 0 && packetIndex != lastPacketIndex + 1 {
let lost = packetIndex - lastPacketIndex - 1
if lost > 0 && lost < 100 { // Sanity check
lostPackets += lost
logger.debug("Lost \(lost) packets (index jump from \(self.lastPacketIndex) to \(packetIndex))")
}
// Reset state on packet loss
pendingFrame = []
lastFrameId = -1
}
lastPacketIndex = packetIndex
// Frame reassembly logic
if frameId == 0 {
// Start of new frame
if !pendingFrame.isEmpty {
// Save previous frame
completeFrame(pendingFrame)
}
pendingFrame = content
lastFrameId = 0
} else if frameId == lastFrameId + 1 {
// Continuation of current frame
pendingFrame.append(contentsOf: content)
lastFrameId = frameId
} else {
// Frame ID mismatch - reset
logger.debug("Frame ID mismatch: expected \(self.lastFrameId + 1), got \(frameId)")
pendingFrame = []
lastFrameId = -1
}
// Check if frame is complete based on expected size
let expectedFrameSize = codec.frameLengthInBytes
if pendingFrame.count >= expectedFrameSize {
completeFrame(pendingFrame)
pendingFrame = []
lastFrameId = -1
}
}
/// Complete a reassembled frame
private func completeFrame(_ frameBytes: [UInt8]) {
guard !frameBytes.isEmpty else { return }
totalFramesProcessed += 1
// Decode and deliver
let frameData = Data(frameBytes)
if let decoder = decoder {
if let samples = decoder.decode(frameData) {
deliverSamples(samples)
}
}
}
/// Deliver decoded samples to delegate and publisher
private func deliverSamples(_ samples: [Int16]) {
guard !samples.isEmpty else { return }
// Send to publisher
pcmSamplesSubject.send(samples)
// Send to delegate
delegate?.bleAudioProcessor(self, didDecodeSamples: samples)
}
}
// MARK: - Audio Data Utilities
extension BleAudioProcessor {
/// Convert PCM samples to Data (little-endian Int16)
static func samplesToData(_ samples: [Int16]) -> Data {
var data = Data(capacity: samples.count * 2)
for sample in samples {
var s = sample
data.append(Data(bytes: &s, count: 2))
}
return data
}
/// Convert Data to PCM samples (little-endian Int16)
static func dataToSamples(_ data: Data) -> [Int16] {
let count = data.count / 2
var samples = [Int16](repeating: 0, count: count)
data.withUnsafeBytes { bytes in
let int16Ptr = bytes.bindMemory(to: Int16.self)
for i in 0..<count {
samples[i] = int16Ptr[i]
}
}
return samples
}
/// Create WAV header for PCM data
static func createWavHeader(dataLength: Int, sampleRate: Int = 16000, channels: Int = 1, bitsPerSample: Int = 16)
-> Data
{
var header = Data(capacity: 44)
let byteRate = sampleRate * channels * (bitsPerSample / 8)
let blockAlign = channels * (bitsPerSample / 8)
// RIFF header
header.append(contentsOf: "RIFF".utf8)
header.append(contentsOf: withUnsafeBytes(of: UInt32(36 + dataLength).littleEndian) { Array($0) })
header.append(contentsOf: "WAVE".utf8)
// fmt subchunk
header.append(contentsOf: "fmt ".utf8)
header.append(contentsOf: withUnsafeBytes(of: UInt32(16).littleEndian) { Array($0) }) // Subchunk1Size
header.append(contentsOf: withUnsafeBytes(of: UInt16(1).littleEndian) { Array($0) }) // AudioFormat (PCM)
header.append(contentsOf: withUnsafeBytes(of: UInt16(channels).littleEndian) { Array($0) })
header.append(contentsOf: withUnsafeBytes(of: UInt32(sampleRate).littleEndian) { Array($0) })
header.append(contentsOf: withUnsafeBytes(of: UInt32(byteRate).littleEndian) { Array($0) })
header.append(contentsOf: withUnsafeBytes(of: UInt16(blockAlign).littleEndian) { Array($0) })
header.append(contentsOf: withUnsafeBytes(of: UInt16(bitsPerSample).littleEndian) { Array($0) })
// data subchunk
header.append(contentsOf: "data".utf8)
header.append(contentsOf: withUnsafeBytes(of: UInt32(dataLength).littleEndian) { Array($0) })
return header
}
/// Create complete WAV file data from PCM samples
static func createWavData(samples: [Int16], sampleRate: Int = 16000, channels: Int = 1) -> Data {
let pcmData = samplesToData(samples)
let header = createWavHeader(dataLength: pcmData.count, sampleRate: sampleRate, channels: channels)
return header + pcmData
}
}
// MARK: - Integration with Device Connections
extension BleAudioProcessor {
/// Create a processor for a specific device type
static func forDevice(_ deviceType: DeviceType) -> BleAudioProcessor {
// Default codec based on device type
let defaultCodec: BleAudioCodec
switch deviceType {
case .omi, .openglass:
defaultCodec = .opus // Will be updated after reading from device
case .plaud, .limitless:
defaultCodec = .opusFS320
case .bee:
defaultCodec = .aac
case .fieldy:
defaultCodec = .opusFS320
case .friendPendant:
defaultCodec = .lc3FS1030
case .frame, .appleWatch:
defaultCodec = .pcm16
}
return BleAudioProcessor(codec: defaultCodec)
}
}