Skip to content

Commit 197a36f

Browse files
authored
Merge pull request #7 from Intentface/update-readme
Update readme
2 parents 301287a + 64e165a commit 197a36f

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

.changeset/lemon-feet-provide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@intentface/react-speech-recognition": patch
3+
---
4+
5+
Update readme

README.md

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,127 @@
11
# @intentface/react-speech-recognition
22

3+
`@intentface/react-speech-recognition` is a custom React hook that provides an easy-to-use interface for integrating speech recognition capabilities into your React applications. It leverages the native browser `SpeechRecognition` API, allowing you to capture and process voice inputs with customizable options.
4+
5+
## Installation
6+
7+
You can install this package via npm:
8+
9+
```bash
10+
npm install @intentface/react-speech-recognition
11+
```
12+
13+
## Usage
14+
15+
Here’s a basic example of how to use the `useSpeechRecognition` hook in your React project:
16+
17+
```tsx
18+
import React from "react";
19+
import { useSpeechRecognition } from "@intentface/react-speech-recognition";
20+
21+
const SpeechComponent = () => {
22+
const {
23+
transcript,
24+
interimTranscript,
25+
isListening,
26+
isFinal,
27+
isSupported,
28+
start,
29+
stop,
30+
error,
31+
} = useSpeechRecognition({
32+
lang: "en-US", // Language for speech recognition (default is "en-US")
33+
continuous: false, // Whether to keep listening or stop after receiving input
34+
timeout: 5000, // Automatically stop listening after 5 seconds
35+
onUpdate: ({ transcript, interimTranscript, isFinal }) => {
36+
console.log("Update:", { transcript, interimTranscript, isFinal });
37+
},
38+
onError: ({ error }) => {
39+
console.error("Speech recognition error:", error);
40+
},
41+
});
42+
43+
if (!isSupported) {
44+
return <p>Your browser does not support Speech Recognition.</p>;
45+
}
46+
47+
return (
48+
<div>
49+
<h1>Speech Recognition Example</h1>
50+
<p>Transcript: {transcript}</p>
51+
<p>Interim Transcript: {interimTranscript}</p>
52+
<p>
53+
Status:{" "}
54+
{isListening ? "Listening..." : isFinal ? "Finalized" : "Stopped"}
55+
</p>
56+
{error && <p>Error: {error.message}</p>}
57+
<button onClick={start}>Start Listening</button>
58+
<button onClick={stop}>Stop Listening</button>
59+
</div>
60+
);
61+
};
62+
63+
export default SpeechComponent;
64+
```
65+
66+
### API
67+
68+
The `useSpeechRecognition` hook returns an object containing the following properties:
69+
70+
#### Returned Values
71+
72+
- `transcript`: The final processed transcript of the recognized speech.
73+
- `interimTranscript`: The current intermediate transcript while speech is being processed.
74+
- `isListening`: A boolean indicating whether the recognition is currently active.
75+
- `isFinal`: A boolean indicating whether the current session has ended and the transcript is finalized.
76+
- `isSupported`: A boolean indicating whether the browser supports speech recognition.
77+
- `start()`: A function to start speech recognition.
78+
- `stop()`: A function to stop speech recognition.
79+
- `error`: An object containing any speech recognition errors that occurred.
80+
81+
#### Options
82+
83+
The hook accepts an options object with the following fields:
84+
85+
- `lang`: The language for speech recognition (default is `"en-US"`).
86+
- `continuous`: If `true`, the recognition will continue until stopped manually (default: `false`).
87+
- `timeout`: Time in milliseconds to automatically stop recognition after no speech is detected (default: `undefined`). If left _undefined_, browsers default behaviour is respected.
88+
- `onUpdate`: A callback function triggered when there is an update in the transcript. Receives an object with `{ transcript, interimTranscript, isFinal }`.
89+
- `onError`: A callback function triggered when an error occurs. Receives an object with `{ error }`.
90+
91+
### Features
92+
93+
- **Customizable Language**: Set the language of recognition with the `lang` option.
94+
- **Continuous Listening**: Option to listen continuously for speech input.
95+
- **Timeout Control**: Automatically stop recognition after a set period of inactivity.
96+
- **Error Handling**: Receive error information via the `onError` callback.
97+
- **Interim and Final Transcripts**: Access both the interim (in-progress) and final transcripts.
98+
99+
### Browser Compatibility
100+
101+
This hook uses the native `SpeechRecognition` API (also known as `webkitSpeechRecognition` in some browsers). Below is a list of supported browsers:
102+
103+
| Browser | Supported Versions |
104+
| ------- | ------------------ |
105+
| Chrome | Version 33+ |
106+
| Safari | Version 14+ |
107+
| Edge | Version 79+ |
108+
109+
> Note: The `SpeechRecognition` API is not supported in Firefox or Internet Explorer. Please check for browser compatibility using the `isSupported` flag provided by the hook.
110+
111+
### Contributing
112+
113+
Contributions are welcome! If you encounter any issues or have suggestions for improvements, feel free to create a pull request or file an issue in the repository.
114+
115+
Please remember to add the changes you are making by running
116+
117+
```bash
118+
npm run add-change
119+
```
120+
121+
### License
122+
123+
MIT
124+
3125
## Dev
4126

5127
### Doing a release
@@ -11,3 +133,7 @@ After finishing your feature normally, to automatically bump version number, upd
11133
3. Commit the changeset file(s) to your feature branch and push to Github
12134
4. Create a PR normally, including the changeset file(s)
13135
5. Eventually accept the PR called "Version Packages" created by the changeset bot
136+
137+
## About us
138+
139+
For more details about who we are, visit our website: [Intentface](https://www.intentface.com/).

0 commit comments

Comments
 (0)