@charivo/stt 0.2.0
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 +41 -0
- package/dist/chunk-EKUYDBHB.mjs +68 -0
- package/dist/index.d.mts +68 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.js +147 -0
- package/dist/index.mjs +57 -0
- package/dist/openai/index.d.mts +39 -0
- package/dist/openai/index.d.ts +39 -0
- package/dist/openai/index.js +204 -0
- package/dist/openai/index.mjs +105 -0
- package/dist/remote/index.d.mts +32 -0
- package/dist/remote/index.d.ts +32 -0
- package/dist/remote/index.js +169 -0
- package/dist/remote/index.mjs +80 -0
- package/dist/web/index.d.mts +46 -0
- package/dist/web/index.d.ts +46 -0
- package/dist/web/index.js +142 -0
- package/dist/web/index.mjs +116 -0
- package/package.json +73 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/openai/index.ts
|
|
31
|
+
var openai_exports = {};
|
|
32
|
+
__export(openai_exports, {
|
|
33
|
+
OpenAISTTTranscriber: () => OpenAISTTTranscriber,
|
|
34
|
+
createOpenAISTTTranscriber: () => createOpenAISTTTranscriber
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(openai_exports);
|
|
37
|
+
|
|
38
|
+
// src/media-recorder-helper.ts
|
|
39
|
+
var MediaRecorderHelper = class {
|
|
40
|
+
mediaRecorder = null;
|
|
41
|
+
audioChunks = [];
|
|
42
|
+
recording = false;
|
|
43
|
+
/**
|
|
44
|
+
* Start audio recording from user's microphone
|
|
45
|
+
*/
|
|
46
|
+
async start() {
|
|
47
|
+
if (this.recording) {
|
|
48
|
+
throw new Error("Already recording");
|
|
49
|
+
}
|
|
50
|
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
51
|
+
this.mediaRecorder = new MediaRecorder(stream);
|
|
52
|
+
this.audioChunks = [];
|
|
53
|
+
this.mediaRecorder.ondataavailable = (event) => {
|
|
54
|
+
if (event.data.size > 0) {
|
|
55
|
+
this.audioChunks.push(event.data);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
this.mediaRecorder.start();
|
|
59
|
+
this.recording = true;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Stop audio recording and return the recorded audio as a Blob
|
|
63
|
+
*/
|
|
64
|
+
async stop() {
|
|
65
|
+
if (!this.recording || !this.mediaRecorder) {
|
|
66
|
+
throw new Error("Not recording");
|
|
67
|
+
}
|
|
68
|
+
return new Promise((resolve, reject) => {
|
|
69
|
+
this.mediaRecorder.onstop = () => {
|
|
70
|
+
const audioBlob = new Blob(this.audioChunks, { type: "audio/webm" });
|
|
71
|
+
this.audioChunks = [];
|
|
72
|
+
this.mediaRecorder?.stream.getTracks().forEach((track) => track.stop());
|
|
73
|
+
this.mediaRecorder = null;
|
|
74
|
+
this.recording = false;
|
|
75
|
+
resolve(audioBlob);
|
|
76
|
+
};
|
|
77
|
+
this.mediaRecorder.onerror = (error) => {
|
|
78
|
+
this.recording = false;
|
|
79
|
+
reject(error);
|
|
80
|
+
};
|
|
81
|
+
this.mediaRecorder.stop();
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Check if currently recording
|
|
86
|
+
*/
|
|
87
|
+
isRecording() {
|
|
88
|
+
return this.recording;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Abort recording immediately without returning data
|
|
92
|
+
*/
|
|
93
|
+
abort() {
|
|
94
|
+
if (this.mediaRecorder) {
|
|
95
|
+
this.mediaRecorder.stream.getTracks().forEach((track) => track.stop());
|
|
96
|
+
this.mediaRecorder = null;
|
|
97
|
+
this.audioChunks = [];
|
|
98
|
+
this.recording = false;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// src/openai/provider.ts
|
|
104
|
+
var import_openai = __toESM(require("openai"));
|
|
105
|
+
var REQUEST_TIMEOUT_MS = 3e4;
|
|
106
|
+
var OpenAISTTProvider = class {
|
|
107
|
+
openai;
|
|
108
|
+
defaultModel;
|
|
109
|
+
defaultLanguage;
|
|
110
|
+
constructor(config) {
|
|
111
|
+
if (typeof window !== "undefined" && !config.dangerouslyAllowBrowser) {
|
|
112
|
+
throw new Error(
|
|
113
|
+
"OpenAI provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing"
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
this.openai = new import_openai.default({
|
|
117
|
+
apiKey: config.apiKey,
|
|
118
|
+
dangerouslyAllowBrowser: config.dangerouslyAllowBrowser
|
|
119
|
+
});
|
|
120
|
+
this.defaultModel = config.defaultModel || "whisper-1";
|
|
121
|
+
this.defaultLanguage = config.defaultLanguage;
|
|
122
|
+
}
|
|
123
|
+
async transcribe(audio, options) {
|
|
124
|
+
const audioBlob = audio instanceof Blob ? audio : new Blob([audio], { type: "audio/wav" });
|
|
125
|
+
const audioFile = new File([audioBlob], "audio.wav", {
|
|
126
|
+
type: "audio/wav"
|
|
127
|
+
});
|
|
128
|
+
const response = await withTimeout(
|
|
129
|
+
this.openai.audio.transcriptions.create({
|
|
130
|
+
file: audioFile,
|
|
131
|
+
model: this.defaultModel,
|
|
132
|
+
language: options?.language || this.defaultLanguage
|
|
133
|
+
}),
|
|
134
|
+
`OpenAI STT request timed out after ${REQUEST_TIMEOUT_MS}ms`
|
|
135
|
+
);
|
|
136
|
+
return response.text;
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
function createOpenAISTTProvider(config) {
|
|
140
|
+
return new OpenAISTTProvider(config);
|
|
141
|
+
}
|
|
142
|
+
async function withTimeout(promise, timeoutMessage) {
|
|
143
|
+
let timeoutId;
|
|
144
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
145
|
+
timeoutId = setTimeout(
|
|
146
|
+
() => reject(new Error(timeoutMessage)),
|
|
147
|
+
REQUEST_TIMEOUT_MS
|
|
148
|
+
);
|
|
149
|
+
});
|
|
150
|
+
try {
|
|
151
|
+
return await Promise.race([promise, timeoutPromise]);
|
|
152
|
+
} finally {
|
|
153
|
+
if (timeoutId) {
|
|
154
|
+
clearTimeout(timeoutId);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// src/openai/index.ts
|
|
160
|
+
var OpenAISTTTranscriber = class {
|
|
161
|
+
provider;
|
|
162
|
+
recorder;
|
|
163
|
+
recordingOptions;
|
|
164
|
+
constructor(config) {
|
|
165
|
+
this.provider = createOpenAISTTProvider({
|
|
166
|
+
...config,
|
|
167
|
+
dangerouslyAllowBrowser: true
|
|
168
|
+
});
|
|
169
|
+
this.recorder = new MediaRecorderHelper();
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Start recording audio from microphone
|
|
173
|
+
*/
|
|
174
|
+
async startRecording(options) {
|
|
175
|
+
this.recordingOptions = options;
|
|
176
|
+
await this.recorder.start();
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Stop recording and transcribe audio to text
|
|
180
|
+
*/
|
|
181
|
+
async stopRecording() {
|
|
182
|
+
const audioBlob = await this.recorder.stop();
|
|
183
|
+
const transcription = await this.provider.transcribe(
|
|
184
|
+
audioBlob,
|
|
185
|
+
this.recordingOptions
|
|
186
|
+
);
|
|
187
|
+
this.recordingOptions = void 0;
|
|
188
|
+
return transcription;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Check if currently recording
|
|
192
|
+
*/
|
|
193
|
+
isRecording() {
|
|
194
|
+
return this.recorder.isRecording();
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
function createOpenAISTTTranscriber(config) {
|
|
198
|
+
return new OpenAISTTTranscriber(config);
|
|
199
|
+
}
|
|
200
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
201
|
+
0 && (module.exports = {
|
|
202
|
+
OpenAISTTTranscriber,
|
|
203
|
+
createOpenAISTTTranscriber
|
|
204
|
+
});
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MediaRecorderHelper
|
|
3
|
+
} from "../chunk-EKUYDBHB.mjs";
|
|
4
|
+
|
|
5
|
+
// src/openai/provider.ts
|
|
6
|
+
import OpenAI from "openai";
|
|
7
|
+
var REQUEST_TIMEOUT_MS = 3e4;
|
|
8
|
+
var OpenAISTTProvider = class {
|
|
9
|
+
openai;
|
|
10
|
+
defaultModel;
|
|
11
|
+
defaultLanguage;
|
|
12
|
+
constructor(config) {
|
|
13
|
+
if (typeof window !== "undefined" && !config.dangerouslyAllowBrowser) {
|
|
14
|
+
throw new Error(
|
|
15
|
+
"OpenAI provider is for server-side use only. Set dangerouslyAllowBrowser: true for testing"
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
this.openai = new OpenAI({
|
|
19
|
+
apiKey: config.apiKey,
|
|
20
|
+
dangerouslyAllowBrowser: config.dangerouslyAllowBrowser
|
|
21
|
+
});
|
|
22
|
+
this.defaultModel = config.defaultModel || "whisper-1";
|
|
23
|
+
this.defaultLanguage = config.defaultLanguage;
|
|
24
|
+
}
|
|
25
|
+
async transcribe(audio, options) {
|
|
26
|
+
const audioBlob = audio instanceof Blob ? audio : new Blob([audio], { type: "audio/wav" });
|
|
27
|
+
const audioFile = new File([audioBlob], "audio.wav", {
|
|
28
|
+
type: "audio/wav"
|
|
29
|
+
});
|
|
30
|
+
const response = await withTimeout(
|
|
31
|
+
this.openai.audio.transcriptions.create({
|
|
32
|
+
file: audioFile,
|
|
33
|
+
model: this.defaultModel,
|
|
34
|
+
language: options?.language || this.defaultLanguage
|
|
35
|
+
}),
|
|
36
|
+
`OpenAI STT request timed out after ${REQUEST_TIMEOUT_MS}ms`
|
|
37
|
+
);
|
|
38
|
+
return response.text;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
function createOpenAISTTProvider(config) {
|
|
42
|
+
return new OpenAISTTProvider(config);
|
|
43
|
+
}
|
|
44
|
+
async function withTimeout(promise, timeoutMessage) {
|
|
45
|
+
let timeoutId;
|
|
46
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
47
|
+
timeoutId = setTimeout(
|
|
48
|
+
() => reject(new Error(timeoutMessage)),
|
|
49
|
+
REQUEST_TIMEOUT_MS
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
try {
|
|
53
|
+
return await Promise.race([promise, timeoutPromise]);
|
|
54
|
+
} finally {
|
|
55
|
+
if (timeoutId) {
|
|
56
|
+
clearTimeout(timeoutId);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/openai/index.ts
|
|
62
|
+
var OpenAISTTTranscriber = class {
|
|
63
|
+
provider;
|
|
64
|
+
recorder;
|
|
65
|
+
recordingOptions;
|
|
66
|
+
constructor(config) {
|
|
67
|
+
this.provider = createOpenAISTTProvider({
|
|
68
|
+
...config,
|
|
69
|
+
dangerouslyAllowBrowser: true
|
|
70
|
+
});
|
|
71
|
+
this.recorder = new MediaRecorderHelper();
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Start recording audio from microphone
|
|
75
|
+
*/
|
|
76
|
+
async startRecording(options) {
|
|
77
|
+
this.recordingOptions = options;
|
|
78
|
+
await this.recorder.start();
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Stop recording and transcribe audio to text
|
|
82
|
+
*/
|
|
83
|
+
async stopRecording() {
|
|
84
|
+
const audioBlob = await this.recorder.stop();
|
|
85
|
+
const transcription = await this.provider.transcribe(
|
|
86
|
+
audioBlob,
|
|
87
|
+
this.recordingOptions
|
|
88
|
+
);
|
|
89
|
+
this.recordingOptions = void 0;
|
|
90
|
+
return transcription;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Check if currently recording
|
|
94
|
+
*/
|
|
95
|
+
isRecording() {
|
|
96
|
+
return this.recorder.isRecording();
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
function createOpenAISTTTranscriber(config) {
|
|
100
|
+
return new OpenAISTTTranscriber(config);
|
|
101
|
+
}
|
|
102
|
+
export {
|
|
103
|
+
OpenAISTTTranscriber,
|
|
104
|
+
createOpenAISTTTranscriber
|
|
105
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { STTTranscriber, STTOptions } from '@charivo/core';
|
|
2
|
+
|
|
3
|
+
interface RemoteSTTConfig {
|
|
4
|
+
apiEndpoint?: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Remote STT Transcriber - Uses remote server's STT API for transcription
|
|
8
|
+
*
|
|
9
|
+
* Processes STT on the server and receives transcribed text
|
|
10
|
+
* Handles recording internally using MediaRecorderHelper
|
|
11
|
+
*/
|
|
12
|
+
declare class RemoteSTTTranscriber implements STTTranscriber {
|
|
13
|
+
private apiEndpoint;
|
|
14
|
+
private recorder;
|
|
15
|
+
private recordingOptions?;
|
|
16
|
+
constructor(config?: RemoteSTTConfig);
|
|
17
|
+
/**
|
|
18
|
+
* Start recording audio from microphone
|
|
19
|
+
*/
|
|
20
|
+
startRecording(options?: STTOptions): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Stop recording and transcribe audio to text using remote API
|
|
23
|
+
*/
|
|
24
|
+
stopRecording(): Promise<string>;
|
|
25
|
+
/**
|
|
26
|
+
* Check if currently recording
|
|
27
|
+
*/
|
|
28
|
+
isRecording(): boolean;
|
|
29
|
+
}
|
|
30
|
+
declare function createRemoteSTTTranscriber(config?: RemoteSTTConfig): RemoteSTTTranscriber;
|
|
31
|
+
|
|
32
|
+
export { type RemoteSTTConfig, RemoteSTTTranscriber, createRemoteSTTTranscriber };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { STTTranscriber, STTOptions } from '@charivo/core';
|
|
2
|
+
|
|
3
|
+
interface RemoteSTTConfig {
|
|
4
|
+
apiEndpoint?: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Remote STT Transcriber - Uses remote server's STT API for transcription
|
|
8
|
+
*
|
|
9
|
+
* Processes STT on the server and receives transcribed text
|
|
10
|
+
* Handles recording internally using MediaRecorderHelper
|
|
11
|
+
*/
|
|
12
|
+
declare class RemoteSTTTranscriber implements STTTranscriber {
|
|
13
|
+
private apiEndpoint;
|
|
14
|
+
private recorder;
|
|
15
|
+
private recordingOptions?;
|
|
16
|
+
constructor(config?: RemoteSTTConfig);
|
|
17
|
+
/**
|
|
18
|
+
* Start recording audio from microphone
|
|
19
|
+
*/
|
|
20
|
+
startRecording(options?: STTOptions): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Stop recording and transcribe audio to text using remote API
|
|
23
|
+
*/
|
|
24
|
+
stopRecording(): Promise<string>;
|
|
25
|
+
/**
|
|
26
|
+
* Check if currently recording
|
|
27
|
+
*/
|
|
28
|
+
isRecording(): boolean;
|
|
29
|
+
}
|
|
30
|
+
declare function createRemoteSTTTranscriber(config?: RemoteSTTConfig): RemoteSTTTranscriber;
|
|
31
|
+
|
|
32
|
+
export { type RemoteSTTConfig, RemoteSTTTranscriber, createRemoteSTTTranscriber };
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/remote/index.ts
|
|
21
|
+
var remote_exports = {};
|
|
22
|
+
__export(remote_exports, {
|
|
23
|
+
RemoteSTTTranscriber: () => RemoteSTTTranscriber,
|
|
24
|
+
createRemoteSTTTranscriber: () => createRemoteSTTTranscriber
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(remote_exports);
|
|
27
|
+
|
|
28
|
+
// src/media-recorder-helper.ts
|
|
29
|
+
var MediaRecorderHelper = class {
|
|
30
|
+
mediaRecorder = null;
|
|
31
|
+
audioChunks = [];
|
|
32
|
+
recording = false;
|
|
33
|
+
/**
|
|
34
|
+
* Start audio recording from user's microphone
|
|
35
|
+
*/
|
|
36
|
+
async start() {
|
|
37
|
+
if (this.recording) {
|
|
38
|
+
throw new Error("Already recording");
|
|
39
|
+
}
|
|
40
|
+
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
41
|
+
this.mediaRecorder = new MediaRecorder(stream);
|
|
42
|
+
this.audioChunks = [];
|
|
43
|
+
this.mediaRecorder.ondataavailable = (event) => {
|
|
44
|
+
if (event.data.size > 0) {
|
|
45
|
+
this.audioChunks.push(event.data);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
this.mediaRecorder.start();
|
|
49
|
+
this.recording = true;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Stop audio recording and return the recorded audio as a Blob
|
|
53
|
+
*/
|
|
54
|
+
async stop() {
|
|
55
|
+
if (!this.recording || !this.mediaRecorder) {
|
|
56
|
+
throw new Error("Not recording");
|
|
57
|
+
}
|
|
58
|
+
return new Promise((resolve, reject) => {
|
|
59
|
+
this.mediaRecorder.onstop = () => {
|
|
60
|
+
const audioBlob = new Blob(this.audioChunks, { type: "audio/webm" });
|
|
61
|
+
this.audioChunks = [];
|
|
62
|
+
this.mediaRecorder?.stream.getTracks().forEach((track) => track.stop());
|
|
63
|
+
this.mediaRecorder = null;
|
|
64
|
+
this.recording = false;
|
|
65
|
+
resolve(audioBlob);
|
|
66
|
+
};
|
|
67
|
+
this.mediaRecorder.onerror = (error) => {
|
|
68
|
+
this.recording = false;
|
|
69
|
+
reject(error);
|
|
70
|
+
};
|
|
71
|
+
this.mediaRecorder.stop();
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Check if currently recording
|
|
76
|
+
*/
|
|
77
|
+
isRecording() {
|
|
78
|
+
return this.recording;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Abort recording immediately without returning data
|
|
82
|
+
*/
|
|
83
|
+
abort() {
|
|
84
|
+
if (this.mediaRecorder) {
|
|
85
|
+
this.mediaRecorder.stream.getTracks().forEach((track) => track.stop());
|
|
86
|
+
this.mediaRecorder = null;
|
|
87
|
+
this.audioChunks = [];
|
|
88
|
+
this.recording = false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// src/remote/index.ts
|
|
94
|
+
var REQUEST_TIMEOUT_MS = 3e4;
|
|
95
|
+
var RemoteSTTTranscriber = class {
|
|
96
|
+
apiEndpoint;
|
|
97
|
+
recorder;
|
|
98
|
+
recordingOptions;
|
|
99
|
+
constructor(config = {}) {
|
|
100
|
+
this.apiEndpoint = config.apiEndpoint || "/api/stt";
|
|
101
|
+
this.recorder = new MediaRecorderHelper();
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Start recording audio from microphone
|
|
105
|
+
*/
|
|
106
|
+
async startRecording(options) {
|
|
107
|
+
this.recordingOptions = options;
|
|
108
|
+
await this.recorder.start();
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Stop recording and transcribe audio to text using remote API
|
|
112
|
+
*/
|
|
113
|
+
async stopRecording() {
|
|
114
|
+
const audioBlob = await this.recorder.stop();
|
|
115
|
+
const formData = new FormData();
|
|
116
|
+
formData.append("audio", audioBlob, "recording.webm");
|
|
117
|
+
if (this.recordingOptions?.language) {
|
|
118
|
+
formData.append("language", this.recordingOptions.language);
|
|
119
|
+
}
|
|
120
|
+
const response = await fetchWithTimeout(
|
|
121
|
+
this.apiEndpoint,
|
|
122
|
+
{
|
|
123
|
+
method: "POST",
|
|
124
|
+
body: formData
|
|
125
|
+
},
|
|
126
|
+
`STT request timed out after ${REQUEST_TIMEOUT_MS}ms`
|
|
127
|
+
);
|
|
128
|
+
if (!response.ok) {
|
|
129
|
+
throw new Error(`STT API failed: ${response.statusText}`);
|
|
130
|
+
}
|
|
131
|
+
const data = await response.json();
|
|
132
|
+
this.recordingOptions = void 0;
|
|
133
|
+
return data.transcription;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Check if currently recording
|
|
137
|
+
*/
|
|
138
|
+
isRecording() {
|
|
139
|
+
return this.recorder.isRecording();
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
function createRemoteSTTTranscriber(config) {
|
|
143
|
+
return new RemoteSTTTranscriber(config);
|
|
144
|
+
}
|
|
145
|
+
async function fetchWithTimeout(input, init, timeoutMessage) {
|
|
146
|
+
const controller = new AbortController();
|
|
147
|
+
const timeoutId = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
|
|
148
|
+
try {
|
|
149
|
+
return await fetch(input, {
|
|
150
|
+
...init,
|
|
151
|
+
signal: controller.signal
|
|
152
|
+
});
|
|
153
|
+
} catch (error) {
|
|
154
|
+
if (isAbortError(error)) {
|
|
155
|
+
throw new Error(timeoutMessage);
|
|
156
|
+
}
|
|
157
|
+
throw error;
|
|
158
|
+
} finally {
|
|
159
|
+
clearTimeout(timeoutId);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
function isAbortError(error) {
|
|
163
|
+
return error instanceof DOMException && error.name === "AbortError" || error instanceof Error && error.name === "AbortError";
|
|
164
|
+
}
|
|
165
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
166
|
+
0 && (module.exports = {
|
|
167
|
+
RemoteSTTTranscriber,
|
|
168
|
+
createRemoteSTTTranscriber
|
|
169
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MediaRecorderHelper
|
|
3
|
+
} from "../chunk-EKUYDBHB.mjs";
|
|
4
|
+
|
|
5
|
+
// src/remote/index.ts
|
|
6
|
+
var REQUEST_TIMEOUT_MS = 3e4;
|
|
7
|
+
var RemoteSTTTranscriber = class {
|
|
8
|
+
apiEndpoint;
|
|
9
|
+
recorder;
|
|
10
|
+
recordingOptions;
|
|
11
|
+
constructor(config = {}) {
|
|
12
|
+
this.apiEndpoint = config.apiEndpoint || "/api/stt";
|
|
13
|
+
this.recorder = new MediaRecorderHelper();
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Start recording audio from microphone
|
|
17
|
+
*/
|
|
18
|
+
async startRecording(options) {
|
|
19
|
+
this.recordingOptions = options;
|
|
20
|
+
await this.recorder.start();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Stop recording and transcribe audio to text using remote API
|
|
24
|
+
*/
|
|
25
|
+
async stopRecording() {
|
|
26
|
+
const audioBlob = await this.recorder.stop();
|
|
27
|
+
const formData = new FormData();
|
|
28
|
+
formData.append("audio", audioBlob, "recording.webm");
|
|
29
|
+
if (this.recordingOptions?.language) {
|
|
30
|
+
formData.append("language", this.recordingOptions.language);
|
|
31
|
+
}
|
|
32
|
+
const response = await fetchWithTimeout(
|
|
33
|
+
this.apiEndpoint,
|
|
34
|
+
{
|
|
35
|
+
method: "POST",
|
|
36
|
+
body: formData
|
|
37
|
+
},
|
|
38
|
+
`STT request timed out after ${REQUEST_TIMEOUT_MS}ms`
|
|
39
|
+
);
|
|
40
|
+
if (!response.ok) {
|
|
41
|
+
throw new Error(`STT API failed: ${response.statusText}`);
|
|
42
|
+
}
|
|
43
|
+
const data = await response.json();
|
|
44
|
+
this.recordingOptions = void 0;
|
|
45
|
+
return data.transcription;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Check if currently recording
|
|
49
|
+
*/
|
|
50
|
+
isRecording() {
|
|
51
|
+
return this.recorder.isRecording();
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
function createRemoteSTTTranscriber(config) {
|
|
55
|
+
return new RemoteSTTTranscriber(config);
|
|
56
|
+
}
|
|
57
|
+
async function fetchWithTimeout(input, init, timeoutMessage) {
|
|
58
|
+
const controller = new AbortController();
|
|
59
|
+
const timeoutId = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS);
|
|
60
|
+
try {
|
|
61
|
+
return await fetch(input, {
|
|
62
|
+
...init,
|
|
63
|
+
signal: controller.signal
|
|
64
|
+
});
|
|
65
|
+
} catch (error) {
|
|
66
|
+
if (isAbortError(error)) {
|
|
67
|
+
throw new Error(timeoutMessage);
|
|
68
|
+
}
|
|
69
|
+
throw error;
|
|
70
|
+
} finally {
|
|
71
|
+
clearTimeout(timeoutId);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function isAbortError(error) {
|
|
75
|
+
return error instanceof DOMException && error.name === "AbortError" || error instanceof Error && error.name === "AbortError";
|
|
76
|
+
}
|
|
77
|
+
export {
|
|
78
|
+
RemoteSTTTranscriber,
|
|
79
|
+
createRemoteSTTTranscriber
|
|
80
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { STTTranscriber, STTOptions } from '@charivo/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Web STT Transcriber - Web Speech API를 사용하는 STT Transcriber
|
|
5
|
+
*
|
|
6
|
+
* 브라우저 내장 음성 인식 기능을 사용
|
|
7
|
+
* 녹음 없이 실시간으로 음성을 텍스트로 변환
|
|
8
|
+
*
|
|
9
|
+
* 장점:
|
|
10
|
+
* - API 키 불필요
|
|
11
|
+
* - 무료
|
|
12
|
+
* - 실시간 인식
|
|
13
|
+
* - MediaRecorder 불필요 (마이크 직접 청취)
|
|
14
|
+
*
|
|
15
|
+
* 제약사항:
|
|
16
|
+
* - 브라우저 지원 필요 (Chrome, Edge 등)
|
|
17
|
+
* - 인터넷 연결 필요 (대부분의 브라우저에서)
|
|
18
|
+
*/
|
|
19
|
+
declare class WebSTTTranscriber implements STTTranscriber {
|
|
20
|
+
private recognition;
|
|
21
|
+
private recording;
|
|
22
|
+
private isSupported;
|
|
23
|
+
private resolveTranscription?;
|
|
24
|
+
private rejectTranscription?;
|
|
25
|
+
private finalTranscript;
|
|
26
|
+
constructor();
|
|
27
|
+
/**
|
|
28
|
+
* Start real-time speech recognition
|
|
29
|
+
*/
|
|
30
|
+
startRecording(options?: STTOptions): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Stop speech recognition and return transcription
|
|
33
|
+
*/
|
|
34
|
+
stopRecording(): Promise<string>;
|
|
35
|
+
/**
|
|
36
|
+
* Check if currently recording
|
|
37
|
+
*/
|
|
38
|
+
isRecording(): boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Check if Web Speech API is supported in this browser
|
|
41
|
+
*/
|
|
42
|
+
isSupportedBrowser(): boolean;
|
|
43
|
+
}
|
|
44
|
+
declare function createWebSTTTranscriber(): WebSTTTranscriber;
|
|
45
|
+
|
|
46
|
+
export { WebSTTTranscriber, createWebSTTTranscriber };
|