malivan-voice
TypeScript icon, indicating that this package has built-in type declarations

0.1.7 • Public • Published

malivan-voice

npm version License: MIT

A powerful and easy-to-use React hook for text-to-speech and speech-to-text functionality, with built-in support for Thai language and audio file processing.

Features

  • 🗣️ Text-to-speech (TTS) functionality
  • 👂 Speech-to-text (STT) functionality
  • 🎵 Audio file processing for speech recognition
  • 🛠️ Customizable options for both TTS and STT
  • 🇹🇭 Built-in support for Thai language
  • 📘 TypeScript support
  • 🚀 Easy integration into React projects
  • 🆓 Free and open-source

Installation

npm install malivan-voice
# or
yarn add malivan-voice

Usage

Here's a basic example of how to use the useSpeech hook:

import React from 'react';
import { useSpeech } from 'malivan-voice';

const SpeechComponent = () => {
  const {
    speak,
    stopSpeaking,
    startListening,
    stopListening,
    processAudioFile,
    transcript,
    isListening,
    isSpeaking,
    isProcessingFile,
    error
  } = useSpeech({
    textToVoiceOptions: { lang: 'th-TH', rate: 1.0 },
    voiceToTextOptions: { lang: 'th-TH', continuous: true }
  });

  const handleFileUpload = (event) => {
    const file = event.target.files?.[0];
    if (file) {
      processAudioFile(file);
    }
  };

  return (
    <div>
      <h1>Speech Demo</h1>
      {error && <p>Error: {error}</p>}
      <div>
        <button onClick={() => speak('สวัสดีครับ')}>Speak</button>
        <button onClick={stopSpeaking} disabled={!isSpeaking}>Stop Speaking</button>
      </div>
      <div>
        <button onClick={startListening} disabled={isListening}>Start Listening</button>
        <button onClick={stopListening} disabled={!isListening}>Stop Listening</button>
      </div>
      <div>
        <input type="file" accept="audio/*" onChange={handleFileUpload} />
      </div>
      <p>Transcript: {transcript}</p>
      <p>Status: 
        {isListening ? 'Listening' : 'Not Listening'}, 
        {isSpeaking ? 'Speaking' : 'Not Speaking'},
        {isProcessingFile ? 'Processing File' : 'Not Processing File'}
      </p>
    </div>
  );
};

API

useSpeech

function useSpeech(options?: UseSpeechOptions): UseSpeechReturn

UseSpeechOptions

The useSpeech hook accepts an optional configuration object with the following properties:

textToVoiceOptions

These options customize the text-to-speech functionality:

  • lang: The language for speech synthesis (e.g., 'th-TH' for Thai)
  • pitch: The pitch of the voice (default is 1, can range from 0 to 2)
  • rate: The speed of speech (default is 1, can range from 0.1 to 10)
  • volume: The volume of speech (default is 1, can range from 0 to 1)
  • voice: A specific SpeechSynthesisVoice object to use for speech

voiceToTextOptions

These options customize the speech recognition functionality:

  • lang: The language for speech recognition (e.g., 'th-TH' for Thai)
  • continuous: Whether to continuously recognize speech (default is false)
  • interimResults: Whether to return interim results while recognizing (default is false)

All of these options are optional. If not provided, the hook will use default values or browser defaults where applicable.

Return Value

Property Type Description
speak (text: string) => void Converts text to speech
stopSpeaking () => void Stops ongoing speech synthesis
startListening () => void Starts speech recognition
stopListening () => void Stops ongoing speech recognition
processAudioFile (file: File) => Promise<void> Processes an audio file for speech recognition
transcript string Text obtained from speech recognition
isListening boolean Indicates if speech recognition is active
isSpeaking boolean Indicates if text-to-speech is active
isProcessingFile boolean Indicates if an audio file is being processed
error string | null Error message, if any

Browser Compatibility

This package uses the Web Speech API and Web Audio API, which are supported in most modern browsers. However, support may vary, especially for older browsers. Please check the browser compatibility for Web Speech API and Web Audio API before using this in production.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

If you encounter any problems or have any questions, please open an issue in the GitHub repository.


Made with ❤️ by Beer Do-san

Readme

Keywords

none

Package Sidebar

Install

npm i malivan-voice

Weekly Downloads

1

Version

0.1.7

License

MIT

Unpacked Size

114 kB

Total Files

12

Last publish

Collaborators

  • beerth21624