// SPDX-License-Identifier: AGPL-3.0-only // Copyright (C) 2026 Neo-Desktop import AVFoundation import DModemShared import Foundation /// Samples (as floats) from one thread to another: the oldest dropped /// when it's full. final class SampleQueue: @unchecked Sendable { private var buf: [Float] private var head = 0 private var count = 0 private let lock = NSLock() /// room for [capacity] samples init(capacity: Int) { buf = [Float](repeating: 0, count: capacity) } /// [x] put in func push(_ x: UnsafeBufferPointer) { lock.lock(); defer { lock.unlock() } for v in x { if count == buf.count { head = (head + 1) % buf.count count -= 1 } buf[(head + count) % buf.count] = v count += 1 } } /// up to [n] into [p]: how many func pop(into p: UnsafeMutablePointer, _ n: Int) -> Int { lock.lock(); defer { lock.unlock() } let k = min(n, count) for i in 0.. Bool { stop() let s = AVAudioSession.sharedInstance() do { try s.setCategory(.playAndRecord, mode: .voiceChat, options: loud ? [.defaultToSpeaker, .allowBluetooth] : [.allowBluetooth]) try s.setActive(true) } catch { NSLog("voice sound: \(error)") } let e = AVAudioEngine() let fmt = format let q = speaker let src = AVAudioSourceNode(format: fmt) { _, _, frames, abl -> OSStatus in let list = UnsafeMutableAudioBufferListPointer(abl) guard let p = list[0].mData?.assumingMemoryBound(to: Float.self) else { return noErr } let n = Int(frames) var i = q.pop(into: p, n) while i < n { p[i] = 0; i += 1 } return noErr } e.attach(src) e.connect(src, to: e.mainMixerNode, format: fmt) if record { let input = e.inputNode let hw = input.outputFormat(forBus: 0) if hw.sampleRate > 0, hw.channelCount > 0, let conv = AVAudioConverter(from: hw, to: fmt) { let m = mic input.installTap(onBus: 0, bufferSize: 1024, format: hw) { buffer, _ in let cap = AVAudioFrameCount(Double(buffer.frameLength) * fmt.sampleRate / hw.sampleRate) + 32 guard let out = AVAudioPCMBuffer(pcmFormat: fmt, frameCapacity: cap) else { return } var given = false var err: NSError? _ = conv.convert(to: out, error: &err) { _, status in if given { status.pointee = .noDataNow; return nil } given = true status.pointee = .haveData return buffer } if let d = out.floatChannelData?[0], out.frameLength > 0 { m.push(UnsafeBufferPointer(start: d, count: Int(out.frameLength))) } } tapped = true } } do { e.prepare() try e.start() } catch { if tapped { e.inputNode.removeTap(onBus: 0) } tapped = false NSLog("voice sound: \(error)") return false } engine = e return true } /// the loudspeaker (or the earpiece) for the call's sound func loudspeaker(_ on: Bool) { try? AVAudioSession.sharedInstance().overrideOutputAudioPort(on ? .speaker : .none) } /// stopped; the session back to the modem's playback func stop() { guard let e = engine else { return } if tapped { e.inputNode.removeTap(onBus: 0) } e.stop() tapped = false engine = nil speaker.clear() mic.clear() try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [.mixWithOthers]) } } /// A voice call's sound moved between its job and the phone every 20 ms /// (the job's Kotlin kept off the audio threads): the far end's samples /// to the speaker, the microphone's to the line. final class VoicePump: @unchecked Sendable { private let job: VoiceJob private let sound: VoiceSound private var timer: DispatchSourceTimer? private let pcm = KotlinShortArray(size: 1600) private var floats = [Float](repeating: 0, count: 1600) private let queue = DispatchQueue(label: "voice-pump") /// [job]'s sound to and from [sound] init(job: VoiceJob, sound: VoiceSound) { self.job = job self.sound = sound } /// the pump started, every 20 ms func start() { guard timer == nil else { return } let t = DispatchSource.makeTimerSource(queue: queue) t.schedule(deadline: .now(), repeating: .milliseconds(20)) t.setEventHandler { [weak self] in self?.pump() } t.resume() timer = t } /// the pump stopped func stop() { timer?.cancel() timer = nil } /// the far end's samples to the speaker's queue, the microphone's to the job private func pump() { // the far end's (no more than a quarter second behind) if sound.speaker.available > 2000 { sound.speaker.clear() } let n = Int(job.read(pcm: pcm, off: 0, len: 1600)) if n > 0 { for i in 0.. 0 { for i in 0.. KotlinByteArray { let b = KotlinByteArray(size: Int32(d.count)) for (i, x) in d.enumerated() { b.set(index: Int32(i), value: Int8(bitPattern: x)) } return b } /// Kotlin's octets as Data static func data(_ b: KotlinByteArray) -> Data { var d = Data(count: Int(b.size)) for i in 0.. KotlinShortArray? { guard let d = try? Data(contentsOf: url) else { return nil } if let pcm = Wav.shared.decode(data: bytes(d)) { return pcm } guard let s = bySystem(url) else { return nil } let pcm = KotlinShortArray(size: Int32(s.count)) for (i, v) in s.enumerated() { pcm.set(index: Int32(i), value: v) } return pcm } /// a sound file the phone decodes, at 8000 Hz, one channel (five /// minutes at most); nil if it can't static func bySystem(_ url: URL, maxSeconds: Int = 300) -> [Int16]? { guard let f = try? AVAudioFile(forReading: url) else { return nil } let src = f.processingFormat guard src.sampleRate > 0, src.channelCount > 0, let dst = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 8000, channels: 1, interleaved: false), let conv = AVAudioConverter(from: src, to: dst), let inBuf = AVAudioPCMBuffer(pcmFormat: src, frameCapacity: 4096), let outBuf = AVAudioPCMBuffer(pcmFormat: dst, frameCapacity: AVAudioFrameCount(4096 * 8000 / src.sampleRate) + 64) else { return nil } // (the channels mixed, not the first alone) conv.downmix = true var out: [Int16] = [] var ended = false while out.count < maxSeconds * 8000 { outBuf.frameLength = 0 var err: NSError? let st = conv.convert(to: outBuf, error: &err) { _, status in if !ended { do { try f.read(into: inBuf) } catch { inBuf.frameLength = 0 } ended = inBuf.frameLength == 0 } if ended { status.pointee = .endOfStream return nil } status.pointee = .haveData return inBuf } if let x = outBuf.floatChannelData?[0] { for i in 0.. Int { let size = (try? url.resourceValues(forKeys: [.fileSizeKey]).fileSize) ?? 0 return Int(Wav.shared.seconds(fileSize: Int64(size))) } }