Files
notes/Shared/Connectivity/AudioNotePayload.swift
T
rzen 86d74806c9 Add voice notes: on-device transcription, multi-language, watch capture
- Schema v2: Note.AudioInfo, .m4a sidecar next to the note JSON in iCloud
- AudioRecorderService (iOS/macOS) + SpeechAnalyzer transcription pipeline
  with enabled-language set and confidence-based auto-pick, re-transcribe menu
- Settings > Transcription > Languages with asset download/reserve handling
- watchOS app + accessory complication: one-button recorder, WCSession
  transferFile to phone, WatchInbox staging, direct-upsert ingestion
- Player UI, transcription status chips, quick-capture mic in notes list
- Version 0.2, changelog + README updated
2026-07-15 13:30:43 -04:00

74 lines
3.0 KiB
Swift

import Foundation
/// Versioned metadata carried alongside a watch audio recording over
/// `WCSession.transferFile(_:metadata:)`. The watch encodes it into the
/// transfer's metadata dictionary; the phone decodes it on delivery.
///
/// Versioned so a newer watch build can extend the field set without an older
/// phone build silently misreading it: a missing, unknown, or newer version
/// decodes to `nil` (a forward gate). On that outcome the phone still stages the
/// audio bytes but skips ingest, so a future phone build can pick them up.
///
/// Lives in `Shared/`, compiled into the iOS app and the watch app (not the Mac
/// app). Foundation-only, so it is safe on both platforms.
struct AudioNotePayload: Equatable, Sendable {
/// The wire version this build writes. Bump only when the field set changes
/// in a way an older decoder can't interpret.
static let currentVersion = 1
/// ULID string minted on the watch at record start — the note's identity and
/// its UTC time bucket. Also the staged filename base on the phone.
let id: String
/// Wall-clock instant recording started.
let recordedAt: Date
/// Recording length in seconds.
let duration: TimeInterval
/// IANA time-zone identifier the recording was made in (e.g. "Europe/Berlin"),
/// or nil if unknown.
let timeZoneID: String?
private enum Key {
static let version = "v"
static let id = "id"
static let recordedAt = "recordedAt"
static let duration = "duration"
static let timeZone = "tz"
}
init(id: String, recordedAt: Date, duration: TimeInterval, timeZoneID: String?) {
self.id = id
self.recordedAt = recordedAt
self.duration = duration
self.timeZoneID = timeZoneID
}
/// A plist-serializable dictionary for `WCSession.transferFile(_:metadata:)`.
/// Every value type (Int, String, Date, Double) survives WatchConnectivity's
/// metadata plist encoding.
func metadata() -> [String: Any] {
var dict: [String: Any] = [
Key.version: Self.currentVersion,
Key.id: id,
Key.recordedAt: recordedAt,
Key.duration: duration,
]
if let timeZoneID { dict[Key.timeZone] = timeZoneID }
return dict
}
/// Decode a received metadata dictionary. Returns `nil` when the version is
/// missing, unknown, or newer than this build understands (the forward gate),
/// or when a required field is absent or malformed.
init?(metadata: [String: Any]) {
guard let version = metadata[Key.version] as? Int,
version >= 1, version <= Self.currentVersion,
let id = metadata[Key.id] as? String,
let recordedAt = metadata[Key.recordedAt] as? Date,
let duration = metadata[Key.duration] as? Double else { return nil }
self.id = id
self.recordedAt = recordedAt
self.duration = duration
self.timeZoneID = metadata[Key.timeZone] as? String
}
}