Skip to content

Open source CapCut alternative.

TypeScript

feat(ios): microphone meter on the camera stage

David Le77e44fec596a
diff --git a/apps/ios/Donkey/Controllers/CameraController.swift b/apps/ios/Donkey/Controllers/CameraController.swift
index ca32c453..5cd5535c 100644
--- a/apps/ios/Donkey/Controllers/CameraController.swift
+++ b/apps/ios/Donkey/Controllers/CameraController.swift
@@ -43,6 +43,7 @@ final class CameraController: CameraControlling {
}
let facing = model?.facing ?? .front
engine.start(facing: facing, settings: model?.settings ?? CameraSettings())
+ engine.setMetering(model?.showsAudioMeter ?? false)
trackRotation(facing: facing)
}
}
@@ -63,6 +64,7 @@ final class CameraController: CameraControlling {
func apply(_ settings: CameraSettings) { engine.apply(settings) }
func startRecording() { engine.startRecording() }
func stopRecording() { engine.stopRecording() }
+ func setAudioMetering(_ on: Bool) { engine.setMetering(on) }
/// Hands small JPEG frames of the live picture to `sink` until it is
/// set to nil. The watch is the one taker.
func setPreviewSink(_ sink: (@Sendable (Data) -> Void)?) { engine.setPreviewSink(sink) }
@@ -117,6 +119,8 @@ final class CameraController: CameraControlling {
onRecordingFinished?(url, duration, thumbnail)
case .recordingFailed:
model?.recordingDidFinish()
+ case .audioLevel(let level):
+ model?.audioLevelDidChange(level)
}
}
}
@@ -131,6 +135,9 @@ private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputReco
case recordingStarted
case recordingFinished(URL, TimeInterval, Data?)
case recordingFailed
+ /// The microphone's level on a 0...1 scale, nil when the session
+ /// has no microphone connected to the movie output.
+ case audioLevel(Double?)
}
let session = AVCaptureSession()
@@ -164,6 +171,15 @@ private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputReco
/// keeps the angle it started with, since a movie file output cannot be
/// re-aimed once it is writing.
private var captureRotation: CGFloat = 90
+ /// Polls the movie output's audio channels while the meter is on screen.
+ /// Reading the movie output's own connection means the meter reports
+ /// what the file gets: a microphone the session never picked up shows
+ /// as no level at all.
+ private var meterTimer: DispatchSourceTimer?
+ private static let meterInterval: DispatchTimeInterval = .milliseconds(50)
+ /// The quietest level the meter shows, in decibels below full scale.
+ /// Room tone sits under it; speech sits well above.
+ private static let meterFloorDecibels: Float = -50
private static let recordableDimensions: Set<String> = [
"1280x720", "1920x1080", "2560x1440", "3840x2160",
@@ -219,6 +235,8 @@ private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputReco
func stop() {
queue.async {
+ self.meterTimer?.cancel()
+ self.meterTimer = nil
if self.movieOutput.isRecording {
self.movieOutput.stopRecording()
}
@@ -226,6 +244,31 @@ private nonisolated final class CaptureEngine: NSObject, AVCaptureFileOutputReco
}
}
+ func setMetering(_ on: Bool) {
+ queue.async {
+ self.meterTimer?.cancel()
+ self.meterTimer = nil
+ guard on else { return }
+ let timer = DispatchSource.makeTimerSource(queue: self.queue)
+ timer.schedule(deadline: .now(), repeating: Self.meterInterval)
+ timer.setEventHandler { [weak self] in self?.emitAudioLevel() }
+ timer.resume()
+ self.meterTimer = timer
+ }
+ }
+
+ private func emitAudioLevel() {
+ guard session.isRunning,
+ let channels = movieOutput.connection(with: .audio)?.audioChannels,
+ !channels.isEmpty else {
+ events?(.audioLevel(nil))
+ return
+ }
+ let decibels = channels.map(\.averagePowerLevel).max() ?? Self.meterFloorDecibels
+ let level = (decibels - Self.meterFloorDecibels) / -Self.meterFloorDecibels
+ events?(.audioLevel(Double(min(max(level, 0), 1))))
+ }
+
func set(facing: CameraFacing) {
queue.async {
self.installCamera(facing: facing)
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraModel.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraModel.swift
index a3688e95..f7672fb9 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraModel.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CameraModel.swift
@@ -12,6 +12,9 @@ public protocol CameraControlling: AnyObject {
func apply(_ settings: CameraSettings)
func startRecording()
func stopRecording()
+ /// Starts or stops the stream of microphone levels that feeds
+ /// `CameraModel.audioLevel`.
+ func setAudioMetering(_ on: Bool)
}
nonisolated public enum CameraAvailability: Equatable, Sendable {
@@ -61,6 +64,21 @@ public final class CameraModel {
}
}
+ /// Whether the microphone meter sits on the picture. Kept across
+ /// sessions: a person burned by a silent take wants it every time.
+ public private(set) var showsAudioMeter = false {
+ didSet {
+ if showsAudioMeter != oldValue {
+ defaults.set(showsAudioMeter, forKey: Self.audioMeterKey)
+ }
+ }
+ }
+ /// How loud the microphone is right now, 0 for silence up to 1 for full
+ /// scale, read from the connection the movie file is written from. Nil
+ /// while the meter is off, and while the session has no microphone at
+ /// all, which is the case the meter exists to expose.
+ public internal(set) var audioLevel: Double?
+
public var isRecording: Bool { recordingStartedAt != nil }
public var controller: (any CameraControlling)?
@@ -68,6 +86,7 @@ public final class CameraModel {
private let defaults: UserDefaults
private static let teleprompterKey = "teleprompterSettings"
private static let safeZonesKey = "showsSafeZones"
+ private static let audioMeterKey = "showsAudioMeter"
private static let settingsKey = "cameraSettings"
private static let facingKey = "cameraFacing"
@@ -78,6 +97,7 @@ public final class CameraModel {
teleprompter.settings = settings
}
showsSafeZones = defaults.bool(forKey: Self.safeZonesKey)
+ showsAudioMeter = defaults.bool(forKey: Self.audioMeterKey)
if let data = defaults.data(forKey: Self.settingsKey),
let settings = try? JSONDecoder().decode(CameraSettings.self, from: data) {
self.settings = settings
@@ -91,6 +111,12 @@ public final class CameraModel {
showsSafeZones.toggle()
}
+ public func toggleAudioMeter() {
+ showsAudioMeter.toggle()
+ if !showsAudioMeter { audioLevel = nil }
+ controller?.setAudioMetering(showsAudioMeter)
+ }
+
private func persistSettings() {
guard let data = try? JSONEncoder().encode(settings) else { return }
defaults.set(data, forKey: Self.settingsKey)
@@ -196,6 +222,11 @@ public final class CameraModel {
availability = .unavailable(reason: reason)
}
+ public func audioLevelDidChange(_ level: Double?) {
+ guard showsAudioMeter else { return }
+ audioLevel = level
+ }
+
public func recordingDidStart() {
recordingStartedAt = .now
// A take starts the script from the top. The card is off screen while
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift
index e2120c92..22b30478 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/CameraScreen.swift
@@ -74,6 +74,19 @@ struct CameraScreen<CameraPreview: View>: View {
TeleprompterOverlay(camera: camera, onTap: toggleChrome)
}
controls
+ // The meter rides above the chrome too: a take clears the stage,
+ // and the sliver is how a person sees the microphone is live. It
+ // sits mid-way down the trailing edge, and climbs to the top
+ // corner when the phone is sideways and the shutter takes that
+ // edge.
+ if camera.showsAudioMeter, camera.availability == .running {
+ AudioMeter(level: camera.audioLevel)
+ .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: isSideways ? .topTrailing : .trailing)
+ .padding(.trailing, 14)
+ .padding(.top, isSideways ? 8 : 0)
+ .allowsHitTesting(false)
+ .transition(.opacity)
+ }
// The timer rides above the chrome and outlives it: a take clears
// the stage, and the red clock is how a person knows the camera is
// still rolling.
@@ -305,6 +318,15 @@ struct CameraScreen<CameraPreview: View>: View {
.glassEffect(camera.showsSafeZones ? .regular.tint(.white.opacity(0.25)).interactive() : .regular.interactive())
.accessibilityLabel(camera.showsSafeZones ? "Hide short-form guide" : "Show short-form guide")
+ Button {
+ camera.toggleAudioMeter()
+ } label: {
+ Image(systemName: "waveform")
+ .frame(width: 40, height: 40)
+ }
+ .glassEffect(camera.showsAudioMeter ? .regular.tint(.white.opacity(0.25)).interactive() : .regular.interactive())
+ .accessibilityLabel(camera.showsAudioMeter ? "Hide microphone meter" : "Show microphone meter")
+
Spacer().frame(height: 10)
Button {
@@ -563,6 +585,46 @@ struct FillLightOverlay: View {
}
}
+/// A vertical sliver that fills from the bottom with the microphone's level,
+/// red at a whisper through amber to green at speaking volume. A session
+/// with no microphone shows a crossed-out mic over an empty sliver: the take
+/// about to be shot would come out silent.
+struct AudioMeter: View {
+ /// 0...1, nil when the session has no microphone.
+ var level: Double?
+
+ private static var length: CGFloat { 140 }
+ private static var thickness: CGFloat { 6 }
+
+ private var fill: Color {
+ let level = level ?? 0
+ // Red below a quarter, amber through the middle, green from a half
+ // up: speech at a normal distance lands in the green.
+ return Color(hue: 0.33 * min(level * 2, 1), saturation: 0.9, brightness: 0.95)
+ }
+
+ var body: some View {
+ VStack(spacing: 6) {
+ Image(systemName: level == nil ? "mic.slash.fill" : "mic.fill")
+ .font(.caption2.weight(.bold))
+ .foregroundStyle(level == nil ? .red : .white)
+ Capsule()
+ .fill(.white.opacity(0.25))
+ .overlay(alignment: .bottom) {
+ Capsule()
+ .fill(fill)
+ .frame(height: Self.length * (level ?? 0))
+ .animation(.linear(duration: 0.05), value: level)
+ }
+ .frame(width: Self.thickness, height: Self.length)
+ }
+ .padding(8)
+ .background(.black.opacity(0.35), in: Capsule())
+ .accessibilityElement(children: .ignore)
+ .accessibilityLabel(level == nil ? "No microphone" : "Microphone level \(Int((level ?? 0) * 100)) percent")
+ }
+}
+
struct RecordingTimer: View {
var camera: CameraModel
diff --git a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
index 0c0a44ab..abb6aaad 100644
--- a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
+++ b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
@@ -1263,6 +1263,25 @@ import Testing
func apply(_ settings: CameraSettings) { recorded.append("apply") }
func startRecording() { recorded.append("start") }
func stopRecording() { recorded.append("stop") }
+ func setAudioMetering(_ on: Bool) { recorded.append("meter \(on)") }
+ }
+
+ @Test func audioMeterTogglesIsRememberedAndOnlyReadsWhileShown() {
+ let defaults = UserDefaults(suiteName: "meter-\(UUID())")!
+ let model = CameraModel(defaults: defaults)
+ let controller = FakeController()
+ model.controller = controller
+ #expect(!model.showsAudioMeter)
+ model.audioLevelDidChange(0.5)
+ #expect(model.audioLevel == nil)
+ model.toggleAudioMeter()
+ #expect(model.showsAudioMeter)
+ model.audioLevelDidChange(0.5)
+ #expect(model.audioLevel == 0.5)
+ #expect(CameraModel(defaults: defaults).showsAudioMeter)
+ model.toggleAudioMeter()
+ #expect(model.audioLevel == nil)
+ #expect(controller.recorded == ["meter true", "meter false"])
}
@Test func recordToggleRoutesThroughController() {
diff --git a/docs/guides/ios-app.md b/docs/guides/ios-app.md
index 575d5484..18832086 100644
--- a/docs/guides/ios-app.md
+++ b/docs/guides/ios-app.md
@@ -136,6 +136,11 @@ red keep-out the editor's preview draws for TikTok, Reels and Shorts, placed
against the recorded frame rather than the screen, kept across sessions, and
drawn under the script so a take is framed against it too.
+The rail button under it toggles a microphone meter, a vertical sliver that
+fills red through green as a person speaks, read from the connection the movie
+file is written from, so a take that would come out silent shows an empty meter
+before the shutter is pressed.
+
## The Watch
The watch app is a remote for the phone's camera: a record button, a red