AlertSounds.swift (3317B)
1 // SPDX-License-Identifier: GPL-2.0-only 2 // Alert sounds: the leak alarm and the cues of the dive computer. They are played through the app's own 3 // audio session and bundled tones (tools/make-sounds.py), not as system sounds, so that they are heard even 4 // when the iPhone's ring/silent switch is on silent. The player volume is at its maximum; the volume set 5 // with the iPhone's buttons still applies (an app cannot override it). 6 @preconcurrency import AVFoundation 7 import RemoraDeco 8 9 /// The two bundled tones. 10 enum AlertSound: String, CaseIterable, Sendable { 11 /// Urgent: leak, dive computer alarm (violation, depth unknown). 12 case alarm 13 /// Softer: dive computer warning, safety stop started or done. 14 case notice 15 16 var url: URL? { Bundle.main.url(forResource: rawValue, withExtension: "wav") } 17 } 18 19 /// Which sound, if any: pure rules, so that they can be tested. 20 /// The leak alarm has priority (no dive sound over it); the first button press silences an alarm. 21 enum AlertPolicy { 22 /// Leak alarm, asked every second while the leak lasts. 23 static func leakSound(leaking: Bool, silenced: Bool) -> AlertSound? { 24 leaking && !silenced ? .alarm : nil 25 } 26 27 /// A cue from the dive logic. 28 static func sound(for cue: DiveCue, leaking: Bool, alarmSilenced: Bool) -> AlertSound? { 29 guard !leaking else { return nil } 30 switch cue { 31 case .alarm, .depthLost: return alarmSilenced ? nil : .alarm 32 case .warning, .safetyStopStarted, .safetyStopDone: return .notice 33 case .diveSaved: return nil 34 } 35 } 36 } 37 38 /// Plays the alert sounds. The audio session is "play and record" on the loudspeaker: that category ignores 39 /// the ring/silent switch and lets the camera record the sound of videos at the same time (the capture 40 /// session is told not to reconfigure the audio session: see `CameraEngine.configure`). 41 @MainActor 42 final class AlertPlayer { 43 private var players: [AlertSound: AVAudioPlayer] = [:] 44 /// Last sound asked for (tests, diagnostics). 45 private(set) var lastPlayed: AlertSound? 46 47 /// The category the app wants; `.playback` if the device refuses it (also ignores the switch). 48 static let category = AVAudioSession.Category.playAndRecord 49 static let options: AVAudioSession.CategoryOptions = [.defaultToSpeaker, .mixWithOthers] 50 51 /// To be called once at launch, before the camera starts. 52 func prepare() { 53 let session = AVAudioSession.sharedInstance() 54 do { 55 try session.setCategory(Self.category, mode: .videoRecording, options: Self.options) 56 } catch { 57 try? session.setCategory(.playback, options: [.mixWithOthers]) 58 } 59 try? session.setActive(true) 60 for sound in AlertSound.allCases { 61 guard let url = sound.url, let player = try? AVAudioPlayer(contentsOf: url) else { continue } 62 player.volume = 1 63 player.prepareToPlay() 64 players[sound] = player 65 } 66 } 67 68 func play(_ sound: AlertSound) { 69 lastPlayed = sound 70 guard let player = players[sound] else { return } 71 // The session may have been deactivated by an interruption (a phone call). 72 try? AVAudioSession.sharedInstance().setActive(true) 73 player.currentTime = 0 74 player.play() 75 } 76 }