diff --git a/apps/mac/Packages/SillokKit/Sources/SillokEngine/Job.swift b/apps/mac/Packages/SillokKit/Sources/SillokEngine/Job.swift index 5437f1e..e3766a6 100644 --- a/apps/mac/Packages/SillokKit/Sources/SillokEngine/Job.swift +++ b/apps/mac/Packages/SillokKit/Sources/SillokEngine/Job.swift @@ -34,12 +34,11 @@ public struct TranscriptionResult: Sendable { public enum Transcription { public static func copy( - engine: Engine, input: URL, output: URL, events: @escaping @Sendable (TranscriptionEvent) -> Void ) async throws { - let job = try Job(engine: engine, request: Job.Request()) + let job = try Job(engine: nil, request: Job.Request()) try await job.perform { try job.copy(input: input, output: output, events: events) } } @@ -69,9 +68,9 @@ final class Job: @unchecked Sendable { private let handle: OpaquePointer - init(engine: Engine, request: Request) throws { + init(engine: Engine?, request: Request) throws { var job: OpaquePointer? - try check(sillok_job_create(engine.handle, try encodeJSON(request), &job)) + try check(sillok_job_create(engine?.handle, try encodeJSON(request), &job)) guard let job else { throw SillokError(code: .internalError, message: "no job") } handle = job } diff --git a/apps/mac/Packages/SillokKit/Tests/SillokEngineTests/TranscriptionTests.swift b/apps/mac/Packages/SillokKit/Tests/SillokEngineTests/TranscriptionTests.swift index d189df2..1a9c96d 100644 --- a/apps/mac/Packages/SillokKit/Tests/SillokEngineTests/TranscriptionTests.swift +++ b/apps/mac/Packages/SillokKit/Tests/SillokEngineTests/TranscriptionTests.swift @@ -44,13 +44,38 @@ import Testing #expect(previews.pieces.allSatisfy { !$0.draft }) } +let tone = URL(filePath: #filePath).appending(path: "../../../../../../../tests/data/media/tone.flac").standardized + +func temporaryCopy() -> URL { + FileManager.default.temporaryDirectory.appending(path: "sillok-\(UUID().uuidString).m4a") +} + +@Test func copiesARecordingWithoutModels() async throws { + let copy = temporaryCopy() + defer { try? FileManager.default.removeItem(at: copy) } + let events = Mutex<[TranscriptionEvent]>([]) + try await Transcription.copy(input: tone, output: copy) { event in events.withLock { $0.append(event) } } + #expect(FileManager.default.fileExists(atPath: copy.path())) + #expect(events.withLock { $0.contains(.reading(1)) && $0.last == .saving(1) }) +} + +@Test func cancellingStopsTheCopy() async throws { + let copy = temporaryCopy() + let task = Task { + withUnsafeCurrentTask { $0?.cancel() } + try await Transcription.copy(input: tone, output: copy) { _ in } + } + await #expect(throws: CancellationError.self) { try await task.value } + #expect(!FileManager.default.fileExists(atPath: copy.path())) +} + struct RealModels { static let environment = ProcessInfo.processInfo.environment static let available = environment["SILLOK_TEST_MODELS"] != nil && environment["SILLOK_TEST_AUDIO"] != nil let engine: Engine let audio: URL - let copy = FileManager.default.temporaryDirectory.appending(path: "sillok-\(UUID().uuidString).m4a") + let copy = temporaryCopy() let checkpoint = FileManager.default.temporaryDirectory.appending(path: "sillok-\(UUID().uuidString).jsonl") init() throws { @@ -64,7 +89,7 @@ struct RealModels { func transcribesACopyOfARecording() async throws { let real = try RealModels() defer { try? FileManager.default.removeItem(at: real.copy) } - try await Transcription.copy(engine: real.engine, input: real.audio, output: real.copy) { _ in } + try await Transcription.copy(input: real.audio, output: real.copy) { _ in } #expect(FileManager.default.fileExists(atPath: real.copy.path())) let events = Mutex<[TranscriptionEvent]>([]) @@ -81,14 +106,3 @@ func transcribesACopyOfARecording() async throws { #expect(previews) print("TRANSCRIPT", result.transcript.segments.map { "[\($0.speaker ?? -1)] \($0.text)" }) } - -@Test(.enabled(if: RealModels.available)) -func cancellingStopsTheCopy() async throws { - let real = try RealModels() - let task = Task { - try await Transcription.copy(engine: real.engine, input: real.audio, output: real.copy) { _ in } - } - task.cancel() - await #expect(throws: CancellationError.self) { try await task.value } - #expect(!FileManager.default.fileExists(atPath: real.copy.path())) -} diff --git a/apps/mac/Sillok/NewJob/JobQueue.swift b/apps/mac/Sillok/NewJob/JobQueue.swift index 63987a4..c8ee941 100644 --- a/apps/mac/Sillok/NewJob/JobQueue.swift +++ b/apps/mac/Sillok/NewJob/JobQueue.swift @@ -69,7 +69,6 @@ final class JobQueue { private(set) var completed: UUID? private let models: ModelManager private let library: Library - private var copier: (config: EngineConfig, engine: Engine)? private var transcriber: (config: EngineConfig, engine: Engine)? private var adder: Task? private var worker: Task? @@ -194,15 +193,11 @@ final class JobQueue { } let store = library.store do { - let config = EngineSettings.config( - modelsDirectory: models.directory, transcriptionModel: models.transcriptionModel?.id ?? item.modelID) - let engine = try cached(&copier, config) let staging = try store.makeStaging() do { try await perform(item.id) { events in try await Transcription.copy( - engine: engine, input: source, output: staging.appending(path: RecordingStore.audioName), - events: events) + input: source, output: staging.appending(path: RecordingStore.audioName), events: events) } var recording = Recording(id: item.id, title: item.title, sourceName: item.sourceName, options: item.options, transcriptionModel: item.modelID) diff --git a/include/sillok/sillok.h b/include/sillok/sillok.h index e87ea18..0e56250 100644 --- a/include/sillok/sillok.h +++ b/include/sillok/sillok.h @@ -165,7 +165,10 @@ SILLOK_API void sillok_engine_destroy(sillok_engine* engine); * file anew. Detecting speech and diarization run again in full, as they are * quick. The file is removed when the job succeeds and kept when it fails or * is cancelled: a new job with the same file then goes on, as after a crash. - * A file that cannot be written is dropped and the job carries on. */ + * A file that cannot be written is dropped and the job carries on. + * With `engine` NULL, the job only reads audio and saves it + * (sillok_job_save_audio), which needs no models: the language is not + * checked, and sillok_job_run() returns SILLOK_INVALID_ARGUMENT. */ SILLOK_API sillok_status sillok_job_create(sillok_engine* engine, const char* options_json, sillok_job** out_job); diff --git a/src/capi/sillok.cpp b/src/capi/sillok.cpp index 58f40e2..25b22cb 100644 --- a/src/capi/sillok.cpp +++ b/src/capi/sillok.cpp @@ -354,16 +354,18 @@ void sillok_engine_destroy(sillok_engine* engine) { delete engine; } sillok_status sillok_job_create(sillok_engine* engine, const char* options_json, sillok_job** out_job) { return Guard([&] { - RequireArgument(engine != nullptr && out_job != nullptr, - "engine and out_job are required"); + RequireArgument(out_job != nullptr, "out_job is required"); *out_job = nullptr; sillok::JobOptions options = sillok::ParseJobOptions(options_json ? options_json : ""); - const std::string& language = options.pipeline.language; - const std::vector languages = engine->state->models->Languages(); - RequireArgument(language == "auto" || - std::find(languages.begin(), languages.end(), language) != languages.end(), - "\"language\" must be \"auto\" or a language the transcription model knows"); - auto job = std::make_unique(engine->state, std::move(options)); + if (engine != nullptr) { + const std::string& language = options.pipeline.language; + const std::vector languages = engine->state->models->Languages(); + RequireArgument(language == "auto" || + std::find(languages.begin(), languages.end(), language) != languages.end(), + "\"language\" must be \"auto\" or a language the transcription model knows"); + } + auto job = std::make_unique(engine != nullptr ? engine->state : nullptr, + std::move(options)); *out_job = job.release(); return SILLOK_OK; }); @@ -446,6 +448,7 @@ sillok_status sillok_job_run(sillok_job* job, sillok_progress_fn progress, "job and out_json are required"); *out_json = nullptr; RequireArgument(!job->ran, "a job runs only once"); + RequireArgument(job->engine != nullptr, "a job without an engine cannot run"); bool idle = false; if (!job->engine->busy.compare_exchange_strong(idle, true)) { return sillok::Fail(SILLOK_BUSY, "the engine is running another job"); diff --git a/tests/unit/capi_test.cpp b/tests/unit/capi_test.cpp index 1a759cf..65d9e30 100644 --- a/tests/unit/capi_test.cpp +++ b/tests/unit/capi_test.cpp @@ -305,7 +305,7 @@ TEST_F(CapiTest, RejectsBadInput) { sillok_job* job = nullptr; EXPECT_EQ(sillok_job_create(Engine(), R"({"language": "fr"})", &job), SILLOK_INVALID_ARGUMENT); - EXPECT_EQ(sillok_job_create(nullptr, nullptr, &job), SILLOK_INVALID_ARGUMENT); + EXPECT_EQ(sillok_job_create(Engine(), nullptr, nullptr), SILLOK_INVALID_ARGUMENT); } TEST_F(CapiTest, JobRunsOnceAndTakesNoAudioAfterwards) { @@ -560,6 +560,22 @@ TEST_F(CapiTest, SavesTheRecordingForListeningBack) { SILLOK_MEDIA_ERROR); } +TEST_F(CapiTest, SavesTheRecordingWithoutAnEngine) { + sillok_job* job = Job(nullptr, R"({"language": "xx"})"); + ASSERT_EQ(sillok_job_push_file(job, PathToUtf8(MediaFile("tone.flac")).c_str(), nullptr, + nullptr), + SILLOK_OK) + << sillok_last_error(); + ASSERT_EQ(sillok_job_end_audio(job), SILLOK_OK); + const auto copy = models_dir_ / "copy.m4a"; + ASSERT_EQ(sillok_job_save_audio(job, PathToUtf8(copy).c_str(), nullptr, nullptr), SILLOK_OK) + << sillok_last_error(); + EXPECT_TRUE(std::filesystem::exists(copy)); + char* out = nullptr; + EXPECT_EQ(sillok_job_run(job, nullptr, nullptr, &out), SILLOK_INVALID_ARGUMENT); + EXPECT_EQ(out, nullptr); +} + TEST_F(CapiTest, CancelStopsReadingAFile) { sillok_job* job = Job(Engine()); sillok_job_cancel(job);