Skip to content

SOLUTION-LAB-012: Audio AI

⏱ 1 min read·📅 Last updated: 2026-10-13

Reference solution for LAB-012: Audio AI.

Overview

Complete solution for audio processing with AI models, including speech-to-text, text-to-speech, and audio analysis.


Prerequisites

uv pip install torch torchaudio transformers datasets librosa soundfile pydub
uv pip install openai-whisper  # For Whisper
uv pip install TTS              # For Coqui TTS
uv pip install speechbrain      # For audio classification

Solution 1: Speech-to-Text with Whisper

import torch
import torchaudio
import librosa
from transformers import WhisperProcessor, WhisperForConditionalGeneration
from pathlib import Path


class WhisperSTT:
    """Speech-to-Text using OpenAI Whisper."""

    def __init__(self, model_size="base"):
        """
        Initialize Whisper model.

        Args:
            model_size: tiny, base, small, medium, large
        """
        model_name = f"openai/whisper-{model_size}"

        self.processor = WhisperProcessor.from_pretrained(model_name)
        self.model = WhisperForConditionalGeneration.from_pretrained(model_name)
        self.model.config.forced_decoder_ids = None

        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
        self.model.to(self.device)

    def load_audio(self, audio_path, target_sr=16000):
        """Load and resample audio file."""

        # Load audio
        waveform, original_sr = torchaudio.load(audio_path)

        # Convert to mono if stereo
        if waveform.shape[0] > 1:
            waveform = torch.mean(waveform, dim=0, keepdim=True)

        # Resample if needed
        if original_sr != target_sr:
            resampler = torchaudio.transforms.Resample(
                orig_freq=original_sr,
                new_freq=target_sr
            )
            waveform = resampler(waveform)

        return waveform.squeeze(0), target_sr

    def transcribe(
        self,
        audio_path,
        language=None,
        task="transcribe",  # or "translate"
        max_length=448
    ):
        """
        Transcribe audio file to text.

        Args:
            audio_path: Path to audio file
            language: Language code (e.g., 'en', 'tr', 'es')
            task: 'transcribe' or 'translate' (to English)
            max_length: Max sequence length

        Returns:
            Transcription text
        """

        # Load audio
        waveform, sr = self.load_audio(audio_path)

        # Prepare inputs
        inputs = self.processor(
            waveform,
            sampling_rate=sr,
            return_tensors="pt"
        ).input_features.to(self.device)

        # Generate forced decoder ids for language/task
        forced_decoder_ids = self.processor.get_decoder_prompt_ids(
            language=language,
            task=task
        ) if language else None

        # Generate transcription
        with torch.no_grad():
            predicted_ids = self.model.generate(
                inputs,
                forced_decoder_ids=forced_decoder_ids,
                max_length=max_length
            )

        # Decode
        transcription = self.processor.batch_decode(
            predicted_ids,
            skip_special_tokens=True
        )[0]

        return transcription

    def transcribe_with_timestamps(self, audio_path, language=None):
        """Transcribe with word-level timestamps."""

        waveform, sr = self.load_audio(audio_path)

        inputs = self.processor(
            waveform,
            sampling_rate=sr,
            return_tensors="pt"
        ).input_features.to(self.device)

        forced_decoder_ids = self.processor.get_decoder_prompt_ids(
            language=language,
            task="transcribe"
        ) if language else None

        with torch.no_grad():
            outputs = self.model.generate(
                inputs,
                forced_decoder_ids=forced_decoder_ids,
                return_timestamps=True,
                output_scores=True,
                return_dict_in_generate=True
            )

        # Decode with timestamps
        transcription = self.processor.decode(
            outputs.sequences[0],
            skip_special_tokens=True
        )

        return {
            "text": transcription,
            "chunks": outputs.get("timestamps", [])
        }


# Usage
if __name__ == "__main__":
    stt = WhisperSTT(model_size="base")

    # Simple transcription
    text = stt.transcribe("audio_sample.wav", language="en")
    print(f"Transcription: {text}")

    # With timestamps
    result = stt.transcribe_with_timestamps("audio_sample.wav", language="en")
    print(f"Text: {result['text']}")
    print(f"Chunks: {result['chunks']}")

Solution 2: Text-to-Speech with Coqui TTS

from TTS.api import TTS
import torch
import soundfile as sf


class CoquiTTS:
    """Text-to-Speech using Coqui TTS."""

    def __init__(self, model_name="tts_models/en/ljspeech/vits"):
        """
        Initialize TTS model.

        Model options:
        - tts_models/en/ljspeech/vits (English, fast)
        - tts_models/multilingual/multi-dataset/xtts_v2 (Multilingual)
        - tts_models/en/blizzard2013/capacitron-t2-c (High quality)
        """
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        self.tts = TTS(model_name=model_name).to(self.device)

    def synthesize(self, text, output_path="output.wav"):
        """Synthesize speech from text."""

        self.tts.tts_to_file(
            text=text,
            file_path=output_path
        )

        return output_path

    def synthesize_with_voice_cloning(
        self,
        text,
        reference_audio,
        output_path="output_cloned.wav"
    ):
        """Synthesize with voice cloning (XTTS only)."""

        # Using XTTS for voice cloning
        if "xtts" not in self.tts.model_name:
            print("Voice cloning requires XTTS model")
            return self.synthesize(text, output_path)

        self.tts.tts_to_file(
            text=text,
            file_path=output_path,
            speaker_wav=reference_audio,
            language="en"
        )

        return output_path

    def synthesize_batch(self, texts, output_dir="outputs"):
        """Synthesize multiple texts."""

        from pathlib import Path

        output_dir = Path(output_dir)
        output_dir.mkdir(exist_ok=True)

        output_paths = []

        for i, text in enumerate(texts):
            output_path = output_dir / f"output_{i}.wav"
            self.synthesize(text, str(output_path))
            output_paths.append(output_path)

        return output_paths


# Multilingual TTS
class MultilingualTTS:
    """Multilingual TTS using XTTS."""

    def __init__(self):
        self.device = "cuda" if torch.cuda.is_available() else "cpu"
        self.tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(self.device)
        self.supported_langs = [
            'en', 'es', 'fr', 'de', 'it', 'pt', 'pl', 'tr', 'ru',
            'nl', 'cs', 'ar', 'zh-cn', 'ja', 'ko', 'hu'
        ]

    def synthesize(self, text, language="en", speaker_wav=None, output_path="output.wav"):
        """Synthesize in specified language."""

        if language not in self.supported_langs:
            raise ValueError(f"Language {language} not supported")

        kwargs = {
            "text": text,
            "file_path": output_path,
            "language": language
        }

        if speaker_wav:
            kwargs["speaker_wav"] = speaker_wav

        self.tts.tts_to_file(**kwargs)

        return output_path


# Usage
if __name__ == "__main__":
    # Basic TTS
    tts = CoquiTTS()
    tts.synthesize("Hello, world!", "hello.wav")

    # Multilingual
    multi_tts = MultilingualTTS()
    multi_tts.synthesize("Merhaba dünya!", language="tr", output_path="merhaba.wav")
    multi_tts.synthesize("Hola mundo!", language="es", output_path="hola.wav")

Solution 3: Audio Classification with SpeechBrain

import torch
import torchaudio
from speechbrain.inference.classifiers import AudioClassifier


class AudioClassifier:
    """Audio scene classification using SpeechBrain."""

    def __init__(self):
        """Initialize classifier."""
        self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

        # Load pre-trained classifier
        from speechbrain.inference.classifiers import SpeakerRecognition
        self.classifier = SpeakerRecognition.from_hparams(
            source="speechbrain/spkrec-ecapa-voxceleb",
            run_opts={"device": self.device}
        )

    def classify_speaker(self, audio_path, reference_embeddings):
        """Classify speaker against reference embeddings."""

        # Extract embedding
        embedding = self.classifier.encode_batch(audio_path)

        # Compare with references
        similarities = {}
        for name, ref_emb in reference_embeddings.items():
            similarity = torch.nn.functional.cosine_similarity(
                embedding, ref_emb, dim=-1
            ).item()
            similarities[name] = similarity

        return similarities

    def verify_speaker(self, audio_path1, audio_path2, threshold=0.25):
        """Verify if two audio files are from the same speaker."""

        # Extract embeddings
        emb1 = self.classifier.encode_batch(audio_path1)
        emb2 = self.classifier.encode_batch(audio_path2)

        # Calculate similarity
        similarity = torch.nn.functional.cosine_similarity(
            emb1, emb2, dim=-1
        ).item()

        is_same = similarity > threshold

        return {
            "same_speaker": is_same,
            "similarity": similarity,
            "threshold": threshold
        }


class EmotionRecognizer:
    """Speech emotion recognition."""

    def __init__(self):
        """Initialize emotion recognizer."""
        from speechbrain.inference.classifiers import AudioClassifier

        self.classifier = AudioClassifier.from_hparams(
            source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP",
            run_opts={"device": "cuda" if torch.cuda.is_available() else "cpu"}
        )

        self.emotions = {
            0: "happy",
            1: "angry",
            2: "sad",
            3: "neutral"
        }

    def recognize_emotion(self, audio_path):
        """Recognize emotion in speech."""

        # Classify
        emotion_probs = self.classifier.classify_file(audio_path)

        # Get top emotion
        top_idx = torch.argmax(emotion_probs).item()
        confidence = emotion_probs[0][top_idx].item()

        return {
            "emotion": self.emotions[top_idx],
            "confidence": confidence,
            "all_probs": {
                self.emotions[i]: emotion_probs[0][i].item()
                for i in range(len(self.emotions))
            }
        }


# Usage
if __name__ == "__main__":
    # Speaker verification
    verifier = AudioClassifier()
    result = verifier.verify_speaker("speaker1.wav", "speaker1_test.wav")
    print(f"Same speaker: {result['same_speaker']} ({result['similarity']:.3f})")

    # Emotion recognition
    emotion_rec = EmotionRecognizer()
    emotion = emotion_rec.recognize_emotion("emotional_speech.wav")
    print(f"Detected: {emotion['emotion']} ({emotion['confidence']:.2%})")

Solution 4: Audio Augmentation

import torch
import torchaudio
import random
import numpy as np


class AudioAugmentation:
    """Audio augmentation for training."""

    def __init__(self, sample_rate=16000):
        self.sample_rate = sample_rate

    def add_noise(self, waveform, noise_level=0.01):
        """Add random noise."""

        noise = torch.randn_like(waveform) * noise_level
        return waveform + noise

    def time_shift(self, waveform, shift_max=0.2):
        """Shift audio in time."""

        shift_ms = int(random.uniform(-shift_max, shift_max) * self.sample_rate)
        shift_samples = int(shift_ms * self.sample_rate / 1000)

        if shift_samples > 0:
            waveform = torch.cat([waveform[shift_samples:], torch.zeros(shift_samples)])
        elif shift_samples < 0:
            waveform = torch.cat([torch.zeros(-shift_samples), waveform[:shift_samples]])

        return waveform

    def speed_change(self, waveform, speed_factor=1.0):
        """Change playback speed."""

        # Use resampling for speed change
        new_sr = int(self.sample_rate * speed_factor)
        resampler = torchaudio.transforms.Resample(
            orig_freq=self.sample_rate,
            new_freq=new_sr
        )

        resampled = resampler(waveform)

        # Resample back to original rate
        resampler_back = torchaudio.transforms.Resample(
            orig_freq=new_sr,
            new_freq=self.sample_rate
        )

        return resampler_back(resampled)

    def pitch_shift(self, waveform, n_steps=0):
        """Shift pitch (requires librosa)."""

        waveform_np = waveform.numpy()
        shifted = librosa.effects.pitch_shift(
            waveform_np,
            sr=self.sample_rate,
            n_steps=n_steps
        )

        return torch.from_numpy(shifted).float()

    def gain(self, waveform, gain_factor=1.0):
        """Adjust gain/volume."""

        return waveform * gain_factor

    def reverse(self, waveform):
        """Reverse audio."""

        return torch.flip(waveform, [0])

    def augment(self, waveform):
        """Apply random augmentation."""

        # Randomly select augmentations
        if random.random() > 0.5:
            waveform = self.add_noise(waveform)

        if random.random() > 0.5:
            waveform = self.time_shift(waveform)

        if random.random() > 0.7:
            speed = random.uniform(0.9, 1.1)
            waveform = self.speed_change(waveform, speed)

        if random.random() > 0.7:
            gain = random.uniform(0.8, 1.2)
            waveform = self.gain(waveform, gain)

        return waveform


# Usage
if __name__ == "__main__":
    augmenter = AudioAugmentation()

    waveform, sr = torchaudio.load("audio.wav")

    # Apply augmentations
    noisy = augmenter.add_noise(waveform)
    shifted = augmenter.time_shift(waveform)
    faster = augmenter.speed_change(waveform, 1.2)
    pitched = augmenter.pitch_shift(waveform, n_steps=2)

Solution 5: Audio Analysis and Visualization

import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np


class AudioAnalyzer:
    """Audio analysis and visualization."""

    def __init__(self, audio_path, sr=22050):
        """Load audio file."""

        self.audio_path = audio_path
        self.y, self.sr = librosa.load(audio_path, sr=sr)
        self.duration = len(self.y) / self.sr

    def plot_waveform(self):
        """Plot waveform."""

        plt.figure(figsize=(14, 5))
        librosa.display.waveshow(self.y, sr=self.sr)
        plt.title('Waveform')
        plt.xlabel('Time (s)')
        plt.ylabel('Amplitude')
        plt.tight_layout()
        plt.savefig('waveform.png')
        plt.show()

    def plot_spectrogram(self):
        """Plot spectrogram."""

        # Compute spectrogram
        D = librosa.amplitude_to_db(np.abs(librosa.stft(self.y)), ref=np.max)

        plt.figure(figsize=(14, 5))
        librosa.display.specshow(D, sr=self.sr, x_axis='time', y_axis='hz')
        plt.colorbar(format='%+2.0f dB')
        plt.title('Spectrogram')
        plt.tight_layout()
        plt.savefig('spectrogram.png')
        plt.show()

    def plot_mel_spectrogram(self):
        """Plot mel spectrogram."""

        # Compute mel spectrogram
        mel_spec = librosa.feature.melspectrogram(y=self.y, sr=self.sr)
        mel_spec_db = librosa.power_to_db(mel_spec, ref=np.max)

        plt.figure(figsize=(14, 5))
        librosa.display.specshow(
            mel_spec_db,
            sr=self.sr,
            x_axis='time',
            y_axis='mel'
        )
        plt.colorbar(format='%+2.0f dB')
        plt.title('Mel Spectrogram')
        plt.tight_layout()
        plt.savefig('mel_spectrogram.png')
        plt.show()

    def plot_mfcc(self):
        """Plot MFCCs."""

        # Compute MFCCs
        mfccs = librosa.feature.mfcc(y=self.y, sr=self.sr, n_mfcc=13)

        plt.figure(figsize=(14, 5))
        librosa.display.specshow(
            mfccs,
            sr=self.sr,
            x_axis='time'
        )
        plt.colorbar()
        plt.title('MFCC')
        plt.tight_layout()
        plt.savefig('mfcc.png')
        plt.show()

    def extract_features(self):
        """Extract audio features."""

        features = {
            'tempo': librosa.beat.tempo(y=self.y, sr=self.sr)[0],
            'spectral_centroid': librosa.feature.spectral_centroid(y=self.y, sr=self.sr)[0].mean(),
            'spectral_rolloff': librosa.feature.spectral_rolloff(y=self.y, sr=self.sr)[0].mean(),
            'zero_crossing_rate': librosa.feature.zero_crossing_rate(self.y)[0].mean(),
            'mfccs': librosa.feature.mfcc(y=self.y, sr=self.sr, n_mfcc=13),
            'chroma': librosa.feature.chroma_stft(y=self.y, sr=self.sr),
            'duration': self.duration
        }

        return features

    def detect_silence(self, threshold=30):
        """Detect silent segments."""

        # Compute RMS energy
        rms = librosa.feature.rms(y=self.y)[0]

        # Find frames below threshold
        silent_frames = rms < np.mean(rms) / (10 ** (threshold / 20))

        # Convert to time
        silent_times = librosa.frames_to_time(
            np.where(silent_frames)[0],
            sr=self.sr
        )

        return silent_times

    def analyze(self):
        """Full analysis pipeline."""

        print(f"Analyzing: {self.audio_path}")
        print(f"Duration: {self.duration:.2f} seconds")
        print(f"Sample rate: {self.sr} Hz")

        # Extract features
        features = self.extract_features()

        print(f"\nFeatures:")
        print(f"  Tempo: {features['tempo']:.2f} BPM")
        print(f"  Spectral centroid: {features['spectral_centroid']:.2f} Hz")
        print(f"  Spectral rolloff: {features['spectral_rolloff']:.2f} Hz")
        print(f"  ZCR: {features['zero_crossing_rate']:.4f}")

        # Generate plots
        self.plot_waveform()
        self.plot_spectrogram()
        self.plot_mel_spectrogram()
        self.plot_mfcc()

        return features


# Usage
if __name__ == "__main__":
    analyzer = AudioAnalyzer("audio.wav")
    features = analyzer.analyze()

Solution 6: Complete Audio Pipeline

import asyncio
from concurrent.futures import ThreadPoolExecutor


class AudioPipeline:
    """Complete audio processing pipeline."""

    def __init__(self):
        self.stt = WhisperSTT(model_size="base")
        self.tts = CoquiTTS()
        self.emotion_recognizer = EmotionRecognizer()

    async def transcribe_emotional_response(
        self,
        audio_input_path,
        output_audio_path="response.wav"
    ):
        """
        Transcribe audio, detect emotion, and generate emotional response.

        Args:
            audio_input_path: Input audio file
            output_audio_path: Where to save response audio

        Returns:
            Dict with transcription, emotion, and response
        """

        # Step 1: Transcribe
        print("Transcribing...")
        transcription = self.stt.transcribe(audio_input_path)

        # Step 2: Detect emotion
        print("Detecting emotion...")
        emotion_data = self.emotion_recognizer.recognize_emotion(audio_input_path)

        # Step 3: Generate response based on emotion
        response_text = self.generate_emotional_response(
            transcription,
            emotion_data['emotion']
        )

        # Step 4: Synthesize response
        print("Synthesizing response...")
        self.tts.synthesize(response_text, output_audio_path)

        return {
            "transcription": transcription,
            "emotion": emotion_data['emotion'],
            "emotion_confidence": emotion_data['confidence'],
            "response": response_text,
            "response_audio": output_audio_path
        }

    def generate_emotional_response(self, user_input, emotion):
        """Generate contextually appropriate response."""

        responses = {
            "happy": [
                "That's wonderful to hear!",
                "I'm so glad you're feeling good!",
                "Great! Let's keep this positive energy going!"
            ],
            "sad": [
                "I'm sorry to hear that.",
                "It sounds like you're going through a difficult time.",
                "I'm here for you."
            ],
            "angry": [
                "I understand you're frustrated.",
                "Let's take a deep breath together.",
                "I hear your concerns."
            ],
            "neutral": [
                "I see.",
                "Thank you for sharing that.",
                "Tell me more."
            ]
        }

        import random
        return random.choice(responses.get(emotion, responses["neutral"]))

    def batch_process(self, audio_files):
        """Process multiple audio files."""

        results = []

        for audio_file in audio_files:
            result = asyncio.run(
                self.transcribe_emotional_response(audio_file)
            )
            results.append(result)

        return results


# Voice Assistant
class VoiceAssistant:
    """Simple voice assistant."""

    def __init__(self):
        self.pipeline = AudioPipeline()

    def chat(self):
        """Interactive voice chat."""

        import pyaudio

        print("Voice Assistant Ready!")
        print("Press Enter to start speaking, 'q' to quit")

        while True:
            user_input = input("\n> ")

            if user_input.lower() == 'q':
                break

            if user_input == "":
                print("Listening...")
                # Record audio (would use pyaudio here)
                audio_path = "user_input.wav"

                # Process
                result = asyncio.run(
                    self.pipeline.transcribe_emotional_response(audio_path)
                )

                print(f"\nYou said: {result['transcription']}")
                print(f"Emotion: {result['emotion']} ({result['emotion_confidence']:.2%})")
                print(f"Response: {result['response']}")

                # Play response
                print("Playing response...")
                # Would use audio library here


# Usage
if __name__ == "__main__":
    # Complete pipeline
    pipeline = AudioPipeline()

    result = asyncio.run(
        pipeline.transcribe_emotional_response(
            "input.wav",
            "response.wav"
        )
    )

    print(f"Transcription: {result['transcription']}")
    print(f"Emotion: {result['emotion']}")
    print(f"Response: {result['response']}")

Expected Results

Speech-to-Text

  • Accuracy: >95% on clear speech
  • Languages: 99+ languages supported
  • Speed: Real-time on GPU

Text-to-Speech

  • Quality: Natural sounding speech
  • Languages: 20+ languages with XTTS
  • Voice Cloning: Good quality with 3-10 seconds reference

Audio Classification

  • Speaker Verification: >98% accuracy
  • Emotion Recognition: 70-80% accuracy

Key Features Implemented

1. Speech-to-Text

  • Whisper integration
  • Multi-language support
  • Word-level timestamps
  • Batch processing

2. Text-to-Speech

  • Coqui TTS integration
  • Multilingual support
  • Voice cloning with XTTS
  • Batch synthesis

3. Audio Classification

  • Speaker verification
  • Emotion recognition
  • Feature extraction

4. Audio Augmentation

  • Noise injection
  • Time shifting
  • Speed/pitch changing
  • Gain adjustment

5. Audio Analysis

  • Waveform visualization
  • Spectrogram generation
  • MFCC extraction
  • Feature analysis

6. Complete Pipeline

  • STT → Emotion Detection → TTS
  • Async processing
  • Interactive voice assistant

Difficulty: ⭐⭐⭐ Advanced

Lines of Code: ~700