1.24.1 β€’ Published 9 days ago

@lobehub/tts v1.24.1

Weekly downloads
-
License
MIT
Repository
github
Last release
9 days ago

A high-quality & reliable TTS/STT library for Server and Browser

Changelog Β· Report Bug Β· Request Feature

npm.io

TOC

πŸ“– Introduction

πŸ€– Lobe Chat supports Text-to-Speech (TTS) and Speech-to-Text (STT) technologies, enabling our application to convert text messages into clear voice outputs, allowing users to interact with our conversational agent as if they were talking to a real person. Users can choose from a variety of voices to pair with the agent.

In the process of implementing this feature, we found that there was no satisfactory TTS (Text-to-Speech) frontend library available on the market. As a result, we invested a lot of effort, including data conversion, audio progress management, and speech visualization, among other tasks.

[!NOTE]

Therefore, we decided to refine our implementation and make it open source, hoping to assist developers who wish to implement TTS. @lobehub/tts is a high-quality TTS toolkit developed in TypeScript, which supports usage both on the server-side and in the browser.

  • Server-side: With just 15 lines of code, you can achieve high-quality voice generation capabilities comparable to OpenAI's TTS service. It currently supports EdgeSpeechTTS, MicrosoftTTS, OpenAITTS, and OpenAISTT.
  • Browser-side: It provides high-quality React Hooks and visual audio components, supporting common functions such as loading, playing, pausing, and dragging the timeline. Additionally, it offers a very rich set of capabilities for adjusting the audio track styles.

πŸ“¦ 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.js

As the Node.js environment lacks the WebSocket instance, 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/tts

Compile with Next.js

[!NOTE]\ By work correct with Next.js SSR, add transpilePackages: ['@lobehub/tts'] to next.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.

🩷 Sponsor

Every bit counts and your one-time donation sparkles in our galaxy of support! You're a shooting star, making a swift and bright impact on our journey. Thank you for believing in us – your generosity guides us toward our mission, one brilliant flash at a time.

πŸ”— 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 Vidol - Experience the magic of virtual idol creation with Lobe Vidol, enjoy the elegance of our Exquisite UI Design, dance along using MMD Dance Support, and engage in Smooth Conversations.
  • πŸ…°οΈ Lobe theme - The modern theme for stable diffusion webui, exquisite interface design, highly customizable UI, and efficiency boosting features.


πŸ“ License

Copyright Β© 2023 LobeHub. This project is MIT licensed.

1.24.1

9 days ago

1.24.0

4 months ago

1.23.5

4 months ago

1.23.4

5 months ago

1.23.4-beta.1

5 months ago

1.23.3

5 months ago

1.23.2

5 months ago

1.23.1

5 months ago

1.23.0

5 months ago

1.22.2

6 months ago

1.22.1

6 months ago

1.22.0

6 months ago

1.21.5

6 months ago

1.21.4

6 months ago

1.21.3

6 months ago

1.21.2

6 months ago

1.21.1

6 months ago

1.21.0

6 months ago

1.20.3

6 months ago

1.20.2

6 months ago

1.20.1

6 months ago

1.20.0

6 months ago

1.20.0-beta.3

6 months ago

1.20.0-beta.2

6 months ago

1.20.0-beta.1

6 months ago

1.19.1

6 months ago

1.19.0

6 months ago

1.18.3

6 months ago

1.18.2

6 months ago

1.18.1

6 months ago

1.18.0

6 months ago

1.17.2

6 months ago

1.17.1

6 months ago

1.17.0

6 months ago

1.16.2

6 months ago

1.16.1

6 months ago

1.16.0

6 months ago

1.15.0

6 months ago

1.14.3

6 months ago

1.14.2

6 months ago

1.14.1

6 months ago

1.14.0

6 months ago

1.13.0

6 months ago

1.13.0-beta.4

6 months ago

1.12.1

6 months ago

1.13.0-beta.3

6 months ago

1.13.0-beta.2

6 months ago

1.13.0-beta.1

6 months ago

1.12.1-beta.6

6 months ago

1.12.1-beta.5

6 months ago

1.12.1-beta.4

6 months ago

1.12.1-beta.3

6 months ago

1.12.1-beta.2

6 months ago

1.12.1-beta.1

6 months ago

1.12.0-beta.1

6 months ago

1.12.0

6 months ago

1.11.1

6 months ago

1.11.0

6 months ago

1.10.0

6 months ago

1.9.0

6 months ago

1.8.1

6 months ago

1.8.0

6 months ago

1.7.1

6 months ago

1.7.0

6 months ago

1.6.1

6 months ago

1.6.0

6 months ago

1.5.0

6 months ago

1.4.1

6 months ago

1.4.0

6 months ago

1.3.0

6 months ago

1.2.0

6 months ago

1.1.0

6 months ago

1.0.0

6 months ago