help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • Blog
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
speech-recognition
/
scripts
RSK World
speech-recognition
Speech Recognition Dataset - Audio AI + Speech-to-Text + Voice Recognition
scripts
  • __init__.py848 B
  • augmentation.py13.9 KB
  • evaluate_model.py13.9 KB
  • example_usage.py5.3 KB
  • generate_sample_audio.py10.2 KB
  • load_dataset.py9 KB
  • preprocess.py8.5 KB
  • train_model.py9.5 KB
  • transformer_model.py14.9 KB
generate_sample_audio.py.gitkeepsample_data.json.keepload_dataset.py
scripts/generate_sample_audio.py
Raw Download
Find: Go to:
"""
============================================================================
Speech Recognition Dataset - Sample Audio Generator with Text-to-Speech
============================================================================

Project: Speech Recognition Dataset
Description: Audio speech recognition dataset with labeled speech samples 
             for training speech-to-text and voice recognition models.

============================================================================
DEVELOPER INFORMATION
============================================================================
Website: https://rskworld.in
Founded by: Molla Samser
Designer & Tester: Rima Khatun
Email: help@rskworld.in
Support: support@rskworld.in
Phone: +91 93305 39277
Address: Nutanhat, Mongolkote, Purba Burdwan, West Bengal, India, 713147

============================================================================
COPYRIGHT NOTICE
============================================================================
© 2026 RSK World. All rights reserved.
This dataset is provided for educational and research purposes.

============================================================================

This script generates sample audio files using Text-to-Speech (TTS).
It speaks the actual transcripts like "Hello, how are you today?"
"""

import numpy as np
import pandas as pd
from pathlib import Path
from scipy.io import wavfile
import os

# Try to import TTS libraries
TTS_ENGINE = None

try:
    import pyttsx3
    TTS_ENGINE = 'pyttsx3'
    print("[OK] Found pyttsx3 - Will generate real speech!")
except ImportError:
    print("[!] pyttsx3 not found")
    try:
        from gtts import gTTS
        TTS_ENGINE = 'gtts'
        print("[OK] Found gTTS - Will generate real speech (requires internet)")
    except ImportError:
        print("[!] gTTS not found")
        print("[!] Install TTS: pip install pyttsx3")
        print("[!] Or: pip install gtts")


class SpeechAudioGenerator:
    """
    Generate speech audio files using Text-to-Speech.
    Speaks the actual words like "Hello, how are you today?"
    """
    
    def __init__(self, output_dir='data/audio', sr=22050):
        """
        Initialize the speech generator.
        
        Args:
            output_dir: Directory to save audio files
            sr: Sample rate (22050 is common for speech)
        """
        self.output_dir = Path(output_dir)
        self.sr = sr
        self.output_dir.mkdir(parents=True, exist_ok=True)
        
        # Initialize TTS engine
        self.engine = None
        self.voices = []
        
        if TTS_ENGINE == 'pyttsx3':
            try:
                self.engine = pyttsx3.init()
                # Configure for natural speech
                self.engine.setProperty('rate', 150)  # Words per minute
                self.engine.setProperty('volume', 0.9)
                
                # Get available voices
                self.voices = self.engine.getProperty('voices') or []
                print(f"[OK] Initialized pyttsx3 with {len(self.voices)} voice(s)")
                
                # Show available voices
                for i, voice in enumerate(self.voices):
                    print(f"    Voice {i}: {voice.name}")
            except Exception as e:
                print(f"[!] Error initializing pyttsx3: {e}")
                self.engine = None
    
    def generate_speech_pyttsx3(self, text, output_path, voice_index=0):
        """
        Generate speech using pyttsx3 (Windows SAPI / espeak).
        
        Args:
            text: Text to speak
            output_path: Where to save the audio
            voice_index: Which voice to use
            
        Returns:
            True if successful
        """
        if not self.engine:
            return False
        
        try:
            # Select voice
            if self.voices and voice_index < len(self.voices):
                self.engine.setProperty('voice', self.voices[voice_index].id)
            
            # Generate speech and save to file
            self.engine.save_to_file(text, str(output_path))
            self.engine.runAndWait()
            
            # Verify file was created
            if output_path.exists() and output_path.stat().st_size > 0:
                return True
            return False
            
        except Exception as e:
            print(f"[!] pyttsx3 error for '{text[:30]}...': {e}")
            return False
    
    def generate_speech_gtts(self, text, output_path):
        """
        Generate speech using Google Text-to-Speech.
        
        Args:
            text: Text to speak
            output_path: Where to save (will be MP3)
            
        Returns:
            True if successful
        """
        try:
            from gtts import gTTS
            
            tts = gTTS(text=text, lang='en', slow=False)
            
            # gTTS saves as MP3
            mp3_path = str(output_path).replace('.wav', '.mp3')
            tts.save(mp3_path)
            
            # Try to convert to WAV
            try:
                from pydub import AudioSegment
                audio = AudioSegment.from_mp3(mp3_path)
                audio = audio.set_frame_rate(self.sr)
                audio.export(str(output_path), format='wav')
                os.remove(mp3_path)
            except ImportError:
                # Keep as MP3
                if output_path.suffix == '.wav':
                    os.rename(mp3_path, str(output_path).replace('.wav', '.mp3'))
            
            return True
            
        except Exception as e:
            print(f"[!] gTTS error: {e}")
            return False
    
    def generate_fallback(self, text, output_path, duration):
        """
        Generate fallback audio when TTS is not available.
        Creates speech-like synthetic sounds.
        """
        num_samples = int(duration * self.sr)
        t = np.linspace(0, duration, num_samples)
        
        # Fundamental frequency (pitch)
        f0 = 120
        
        # Generate harmonics
        audio = np.zeros(num_samples)
        for harmonic in range(1, 8):
            freq = f0 * harmonic
            amp = 1.0 / harmonic
            audio += amp * np.sin(2 * np.pi * freq * t)
        
        # Create envelope based on words
        words = text.split() if text else ['word']
        word_dur = duration / max(len(words), 1)
        
        envelope = np.zeros(num_samples)
        for i in range(len(words)):
            start = int(i * word_dur * self.sr)
            end = int((i + 0.7) * word_dur * self.sr)
            end = min(end, num_samples)
            if start < num_samples and end > start:
                length = end - start
                envelope[start:end] = np.hanning(length)
        
        audio *= envelope
        
        # Add subtle noise
        audio += np.random.randn(num_samples) * 0.03
        
        # Normalize
        if np.max(np.abs(audio)) > 0:
            audio = audio / np.max(np.abs(audio)) * 0.7
        
        # Save
        audio_int16 = (audio * 32767).astype(np.int16)
        wavfile.write(str(output_path), self.sr, audio_int16)
    
    def generate_from_metadata(self, metadata_path='data/metadata.csv'):
        """
        Generate speech audio files from metadata.
        
        Args:
            metadata_path: Path to CSV with transcripts
        """
        metadata = pd.read_csv(metadata_path)
        
        print("\n" + "=" * 60)
        print("GENERATING SPEECH AUDIO")
        print("=" * 60)
        print(f"Total files: {len(metadata)}")
        print(f"Output: {self.output_dir}")
        print(f"TTS Engine: {TTS_ENGINE or 'Fallback (synthetic)'}")
        print("=" * 60 + "\n")
        
        success_tts = 0
        success_fallback = 0
        
        for idx, row in metadata.iterrows():
            file_name = row['file_name']
            duration = row['duration']
            transcript = row['transcript']
            speaker = row.get('speaker', 'Speaker_001')
            
            output_path = self.output_dir / file_name
            
            # Progress
            print(f"[{idx+1}/{len(metadata)}] {file_name}")
            print(f"    Text: \"{transcript}\"")
            
            success = False
            
            # Try pyttsx3
            if TTS_ENGINE == 'pyttsx3' and self.engine:
                # Vary voice based on speaker
                speaker_num = int(speaker.split('_')[1]) if '_' in speaker else 0
                voice_idx = speaker_num % max(1, len(self.voices))
                success = self.generate_speech_pyttsx3(transcript, output_path, voice_idx)
            
            # Try gTTS
            elif TTS_ENGINE == 'gtts':
                success = self.generate_speech_gtts(transcript, output_path)
            
            if success:
                print(f"    [OK] Generated with TTS")
                success_tts += 1
            else:
                # Fallback
                self.generate_fallback(transcript, output_path, duration)
                print(f"    [OK] Generated with fallback")
                success_fallback += 1
        
        # Summary
        print("\n" + "=" * 60)
        print("GENERATION COMPLETE")
        print("=" * 60)
        print(f"TTS generated:      {success_tts} files")
        print(f"Fallback generated: {success_fallback} files")
        print(f"Total:              {len(metadata)} files")
        print(f"Location:           {self.output_dir}")
        print("=" * 60)


def main():
    """Generate speech audio files."""
    print("\n" + "=" * 60)
    print("SPEECH RECOGNITION DATASET")
    print("Audio Generator with Text-to-Speech")
    print("RSK World - https://rskworld.in")
    print("=" * 60)
    
    # Check for TTS
    if not TTS_ENGINE:
        print("\n[!] No TTS engine found!")
        print("    To generate real speech, install pyttsx3:")
        print("    pip install pyttsx3")
        print("\n    Continuing with fallback audio...")
    
    generator = SpeechAudioGenerator(
        output_dir='data/audio',
        sr=22050  # Good quality for speech
    )
    
    generator.generate_from_metadata('data/metadata.csv')
    
    print("\n" + "=" * 60)
    print("(C) 2026 RSK World. All rights reserved.")
    print("=" * 60 + "\n")


if __name__ == '__main__':
    main()
307 lines•10.2 KB
python
scripts/load_dataset.py
Raw Download
Find: Go to:
"""
============================================================================
Speech Recognition Dataset - Dataset Loader
============================================================================

Project: Speech Recognition Dataset
Description: Audio speech recognition dataset with labeled speech samples 
             for training speech-to-text and voice recognition models.

============================================================================
DEVELOPER INFORMATION
============================================================================
Website: https://rskworld.in
Founded by: Molla Samser
Designer & Tester: Rima Khatun
Email: help@rskworld.in
Support: support@rskworld.in
Phone: +91 93305 39277
Address: Nutanhat, Mongolkote, Purba Burdwan, West Bengal, India, 713147

============================================================================
COPYRIGHT NOTICE
============================================================================
© 2026 RSK World. All rights reserved.
This dataset is provided for educational and research purposes.

============================================================================
"""

import pandas as pd
import numpy as np
import librosa
from pathlib import Path
import json

class SpeechRecognitionDataset:
    """
    Dataset loader for Speech Recognition Dataset
    
    Provides easy access to audio files, metadata, and transcripts
    """
    
    def __init__(self, data_dir='data'):
        """
        Initialize the dataset loader
        
        Args:
            data_dir: Root directory of the dataset
        """
        self.data_dir = Path(data_dir)
        self.audio_dir = self.data_dir / 'audio'
        self.metadata_path = self.data_dir / 'metadata.csv'
        self.transcripts_path = self.data_dir / 'transcripts.json'
        
        # Load metadata
        if self.metadata_path.exists():
            self.metadata = pd.read_csv(self.metadata_path)
        else:
            self.metadata = None
            print(f"Warning: Metadata file not found at {self.metadata_path}")
        
        # Load transcripts
        if self.transcripts_path.exists():
            with open(self.transcripts_path, 'r') as f:
                self.transcripts = json.load(f)
        else:
            self.transcripts = {}
            print(f"Warning: Transcripts file not found at {self.transcripts_path}")
    
    def get_audio_file(self, file_id):
        """
        Get path to audio file by ID
        
        Args:
            file_id: ID of the audio file
            
        Returns:
            Path to audio file
        """
        if self.metadata is None:
            raise ValueError("Metadata not loaded")
        
        row = self.metadata[self.metadata['id'] == file_id]
        if row.empty:
            raise ValueError(f"File ID {file_id} not found in metadata")
        
        file_name = row.iloc[0]['file_name']
        audio_path = self.audio_dir / file_name
        
        if not audio_path.exists():
            raise FileNotFoundError(f"Audio file not found: {audio_path}")
        
        return audio_path
    
    def load_audio(self, file_id, sr=16000):
        """
        Load audio file as numpy array
        
        Args:
            file_id: ID of the audio file
            sr: Sample rate
            
        Returns:
            Audio array and sample rate
        """
        audio_path = self.get_audio_file(file_id)
        y, sr = librosa.load(str(audio_path), sr=sr)
        return y, sr
    
    def get_transcript(self, file_id):
        """
        Get transcript for audio file
        
        Args:
            file_id: ID of the audio file
            
        Returns:
            Transcript text
        """
        if file_id in self.transcripts:
            return self.transcripts[file_id]
        
        # Try to get from metadata
        if self.metadata is not None:
            row = self.metadata[self.metadata['id'] == file_id]
            if not row.empty and 'transcript' in row.columns:
                return row.iloc[0]['transcript']
        
        return None
    
    def get_speaker(self, file_id):
        """
        Get speaker ID for audio file
        
        Args:
            file_id: ID of the audio file
            
        Returns:
            Speaker ID
        """
        if self.metadata is None:
            return None
        
        row = self.metadata[self.metadata['id'] == file_id]
        if not row.empty and 'speaker' in row.columns:
            return row.iloc[0]['speaker']
        
        return None
    
    def get_metadata(self, file_id):
        """
        Get all metadata for audio file
        
        Args:
            file_id: ID of the audio file
            
        Returns:
            Dictionary with metadata
        """
        if self.metadata is None:
            return None
        
        row = self.metadata[self.metadata['id'] == file_id]
        if row.empty:
            return None
        
        return row.iloc[0].to_dict()
    
    def get_files_by_speaker(self, speaker_id):
        """
        Get all file IDs for a specific speaker
        
        Args:
            speaker_id: ID of the speaker
            
        Returns:
            List of file IDs
        """
        if self.metadata is None:
            return []
        
        rows = self.metadata[self.metadata['speaker'] == speaker_id]
        return rows['id'].tolist()
    
    def get_files_by_category(self, category):
        """
        Get all file IDs for a specific category
        
        Args:
            category: Category name (e.g., 'Greeting', 'Command')
            
        Returns:
            List of file IDs
        """
        if self.metadata is None:
            return []
        
        if 'category' not in self.metadata.columns:
            return []
        
        rows = self.metadata[self.metadata['category'] == category]
        return rows['id'].tolist()
    
    def get_statistics(self):
        """
        Get dataset statistics
        
        Returns:
            Dictionary with statistics
        """
        if self.metadata is None:
            return {}
        
        stats = {
            'total_files': len(self.metadata),
            'unique_speakers': self.metadata['speaker'].nunique() if 'speaker' in self.metadata.columns else 0,
            'total_duration': self.metadata['duration'].sum() if 'duration' in self.metadata.columns else 0,
            'average_duration': self.metadata['duration'].mean() if 'duration' in self.metadata.columns else 0,
            'min_duration': self.metadata['duration'].min() if 'duration' in self.metadata.columns else 0,
            'max_duration': self.metadata['duration'].max() if 'duration' in self.metadata.columns else 0,
        }
        
        if 'category' in self.metadata.columns:
            stats['categories'] = self.metadata['category'].value_counts().to_dict()
        
        return stats
    
    def sample(self, n=5, speaker_id=None, category=None):
        """
        Get random sample of files
        
        Args:
            n: Number of samples
            speaker_id: Filter by speaker (optional)
            category: Filter by category (optional)
            
        Returns:
            DataFrame with sample metadata
        """
        if self.metadata is None:
            return None
        
        df = self.metadata.copy()
        
        if speaker_id:
            df = df[df['speaker'] == speaker_id]
        
        if category:
            if 'category' in df.columns:
                df = df[df['category'] == category]
        
        return df.sample(min(n, len(df)))


def main():
    """Example usage of the dataset loader"""
    # Initialize dataset
    dataset = SpeechRecognitionDataset(data_dir='data')
    
    # Get statistics
    stats = dataset.get_statistics()
    print("Dataset Statistics:")
    for key, value in stats.items():
        print(f"  {key}: {value}")
    
    # Get a sample
    print("\nSample files:")
    sample = dataset.sample(n=5)
    if sample is not None:
        print(sample[['id', 'file_name', 'speaker', 'duration', 'transcript']].head())
    
    # Load an audio file
    if sample is not None and len(sample) > 0:
        file_id = sample.iloc[0]['id']
        print(f"\nLoading audio file {file_id}...")
        try:
            audio, sr = dataset.load_audio(file_id)
            transcript = dataset.get_transcript(file_id)
            speaker = dataset.get_speaker(file_id)
            
            print(f"  Audio shape: {audio.shape}")
            print(f"  Sample rate: {sr}")
            print(f"  Duration: {len(audio) / sr:.2f} seconds")
            print(f"  Speaker: {speaker}")
            print(f"  Transcript: {transcript}")
        except Exception as e:
            print(f"  Error loading audio: {str(e)}")


if __name__ == '__main__':
    main()

293 lines•9 KB
python
🚀 Support RSK World

Subscribe to our YouTube channel for latest tutorials & updates!



Click subscribe & support our work ❤️

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer