fluxion_ai.utils.audio_utils module

fluxion_ai.utils.audio_utils

This module provides utility functions for audio processing tasks such as speech-to-text (STT) and text-to-speech (TTS) conversion.

Classes:
  • AudioUtils: A utility class for handling audio-related tasks.

Exceptions:
  • AudioUtilsError: Base exception for AudioUtils errors.

  • SpeechToTextError: Exception raised for Speech-to-Text errors.

  • TextToSpeechError: Exception raised for Text-to-Speech errors.

Functions:
  • google_text_to_speech: Converts text to speech and saves it as an audio file.

  • play_audio: Plays an audio file.

  • load_audio: Loads audio data from a file or microphone.

class fluxion_ai.utils.audio_utils.AudioUtils(recognizer=None, lang='en')[source]

Bases: object

A utility class for handling audio-related tasks such as speech-to-text (STT) and text-to-speech (TTS).

text_to_speech(text: str)[source]

Converts text to speech and plays the audio using provided save and play functions.

Parameters:
  • text (str) – Text to convert to speech.

  • save_fn (callable) – Function to save TTS output to a file.

  • play_fn (callable) – Function to play the audio file.

Raises:

TextToSpeechError – If text-to-speech conversion fails.

transcribe_audio(audio_path: str = None) str[source]

Transcribes audio to text using a provided load function.

Parameters:

load_audio_fn (callable) – Function to load audio, returning an audio object.

Returns:

Transcribed text.

Return type:

str

Raises:

SpeechToTextError – If transcription fails.

exception fluxion_ai.utils.audio_utils.AudioUtilsError[source]

Bases: Exception

Base exception for AudioUtils errors.

exception fluxion_ai.utils.audio_utils.SpeechToTextError[source]

Bases: AudioUtilsError

Exception raised for Speech-to-Text errors.

exception fluxion_ai.utils.audio_utils.TextToSpeechError[source]

Bases: AudioUtilsError

Exception raised for Text-to-Speech errors.

fluxion_ai.utils.audio_utils.google_text_to_speech(text: str, filepath: str, lang: str = 'en')[source]

Converts text to speech and saves it as an audio file.

Parameters:
  • text (str) – Text to be converted to speech.

  • filepath (str) – Path where the audio file will be saved.

  • lang (str) – Language code for the TTS (default: “en”).

fluxion_ai.utils.audio_utils.load_audio(recognizer: Any, audio_path: str = None)[source]

Loads audio data from a file or microphone.

Parameters:

audio_path (str) – Path to the audio file. If None, loads audio from the microphone.

Returns:

The loaded audio data.

Return type:

speech_recognition.AudioData

fluxion_ai.utils.audio_utils.play_audio(filepath: str)[source]

Plays an audio file.

Parameters:

filepath (str) – Path to the audio file to be played.