import numpy as np
import matplotlib.pyplot as plt
import librosa
from IPython.display import Audio

def create_speech_dataset():
    """Generate synthetic vowel sounds for testing"""
    sr = 8000
    t = np.linspace(0, 1, sr)
    
    y_aa = 0.5*np.sin(2*np.pi*120*t) + 0.2*np.sin(2*np.pi*800*t) + 0.1*np.sin(2*np.pi*2400*t)
    y_aa = y_aa / np.max(np.abs(y_aa))
    
    y_ee = 0.5*np.sin(2*np.pi*270*t) + 0.3*np.sin(2*np.pi*2290*t) + 0.1*np.sin(2*np.pi*3010*t)
    y_ee = y_ee / np.max(np.abs(y_ee))
    
    y_oo = 0.5*np.sin(2*np.pi*120*t) + 0.2*np.sin(2*np.pi*800*t) + 0.1*np.sin(2*np.pi*2460*t)
    y_oo = y_oo / np.max(np.abs(y_oo))
    
    return {'aa': y_aa, 'ee': y_ee, 'oo': y_oo}, sr

def analyze_audio_file(filename, target_sr=16000):
    """Quick analysis of audio file"""
    from .core import Evolvo
    
    evo = Evolvo()
    y, sr = evo.load_audio(filename, target_sr)
    
    print("=== Quick Audio Analysis ===")
    print(f"Duration: {len(y)/sr:.2f} seconds")
    print(f"Sampling Rate: {sr} Hz")
    print(f"Max Amplitude: {np.max(np.abs(y)):.4f}")
    print(f"RMS Energy: {np.sqrt(np.mean(y**2)):.4f}")
    
    return evo

def save_analysis_report(filename, output_file="analysis_report.py"):
    """Generate and save complete analysis code"""
    from .code_generator import CodeGenerator
    
    generator = CodeGenerator()
    code = generator.generate_analysis_code(filename)
    
    with open(output_file, 'w') as f:
        f.write(f'# Audio Analysis Report for {filename}\n')
        f.write('# Generated by Evolvo Library\n\n')
        f.write(code)
    
    print(f"Analysis report saved to: {output_file}")
    return code