Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions Sources/verbemo/Audio/AudioSlicer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import Foundation

/// Cuts one audio file into consecutive pieces small enough to upload.
///
/// This exists for OpenAI, which refuses anything over 25 MB per request — a
/// limit an hour-long meeting passes at the mix's own bitrate. Nobody should
/// have to know that, so the engine slices, transcribes each piece, and shifts
/// OpenAI's engine chooses the tighter size or duration limit, then
/// slices, transcribes each piece, and shifts
/// the timings back onto the session's clock; the transcript comes out looking
/// like one pass.
///
Expand All @@ -19,6 +18,8 @@ enum AudioSlicer {
/// Where this piece starts in the source, in seconds. What gets added
/// back to every timestamp the engine returns for it.
let offset: TimeInterval
/// Source samples covered, excluding padding added by the AAC encoder.
let duration: TimeInterval
}

enum SliceError: Error, CustomStringConvertible {
Expand Down Expand Up @@ -63,7 +64,7 @@ enum AudioSlicer {
limit: Int64,
margin: Double = 0.85
) -> TimeInterval? {
guard bytes > limit, duration > 0, bytes > 0 else { return nil }
guard bytes > limit, duration.isFinite, duration > 0, bytes > 0 else { return nil }
let allowed = Double(limit) * margin
let seconds = duration * allowed / Double(bytes)
// Never below a minute: a limit that small means the numbers are wrong,
Expand All @@ -86,15 +87,18 @@ enum AudioSlicer {
}
let format = file.processingFormat
let rate = format.sampleRate
guard file.length > 0, rate > 0 else { throw SliceError.unreadable(source, nil) }
guard file.length > 0, rate.isFinite, rate > 0, seconds.isFinite, seconds > 0 else {
throw SliceError.unreadable(source, nil)
}

let duration = Double(file.length) / rate
guard duration > seconds else { return [Slice(url: source, offset: 0)] }
guard duration > seconds else { return [Slice(url: source, offset: 0, duration: duration)] }

try FileManager.default.createDirectory(
at: directory, withIntermediateDirectories: true)

let sliceFrames = AVAudioFramePosition(seconds * rate)
guard sliceFrames > 0 else { throw SliceError.unreadable(source, nil) }
// A second at a time, like the mixer: small enough that a long meeting
// never holds more than a few hundred kilobytes of audio in memory.
let blockFrames = AVAudioFrameCount(rate)
Expand Down Expand Up @@ -152,7 +156,7 @@ enum AudioSlicer {
try? FileManager.default.removeItem(at: url)
break
}
slices.append(Slice(url: url, offset: Double(start) / rate))
slices.append(Slice(url: url, offset: Double(start) / rate, duration: Double(written) / rate))
index += 1
}
return slices
Expand Down
74 changes: 57 additions & 17 deletions Sources/verbemo/Transcription/OpenAIEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import Foundation
/// returns, which is why both are `.mixed` engines and share everything
/// downstream.
///
/// Two things about the API leak into the code. Requests are capped at 25 MB,
/// which the mix passes at around 55 minutes, so a long meeting is cut into
/// pieces and the timings shifted back onto the session clock; the person who
/// recorded it never hears about any of this. And `chunking_strategy` is
/// Uploads are bounded by size and a defensive duration cap (see below),
/// with timings shifted back onto the session clock. `chunking_strategy` is
/// required for anything over 30 seconds — the API refuses outright without
/// it, so it is always sent.
///
Expand All @@ -25,6 +23,8 @@ actor OpenAITranscriptionEngine: TranscriptionEngine {
enum EngineError: TranscriptionFailure, CustomStringConvertible {
case noAPIKey
case empty
case invalidAudio
case uploadLimit

/// Only "there was no speech in this audio" is permanent; a silent
/// recording will still be silent tomorrow. HTTP answers are
Expand All @@ -46,6 +46,10 @@ actor OpenAITranscriptionEngine: TranscriptionEngine {
+ " (chmod 600) or set OPENAI_API_KEY"
case .empty:
return "openai returned no speech"
case .invalidAudio:
return "openai needs readable audio with a positive finite duration and upload limits"
case .uploadLimit:
return "openai audio exceeds the upload limits after encoding"
}
}
}
Expand All @@ -55,6 +59,12 @@ actor OpenAITranscriptionEngine: TranscriptionEngine {
/// documentation states and what a larger file is refused with.
static let defaultRequestLimit: Int64 = 25 * 1_048_576

/// Defensive, not a documented API contract: amanu PR #18 reported a
/// 1400s rejection. Cut at 1390s to leave 10s for AAC padding. Official
/// docs checked 2026-10-22 only establish 25 MB; see docs/testing/openai-duration-cap.md.
static let defaultDurationLimit: TimeInterval = 1390
private static let durationSlack: TimeInterval = 10

nonisolated let name = "openai"
nonisolated let model: String
nonisolated let input: TranscriptionInput = .mixed
Expand All @@ -69,12 +79,14 @@ actor OpenAITranscriptionEngine: TranscriptionEngine {
/// against real audio and the real API without a meeting long enough to
/// pass 25 MB — an hour of it, every time anyone wanted to check.
private let requestLimit: Int64
private let durationLimit: TimeInterval
private let http: CloudHTTP

/// Throws rather than failing at transcribe time — a missing key should
/// show up in the log the moment the engine is picked, not an upload later.
init(
requestLimit: Int64 = OpenAITranscriptionEngine.defaultRequestLimit,
durationLimit: TimeInterval = OpenAITranscriptionEngine.defaultDurationLimit,
apiKey: String? = nil,
session: URLSession = .shared,
retry: CloudHTTP.RetryPolicy = .standard,
Expand All @@ -85,6 +97,7 @@ actor OpenAITranscriptionEngine: TranscriptionEngine {
expected = MeetingLanguages.expected(primary: Config.transcriptionLanguage())
model = Config.openAITranscriptionModel()
self.requestLimit = requestLimit
self.durationLimit = durationLimit
http = CloudHTTP(service: .openAI, session: session, retry: retry, sleep: sleep)
}

Expand All @@ -100,7 +113,7 @@ actor OpenAITranscriptionEngine: TranscriptionEngine {

var segments: [TranscriptSegment] = []
for (index, slice) in slices.enumerated() {
let cache = cacheURL(in: dir, audio: audio, piece: index, of: slices.count)
let cache = cacheURL(in: dir, audio: audio, slice: slice, piece: index, of: slices.count)
let response: Response
if let cached = try? Data(contentsOf: cache),
let decoded = try? JSONDecoder().decode(Response.self, from: cached) {
Expand Down Expand Up @@ -130,15 +143,42 @@ actor OpenAITranscriptionEngine: TranscriptionEngine {
// MARK: - slicing

private func sliced(_ audio: URL) async throws -> [AudioSlicer.Slice] {
let whole = [AudioSlicer.Slice(url: audio, offset: 0)]
let attributes = try? FileManager.default.attributesOfItem(atPath: audio.path)
guard let bytes = attributes?[.size] as? Int64, bytes > requestLimit else {
return whole
let attributes = try FileManager.default.attributesOfItem(atPath: audio.path)
let bytes = attributes[.size] as? Int64 ?? 0
let duration = Self.duration(of: audio)
let slices: [AudioSlicer.Slice]
if let length = try Self.pieceLength(
bytes: bytes, duration: duration, requestLimit: requestLimit, durationLimit: durationLimit
) {
slices = try await AudioSlicer.slice(audio, every: length, into: Self.sliceDirectory(audio))
} else {
slices = [AudioSlicer.Slice(url: audio, offset: 0, duration: duration)]
}
// Average source bitrate is only an estimate; check the encoded files before any upload.
for slice in slices {
let size = try slice.url.resourceValues(forKeys: [.fileSizeKey]).fileSize ?? 0
let encodedDuration = try await AVURLAsset(url: slice.url).load(.duration).seconds
guard size > 0, encodedDuration.isFinite, encodedDuration > 0 else {
throw EngineError.invalidAudio
}
guard size <= requestLimit, encodedDuration <= durationLimit + Self.durationSlack else {
throw EngineError.uploadLimit
}
}
guard let length = AudioSlicer.sliceLength(
bytes: bytes, duration: Self.duration(of: audio), limit: requestLimit
) else { return whole }
return try await AudioSlicer.slice(audio, every: length, into: Self.sliceDirectory(audio))
return slices
}

/// Adapted from Dmitry Tsoy's Amanu PR #18: the tighter ceiling wins (see docs/history/openai-duration-cap-upstream.md).
static func pieceLength(
bytes: Int64, duration: TimeInterval,
requestLimit: Int64 = defaultRequestLimit,
durationLimit: TimeInterval = defaultDurationLimit
) throws -> TimeInterval? {
guard bytes > 0, requestLimit > 0, duration.isFinite, duration > 0,
durationLimit.isFinite, durationLimit > 0 else { throw EngineError.invalidAudio }
let bySize = AudioSlicer.sliceLength(bytes: bytes, duration: duration, limit: requestLimit)
guard bySize != nil || duration > durationLimit else { return nil }
return min(bySize ?? durationLimit, durationLimit)
}

private static func sliceDirectory(_ audio: URL) -> URL {
Expand Down Expand Up @@ -190,14 +230,14 @@ actor OpenAITranscriptionEngine: TranscriptionEngine {
// MARK: - shaping

/// Where one piece's response is cached: named for the model, the
/// language sent, the request limit that decided the cut, and which piece
/// of how many it is.
func cacheURL(in dir: URL, audio: URL, piece index: Int, of count: Int) -> URL {
/// language sent, and actual source boundaries. A changed cut must never
/// reuse a response just because the piece number and count still match.
func cacheURL(in dir: URL, audio: URL, slice: AudioSlicer.Slice, piece index: Int, of count: Int) -> URL {
ProviderCache.url(
in: dir, provider: .openAI,
parts: [
audio.lastPathComponent, model, Self.languageField(for: expected) ?? "detect",
"\(requestLimit)", "\(count)",
"\(requestLimit)", "\(count)", "slices-v2", "\(slice.offset)", "\(slice.duration)",
],
suffix: count > 1 ? "\(index + 1)" : nil)
}
Expand Down
Loading
Loading