transcribe-cpp 0.2.1 → 0.2.3

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/README.md CHANGED
@@ -27,7 +27,7 @@ const model = await TranscribeModel.load("whisper-tiny-Q5_K_M.gguf");
27
27
  const result = await model.transcribe(pcm, { timestamps: "segment" });
28
28
 
29
29
  console.log(result.text);
30
- console.log(result.language); // detected or requested
30
+ console.log(result.language); // model-detected, or "" when unavailable/a hint was supplied
31
31
  for (const seg of result.segments) {
32
32
  console.log(`[${seg.t0Ms}–${seg.t1Ms}ms] ${seg.text}`);
33
33
  }
@@ -35,6 +35,17 @@ for (const seg of result.segments) {
35
35
  model.dispose();
36
36
  ```
37
37
 
38
+ ### Punctuation, capitalization, and text normalization
39
+
40
+ `pnc` and `itn` default to `"default"`, preserving each model family's shipped
41
+ behavior. Probe `model.supports("pnc")` or `model.supports("itn")` before
42
+ selecting `"off"`/`"on"`. Both options are available on single runs, batches,
43
+ and streams.
44
+
45
+ ```ts
46
+ const result = await model.transcribe(pcm, { pnc: "off", itn: "on" });
47
+ ```
48
+
38
49
  ### Streaming
39
50
 
40
51
  ```ts
@@ -47,6 +58,7 @@ for (const chunk of pcmChunks) {
47
58
  render(committed, tentative);
48
59
  }
49
60
  await stream.finalize();
61
+ const snapshot = stream.snapshot; // text, language, segments, words, tokens, timings
50
62
  stream.reset();
51
63
  ```
52
64
 
@@ -146,8 +158,9 @@ the teardown — is refused with `Busy`, by design.
146
158
  Because the compute is genuinely on another thread, **do not touch a session
147
159
  while a call against it is in flight** — it is single-threaded in the C library:
148
160
 
149
- - Reading a stream's `text`/`state`/`revision`/`lastStatus`, or a session's
150
- `limits`/`wasAborted`, during an un-awaited `feed`/`finalize`/`run` **throws**.
161
+ - Reading a stream's `text`/`snapshot`/`state`/`revision`/`lastStatus`, or a
162
+ session's `limits`/`wasAborted`, during an un-awaited
163
+ `feed`/`finalize`/`run`/`runBatch` **throws**.
151
164
  - `reset()` and `dispose()` are safe to call any time: the native teardown is
152
165
  deferred behind any in-flight call, so it never frees a session mid-compute.
153
166
  - Disposing a `Session` or `TranscribeModel` while a stream is still active
package/dist/index.d.ts CHANGED
@@ -7,7 +7,7 @@
7
7
  */
8
8
  import { setLogHandler, type LogHandler } from "./native.js";
9
9
  import { TranscribeError } from "./errors.js";
10
- import type { Backend, BackendInfo, BatchItem, Capabilities, FamilyExtension, Feature, ModelOptions, PcmLike, SessionLimits, SessionOptions, StreamOptions, StreamState, StreamText, StreamUpdate, TranscribeOptions, TranscriptionResult } from "./types.js";
10
+ import type { Backend, BackendInfo, BatchItem, Capabilities, FamilyExtension, Feature, ModelOptions, PcmLike, SessionLimits, SessionOptions, StreamOptions, StreamState, StreamText, StreamUpdate, Transcript, TranscribeOptions, TranscriptionResult } from "./types.js";
11
11
  export * from "./types.js";
12
12
  export * from "./errors.js";
13
13
  export { setLogHandler, type LogHandler };
@@ -65,6 +65,8 @@ export declare class Stream {
65
65
  finalize(): Promise<StreamUpdate>;
66
66
  /** Current text snapshot (copied at the boundary). */
67
67
  get text(): StreamText;
68
+ /** Full structured snapshot of the current hypothesis (owned copies). */
69
+ get snapshot(): Transcript;
68
70
  get state(): StreamState;
69
71
  get revision(): number;
70
72
  /**
package/dist/index.js CHANGED
@@ -39,6 +39,16 @@ const TIMESTAMPS = {
39
39
  token: g.TRANSCRIBE_TIMESTAMPS_TOKEN,
40
40
  };
41
41
  const TIMESTAMP_NAMES = Object.fromEntries(Object.entries(TIMESTAMPS).map(([k, v]) => [v, k]));
42
+ const PNC = {
43
+ default: g.TRANSCRIBE_PNC_MODE_DEFAULT,
44
+ off: g.TRANSCRIBE_PNC_MODE_OFF,
45
+ on: g.TRANSCRIBE_PNC_MODE_ON,
46
+ };
47
+ const ITN = {
48
+ default: g.TRANSCRIBE_ITN_MODE_DEFAULT,
49
+ off: g.TRANSCRIBE_ITN_MODE_OFF,
50
+ on: g.TRANSCRIBE_ITN_MODE_ON,
51
+ };
42
52
  const DIARIZE = {
43
53
  default: g.TRANSCRIBE_DIARIZE_MODE_DEFAULT,
44
54
  off: g.TRANSCRIBE_DIARIZE_MODE_OFF,
@@ -564,6 +574,7 @@ export class Session {
564
574
  if (this.#inFlight === kind)
565
575
  this.#inFlight = null;
566
576
  },
577
+ currentCompute: () => this.#inFlight,
567
578
  isCurrentStream: (stream) => this.#activeStream === stream,
568
579
  replaceCurrentStream: (stream) => {
569
580
  if (this.#activeStream && this.#activeStream !== stream) {
@@ -657,6 +668,8 @@ export class Session {
657
668
  // whisper resolves it to "segment" (its robust path), no-timestamp
658
669
  // families resolve to "none".
659
670
  p.timestamps = lookup(TIMESTAMPS, opts.timestamps ?? "auto", "timestamps");
671
+ p.pnc = lookup(PNC, opts.pnc ?? "default", "pnc");
672
+ p.itn = lookup(ITN, opts.itn ?? "default", "itn");
660
673
  p.diarize = lookup(DIARIZE, opts.diarize ?? "default", "diarize");
661
674
  if (opts.language !== undefined)
662
675
  p.language = opts.language;
@@ -744,6 +757,8 @@ export class Session {
744
757
  language: opts.language,
745
758
  targetLanguage: opts.targetLanguage,
746
759
  timestamps: opts.timestamps,
760
+ pnc: opts.pnc,
761
+ itn: opts.itn,
747
762
  diarize: opts.diarize,
748
763
  keepSpecialTags: opts.keepSpecialTags,
749
764
  specKDrafts: -1,
@@ -840,7 +855,6 @@ export class Stream {
840
855
  #keepalive;
841
856
  #active = true;
842
857
  #stale = false; // true once the session has begun a newer native stream
843
- #inFlight = false; // true while a feed/finalize native call runs on a worker
844
858
  #holdsLease = true; // born holding the model's compute lease (claimed at begin)
845
859
  /** @internal */
846
860
  constructor(n, session, lock, keepalive) {
@@ -904,9 +918,8 @@ export class Stream {
904
918
  // The native feed runs on a libuv worker. While it is in flight the
905
919
  // session must not be touched from the main thread — the C session API
906
920
  // is single-threaded (transcribe.h), and stream_get_text hands back
907
- // pointers the feed may free/realloc. Flag it so the read getters fail
908
- // fast instead of racing into a use-after-free.
909
- this.#inFlight = true;
921
+ // pointers the feed may free/realloc. Flag the owning session so every
922
+ // result getter fails fast instead of racing into a use-after-free.
910
923
  this.#sessionControl.enterCompute("feed()/finalize()");
911
924
  try {
912
925
  const status = await callAsync(n.F.streamFeed, h, samples, samples.length, u);
@@ -919,7 +932,6 @@ export class Stream {
919
932
  check(n, status, "transcribe_stream_feed");
920
933
  }
921
934
  finally {
922
- this.#inFlight = false;
923
935
  this.#sessionControl.leaveCompute("feed()/finalize()");
924
936
  }
925
937
  return toStreamUpdate(u);
@@ -935,13 +947,11 @@ export class Stream {
935
947
  return this.#lock.run(async () => {
936
948
  const u = {};
937
949
  n.F.streamUpdateInit(u);
938
- this.#inFlight = true; // see feed(): worker-thread compute, no concurrent reads
939
950
  this.#sessionControl.enterCompute("feed()/finalize()");
940
951
  try {
941
952
  check(n, await callAsync(n.F.streamFinalize, h, u), "transcribe_stream_finalize");
942
953
  }
943
954
  finally {
944
- this.#inFlight = false;
945
955
  this.#sessionControl.leaveCompute("feed()/finalize()");
946
956
  // Finalize ends the active stream (FINISHED on success, FAILED on
947
957
  // error), so the model is free again — release the lease either way.
@@ -951,22 +961,21 @@ export class Stream {
951
961
  });
952
962
  }
953
963
  /**
954
- * Reads borrow session-owned snapshot memory, so they are forbidden while a
955
- * feed()/finalize() is computing on a worker thread (concurrent use of a
956
- * single session is undefined per transcribe.h). The natural pattern —
957
- * `await stream.feed(chunk)` then read — is unaffected; this only rejects a
958
- * read issued against an un-awaited feed.
964
+ * Reads borrow session-owned snapshot memory, so they are forbidden while
965
+ * any worker call is computing on this session (concurrent use is undefined
966
+ * per transcribe.h). The natural await-then-read pattern is unaffected.
959
967
  */
960
- #assertNotFeeding(what) {
961
- if (this.#inFlight) {
962
- throw new TranscribeError(`cannot read stream ${what} while a feed()/finalize() is in flight; await it first`);
968
+ #assertNotComputing(what) {
969
+ const compute = this.#sessionControl.currentCompute();
970
+ if (compute) {
971
+ throw new TranscribeError(`cannot read stream ${what} while ${compute} is in flight; await it first`);
963
972
  }
964
973
  }
965
974
  /** Current text snapshot (copied at the boundary). */
966
975
  get text() {
967
976
  const h = this.#session.handle; // throws if the session was disposed
968
977
  this.#assertCurrent("read stream text");
969
- this.#assertNotFeeding("text");
978
+ this.#assertNotComputing("text");
970
979
  const n = this.#n;
971
980
  const t = {};
972
981
  n.F.streamTextInit(t);
@@ -977,18 +986,27 @@ export class Stream {
977
986
  tentative: t.tentative_text ?? "",
978
987
  };
979
988
  }
989
+ /** Full structured snapshot of the current hypothesis (owned copies). */
990
+ get snapshot() {
991
+ const h = this.#session.handle; // throws if the session was disposed
992
+ this.#assertCurrent("read stream snapshot");
993
+ if (!this.#active)
994
+ throw new TranscribeError("stream has been reset");
995
+ this.#assertNotComputing("snapshot");
996
+ return materialize(this.#n, singleAccessors(this.#n, h));
997
+ }
980
998
  get state() {
981
999
  const h = this.#session.handle; // throws if the session was disposed
982
1000
  this.#assertCurrent("read stream state");
983
1001
  if (!this.#active)
984
1002
  return "idle"; // reset() returns to idle; native reset may still be queued
985
- this.#assertNotFeeding("state");
1003
+ this.#assertNotComputing("state");
986
1004
  return STREAM_STATES[this.#n.F.streamGetState(h)] ?? "idle";
987
1005
  }
988
1006
  get revision() {
989
1007
  const h = this.#session.handle; // throws if the session was disposed
990
1008
  this.#assertCurrent("read stream revision");
991
- this.#assertNotFeeding("revision");
1009
+ this.#assertNotComputing("revision");
992
1010
  return this.#n.F.streamRevision(h);
993
1011
  }
994
1012
  /**
@@ -999,7 +1017,7 @@ export class Stream {
999
1017
  get lastStatus() {
1000
1018
  const h = this.#session.handle; // throws if the session was disposed
1001
1019
  this.#assertCurrent("read stream lastStatus");
1002
- this.#assertNotFeeding("lastStatus");
1020
+ this.#assertNotComputing("lastStatus");
1003
1021
  const n = this.#n;
1004
1022
  const status = n.F.streamLastStatus(h);
1005
1023
  if (status === g.TRANSCRIBE_OK)
package/dist/types.d.ts CHANGED
@@ -4,6 +4,8 @@ export type Backend = "auto" | "cpu" | "cpu_accel" | "cuda" | "rocm" | "vulkan"
4
4
  export type KvType = "auto" | "f32" | "f16";
5
5
  export type Task = "transcribe" | "translate";
6
6
  export type TimestampKind = "none" | "auto" | "segment" | "word" | "token";
7
+ export type Pnc = "default" | "off" | "on";
8
+ export type Itn = "default" | "off" | "on";
7
9
  export type Diarize = "default" | "off" | "on";
8
10
  export type Feature = "initial_prompt" | "temperature_fallback" | "long_form" | "cancellation" | "pnc" | "itn" | "diarization";
9
11
  /** Mono float32 PCM at the model's native sample rate (16 kHz for v1). */
@@ -65,12 +67,13 @@ export interface SessionLimits {
65
67
  effectiveMaxAudioMs: number;
66
68
  maxKvBytes: number;
67
69
  }
68
- export interface TranscriptionResult {
70
+ export interface Transcript {
69
71
  text: string;
70
72
  /** The model's decoded output before family post-processing (diarization
71
73
  * markers, timestamp/special tokens, tag filtering, whitespace trims).
72
74
  * Equal to `text` modulo whitespace for families that emit clean text. */
73
75
  rawText: string;
76
+ /** Model-detected language, or an empty string when none applies. */
74
77
  language: string;
75
78
  timestampKind: TimestampKind;
76
79
  segments: Segment[];
@@ -78,6 +81,9 @@ export interface TranscriptionResult {
78
81
  words: Word[];
79
82
  tokens: Token[];
80
83
  timings: Timings;
84
+ }
85
+ /** An offline transcript plus terminal run status flags. */
86
+ export interface TranscriptionResult extends Transcript {
81
87
  aborted: boolean;
82
88
  truncated: boolean;
83
89
  }
@@ -125,6 +131,10 @@ export interface TranscribeOptions {
125
131
  targetLanguage?: string;
126
132
  /** Default "auto" (richest the model supports, per-family). */
127
133
  timestamps?: TimestampKind;
134
+ /** Punctuation and capitalization control; default preserves the family default. */
135
+ pnc?: Pnc;
136
+ /** Inverse text normalization control; default preserves the family default. */
137
+ itn?: Itn;
128
138
  /** Default "default" (speaker attribution off for every family). */
129
139
  diarize?: Diarize;
130
140
  keepSpecialTags?: boolean;
@@ -171,6 +181,8 @@ export interface StreamOptions {
171
181
  language?: string;
172
182
  targetLanguage?: string;
173
183
  timestamps?: TimestampKind;
184
+ pnc?: Pnc;
185
+ itn?: Itn;
174
186
  diarize?: Diarize;
175
187
  keepSpecialTags?: boolean;
176
188
  commitPolicy?: CommitPolicy;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "transcribe-cpp",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "TypeScript/Node.js bindings for transcribe.cpp — a C/C++ speech-to-text library built on ggml",
5
5
  "type": "module",
6
6
  "exports": {
@@ -51,11 +51,11 @@
51
51
  "koffi": "^3.0.2"
52
52
  },
53
53
  "optionalDependencies": {
54
- "@transcribe-cpp/darwin-arm64-metal": "0.2.1",
55
- "@transcribe-cpp/darwin-x64-cpu": "0.2.1",
56
- "@transcribe-cpp/linux-x64-cpu-vulkan": "0.2.1",
57
- "@transcribe-cpp/linux-arm64-cpu-vulkan": "0.2.1",
58
- "@transcribe-cpp/win32-x64-cpu-vulkan": "0.2.1"
54
+ "@transcribe-cpp/darwin-arm64-metal": "0.2.3",
55
+ "@transcribe-cpp/darwin-x64-cpu": "0.2.3",
56
+ "@transcribe-cpp/linux-x64-cpu-vulkan": "0.2.3",
57
+ "@transcribe-cpp/linux-arm64-cpu-vulkan": "0.2.3",
58
+ "@transcribe-cpp/win32-x64-cpu-vulkan": "0.2.3"
59
59
  },
60
60
  "devDependencies": {
61
61
  "@types/node": "^22.0.0",