File size: 3,446 Bytes
64f7370
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# SpeechKit ASR

On-device speech recognition SDK for iOS 18+ / macOS 15+. Runs
Audio8-ASR-0.1B locally: Apple Neural Engine for the audio encoder, CPU int4
for the language-model decoder. No network request is required by the SDK.

The public demo also exposes process memory footprint, peak footprint, CPU,
thermal state, battery state, and a rough whole-device power estimate for
device-side validation.

## Installation

### 1. Add the package

```swift
dependencies: [
    .package(path: "../SpeechKit"),   // or the git URL + version once hosted
]
targets: [
    .target(name: "YourApp", dependencies: [
        .product(name: "SpeechKit", package: "SpeechKit"),
    ]),
]
```

### 2. Add the model bundle

Build it once (`python3 Scripts/make_asset_bundle.py`) -> `dist/ASRModels.bundle`
(~437 MB, models pre-compiled to `.mlmodelc`). Drag the bundle into your app
target as a **folder reference** (blue icon), or download it on first launch
and pass its URL to `ASRTranscriber(bundleURL:)`.

### 3. Info.plist

Only needed if you capture the microphone yourself:
`NSMicrophoneUsageDescription`.

## Usage

### One-shot transcription

```swift
import SpeechKit

let transcriber = try ASRTranscriber()      // finds ASRModels.bundle in main bundle
transcriber.warmUp()                        // optional; off main thread

let result = try transcriber.transcribe(samples)   // [Float], 16 kHz mono
print(result.text, result.timings.total)

// Async, with cancellation:
let token = ASRTranscriber.CancellationToken()
let result = try await transcriber.transcribe(samples, cancellation: token)

// Any audio file:
let result = try transcriber.transcribeFile(url)
```

Streaming and VAD are not part of this ASR-only build. Capture an utterance,
convert it to 16 kHz mono Float32, then call `transcribe`.

## Error handling

Every API throws `SpeechError` (SpeechCore). Cases cover asset validation
(`assetCorrupted`, `assetSchemaUnsupported`), audio input (`audioTooShort`),
inference (`inferenceFailed`, `promptTooLong`), and `cancelled`.

Pass `Options.verifyAssets = true` on the first launch after install/update to
SHA-256-check every asset (~1-2 s), then persist a flag and skip it.

## Threading contract

- `ASRTranscriber` methods are thread-safe; inference is serialized internally.
- Synchronous `transcribe` blocks; never call from the main thread.
- Async callbacks resume from an internal queue; dispatch to main for UI.

## Constraints

| | |
|---|---|
| Language | Model-defined |
| Audio window | 30 s max per call |
| First launch | ANE compilation ~30-60 s (system-cached afterwards); `warmUp()` recommended |
| Memory | ~600 MB resident with models loaded |
| Min OS | iOS 18 / macOS 15 (multifunction Core ML models) |

## Regression testing

```bash
swift run dev-check                              # lightweight local smoke check
swift test                                       # full Xcode recommended
swift run -c release asrkit-cli <workspace-dir>   # numeric regression vs Python refs
swift run -c release asrkit-cli <workspace-dir> --stress   # 100-pass memory check
swift run -c release asrkit-cli <workspace-dir> --file /path/to/audio.wav --repeat 10
```

`dev-check` does not require model assets and is the recommended first check on
a fresh machine. Some Command Line Tools-only installations cannot expose the
Apple Testing/XCTest frameworks cleanly; install and select full Xcode before
using `swift test`.