Package Exports
- @lobehub/tts
- @lobehub/tts/package.json
- @lobehub/tts/react
- @lobehub/tts/server
Readme
Table of contents
TOC
π¦ Usage
Generate Speech on server
run the script below use Bun: bun index.js
// index.js
import { EdgeSpeechTTS } from '@lobehub/tts';
import { Buffer } from 'buffer';
import fs from 'fs';
import path from 'path';
// Instantiate EdgeSpeechTTS
const tts = new EdgeSpeechTTS({ locale: 'en-US' });
// Create speech synthesis request payload
const payload = {
input: 'This is a speech demonstration',
options: {
voice: 'en-US-GuyNeural',
},
};
// Call create method to synthesize speech
const response = await tts.create(payload);
// generate speech file
const mp3Buffer = Buffer.from(await response.arrayBuffer());
const speechFile = path.resolve('./speech.mp3');
fs.writeFileSync(speechFile, mp3Buffer);https://github.com/lobehub/lobe-tts/assets/28616219/3ab68c5a-2745-442e-8d66-ca410192ace1
[!IMPORTANT]
Run on Node.jsAs the Node.js environment lacks the
WebSocketinstance, we need to polyfill WebSocket. This can be done by importing the ws package.
// import at the top of the file
import WebSocket from 'ws';
global.WebSocket = WebSocket;Use the React Component
import { AudioPlayer, AudioVisualizer, useAudioPlayer } from '@lobehub/tts/react';
export default () => {
const { ref, isLoading, ...audio } = useAudioPlayer(url);
return (
<Flexbox align={'center'} gap={8}>
<AudioPlayer audio={audio} isLoading={isLoading} style={{ width: '100%' }} />
<AudioVisualizer audioRef={ref} isLoading={isLoading} />
</Flexbox>
);
};https://github.com/lobehub/lobe-tts/assets/28616219/c2638383-314f-44c3-b358-8fbbd3028d61
π¦ Installation
[!IMPORTANT]
This package is ESM only.
To install @lobehub/tts, run the following command:
$ pnpm i @lobehub/tts$ bun add @lobehub/ttsCompile with Next.js
[!NOTE]
By work correct with Next.js SSR, addtranspilePackages: ['@lobehub/tts']tonext.config.js. For example:
const nextConfig = {
transpilePackages: ['@lobehub/tts'],
};β¨οΈ Local Development
You can use Github Codespaces for online development:
Or clone it for local development:
$ git clone https://github.com/lobehub/lobe-tts.git
$ cd lobe-tts
$ bun install
$ bun devπ€ Contributing
Contributions of all types are more than welcome, if you are interested in contributing code, feel free to check out our GitHub Issues to get stuck in to show us what youβre made of.
π More Products
- π€ Lobe Chat - An open-source, extensible (Function Calling), high-performance chatbot framework. It supports one-click free deployment of your private ChatGPT/LLM web application.
- π€― Lobe theme - The modern theme for stable diffusion webui, exquisite interface design, highly customizable UI, and efficiency boosting features.
