Files
openchamber-android-native/third_party/mygo/internal/mobile/ios/Sources/MyGoHost/Sound.swift
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

76 lines
2.7 KiB
Swift

// The sound of package audio, through AVAudioEngine: Go's output writes from a
// thread of its own, which waits here while the engine holds enough.
import AVFoundation
final class Sound {
static let shared = Sound()
private let engine = AVAudioEngine()
private let player = AVAudioPlayerNode()
private var format: AVAudioFormat?
private var room: DispatchSemaphore?
private var attached = false
func open(_ c: AudioOpenCall) -> AudioOpenReply {
var r = AudioOpenReply()
do {
let s = AVAudioSession.sharedInstance()
// Sounds follow the side switch, and mix with what else plays,
// until the app asks for more of the session.
if s.category == .soloAmbient { try s.setCategory(.ambient) }
try s.setActive(true)
guard let f = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: Double(c.rate), channels: 2, interleaved: false) else {
r.error = "audio: no such format"
return r
}
format = f
if !attached {
engine.attach(player)
engine.connect(player, to: engine.mainMixerNode, format: f)
attached = true
}
// Writes of 10 ms: as many as hold what was asked for.
room = DispatchSemaphore(value: max(2, c.ahead / 10))
try engine.start()
player.play()
} catch {
r.error = "audio: \(error.localizedDescription)"
}
return r
}
func write(_ c: AudioWriteCall) -> AudioWriteReply {
var r = AudioWriteReply()
guard let f = format, let room else {
r.error = "audio: closed"
return r
}
let frames = c.samples.count / 8
guard frames > 0, let b = AVAudioPCMBuffer(pcmFormat: f, frameCapacity: AVAudioFrameCount(frames)),
let left = b.floatChannelData?[0], let right = b.floatChannelData?[1] else { return r }
b.frameLength = AVAudioFrameCount(frames)
c.samples.withUnsafeBytes { raw in
let s = raw.bindMemory(to: Float32.self)
for i in 0..<frames {
left[i] = s[2 * i]
right[i] = s[2 * i + 1]
}
}
// An engine that stopped, as for a phone call, plays nothing more and
// frees no room: the output opens it again.
if !engine.isRunning || room.wait(timeout: .now() + 1) == .timedOut {
r.error = "audio: the engine stopped"
return r
}
player.scheduleBuffer(b) { room.signal() }
return r
}
func close() {
player.stop()
engine.stop()
room = nil
}
}