#!/usr/bin/env python3
"""
Realistic Female Voice Text-to-Speech
File: tts_stt.py

Focus: High-quality, realistic female voice at normal talking speed
Uses multiple premium TTS services for best voice quality
"""

import requests
import json
import os
import base64
from urllib.parse import quote
import time

# Configuration for realistic female voice
TEXT_TO_SPEAK = "I stand up, prepared to end the meeting* \"I'm not going to waste any more of my time on this conversation. If you're not prepared to have a serious discussion about how your tool can help my company, then I think we're done here."
OUTPUT_FILE = "realistic_female_voice.mp3"

class RealisticFemaleTTS:
    """High-quality female TTS with multiple premium services"""
    
    def __init__(self, text, output_file):
        self.text = text
        self.output_file = output_file
    
    def convert_with_elevenlabs_demo(self):
        """Using ElevenLabs-like API with realistic female voice"""
        
        print("🎤 Generating realistic female voice...")
        print(f"Text: {self.text[:80]}{'...' if len(self.text) > 80 else ''}")
        
        try:
            # Using a free TTS service with high-quality voices
            url = "https://api.streamelements.com/kappa/v2/speech"
            
            params = {
                'voice': 'Emma',  # High-quality female voice
                'text': self.text
            }
            
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
            }
            
            response = requests.get(url, params=params, headers=headers)
            
            if response.status_code == 200:
                with open(self.output_file, 'wb') as f:
                    f.write(response.content)
                print(f"✅ Realistic voice generated: {self.output_file}")
                print(f"📁 File size: {len(response.content)} bytes")
                return True
            else:
                print(f"❌ StreamElements API failed: {response.status_code}")
                return False
                
        except Exception as e:
            print(f"❌ StreamElements error: {str(e)}")
            return False
    
    def convert_with_google_wavenet(self):
        """Using Google TTS with WaveNet-quality voice parameters"""
        
        print("🎤 Trying Google TTS with enhanced female voice...")
        
        try:
            # Enhanced Google TTS with better voice parameters
            text_encoded = quote(self.text)
            
            # Using specific parameters for more natural female voice
            url = f"https://translate.google.com/translate_tts?ie=UTF-8&q={text_encoded}&tl=en-us&client=tw-ob&ttsspeed=0.5&idx=0&textlen={len(self.text)}&tk=&total=1"
            
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
                'Referer': 'https://translate.google.com/',
                'Accept': 'audio/mpeg, audio/*, */*',
                'Accept-Language': 'en-US,en;q=0.9'
            }
            
            response = requests.get(url, headers=headers)
            
            if response.status_code == 200 and len(response.content) > 1000:  # Ensure we got actual audio
                with open(self.output_file, 'wb') as f:
                    f.write(response.content)
                print(f"✅ Enhanced Google TTS success: {self.output_file}")
                print(f"📁 File size: {len(response.content)} bytes")
                return True
            else:
                print(f"❌ Enhanced Google TTS failed: {response.status_code}")
                return False
                
        except Exception as e:
            print(f"❌ Enhanced Google TTS error: {str(e)}")
            return False
    
    def convert_with_realistic_speech(self):
        """Using RealisticSpeech.com API (free tier)"""
        
        print("🎤 Trying RealisticSpeech API...")
        
        try:
            url = "https://realistic-text-to-speech.p.rapidapi.com/v3/generate_voice_over_v3"
            
            payload = {
                "voice_obj": {
                    "id": 2014,  # High-quality female voice
                    "voice_id": "en-US-JennyNeural",
                    "gender": "Female",
                    "language_code": "en-US",
                    "language_name": "US English",
                    "voice_name": "Jenny",
                    "sample_text": "Hello, this is a sample of my voice.",
                    "sample_audio_url": "https://s3.amazonaws.com/realistic-speech-prod/output/20210825/214205-4c7f7b5e-7e3a-4e7c-8b9d-5b5b4e5f1b2a.mp3",
                    "status": 2,
                    "rank": 0,
                    "type": "neural",
                    "isCustom": False
                },
                "json_data": [
                    {
                        "block_index": 0,
                        "text": self.text
                    }
                ]
            }
            
            headers = {
                "content-type": "application/json",
                "X-RapidAPI-Key": "demo",  # Free tier
                "X-RapidAPI-Host": "realistic-text-to-speech.p.rapidapi.com"
            }
            
            response = requests.post(url, json=payload, headers=headers)
            
            if response.status_code == 200:
                result = response.json()
                if 'output_url' in result:
                    # Download the generated audio
                    audio_response = requests.get(result['output_url'])
                    if audio_response.status_code == 200:
                        with open(self.output_file, 'wb') as f:
                            f.write(audio_response.content)
                        print(f"✅ RealisticSpeech success: {self.output_file}")
                        return True
            
            print("❌ RealisticSpeech API failed")
            return False
            
        except Exception as e:
            print(f"❌ RealisticSpeech error: {str(e)}")
            return False
    
    def convert_with_azure_like_voice(self):
        """Using Azure-like voice synthesis"""
        
        print("🎤 Trying high-quality voice synthesis...")
        
        try:
            # Using a different high-quality TTS endpoint
            url = "https://countik-text-to-speech.p.rapidapi.com/text-to-speech"
            
            payload = {
                "text": self.text,
                "voice": "en-US-JennyNeural",  # High-quality neural female voice
                "speed": 1.0,  # Normal talking speed
                "pitch": 1.0,
                "format": "mp3"
            }
            
            headers = {
                "content-type": "application/json",
                "X-RapidAPI-Key": "demo",
                "X-RapidAPI-Host": "countik-text-to-speech.p.rapidapi.com"
            }
            
            response = requests.post(url, json=payload, headers=headers)
            
            if response.status_code == 200:
                with open(self.output_file, 'wb') as f:
                    f.write(response.content)
                print(f"✅ High-quality synthesis success: {self.output_file}")
                return True
            else:
                print(f"❌ High-quality synthesis failed: {response.status_code}")
                return False
                
        except Exception as e:
            print(f"❌ High-quality synthesis error: {str(e)}")
            return False
    
    def convert_with_advanced_pyttsx3(self):
        """Advanced pyttsx3 with optimized female voice settings"""
        
        try:
            import pyttsx3
            
            print("🎤 Using advanced offline TTS with optimized settings...")
            
            engine = pyttsx3.init()
            
            # Get all available voices
            voices = engine.getProperty('voices')
            
            if voices:
                print("Available voices:")
                best_female_voice = None
                
                for i, voice in enumerate(voices):
                    print(f"{i}: {voice.name} ({'Female' if 'female' in voice.name.lower() or any(name in voice.name.lower() for name in ['zira', 'hazel', 'susan', 'samantha', 'karen', 'moira', 'tessa', 'paulina']) else 'Unknown'})")
                    
                    # Priority selection for best female voices
                    if any(name in voice.name.lower() for name in ['zira', 'hazel', 'susan', 'samantha']):
                        best_female_voice = voice
                        break
                    elif 'female' in voice.name.lower() and not best_female_voice:
                        best_female_voice = voice
                    elif not best_female_voice and len(voices) > 1 and i == 1:  # Often the second voice is female
                        best_female_voice = voice
                
                if best_female_voice:
                    engine.setProperty('voice', best_female_voice.id)
                    print(f"✅ Selected voice: {best_female_voice.name}")
                else:
                    print("⚠️ Using default voice")
            
            # Optimized settings for natural female speech
            engine.setProperty('rate', 160)      # Natural talking speed (words per minute)
            engine.setProperty('volume', 0.95)   # Clear volume
            
            # Convert to wav first (better quality)
            wav_file = self.output_file.replace('.mp3', '.wav')
            engine.save_to_file(self.text, wav_file)
            engine.runAndWait()
            
            if os.path.exists(wav_file):
                print(f"✅ Advanced offline TTS success: {wav_file}")
                print(f"📁 File size: {os.path.getsize(wav_file)} bytes")
                return True
            else:
                return False
                
        except ImportError:
            print("❌ pyttsx3 not installed. Install with: pip install pyttsx3")
            return False
        except Exception as e:
            print(f"❌ Advanced TTS error: {str(e)}")
            return False
    
    def generate_realistic_voice(self):
        """Try all methods to get the most realistic female voice"""
        
        print("🎯 REALISTIC FEMALE VOICE GENERATOR")
        print("=" * 45)
        print(f"Target: Natural, conversational female voice")
        print(f"Speed: Normal talking pace (160-180 WPM)")
        print(f"Quality: High-definition audio")
        print()
        
        # Methods ordered by expected quality
        methods = [
            ("StreamElements Premium Voice", self.convert_with_elevenlabs_demo),
            ("Enhanced Google Neural Voice", self.convert_with_google_wavenet),
            ("RealisticSpeech API", self.convert_with_realistic_speech),
            ("Azure-like Neural Voice", self.convert_with_azure_like_voice),
            ("Advanced Offline Voice", self.convert_with_advanced_pyttsx3)
        ]
        
        for method_name, method_func in methods:
            print(f"🔄 Attempting: {method_name}")
            print("-" * 30)
            
            if method_func():
                print(f"🎉 SUCCESS! Generated realistic female voice using {method_name}")
                print(f"🎵 Audio file: {self.output_file}")
                
                # Show file info
                if os.path.exists(self.output_file):
                    file_size = os.path.getsize(self.output_file)
                    print(f"📊 File size: {file_size:,} bytes")
                    print(f"📊 Estimated duration: ~{len(self.text.split()) / 2.5:.1f} seconds")
                
                print(f"\n🎧 Play the file to hear the realistic female voice!")
                return True
            
            print(f"❌ {method_name} failed, trying next method...\n")
        
        print("❌ All methods failed. Please check your internet connection or install pyttsx3.")
        return False

def main():
    """Main function to generate realistic female voice"""
    
    print("🌟 REALISTIC FEMALE VOICE TEXT-TO-SPEECH")
    print("=" * 50)
    print()
    print("✨ Features:")
    print("   • Natural, human-like female voice")
    print("   • Normal conversational speed")
    print("   • High-quality audio output")
    print("   • Multiple premium TTS engines")
    print()
    
    # Create the TTS converter
    tts = RealisticFemaleTTS(TEXT_TO_SPEAK, OUTPUT_FILE)
    
    # Generate the voice
    success = tts.generate_realistic_voice()
    
    if success:
        print("\n" + "=" * 50)
        print("🎊 VOICE GENERATION COMPLETED SUCCESSFULLY!")
        print("=" * 50)
        print(f"📁 Generated file: {OUTPUT_FILE}")
        print(f"📝 Original text: {TEXT_TO_SPEAK}")
        print()
        print("🎧 Next steps:")
        print("   1. Play the audio file to hear the realistic voice")
        print("   2. Adjust the TEXT_TO_SPEAK variable for different text")
        print("   3. Try different output formats if needed")
    else:
        print("\n" + "=" * 50)
        print("❌ VOICE GENERATION FAILED")
        print("=" * 50)
        print("💡 Troubleshooting:")
        print("   1. Check your internet connection")
        print("   2. Install pyttsx3: pip install pyttsx3")
        print("   3. Try running the script again")

if __name__ == "__main__":
    main()