Skip to content
Draft
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
4 changes: 4 additions & 0 deletions apps/mobile/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ const config: ExpoConfig = {
extraPods: [
{ name: "GoogleUtilities", modular_headers: true },
{ name: "RecaptchaInterop", modular_headers: true },
{
name: "TranscribeCpp",
podspec: "../modules/t3-voice-transcription/ios/vendor/TranscribeCpp.podspec.json",
},
],
},
},
Expand Down
5 changes: 5 additions & 0 deletions apps/mobile/fingerprint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ module.exports = {
filePath: "../../native/apple-speech/AppleSpeechEngine.swift",
reasons: ["appleSpeech"],
},
{
type: "file",
filePath: "../server/src/speech/catalog.json",
reasons: ["deviceSpeechCatalog"],
},
],
};
98 changes: 98 additions & 0 deletions apps/mobile/modules/t3-voice-transcription/THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# transcribe.cpp v0.4.1 for iOS

## LICENSE

MIT License

Copyright (c) 2026 The transcribe.cpp authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

## LICENSE.ggml

MIT License

Copyright (c) 2023-2026 The ggml authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

## LICENSE.miniz

Copyright 2013-2014 RAD Game Tools and Valve Software
Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC

All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

## LICENSE.silero_vad

MIT License

Copyright (c) 2020-present Silero Team

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import CTranscribe
import Foundation
import os

struct DeviceSpeechModel: Decodable, Sendable {
struct Accuracy: Decodable, Sendable {
let grade: String
let errorPercent: Double
}

struct Benchmark: Decodable, Sendable {
let accuracy: [String: Accuracy]
}

let id: String
let name: String
let description: String
let languages: [String]
let revision: String
let filename: String
let size: Int
let sha256: String
let benchmark: Benchmark

var payload: [String: Any] {
[
"id": id, "name": name, "description": description, "languages": languages, "size": size,
"benchmark": [
"accuracy": benchmark.accuracy.mapValues { ["grade": $0.grade, "errorPercent": $0.errorPercent] },
"secondsPer30s": ["accelerated": NSNull()],
],
]
}

func fits(availableMemory: Int) -> Bool {
// Budget for another copy of the weights and graph/audio buffers.
let buffers = 512 * 1024 * 1024
return size > 0 && availableMemory >= buffers && size <= (availableMemory - buffers) / 2
}
}

enum DeviceSpeechCatalog {
private struct Manifest: Decodable {
let engineVersion: String
let models: [DeviceSpeechModel]
}

static let models: [DeviceSpeechModel] = {
guard let resources = Bundle.main.url(forResource: "T3SpeechModels", withExtension: "bundle"),
let url = Bundle(url: resources)?.url(forResource: "catalog", withExtension: "json"),
let data = try? Data(contentsOf: url),
let catalog = try? JSONDecoder().decode(Manifest.self, from: data),
let version = transcribe_version(), catalog.engineVersion == String(cString: version)
else { return [] }
return catalog.models
}()

static let availableModels = {
let budget = availableMemory
return models.filter { $0.fits(availableMemory: budget) }
}()

static var availableMemory: Int {
#if os(iOS) && !targetEnvironment(simulator)
Int(clamping: os_proc_available_memory())
#else
Int(clamping: ProcessInfo.processInfo.physicalMemory)
#endif
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import CTranscribe
import Foundation
import Synchronization

final class SpeechInferenceCancellation: Sendable {
private let cancelled = Mutex(false)

var isCancelled: Bool { cancelled.withLock { $0 } }

func cancel() {
cancelled.withLock { $0 = true }
}

func check() throws {
if isCancelled { throw CancellationError() }
}
}

enum DeviceSpeechEngine {
struct Transcript: Sendable {
let text: String
let language: String?

var payload: [String: String] {
var result = ["text": text]
result["language"] = language
return result
}
}

private struct Failure: Error {
let status: transcribe_status
}

static func transcribe(file: URL, model: URL, cancellation: SpeechInferenceCancellation) throws
-> Transcript
{
try cancellation.check()
let samples = try VoiceRecordingPCM.float32(file: file, checkCancellation: cancellation.check)
do {
return try run(samples, model: model, backend: TRANSCRIBE_BACKEND_AUTO, cancellation: cancellation)
} catch let error as Failure where error.status == TRANSCRIBE_ERR_BACKEND {
return try run(samples, model: model, backend: TRANSCRIBE_BACKEND_CPU, cancellation: cancellation)
}
}

private static func run(
_ samples: [Float], model: URL, backend: transcribe_backend_request,
cancellation: SpeechInferenceCancellation
) throws -> Transcript {
try cancellation.check()

var loadParameters = transcribe_model_load_params()
transcribe_model_load_params_init(&loadParameters)
loadParameters.backend = backend
var opened: OpaquePointer?
try check(transcribe_open(model.path, &loadParameters, nil, &opened))
guard let session = opened else { throw CocoaError(.fileReadCorruptFile) }
defer { transcribe_session_free(session) }
try cancellation.check()

// The run can call back on this pointer until it returns. Clear it before releasing the retain.
let context = Unmanaged.passRetained(cancellation)
transcribe_set_abort_callback(
session,
{ data in
guard let data else { return false }
return Unmanaged<SpeechInferenceCancellation>.fromOpaque(data).takeUnretainedValue()
.isCancelled
}, context.toOpaque())
defer {
transcribe_set_abort_callback(session, nil, nil)
context.release()
}

var parameters = transcribe_run_params()
transcribe_run_params_init(&parameters)
parameters.timestamps = TRANSCRIBE_TIMESTAMPS_NONE
let hint = try languageHint(session)
let status = samples.withUnsafeBufferPointer { buffer in
if let hint {
return hint.withCString { value in
parameters.language = value
return transcribe_run(session, buffer.baseAddress, Int32(buffer.count), &parameters)
}
}
return transcribe_run(session, buffer.baseAddress, Int32(buffer.count), &parameters)
}
try cancellation.check()
try check(status)
guard !transcribe_was_truncated(session) else { throw CocoaError(.fileReadTooLarge) }
let language = transcribe_detected_language(session).flatMap { code in
let language = String(cString: code)
return language.isEmpty ? nil : language
}
return Transcript(
text: transcribe_full_text(session).map { String(cString: $0) } ?? "",
language: language ?? hint)
}

private static func languageHint(_ session: OpaquePointer) throws -> String? {
var capabilities = transcribe_capabilities()
transcribe_capabilities_init(&capabilities)
try check(transcribe_model_get_capabilities(transcribe_get_model(session), &capabilities))
guard !capabilities.supports_language_detect, let languages = capabilities.languages else {
return nil
}
let codes = (0..<Int(capabilities.n_languages)).compactMap {
languages[$0].map { String(cString: $0) }
}
for preferred in Locale.preferredLanguages + ["en"] {
let tag = preferred.lowercased().replacingOccurrences(of: "_", with: "-")
if let exact = codes.first(where: { $0.lowercased() == tag }) { return exact }
let base = tag.split(separator: "-").first
if let match = codes.first(where: { $0.lowercased().split(separator: "-").first == base }) {
return match
}
}
return codes.first
}

private static func check(_ status: transcribe_status) throws {
guard status == TRANSCRIBE_OK else {
throw Failure(status: status)
}
}
}
Loading
Loading