@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
swift 63 lines 2.67 KB
Raw
1 import AudioToolbox
2 import ExpoModulesCore
3 import Foundation
4
5 public class T3VoiceTranscriptionModule: Module {
6 public func definition() -> ModuleDefinition {
7 Name("T3VoiceTranscription")
8
9 Function("isAppleSpeechAvailable") {
10 if #available(iOS 26, *) { return AppleSpeechEngine.isAvailable }
11 return false
12 }
13
14 AsyncFunction("prepareAppleSpeech") { (locale: String) async throws -> String? in
15 guard #available(iOS 26, *) else { throw CocoaError(.featureUnsupported) }
16 do {
17 return try await AppleSpeechEngine.prepare(locale)
18 } catch AppleSpeechEngine.Failure.unsupportedLocale {
19 return nil
20 }
21 }
22
23 AsyncFunction("transcribeWithAppleSpeech") { (uri: URL, locale: String) async throws -> String in
24 guard #available(iOS 26, *) else { throw CocoaError(.featureUnsupported) }
25 return try await AppleSpeechEngine.transcribe(locale, file: uri)
26 }
27
28 AsyncFunction("decodeRecording") { (uri: URL) throws -> Data in
29 guard uri.isFileURL else { throw CocoaError(.fileReadUnsupportedScheme) }
30 var opened: ExtAudioFileRef?
31 try check(ExtAudioFileOpenURL(uri as CFURL, &opened))
32 guard let file = opened else { throw CocoaError(.fileReadCorruptFile) }
33 defer { ExtAudioFileDispose(file) }
34 var format = AudioStreamBasicDescription(
35 mSampleRate: 16_000, mFormatID: kAudioFormatLinearPCM,
36 mFormatFlags: kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked,
37 mBytesPerPacket: 2, mFramesPerPacket: 1, mBytesPerFrame: 2,
38 mChannelsPerFrame: 1, mBitsPerChannel: 16, mReserved: 0)
39 let formatSize = UInt32(MemoryLayout<AudioStreamBasicDescription>.size)
40 try check(ExtAudioFileSetProperty(file, kExtAudioFileProperty_ClientDataFormat, formatSize, &format))
41 var pcm = Data()
42 var chunk = [Int16](repeating: 0, count: 8192)
43 try chunk.withUnsafeMutableBytes { bytes in
44 while true {
45 var frames = UInt32(bytes.count / 2)
46 var buffers = AudioBufferList(mNumberBuffers: 1, mBuffers: AudioBuffer(
47 mNumberChannels: 1, mDataByteSize: UInt32(bytes.count), mData: bytes.baseAddress))
48 try check(ExtAudioFileRead(file, &frames, &buffers))
49 if frames == 0 { break }
50 let count = Int(frames) * 2
51 guard pcm.count + count <= 16_000 * 2 * 301 else { throw CocoaError(.fileReadTooLarge) }
52 pcm.append(contentsOf: bytes.prefix(count))
53 }
54 }
55 guard !pcm.isEmpty else { throw CocoaError(.fileReadCorruptFile) }
56 return pcm
57 }
58 }
59 }
60
61 private func check(_ status: OSStatus) throws {
62 guard status == noErr else { throw NSError(domain: NSOSStatusErrorDomain, code: Int(status)) }
63 }