Speechly browser client
🚧 Browser Client v2.0 is a breaking change. Read more about the major changes and how to upgrade from our blog: https://speechly.com/blog/speechly-browser-client-v2-released
With the browser-client you can add voice features to any website. It handles authentication, audio capture, network streaming and connection management with the Speechly Voice API.
Check out the browser-client-example repository for a demo app built using this client.
NOTE: If you are using React, you can use our React client instead. It provides the same functionalities, but provides a programming model that is idiomatic to React.
API Documentation
Using in web sites built with eg. rollup
Install the package:
# Using Yarn
yarn add @speechly/browser-client
# Using NPM
npm install --save @speechly/browser-client
Start using the client:
import { BrowserClient, BrowserMicrophone, Segment } from '@speechly/browser-client'
const client = new BrowserClient({
appId: 'your-app-id'
vad: { enabled: false, noiseGateDb: -24.0 }
})
const microphone = new BrowserMicrophone()
await microphone.initialize()
await client.attach(microphone.mediaStream)
client.onSegmentChange((segment: Segment) => {
console.log('Received new segment from the API:',
segment.intent,
segment.entities,
segment.words,
segment.isFinal
)
})
await client.start()
setTimeout(async function () {
await client.stop()
}, 3000)
Usage in HTML
This sample HTML loads Speechly's browser-client
ES modules via a CDN that mirrors npm packages. The page displays a text field that you dictate text into. See browser's console log for raw segment feed from Speechly.
Please use a HTML server to view the example. Running it as a file will not work due to browser's security restrictions. For example run serve .
on command line and open localhost:3000
in your browser.
<html>
<body>
<input id="textBox" type="text" placeholder="Hold to talk..." autofocus />
<script type="module">
import { BrowserClient, BrowserMicrophone } from "//unpkg.com/@speechly/browser-client?module=true"
const widget = document.getElementById("textBox")
const speechly = new BrowserClient({
appId: "your-app-id",
debug: true,
logSegments: true,
})
const microphone = new BrowserMicrophone()
speechly.onSegmentChange(segment => {
let transcript = segment.words
.map(w => w.value.toLowerCase())
.join(" ");
if (segment.isFinal) transcript += ".";
widget.value = transcript;
});
const startListening = async () => {
if (microphone.mediaStream === undefined) {
await microphone.initialize()
speechly.attach(microphone.mediaStream)
}
return speechly.start();
}
const stopListening = async () => {
if (speechly.isActive()) {
return speechly.stop();
}
}
widget.addEventListener("mousedown", startListening)
document.addEventListener("mouseup", stopListening)
</script>
</body>
</html>
Documentation
You can find the detailed browser-client API documentation in the GitHub repository.
You can also refer to Speechly Docs for more information.
Contributing
See contribution guide in CONTRIBUTING.md.