Repository Public
1// SP Notes iOS2// File: Features/Voice/VoiceNoteModel.swift3// Description: The model of one voice note (an `audio` node): playback with seek and speed, on-device and server transcription4// with their states, the AI summary inserted only on request, renaming, and deleting the original audio while5// keeping the transcript. Every change is written back into the node through the block detail context.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9import AVFoundation10import Foundation11import Observation12import SPNotesCore1314// MARK: - Saving a recording1516@MainActor17enum VoiceNoteIO {18 nonisolated static func iso(_ date: Date) -> String {19 let formatter = ISO8601DateFormatter()20 formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]21 return formatter.string(from: date)22 }2324 /// Records the file as an attachment of the note and returns the `audio` node that refers to it.25 static func audioNode(for voice: VoiceCapture, app: AppModel, noteId: String) async throws -> PMNode {26 let file = try await app.attach(fileAt: voice.fileURL, to: noteId, filename: voice.fileURL.lastPathComponent, mime: "audio/mp4")27 guard let id = file.stringAttr("attachmentId") else { throw CocoaError(.fileWriteUnknown) }28 var block = AudioBlock(attachmentId: id, duration: (voice.duration * 100).rounded() / 100, title: voice.title, recordedAt: voice.recordedAt)29 if !voice.transcript.isEmpty {30 block.transcript = voice.transcript31 block.transcriptStatus = .done32 block.transcriptEngine = voice.transcriptEngine33 }34 return block.node()35 }3637 nonisolated static func timecode(_ seconds: Double) -> String {38 let total = Int(max(0, seconds).rounded(.down))39 return String(format: "%d:%02d", total / 60, total % 60)40 }41}4243// MARK: - Playback4445@MainActor46protocol AudioPlaying: AnyObject {47 var duration: Double { get }48 var currentTime: Double { get set }49 var isPlaying: Bool { get }50 var rate: Float { get set }51 func play()52 func pause()53}5455@MainActor56final class SystemAudioPlayer: AudioPlaying {57 private let player: AVAudioPlayer58 init(url: URL) throws {59 player = try AVAudioPlayer(contentsOf: url)60 player.enableRate = true61 player.prepareToPlay()62 }63 var duration: Double { player.duration }64 var currentTime: Double { get { player.currentTime } set { player.currentTime = min(max(0, newValue), player.duration) } }65 var isPlaying: Bool { player.isPlaying }66 var rate: Float { get { player.rate } set { player.rate = newValue } }67 func play() {68 try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .spokenAudio)69 try? AVAudioSession.sharedInstance().setActive(true)70 player.play()71 }72 func pause() { player.pause() }73}7475// MARK: - Model7677@MainActor78@Observable79final class VoiceNoteModel {80 enum Transcription: Equatable {81 case idle82 /// The server transcription is paid: the user must confirm (with or without the AI summary).83 case confirmingPaid84 case runningOnDevice85 /// Waiting for the file to reach the server, then for the job.86 case runningOnServer87 case failed(String)88 /// The server has no voice routes.89 case unsupported90 case offline91 /// No on-device model for the language.92 case unavailableOnDevice(String)93 }9495 enum AudioState: Equatable { case loading, ready, missing, removed }9697 private(set) var block: AudioBlock98 private(set) var transcription: Transcription = .idle99 private(set) var audioState: AudioState = .loading100 private(set) var isPlaying = false101 private(set) var position: Double = 0102 private(set) var speed: Float = 1103 /// What the summary actions inserted (so the buttons say so; the user may insert again).104 private(set) var insertedSummary = false105 private(set) var insertedTasks = false106 var isEditable: Bool107108 static let speeds: [Float] = [0.75, 1, 1.25, 1.5, 2]109110 @ObservationIgnored private let commit: (PMNode?) -> Void111 @ObservationIgnored private let insertBelow: (String) -> Void112 @ObservationIgnored private let localFile: (String) async throws -> URL113 @ObservationIgnored private let makePlayer: (URL) throws -> any AudioPlaying114 @ObservationIgnored private let onDevice: any SpeechTranscribing115 @ObservationIgnored private let server: any ServerTranscribing116 /// Makes sure the server has the file (uploads go through the normal synchronisation). False = still not there.117 @ObservationIgnored private let ensureUploaded: (String) async -> Bool118 @ObservationIgnored private let deleteAttachment: (String) async throws -> Void119 @ObservationIgnored private var player: (any AudioPlaying)?120 @ObservationIgnored private var fileURL: URL?121 @ObservationIgnored private var ticker: Task<Void, Never>?122 @ObservationIgnored private var work: Task<Void, Never>?123124 init(block: AudioBlock, isEditable: Bool, commit: @escaping (PMNode?) -> Void, insertBelow: @escaping (String) -> Void,125 localFile: @escaping (String) async throws -> URL, makePlayer: @escaping (URL) throws -> any AudioPlaying = { try SystemAudioPlayer(url: $0) },126 onDevice: any SpeechTranscribing = OnDeviceTranscriber(), server: any ServerTranscribing,127 ensureUploaded: @escaping (String) async -> Bool, deleteAttachment: @escaping (String) async throws -> Void) {128 self.block = block; self.isEditable = isEditable; self.commit = commit; self.insertBelow = insertBelow129 self.localFile = localFile; self.makePlayer = makePlayer; self.onDevice = onDevice; self.server = server130 self.ensureUploaded = ensureUploaded; self.deleteAttachment = deleteAttachment131 }132133 /// The model of the `audio` node a block detail screen was opened on.134 convenience init(node: PMNode, context: BlockDetailContext) {135 let app = context.app136 self.init(block: AudioBlock(node: node) ?? AudioBlock(storage: node), isEditable: context.isEditable, commit: context.commit,137 insertBelow: context.insertMarkdownBelow, localFile: { try await app.localFile(forAttachment: $0) },138 server: RepositoryTranscriber(audio: app.repositories.audio),139 ensureUploaded: { id in140 if (try? await app.store.attachment(id: id))?.uploaded == true { return true }141 await app.syncAndWait()142 return (try? await app.store.attachment(id: id))?.uploaded == true143 }, deleteAttachment: { try await app.deleteAttachment($0) })144 }145146 // MARK: derived147148 var title: String { block.title }149 var duration: Double { max(block.duration, player?.duration ?? 0) }150 var transcript: [TranscriptSegment] { block.transcript }151 var summary: AudioSummary? { block.summary }152 var engineLabel: String { TranscriptEngines.label(block.transcriptEngine) }153 var hasAudio: Bool { block.attachmentId != nil }154 var isBusy: Bool { transcription == .runningOnDevice || transcription == .runningOnServer }155 /// The transcript line being spoken.156 var currentSegment: Int? { isPlaying || position > 0 ? block.segmentIndex(at: position) : nil }157 var recordedAt: Date? {158 guard let text = block.recordedAt else { return nil }159 let formatter = ISO8601DateFormatter()160 formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]161 if let date = formatter.date(from: text) { return date }162 formatter.formatOptions = [.withInternetDateTime]163 return formatter.date(from: text)164 }165166 // MARK: audio167168 func load() async {169 guard let id = block.attachmentId else { audioState = .removed; return }170 guard player == nil else { return }171 do {172 let url = try await localFile(id)173 fileURL = url174 let player = try makePlayer(url)175 self.player = player176 if block.duration <= 0, player.duration > 0 { block.duration = (player.duration * 100).rounded() / 100; commitIfEditable() }177 audioState = .ready178 } catch {179 audioState = .missing180 }181 }182183 func togglePlayback() {184 guard let player else { return }185 if player.isPlaying { player.pause(); isPlaying = false; stopTicker() } else {186 if player.duration > 0, player.currentTime >= player.duration - 0.05 { player.currentTime = 0 }187 player.rate = speed188 player.play()189 isPlaying = true190 startTicker()191 }192 }193194 func stopPlayback() {195 player?.pause()196 isPlaying = false197 stopTicker()198 }199200 /// Moves the playhead (the scrubber, ±15 s).201 func seek(to seconds: Double) {202 let target = min(max(0, seconds), duration)203 player?.currentTime = target204 position = target205 }206207 /// A tap on a transcript line: the player goes to its start and plays.208 func seek(toSegment index: Int) {209 guard transcript.indices.contains(index) else { return }210 seek(to: transcript[index].t)211 if let player, !player.isPlaying { togglePlayback() }212 }213214 func cycleSpeed() {215 let index = Self.speeds.firstIndex(of: speed) ?? 1216 speed = Self.speeds[(index + 1) % Self.speeds.count]217 player?.rate = speed218 }219220 /// One refresh of the playhead (called by the timer; the tests call it themselves).221 func tick() {222 guard let player else { return }223 if !player.isPlaying, isPlaying {224 // the file ended by itself (the system player rewinds to 0): the playhead stays at the end225 isPlaying = false226 stopTicker()227 position = duration228 } else {229 position = player.currentTime230 }231 }232233 private func startTicker() {234 ticker?.cancel()235 ticker = Task { [weak self] in236 while !Task.isCancelled {237 try? await Task.sleep(for: .milliseconds(100))238 guard let self, !Task.isCancelled else { return }239 self.tick()240 }241 }242 }243244 private func stopTicker() { ticker?.cancel(); ticker = nil }245246 // MARK: editing247248 func rename(_ title: String) {249 let clean = title.trimmingCharacters(in: .whitespacesAndNewlines)250 guard isEditable, clean != block.title else { return }251 block.title = clean252 commit(block.node())253 }254255 private func commitIfEditable() { if isEditable { commit(block.node()) } }256257 // MARK: transcription258259 func dismissTranscriptionState() { if !isBusy { transcription = .idle } }260261 /// On this device, free, nothing sent anywhere.262 func transcribeOnDevice() {263 guard isEditable, !isBusy else { return }264 work = Task { await runOnDevice() }265 }266267 func runOnDevice() async {268 guard let fileURL else { transcription = .failed(String(localized: "Le fichier audio n’est pas sur cet appareil.")); return }269 let availability = await onDevice.availability()270 guard availability.available else { transcription = .unavailableOnDevice(availability.language); return }271 transcription = .runningOnDevice272 do {273 let pieces = try await onDevice.transcribe(fileURL: fileURL)274 let segments = TranscriptSegmenter.segments(from: pieces)275 guard !segments.isEmpty else { throw OnDeviceTranscriptionError.noSpeech }276 block.applyOnDevice(segments)277 commit(block.node())278 transcription = .idle279 } catch let error as OnDeviceTranscriptionError {280 if case .unavailable(let language) = error { transcription = .unavailableOnDevice(language) } else { transcription = .failed(error.localizedDescription) }281 } catch {282 transcription = .failed(error.localizedDescription)283 }284 }285286 /// The server transcription costs money: this only asks for the confirmation.287 func requestServerTranscription() {288 guard isEditable, !isBusy, hasAudio else { return }289 transcription = .confirmingPaid290 }291292 func cancelServerTranscription() { if transcription == .confirmingPaid { transcription = .idle } }293294 /// The user confirmed the paid call.295 func confirmServerTranscription(summarize: Bool) {296 guard transcription == .confirmingPaid else { return }297 work = Task { await runOnServer(summarize: summarize) }298 }299300 func runOnServer(summarize: Bool) async {301 guard let id = block.attachmentId else { return }302 transcription = .runningOnServer303 guard await ensureUploaded(id) else { transcription = .offline; return }304 do {305 // a second request on a finished transcript is not charged again by the server; `force` only when one exists from another engine306 let first = try await server.transcribe(attachmentId: id, summarize: summarize, force: false)307 let final = first.isPending ? try await server.wait(attachmentId: id) : first308 switch final.status {309 case TranscriptionStatus.done:310 block.apply(final)311 commit(block.node())312 transcription = .idle313 case TranscriptionStatus.failed:314 block.transcriptStatus = .failed315 commit(block.node())316 transcription = .failed(final.error ?? String(localized: "Le serveur n’a pas pu transcrire cet enregistrement."))317 default:318 transcription = .failed(String(localized: "La transcription est toujours en cours sur le serveur. Réessaie dans un moment."))319 }320 } catch let error as RepositoryError {321 switch error {322 case .unsupported: transcription = .unsupported323 case .offline, .signedOut: transcription = .offline324 default: transcription = .failed(Self.describe(error))325 }326 } catch is CancellationError {327 transcription = .idle328 } catch {329 transcription = .failed(error.localizedDescription)330 }331 }332333 static func describe(_ error: RepositoryError) -> String {334 if case .server(let api) = error {335 switch api.code {336 case "not_configured": return String(localized: "La transcription n’est pas configurée sur le serveur.")337 case "too_large": return String(localized: "Cet enregistrement dépasse 25 Mo, la limite du moteur de transcription.")338 case "not_audio": return String(localized: "Le serveur ne reconnaît pas ce fichier comme un enregistrement audio.")339 default: return api.localizedDescription340 }341 }342 return String(localized: "La transcription du serveur a échoué.")343 }344345 // MARK: AI summary — only ever inserted on an explicit tap346347 func insertSummary() {348 guard isEditable, let summary else { return }349 let markdown = AudioBlock.summaryMarkdown(summary)350 guard !markdown.isEmpty else { return }351 insertBelow(markdown)352 insertedSummary = true353 }354355 func createTasks() {356 guard isEditable, let summary, !summary.tasks.isEmpty else { return }357 insertBelow(AudioBlock.tasksMarkdown(summary))358 insertedTasks = true359 }360361 // MARK: the original audio362363 /// Deletes the recording and keeps the transcript: the node stays, without its attachment (the `audio` node allows a null364 /// `attachmentId`). Only offered once a transcript exists.365 func deleteOriginalAudio() async {366 guard isEditable, let id = block.attachmentId, !transcript.isEmpty else { return }367 stopPlayback()368 player = nil369 fileURL = nil370 do {371 try await deleteAttachment(id)372 block.attachmentId = nil373 commit(block.node())374 audioState = .removed375 position = 0376 } catch {377 transcription = .failed(error.localizedDescription)378 }379 }380381 /// Removes the whole voice note from the note.382 func deleteBlock() async {383 guard isEditable else { return }384 stopPlayback()385 if let id = block.attachmentId { try? await deleteAttachment(id) }386 commit(nil)387 }388389 func cancelWork() { work?.cancel(); stopPlayback() }390}391