@poovit-banton/speech-recognition-sdk 1.0.1 → 1.0.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 +337 -59
- package/dist/lib/speech-recognition-sdk.cjs.js +15 -5
- package/dist/lib/speech-recognition-sdk.cjs.js.map +1 -1
- package/dist/lib/speech-recognition-sdk.es.js +725 -601
- package/dist/lib/speech-recognition-sdk.es.js.map +1 -1
- package/dist/lib/speech-recognition-sdk.umd.js +15 -5
- package/dist/lib/speech-recognition-sdk.umd.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,73 +1,351 @@
|
|
|
1
|
-
#
|
|
1
|
+
# 🎙️ Speech Recognition SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
React SDK สำหรับ Speech Recognition ใช้ Web Speech API พร้อม Widget สวยๆ ใช้งานง่าย
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+

|
|
6
|
+

|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
|
|
8
|
+
## ✨ Features
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
- 🎤 **Speech to Text** - แปลงเสียงพูดเป็นข้อความแบบ Real-time
|
|
11
|
+
- 🌏 **รองรับภาษาไทย** - ตั้งค่า `th-TH` ได้เลย
|
|
12
|
+
- 🎨 **Floating Widget** - ปุ่มลอยพร้อม Modal สวยงาม
|
|
13
|
+
- ⚡ **React Hooks** - ใช้งานง่ายด้วย `useSpeechRecognition`
|
|
14
|
+
- 📦 **TypeScript** - รองรับ TypeScript 100%
|
|
15
|
+
- 🔧 **Customizable** - ปรับแต่งได้ตามต้องการ
|
|
11
16
|
|
|
12
|
-
|
|
17
|
+
## 📦 Installation
|
|
13
18
|
|
|
14
|
-
|
|
19
|
+
```bash
|
|
20
|
+
npm install @poovit-banton/speech-recognition-sdk
|
|
21
|
+
```
|
|
15
22
|
|
|
16
|
-
|
|
23
|
+
หรือ
|
|
17
24
|
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
{
|
|
22
|
-
files: ['**/*.{ts,tsx}'],
|
|
23
|
-
extends: [
|
|
24
|
-
// Other configs...
|
|
25
|
+
```bash
|
|
26
|
+
yarn add @poovit-banton/speech-recognition-sdk
|
|
27
|
+
```
|
|
25
28
|
|
|
26
|
-
|
|
27
|
-
tseslint.configs.recommendedTypeChecked,
|
|
28
|
-
// Alternatively, use this for stricter rules
|
|
29
|
-
tseslint.configs.strictTypeChecked,
|
|
30
|
-
// Optionally, add this for stylistic rules
|
|
31
|
-
tseslint.configs.stylisticTypeChecked,
|
|
29
|
+
## 🚀 Quick Start
|
|
32
30
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
31
|
+
### วิธีที่ 1: ใช้ Widget (แนะนำ - ง่ายสุด)
|
|
32
|
+
|
|
33
|
+
แค่ครอบ App ด้วย `SpeechProvider` แล้วได้ปุ่มลอย + Modal มาเลย!
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
import { SpeechProvider } from "@poovit-banton/speech-recognition-sdk";
|
|
37
|
+
|
|
38
|
+
function App() {
|
|
39
|
+
return (
|
|
40
|
+
<SpeechProvider
|
|
41
|
+
showWidget
|
|
42
|
+
widgetEnabled
|
|
43
|
+
widgetPosition="bottom-left"
|
|
44
|
+
config={{ language: "th-TH" }}
|
|
45
|
+
onGenerate={async (transcript) => {
|
|
46
|
+
// เรียก API ของคุณตรงนี้
|
|
47
|
+
const response = await fetch("/api/process", {
|
|
48
|
+
method: "POST",
|
|
49
|
+
headers: { "Content-Type": "application/json" },
|
|
50
|
+
body: JSON.stringify({ text: transcript }),
|
|
51
|
+
});
|
|
52
|
+
return await response.json();
|
|
53
|
+
}}
|
|
54
|
+
onGenerateComplete={(response) => {
|
|
55
|
+
console.log("ผลลัพธ์:", response);
|
|
56
|
+
}}
|
|
57
|
+
>
|
|
58
|
+
<YourApp />
|
|
59
|
+
</SpeechProvider>
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### วิธีที่ 2: ใช้ Hook (ยืดหยุ่นกว่า)
|
|
65
|
+
|
|
66
|
+
```tsx
|
|
67
|
+
import { useSpeechRecognition } from "@poovit-banton/speech-recognition-sdk";
|
|
68
|
+
|
|
69
|
+
function MyComponent() {
|
|
70
|
+
const {
|
|
71
|
+
transcript, // ข้อความที่พูด
|
|
72
|
+
isListening, // กำลังฟังอยู่ไหม
|
|
73
|
+
isSupported, // browser รองรับไหม
|
|
74
|
+
startListening, // เริ่มฟัง
|
|
75
|
+
stopListening, // หยุดฟัง
|
|
76
|
+
resetTranscript, // ล้างข้อความ
|
|
77
|
+
} = useSpeechRecognition({
|
|
78
|
+
config: { language: "th-TH" },
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
if (!isSupported) {
|
|
82
|
+
return <p>Browser ไม่รองรับ Speech Recognition</p>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<div>
|
|
87
|
+
<button onClick={isListening ? stopListening : startListening}>
|
|
88
|
+
{isListening ? "🛑 หยุด" : "🎤 เริ่มพูด"}
|
|
89
|
+
</button>
|
|
90
|
+
<p>{transcript}</p>
|
|
91
|
+
<button onClick={resetTranscript}>ล้าง</button>
|
|
92
|
+
</div>
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
### วิธีที่ 3: ควบคุม Widget จาก Component
|
|
98
|
+
|
|
99
|
+
```tsx
|
|
100
|
+
import {
|
|
101
|
+
SpeechProvider,
|
|
102
|
+
useSpeechContext,
|
|
103
|
+
} from "@poovit-banton/speech-recognition-sdk";
|
|
104
|
+
|
|
105
|
+
function App() {
|
|
106
|
+
return (
|
|
107
|
+
<SpeechProvider showWidget widgetEnabled>
|
|
108
|
+
<ControlPanel />
|
|
109
|
+
</SpeechProvider>
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function ControlPanel() {
|
|
114
|
+
const { isWidgetEnabled, toggleWidget } = useSpeechContext();
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
<button onClick={toggleWidget}>
|
|
118
|
+
{isWidgetEnabled ? "🔇 ปิด" : "🔊 เปิด"} Voice Widget
|
|
119
|
+
</button>
|
|
120
|
+
);
|
|
121
|
+
}
|
|
44
122
|
```
|
|
45
123
|
|
|
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
|
-
|
|
124
|
+
## 📖 API Reference
|
|
125
|
+
|
|
126
|
+
### `<SpeechProvider>`
|
|
127
|
+
|
|
128
|
+
Provider component สำหรับครอบ App
|
|
129
|
+
|
|
130
|
+
| Prop | Type | Default | Description |
|
|
131
|
+
| -------------------- | ------------------ | --------------------- | ----------------------------- |
|
|
132
|
+
| `showWidget` | `boolean` | `false` | แสดง Floating Widget |
|
|
133
|
+
| `widgetEnabled` | `boolean` | `true` | เปิด/ปิด Widget |
|
|
134
|
+
| `widgetPosition` | `string \| object` | `"bottom-left"` | ตำแหน่ง Widget |
|
|
135
|
+
| `widgetButtonSize` | `number` | `60` | ขนาดปุ่ม (px) |
|
|
136
|
+
| `config` | `object` | `{}` | ตั้งค่า Speech Recognition |
|
|
137
|
+
| `onGenerate` | `function` | - | Callback เมื่อกด Generate |
|
|
138
|
+
| `onGenerateComplete` | `function` | - | Callback เมื่อ Generate เสร็จ |
|
|
139
|
+
| `onGenerateError` | `function` | - | Callback เมื่อเกิด Error |
|
|
140
|
+
| `modalTitle` | `string` | `"AI Speech-to-Data"` | หัวข้อ Modal |
|
|
141
|
+
| `generateButtonText` | `string` | `"Generate"` | ข้อความปุ่ม Generate |
|
|
142
|
+
|
|
143
|
+
#### Widget Position Options
|
|
144
|
+
|
|
145
|
+
```tsx
|
|
146
|
+
// Preset positions
|
|
147
|
+
widgetPosition="bottom-left" // ซ้ายล่าง
|
|
148
|
+
widgetPosition="bottom-right" // ขวาล่าง
|
|
149
|
+
widgetPosition="top-left" // ซ้ายบน
|
|
150
|
+
widgetPosition="top-right" // ขวาบน
|
|
151
|
+
|
|
152
|
+
// Custom position
|
|
153
|
+
widgetPosition={{ bottom: 20, left: 20 }}
|
|
154
|
+
widgetPosition={{ top: 100, right: 50 }}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### `useSpeechRecognition(options)`
|
|
158
|
+
|
|
159
|
+
Hook สำหรับใช้งาน Speech Recognition
|
|
160
|
+
|
|
161
|
+
#### Options
|
|
162
|
+
|
|
163
|
+
```tsx
|
|
164
|
+
const options = {
|
|
165
|
+
config: {
|
|
166
|
+
language: "th-TH", // ภาษา
|
|
167
|
+
continuous: true, // ฟังต่อเนื่อง
|
|
168
|
+
interimResults: true, // แสดงผลระหว่างพูด
|
|
71
169
|
},
|
|
72
|
-
|
|
170
|
+
callbacks: {
|
|
171
|
+
onStart: () => {}, // เริ่มฟัง
|
|
172
|
+
onEnd: () => {}, // หยุดฟัง
|
|
173
|
+
onResult: (result) => {}, // ได้ผลลัพธ์
|
|
174
|
+
onError: (error) => {}, // เกิด error
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### Return Values
|
|
180
|
+
|
|
181
|
+
```tsx
|
|
182
|
+
const {
|
|
183
|
+
// State
|
|
184
|
+
transcript, // string - ข้อความทั้งหมด
|
|
185
|
+
interimTranscript, // string - ข้อความระหว่างพูด
|
|
186
|
+
finalTranscript, // string - ข้อความที่ยืนยันแล้ว
|
|
187
|
+
isListening, // boolean - กำลังฟังอยู่
|
|
188
|
+
isSupported, // boolean - browser รองรับ
|
|
189
|
+
isMicrophoneAvailable, // boolean - มีไมค์
|
|
190
|
+
error, // string | null - error message
|
|
191
|
+
|
|
192
|
+
// Actions
|
|
193
|
+
startListening, // (config?) => Promise<void>
|
|
194
|
+
stopListening, // () => void
|
|
195
|
+
abortListening, // () => void
|
|
196
|
+
resetTranscript, // () => void
|
|
197
|
+
} = useSpeechRecognition(options);
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
### `useSpeechContext()`
|
|
201
|
+
|
|
202
|
+
Hook สำหรับเข้าถึง context ภายใน `SpeechProvider`
|
|
203
|
+
|
|
204
|
+
```tsx
|
|
205
|
+
const {
|
|
206
|
+
// ทุกอย่างจาก useSpeechRecognition +
|
|
207
|
+
isWidgetEnabled, // boolean - Widget เปิดอยู่ไหม
|
|
208
|
+
setWidgetEnabled, // (enabled: boolean) => void
|
|
209
|
+
toggleWidget, // () => void
|
|
210
|
+
} = useSpeechContext();
|
|
73
211
|
```
|
|
212
|
+
|
|
213
|
+
### Components
|
|
214
|
+
|
|
215
|
+
| Component | Description |
|
|
216
|
+
| --------------------- | ----------------------- |
|
|
217
|
+
| `<SpeechWidget>` | Floating button + Modal |
|
|
218
|
+
| `<SpeechButton>` | ปุ่ม Microphone |
|
|
219
|
+
| `<SpeechModal>` | Modal สำหรับพูด |
|
|
220
|
+
| `<TranscriptDisplay>` | แสดงข้อความ |
|
|
221
|
+
|
|
222
|
+
## 🌍 Supported Languages
|
|
223
|
+
|
|
224
|
+
```tsx
|
|
225
|
+
// ตั้งค่าภาษาใน config
|
|
226
|
+
config={{ language: 'th-TH' }}
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
| Code | Language |
|
|
230
|
+
| ------- | ------------ |
|
|
231
|
+
| `th-TH` | ไทย |
|
|
232
|
+
| `en-US` | English (US) |
|
|
233
|
+
| `en-GB` | English (UK) |
|
|
234
|
+
| `zh-CN` | 中文 (简体) |
|
|
235
|
+
| `zh-TW` | 中文 (繁體) |
|
|
236
|
+
| `ja-JP` | 日本語 |
|
|
237
|
+
| `ko-KR` | 한국어 |
|
|
238
|
+
| `vi-VN` | Tiếng Việt |
|
|
239
|
+
|
|
240
|
+
## 🌐 Browser Support
|
|
241
|
+
|
|
242
|
+
| Browser | Support |
|
|
243
|
+
| ------- | ---------------- |
|
|
244
|
+
| Chrome | ✅ Full |
|
|
245
|
+
| Edge | ✅ Full |
|
|
246
|
+
| Safari | ⚠️ Partial |
|
|
247
|
+
| Firefox | ❌ Not supported |
|
|
248
|
+
|
|
249
|
+
> ⚠️ Web Speech API ไม่รองรับทุก browser ควรเช็ค `isSupported` ก่อนใช้งาน
|
|
250
|
+
|
|
251
|
+
## 📝 Example: Full Integration
|
|
252
|
+
|
|
253
|
+
```tsx
|
|
254
|
+
import React, { useState } from "react";
|
|
255
|
+
import {
|
|
256
|
+
SpeechProvider,
|
|
257
|
+
useSpeechContext,
|
|
258
|
+
} from "@poovit-banton/speech-recognition-sdk";
|
|
259
|
+
|
|
260
|
+
// Main App
|
|
261
|
+
function App() {
|
|
262
|
+
const [results, setResults] = useState<string[]>([]);
|
|
263
|
+
|
|
264
|
+
const handleGenerate = async (transcript: string) => {
|
|
265
|
+
// เรียก API
|
|
266
|
+
const response = await fetch("/api/ai/process", {
|
|
267
|
+
method: "POST",
|
|
268
|
+
headers: { "Content-Type": "application/json" },
|
|
269
|
+
body: JSON.stringify({ text: transcript }),
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
const data = await response.json();
|
|
273
|
+
return data.result;
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
return (
|
|
277
|
+
<SpeechProvider
|
|
278
|
+
showWidget
|
|
279
|
+
widgetEnabled
|
|
280
|
+
widgetPosition="bottom-left"
|
|
281
|
+
config={{ language: "th-TH", continuous: true }}
|
|
282
|
+
modalTitle="พูดข้อความ"
|
|
283
|
+
generateButtonText="สร้าง"
|
|
284
|
+
onGenerate={handleGenerate}
|
|
285
|
+
onGenerateComplete={(result) => {
|
|
286
|
+
setResults((prev) => [...prev, result]);
|
|
287
|
+
}}
|
|
288
|
+
onGenerateError={(error) => {
|
|
289
|
+
console.error("Error:", error);
|
|
290
|
+
}}
|
|
291
|
+
>
|
|
292
|
+
<MainContent results={results} />
|
|
293
|
+
</SpeechProvider>
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Content Component
|
|
298
|
+
function MainContent({ results }: { results: string[] }) {
|
|
299
|
+
const { isWidgetEnabled, toggleWidget } = useSpeechContext();
|
|
300
|
+
|
|
301
|
+
return (
|
|
302
|
+
<div style={{ padding: 20 }}>
|
|
303
|
+
<h1>🎙️ Speech Recognition Demo</h1>
|
|
304
|
+
|
|
305
|
+
<button onClick={toggleWidget}>
|
|
306
|
+
{isWidgetEnabled ? "ปิด" : "เปิด"} Voice Widget
|
|
307
|
+
</button>
|
|
308
|
+
|
|
309
|
+
<h2>Results:</h2>
|
|
310
|
+
<ul>
|
|
311
|
+
{results.map((r, i) => (
|
|
312
|
+
<li key={i}>{r}</li>
|
|
313
|
+
))}
|
|
314
|
+
</ul>
|
|
315
|
+
|
|
316
|
+
<p>👈 กดปุ่มไมค์ที่มุมซ้ายล่างเพื่อเริ่มพูด</p>
|
|
317
|
+
</div>
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export default App;
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
## 🔧 Utilities
|
|
325
|
+
|
|
326
|
+
```tsx
|
|
327
|
+
import {
|
|
328
|
+
isSpeechRecognitionSupported,
|
|
329
|
+
requestMicrophonePermission,
|
|
330
|
+
getSupportedLanguages,
|
|
331
|
+
} from "@poovit-banton/speech-recognition-sdk";
|
|
332
|
+
|
|
333
|
+
// เช็คว่า browser รองรับไหม
|
|
334
|
+
if (isSpeechRecognitionSupported()) {
|
|
335
|
+
console.log("รองรับ!");
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// ขอ permission ไมค์
|
|
339
|
+
const hasPermission = await requestMicrophonePermission();
|
|
340
|
+
|
|
341
|
+
// ดูภาษาที่รองรับ
|
|
342
|
+
const languages = getSupportedLanguages();
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
## 📄 License
|
|
346
|
+
|
|
347
|
+
MIT © Poovit Banton
|
|
348
|
+
|
|
349
|
+
---
|
|
350
|
+
|
|
351
|
+
Made with ❤️ by DFM Team
|