@otoji/core 0.0.0-placeholder → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +68 -4
- package/core.darwin-arm64.node +0 -0
- package/core.darwin-x64.node +0 -0
- package/core.linux-x64-gnu.node +0 -0
- package/core.win32-x64-msvc.node +0 -0
- package/main.d.ts +14 -0
- package/main.js +107 -0
- package/package.json +42 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Snowstar Miao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,5 +1,69 @@
|
|
|
1
|
-
#
|
|
1
|
+
# otoji (音字)
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
> realtime speech ⇄ text — *音を字に*
|
|
4
|
+
|
|
5
|
+
`otoji` is a Rust workspace that wires up streaming ASR, LLM-polished transcripts, and TTS behind a single `react-ink`-style terminal UI built on [`ratatui`](https://ratatui.rs).
|
|
6
|
+
|
|
7
|
+
```text
|
|
8
|
+
mic / file ──► AudioChunk ──► AsrProvider ──► AsrEvent ──► Polisher ──► TUI
|
|
9
|
+
└─► transcript.md
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Workspace layout
|
|
13
|
+
|
|
14
|
+
| Crate | Purpose |
|
|
15
|
+
|---|---|
|
|
16
|
+
| `otoji-core` | Shared types: `AudioChunk`, `AsrEvent`, `Word`, `OtojiError` |
|
|
17
|
+
| `otoji-audio` | Audio sources — `cpal` mic capture (with resampling) and PCM file replay |
|
|
18
|
+
| `otoji-asr` | `AsrProvider` trait + `iflytek_rtasr` (HMAC-SHA1 signa, WebSocket) |
|
|
19
|
+
| `otoji-tts` | `TtsProvider` trait + `iflytek_tts` (HMAC-SHA256 auth, MP3/PCM streaming) |
|
|
20
|
+
| `otoji-polish` | `Polisher` trait + `NoopPolisher` and `AnthropicPolisher` (Claude Haiku 4.5 default) |
|
|
21
|
+
| `otoji-cli` | `otoji` binary — clap subcommands + ratatui TUI |
|
|
22
|
+
|
|
23
|
+
See [`./docs/`](./docs/README.md) for the architecture rationale and the comparison of RT ASR providers (iFlytek RTASR / CoLi / SenseVoice / Whisper / Deepgram).
|
|
24
|
+
|
|
25
|
+
## Build
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
cargo build --release
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
# 1) Live mic → RTASR → polished TUI
|
|
35
|
+
export IFLYTEK_APP_ID=...
|
|
36
|
+
export IFLYTEK_API_KEY=...
|
|
37
|
+
export ANTHROPIC_API_KEY=... # optional, enables LLM polish layer
|
|
38
|
+
cargo run -p otoji-cli -- listen
|
|
39
|
+
|
|
40
|
+
# 2) Replay a 16kHz mono PCM file in real time
|
|
41
|
+
cargo run -p otoji-cli -- file 16k_10.pcm
|
|
42
|
+
|
|
43
|
+
# 3) Synthesize speech via iFlytek TTS
|
|
44
|
+
export IFLYTEK_TTS_API_KEY=...
|
|
45
|
+
export IFLYTEK_TTS_API_SECRET=...
|
|
46
|
+
cargo run -p otoji-cli -- speak "你好,世界" --out hello.mp3
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## TUI
|
|
50
|
+
|
|
51
|
+
The transcript view shows:
|
|
52
|
+
|
|
53
|
+
- `[seg_id]` confirmed segments in **white bold** (polished) or **gray** (raw, awaiting polish)
|
|
54
|
+
- The current partial hypothesis as `░ ...` in dark gray italic
|
|
55
|
+
- A header with provider state and counts
|
|
56
|
+
|
|
57
|
+
Press `q` / `Esc` / `Ctrl-C` to quit.
|
|
58
|
+
|
|
59
|
+
## Roadmap
|
|
60
|
+
|
|
61
|
+
- [ ] `otoji-asr/coli.rs` — CoLi ASR via ListenHub
|
|
62
|
+
- [ ] `otoji-asr/sensevoice.rs` — FunASR self-host bridge
|
|
63
|
+
- [ ] `otoji-tts/edge_tts.rs` — Microsoft Edge TTS as a free fallback
|
|
64
|
+
- [ ] `otoji-cli record` — write transcripts to `*.md` next to the source audio
|
|
65
|
+
- [ ] Bench harness (CER / latency / cost) under `crates/otoji-bench`
|
|
66
|
+
|
|
67
|
+
## License
|
|
68
|
+
|
|
69
|
+
MIT
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/main.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Re-export the napi-rs generated types and add the wrapper-only helpers.
|
|
2
|
+
export {
|
|
3
|
+
PolishOptions,
|
|
4
|
+
TranscribeOptions,
|
|
5
|
+
polishText,
|
|
6
|
+
transcribePcm,
|
|
7
|
+
} from "./index";
|
|
8
|
+
|
|
9
|
+
/** True if the platform-specific native binding loaded successfully. */
|
|
10
|
+
export declare function isNativeAvailable(): boolean;
|
|
11
|
+
|
|
12
|
+
/** The error captured the last time we tried to load the native binding,
|
|
13
|
+
* or `null` if it loaded successfully. */
|
|
14
|
+
export declare function nativeLoadError(): Error | null;
|
package/main.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// @otoji/core entry — wraps the napi-rs native binding with a portable
|
|
2
|
+
// fallback so `npm install @otoji/core` works on every platform, even
|
|
3
|
+
// the ones we don't ship a prebuilt `.node` for.
|
|
4
|
+
//
|
|
5
|
+
// Resolution order:
|
|
6
|
+
// 1. Try to load the native binding (./index.js generated by napi-rs;
|
|
7
|
+
// it picks the right `core.<triple>.node` or `@otoji/core-<triple>`
|
|
8
|
+
// sub-package for process.platform/arch).
|
|
9
|
+
// 2. On failure (unsupported OS/arch, missing optional dep, etc.),
|
|
10
|
+
// fall back to a pure-JS implementation.
|
|
11
|
+
//
|
|
12
|
+
// Fallback support:
|
|
13
|
+
// - polishText: full — calls Anthropic via global fetch.
|
|
14
|
+
// - transcribePcm: not implementable without sherpa-onnx; throws a
|
|
15
|
+
// descriptive error pointing the user at the
|
|
16
|
+
// supported native targets.
|
|
17
|
+
|
|
18
|
+
"use strict";
|
|
19
|
+
|
|
20
|
+
let native = null;
|
|
21
|
+
let nativeLoadError = null;
|
|
22
|
+
try {
|
|
23
|
+
// eslint-disable-next-line global-require
|
|
24
|
+
native = require("./index.js");
|
|
25
|
+
} catch (e) {
|
|
26
|
+
nativeLoadError = e;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const SUPPORTED = [
|
|
30
|
+
"darwin-arm64",
|
|
31
|
+
"darwin-x64",
|
|
32
|
+
"linux-x64-gnu",
|
|
33
|
+
"win32-x64-msvc",
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
const ANTHROPIC_URL = "https://api.anthropic.com/v1/messages";
|
|
37
|
+
|
|
38
|
+
async function polishTextFallback(opts) {
|
|
39
|
+
if (!opts || !opts.apiKey) {
|
|
40
|
+
throw new TypeError("polishText: opts.apiKey is required");
|
|
41
|
+
}
|
|
42
|
+
if (typeof opts.raw !== "string") {
|
|
43
|
+
throw new TypeError("polishText: opts.raw must be a string");
|
|
44
|
+
}
|
|
45
|
+
if (typeof fetch !== "function") {
|
|
46
|
+
throw new Error(
|
|
47
|
+
"@otoji/core JS fallback needs a global `fetch` (Node 18+ / Bun / Deno)"
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
const model = opts.model || "claude-haiku-4-5-20251001";
|
|
51
|
+
const prev = opts.prev || "(none)";
|
|
52
|
+
const system =
|
|
53
|
+
"You tidy ASR transcripts.\n" +
|
|
54
|
+
"- Preserve meaning. Do not summarize.\n" +
|
|
55
|
+
"- Add punctuation, drop fillers (uh/um/那个/えーと).\n" +
|
|
56
|
+
"- Normalize numbers, dates, units.\n" +
|
|
57
|
+
"- Keep code-switched text (zh/en/ja) as-is.\n" +
|
|
58
|
+
`- Previous sentence: ${prev}\n` +
|
|
59
|
+
"Output only the tidied sentence.";
|
|
60
|
+
const res = await fetch(ANTHROPIC_URL, {
|
|
61
|
+
method: "POST",
|
|
62
|
+
headers: {
|
|
63
|
+
"x-api-key": opts.apiKey,
|
|
64
|
+
"anthropic-version": "2023-06-01",
|
|
65
|
+
"content-type": "application/json",
|
|
66
|
+
},
|
|
67
|
+
body: JSON.stringify({
|
|
68
|
+
model,
|
|
69
|
+
max_tokens: 512,
|
|
70
|
+
system,
|
|
71
|
+
messages: [{ role: "user", content: opts.raw }],
|
|
72
|
+
}),
|
|
73
|
+
});
|
|
74
|
+
if (!res.ok) {
|
|
75
|
+
const body = await res.text().catch(() => "");
|
|
76
|
+
throw new Error(`anthropic ${res.status}: ${body}`);
|
|
77
|
+
}
|
|
78
|
+
const data = await res.json();
|
|
79
|
+
return data.content
|
|
80
|
+
.map((b) => b.text || "")
|
|
81
|
+
.join("")
|
|
82
|
+
.trim();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function transcribePcmFallback() {
|
|
86
|
+
const platform = `${process.platform}-${process.arch}`;
|
|
87
|
+
const reason = nativeLoadError ? ` (${nativeLoadError.message})` : "";
|
|
88
|
+
throw new Error(
|
|
89
|
+
`@otoji/core: transcribePcm requires the native binding, which is not ` +
|
|
90
|
+
`available for ${platform}${reason}. ` +
|
|
91
|
+
`Supported native targets: ${SUPPORTED.join(", ")}. ` +
|
|
92
|
+
`polishText still works via the JS fallback.`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
module.exports.polishText =
|
|
97
|
+
native && typeof native.polishText === "function"
|
|
98
|
+
? native.polishText
|
|
99
|
+
: polishTextFallback;
|
|
100
|
+
|
|
101
|
+
module.exports.transcribePcm =
|
|
102
|
+
native && typeof native.transcribePcm === "function"
|
|
103
|
+
? native.transcribePcm
|
|
104
|
+
: transcribePcmFallback;
|
|
105
|
+
|
|
106
|
+
module.exports.isNativeAvailable = () => native !== null;
|
|
107
|
+
module.exports.nativeLoadError = () => nativeLoadError;
|
package/package.json
CHANGED
|
@@ -1,11 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@otoji/core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "音字 — realtime speech ⇄ text. Node/Bun bindings for the otoji Rust crate (SenseVoice ASR + Anthropic polish).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/snomiao/otoji.git"
|
|
9
9
|
},
|
|
10
|
-
"
|
|
10
|
+
"main": "main.js",
|
|
11
|
+
"types": "main.d.ts",
|
|
12
|
+
"files": [
|
|
13
|
+
"main.js",
|
|
14
|
+
"main.d.ts",
|
|
15
|
+
"index.js",
|
|
16
|
+
"index.d.ts",
|
|
17
|
+
"*.node"
|
|
18
|
+
],
|
|
19
|
+
"napi": {
|
|
20
|
+
"name": "core",
|
|
21
|
+
"package": {
|
|
22
|
+
"name": "@otoji/core"
|
|
23
|
+
},
|
|
24
|
+
"triples": {
|
|
25
|
+
"defaults": false,
|
|
26
|
+
"additional": [
|
|
27
|
+
"x86_64-pc-windows-msvc",
|
|
28
|
+
"x86_64-apple-darwin",
|
|
29
|
+
"aarch64-apple-darwin",
|
|
30
|
+
"x86_64-unknown-linux-gnu"
|
|
31
|
+
]
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "napi build --platform --release --features node --cargo-flags=--lib",
|
|
36
|
+
"build:debug": "napi build --platform --features node --cargo-flags=--lib"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@napi-rs/cli": "^2.18.4"
|
|
40
|
+
},
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">= 18"
|
|
43
|
+
},
|
|
44
|
+
"optionalDependencies": {
|
|
45
|
+
"@otoji/core-darwin-arm64": "0.1.1",
|
|
46
|
+
"@otoji/core-darwin-x64": "0.1.1",
|
|
47
|
+
"@otoji/core-linux-x64-gnu": "0.1.1",
|
|
48
|
+
"@otoji/core-win32-x64-msvc": "0.1.1"
|
|
49
|
+
}
|
|
11
50
|
}
|