Created
December 30, 2025 20:46
-
-
Save bogged-broker/1e42fe04eeec2a8e073496190c52b449 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Platform Audio Profiles Module | |
| Defines platform-specific audio constraints and optimization profiles for 5M+ view guarantee. | |
| Integrates with audio RL loop, memory manager, and multi-agent virality brain. | |
| """ | |
| import json | |
| from typing import Dict, List, Optional, Tuple, Any | |
| from dataclasses import dataclass, asdict | |
| from enum import Enum | |
| import numpy as np | |
| class Platform(Enum): | |
| """Supported video platforms""" | |
| TIKTOK = "tiktok" | |
| INSTAGRAM_REELS = "instagram_reels" | |
| YOUTUBE_SHORTS = "youtube_shorts" | |
| SNAPCHAT = "snapchat" | |
| FACEBOOK_REELS = "facebook_reels" | |
| class PlaybackDevice(Enum): | |
| """Target playback devices""" | |
| PHONE_SPEAKER = "phone_speaker" | |
| EARBUDS = "earbuds" | |
| HEADPHONES = "headphones" | |
| LAPTOP_SPEAKERS = "laptop_speakers" | |
| MONO_FALLBACK = "mono_fallback" | |
| class ViralityTier(Enum): | |
| """Audio pattern performance tiers""" | |
| HOT = "hot" # Proven viral, use immediately | |
| WARM = "warm" # Moderately successful, occasional reuse | |
| COLD = "cold" # Unproven, experimental, RL exploration | |
| @dataclass | |
| class FrequencyBandProfile: | |
| """Frequency band optimization per platform""" | |
| low_cut_hz: float # High-pass filter cutoff | |
| low_boost_hz: Tuple[float, float] # Bass boost range | |
| mid_presence_hz: Tuple[float, float] # Vocal clarity range | |
| high_shelf_hz: float # Brightness/air frequency | |
| problem_frequencies: List[Tuple[float, float]] # Ranges to attenuate | |
| # Mobile speaker optimization | |
| phone_safe_low_hz: float # Phones can't reproduce below this | |
| earbuds_sweet_spot_hz: Tuple[float, float] # Optimal earbuds range | |
| @dataclass | |
| class CompressionProfile: | |
| """Dynamic range compression settings""" | |
| threshold_db: float | |
| ratio: float | |
| attack_ms: float | |
| release_ms: float | |
| knee_db: float | |
| makeup_gain_db: float | |
| sidechain_filter_hz: Optional[float] # For bass preservation | |
| @dataclass | |
| class LoudnessProfile: | |
| """Platform-specific loudness targets""" | |
| target_lufs: float # Integrated loudness | |
| true_peak_dbtp: float # Maximum true peak | |
| short_term_lufs_range: Tuple[float, float] # Dynamic range bounds | |
| momentary_lufs_max: float # Peak excitement moments | |
| # Streaming normalization targets | |
| platform_normalization_lufs: float # What platform normalizes to | |
| headroom_db: float # Safety margin | |
| @dataclass | |
| class BeatMatchingProfile: | |
| """Trend-aware beat and rhythm recommendations""" | |
| optimal_bpm_range: Tuple[int, int] | |
| viral_bpm_peaks: List[int] # Most viral BPMs for platform | |
| beat_drop_timing_sec: List[float] # When to place drops | |
| loop_point_requirements: List[float] # For seamless looping | |
| rhythm_patterns: List[str] # e.g., "4-on-floor", "trap-hi-hats" | |
| sync_to_trending_audio: bool # Match current viral audio tempo | |
| @dataclass | |
| class SpatialProfile: | |
| """Stereo/mono playback rules""" | |
| primary_format: str # "stereo", "mono", "dual_mono" | |
| mono_compatibility_required: bool | |
| stereo_width_percent: float # 0-200%, 100% = full stereo | |
| center_content_db: float # How much to boost center channel | |
| side_content_limit_db: float # Prevent side info loss in mono | |
| # Device-specific handling | |
| phone_speaker_mode: str # "mono_sum", "left_only", "optimized" | |
| earbuds_enhancement: bool # Apply spatial enhancement for earbuds | |
| @dataclass | |
| class ViralAudioPattern: | |
| """Proven viral audio characteristics""" | |
| pattern_id: str | |
| hook_timing_sec: List[float] # When hooks occur | |
| emotional_peak_timing_sec: List[float] # Emotional crescendos | |
| energy_curve: List[float] # Energy level per second (0-1) | |
| loopability_score: float # 0-1, how well it loops | |
| shareability_score: float # 0-1, predicted share rate | |
| engagement_multiplier: float # Historical engagement boost | |
| # Pattern characteristics | |
| has_earworm_hook: bool | |
| uses_trending_sound: bool | |
| incorporates_silence: bool # Strategic silence for impact | |
| viral_tier: ViralityTier | |
| # Performance tracking | |
| avg_views: float | |
| avg_watch_through_rate: float | |
| avg_replay_rate: float | |
| platforms_successful: List[Platform] | |
| @dataclass | |
| class PlatformAudioProfile: | |
| """Complete audio profile for a platform""" | |
| platform: Platform | |
| # Core audio specs | |
| loudness: LoudnessProfile | |
| compression: CompressionProfile | |
| frequency_bands: FrequencyBandProfile | |
| spatial: SpatialProfile | |
| beat_matching: BeatMatchingProfile | |
| # Platform constraints | |
| max_duration_sec: float | |
| min_duration_sec: float | |
| recommended_duration_sec: float | |
| sample_rate_hz: int | |
| bit_depth: int | |
| codec: str | |
| max_bitrate_kbps: int | |
| # Virality optimization | |
| viral_patterns: List[ViralAudioPattern] | |
| trending_audio_hooks: List[Dict[str, Any]] | |
| niche_customizations: Dict[str, Any] | |
| # Device assumptions | |
| primary_playback_devices: List[PlaybackDevice] | |
| device_distribution: Dict[PlaybackDevice, float] # Percentage | |
| # Engagement predictions | |
| predicted_ctr: float | |
| predicted_watch_through: float | |
| predicted_loop_rate: float | |
| predicted_share_rate: float | |
| # Platform-specific quirks | |
| algorithm_preferences: Dict[str, Any] | |
| compression_artifacts_tolerance: float | |
| requires_sound_on_indicator: bool | |
| class PlatformAudioProfileManager: | |
| """Manages platform-specific audio profiles and optimizations""" | |
| def __init__(self): | |
| self.profiles: Dict[Platform, PlatformAudioProfile] = {} | |
| self._initialize_profiles() | |
| self.memory_manager = None # Injected from audio_memory_manager.py | |
| def _initialize_profiles(self): | |
| """Initialize all platform profiles with 5M+ view optimizations""" | |
| # TikTok Profile - Optimized for viral loops and phone speakers | |
| self.profiles[Platform.TIKTOK] = PlatformAudioProfile( | |
| platform=Platform.TIKTOK, | |
| loudness=LoudnessProfile( | |
| target_lufs=-14.0, # TikTok's sweet spot | |
| true_peak_dbtp=-1.0, | |
| short_term_lufs_range=(-16.0, -10.0), | |
| momentary_lufs_max=-8.0, # Punch for hooks | |
| platform_normalization_lufs=-14.0, | |
| headroom_db=1.0 | |
| ), | |
| compression=CompressionProfile( | |
| threshold_db=-18.0, | |
| ratio=4.0, # Aggressive for phone speakers | |
| attack_ms=5.0, # Fast for transient preservation | |
| release_ms=50.0, | |
| knee_db=6.0, # Smooth compression | |
| makeup_gain_db=3.0, | |
| sidechain_filter_hz=80.0 # Preserve bass punch | |
| ), | |
| frequency_bands=FrequencyBandProfile( | |
| low_cut_hz=30.0, # Remove sub-bass phone can't reproduce | |
| low_boost_hz=(80.0, 150.0), # Punch range for phone speakers | |
| mid_presence_hz=(2000.0, 4000.0), # Vocal intelligibility | |
| high_shelf_hz=8000.0, # Air and clarity | |
| problem_frequencies=[(250.0, 400.0), (3000.0, 3500.0)], # Muddy/harsh | |
| phone_safe_low_hz=50.0, | |
| earbuds_sweet_spot_hz=(200.0, 8000.0) | |
| ), | |
| spatial=SpatialProfile( | |
| primary_format="stereo", | |
| mono_compatibility_required=True, # Critical for phone speakers | |
| stereo_width_percent=120.0, # Wider for earbuds | |
| center_content_db=3.0, # Boost vocals/hooks | |
| side_content_limit_db=-6.0, | |
| phone_speaker_mode="optimized", | |
| earbuds_enhancement=True | |
| ), | |
| beat_matching=BeatMatchingProfile( | |
| optimal_bpm_range=(120, 150), # TikTok viral sweet spot | |
| viral_bpm_peaks=[128, 140, 145], # Trending tempos | |
| beat_drop_timing_sec=[0.5, 3.0, 7.0], # Hook placement | |
| loop_point_requirements=[15.0, 30.0, 60.0], | |
| rhythm_patterns=["trap-hi-hats", "edm-buildup", "drill-pattern"], | |
| sync_to_trending_audio=True | |
| ), | |
| max_duration_sec=180.0, | |
| min_duration_sec=3.0, | |
| recommended_duration_sec=15.0, # 15-sec viral baseline | |
| sample_rate_hz=44100, | |
| bit_depth=16, | |
| codec="AAC", | |
| max_bitrate_kbps=128, | |
| viral_patterns=[ | |
| ViralAudioPattern( | |
| pattern_id="tiktok_hook_3sec", | |
| hook_timing_sec=[0.5, 3.0], | |
| emotional_peak_timing_sec=[7.0], | |
| energy_curve=[0.8, 0.9, 1.0, 0.9, 0.8, 0.9, 1.0, 0.85] * 2, | |
| loopability_score=0.95, | |
| shareability_score=0.88, | |
| engagement_multiplier=2.4, | |
| has_earworm_hook=True, | |
| uses_trending_sound=True, | |
| incorporates_silence=False, | |
| viral_tier=ViralityTier.HOT, | |
| avg_views=8500000.0, | |
| avg_watch_through_rate=0.78, | |
| avg_replay_rate=0.42, | |
| platforms_successful=[Platform.TIKTOK] | |
| ) | |
| ], | |
| trending_audio_hooks=[], | |
| niche_customizations={}, | |
| primary_playback_devices=[ | |
| PlaybackDevice.PHONE_SPEAKER, | |
| PlaybackDevice.EARBUDS | |
| ], | |
| device_distribution={ | |
| PlaybackDevice.PHONE_SPEAKER: 0.60, | |
| PlaybackDevice.EARBUDS: 0.35, | |
| PlaybackDevice.HEADPHONES: 0.05 | |
| }, | |
| predicted_ctr=0.18, | |
| predicted_watch_through=0.75, | |
| predicted_loop_rate=0.38, | |
| predicted_share_rate=0.12, | |
| algorithm_preferences={ | |
| "favors_looping": True, | |
| "penalizes_silence_start": True, | |
| "rewards_immediate_hook": True, | |
| "prefers_trending_audio": True | |
| }, | |
| compression_artifacts_tolerance=0.7, | |
| requires_sound_on_indicator=False | |
| ) | |
| # Instagram Reels Profile - Polished, broadcast-quality optimization | |
| self.profiles[Platform.INSTAGRAM_REELS] = PlatformAudioProfile( | |
| platform=Platform.INSTAGRAM_REELS, | |
| loudness=LoudnessProfile( | |
| target_lufs=-13.0, # Slightly louder than TikTok | |
| true_peak_dbtp=-1.0, | |
| short_term_lufs_range=(-15.0, -9.0), | |
| momentary_lufs_max=-7.0, | |
| platform_normalization_lufs=-14.0, | |
| headroom_db=1.0 | |
| ), | |
| compression=CompressionProfile( | |
| threshold_db=-16.0, | |
| ratio=3.5, # Less aggressive, more dynamic | |
| attack_ms=10.0, | |
| release_ms=80.0, | |
| knee_db=8.0, | |
| makeup_gain_db=2.5, | |
| sidechain_filter_hz=100.0 | |
| ), | |
| frequency_bands=FrequencyBandProfile( | |
| low_cut_hz=35.0, | |
| low_boost_hz=(60.0, 120.0), | |
| mid_presence_hz=(1800.0, 5000.0), | |
| high_shelf_hz=10000.0, # More air for polished sound | |
| problem_frequencies=[(200.0, 350.0)], | |
| phone_safe_low_hz=45.0, | |
| earbuds_sweet_spot_hz=(150.0, 10000.0) | |
| ), | |
| spatial=SpatialProfile( | |
| primary_format="stereo", | |
| mono_compatibility_required=True, | |
| stereo_width_percent=140.0, # Wider stereo for polish | |
| center_content_db=2.0, | |
| side_content_limit_db=-4.0, | |
| phone_speaker_mode="optimized", | |
| earbuds_enhancement=True | |
| ), | |
| beat_matching=BeatMatchingProfile( | |
| optimal_bpm_range=(110, 140), | |
| viral_bpm_peaks=[120, 128, 135], | |
| beat_drop_timing_sec=[1.0, 5.0, 10.0], | |
| loop_point_requirements=[15.0, 30.0, 60.0, 90.0], | |
| rhythm_patterns=["pop-structure", "edm-buildup", "hip-hop-bounce"], | |
| sync_to_trending_audio=True | |
| ), | |
| max_duration_sec=90.0, | |
| min_duration_sec=3.0, | |
| recommended_duration_sec=20.0, | |
| sample_rate_hz=48000, | |
| bit_depth=16, | |
| codec="AAC", | |
| max_bitrate_kbps=192, | |
| viral_patterns=[ | |
| ViralAudioPattern( | |
| pattern_id="reels_aesthetic_hook", | |
| hook_timing_sec=[1.0, 5.0], | |
| emotional_peak_timing_sec=[10.0, 18.0], | |
| energy_curve=[0.7] * 5 + [0.9] * 5 + [1.0] * 5 + [0.8] * 5, | |
| loopability_score=0.85, | |
| shareability_score=0.82, | |
| engagement_multiplier=2.1, | |
| has_earworm_hook=True, | |
| uses_trending_sound=True, | |
| incorporates_silence=True, | |
| viral_tier=ViralityTier.HOT, | |
| avg_views=6200000.0, | |
| avg_watch_through_rate=0.72, | |
| avg_replay_rate=0.35, | |
| platforms_successful=[Platform.INSTAGRAM_REELS] | |
| ) | |
| ], | |
| trending_audio_hooks=[], | |
| niche_customizations={}, | |
| primary_playback_devices=[ | |
| PlaybackDevice.PHONE_SPEAKER, | |
| PlaybackDevice.EARBUDS | |
| ], | |
| device_distribution={ | |
| PlaybackDevice.PHONE_SPEAKER: 0.55, | |
| PlaybackDevice.EARBUDS: 0.40, | |
| PlaybackDevice.HEADPHONES: 0.05 | |
| }, | |
| predicted_ctr=0.16, | |
| predicted_watch_through=0.70, | |
| predicted_loop_rate=0.32, | |
| predicted_share_rate=0.15, | |
| algorithm_preferences={ | |
| "favors_looping": True, | |
| "penalizes_silence_start": False, | |
| "rewards_immediate_hook": True, | |
| "prefers_trending_audio": True, | |
| "aesthetic_audio_bonus": True | |
| }, | |
| compression_artifacts_tolerance=0.6, | |
| requires_sound_on_indicator=False | |
| ) | |
| # YouTube Shorts Profile - Watch-through optimized | |
| self.profiles[Platform.YOUTUBE_SHORTS] = PlatformAudioProfile( | |
| platform=Platform.YOUTUBE_SHORTS, | |
| loudness=LoudnessProfile( | |
| target_lufs=-14.0, | |
| true_peak_dbtp=-1.0, | |
| short_term_lufs_range=(-16.0, -11.0), | |
| momentary_lufs_max=-9.0, | |
| platform_normalization_lufs=-14.0, | |
| headroom_db=1.0 | |
| ), | |
| compression=CompressionProfile( | |
| threshold_db=-20.0, | |
| ratio=3.0, # Gentle, broadcast-style | |
| attack_ms=15.0, | |
| release_ms=100.0, | |
| knee_db=10.0, | |
| makeup_gain_db=2.0, | |
| sidechain_filter_hz=120.0 | |
| ), | |
| frequency_bands=FrequencyBandProfile( | |
| low_cut_hz=40.0, | |
| low_boost_hz=(70.0, 130.0), | |
| mid_presence_hz=(2000.0, 5000.0), | |
| high_shelf_hz=9000.0, | |
| problem_frequencies=[(300.0, 450.0)], | |
| phone_safe_low_hz=50.0, | |
| earbuds_sweet_spot_hz=(180.0, 12000.0) | |
| ), | |
| spatial=SpatialProfile( | |
| primary_format="stereo", | |
| mono_compatibility_required=True, | |
| stereo_width_percent=130.0, | |
| center_content_db=2.5, | |
| side_content_limit_db=-5.0, | |
| phone_speaker_mode="optimized", | |
| earbuds_enhancement=True | |
| ), | |
| beat_matching=BeatMatchingProfile( | |
| optimal_bpm_range=(100, 145), | |
| viral_bpm_peaks=[115, 128, 140], | |
| beat_drop_timing_sec=[2.0, 10.0, 20.0], | |
| loop_point_requirements=[30.0, 60.0], | |
| rhythm_patterns=["storytelling-pace", "buildup-payoff", "sustained-energy"], | |
| sync_to_trending_audio=False # Less trend-dependent | |
| ), | |
| max_duration_sec=60.0, | |
| min_duration_sec=15.0, | |
| recommended_duration_sec=30.0, | |
| sample_rate_hz=48000, | |
| bit_depth=16, | |
| codec="AAC", | |
| max_bitrate_kbps=256, | |
| viral_patterns=[ | |
| ViralAudioPattern( | |
| pattern_id="shorts_watchthrough_30sec", | |
| hook_timing_sec=[2.0, 15.0], | |
| emotional_peak_timing_sec=[25.0], | |
| energy_curve=[0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1.0, 0.9, 0.85, 0.8] * 3, | |
| loopability_score=0.70, | |
| shareability_score=0.75, | |
| engagement_multiplier=1.9, | |
| has_earworm_hook=False, | |
| uses_trending_sound=False, | |
| incorporates_silence=True, | |
| viral_tier=ViralityTier.WARM, | |
| avg_views=5800000.0, | |
| avg_watch_through_rate=0.68, | |
| avg_replay_rate=0.28, | |
| platforms_successful=[Platform.YOUTUBE_SHORTS] | |
| ) | |
| ], | |
| trending_audio_hooks=[], | |
| niche_customizations={}, | |
| primary_playback_devices=[ | |
| PlaybackDevice.PHONE_SPEAKER, | |
| PlaybackDevice.EARBUDS, | |
| PlaybackDevice.LAPTOP_SPEAKERS | |
| ], | |
| device_distribution={ | |
| PlaybackDevice.PHONE_SPEAKER: 0.50, | |
| PlaybackDevice.EARBUDS: 0.35, | |
| PlaybackDevice.LAPTOP_SPEAKERS: 0.10, | |
| PlaybackDevice.HEADPHONES: 0.05 | |
| }, | |
| predicted_ctr=0.14, | |
| predicted_watch_through=0.65, | |
| predicted_loop_rate=0.25, | |
| predicted_share_rate=0.10, | |
| algorithm_preferences={ | |
| "favors_looping": False, | |
| "penalizes_silence_start": False, | |
| "rewards_immediate_hook": True, | |
| "prefers_trending_audio": False, | |
| "watch_time_priority": True | |
| }, | |
| compression_artifacts_tolerance=0.5, | |
| requires_sound_on_indicator=False | |
| ) | |
| def get_platform_profile(self, platform: Platform) -> PlatformAudioProfile: | |
| """ | |
| Get complete audio profile for specified platform. | |
| Args: | |
| platform: Target platform enum | |
| Returns: | |
| PlatformAudioProfile with all optimization parameters | |
| """ | |
| if platform not in self.profiles: | |
| raise ValueError(f"Unsupported platform: {platform}") | |
| return self.profiles[platform] | |
| def get_optimal_lufs(self, platform: Platform) -> float: | |
| """Get target LUFS for platform""" | |
| return self.profiles[platform].loudness.target_lufs | |
| def get_compression_settings(self, platform: Platform) -> CompressionProfile: | |
| """Get compression profile for platform""" | |
| return self.profiles[platform].compression | |
| def get_frequency_profile(self, platform: Platform) -> FrequencyBandProfile: | |
| """Get frequency optimization profile""" | |
| return self.profiles[platform].frequency_bands | |
| def get_spatial_rules(self, platform: Platform) -> SpatialProfile: | |
| """Get mono/stereo playback rules""" | |
| return self.profiles[platform].spatial | |
| def get_beat_matching_recommendations(self, platform: Platform) -> BeatMatchingProfile: | |
| """Get trend-aware beat matching recommendations""" | |
| return self.profiles[platform].beat_matching | |
| def simulate_playback( | |
| self, | |
| audio_data: np.ndarray, | |
| platform: Platform, | |
| device: PlaybackDevice | |
| ) -> Tuple[np.ndarray, Dict[str, Any]]: | |
| """ | |
| Simulate audio playback on specific device for platform. | |
| Args: | |
| audio_data: Input audio array | |
| platform: Target platform | |
| device: Playback device to simulate | |
| Returns: | |
| Tuple of (simulated_audio, diagnostic_report) | |
| """ | |
| profile = self.profiles[platform] | |
| freq_profile = profile.frequency_bands | |
| simulated_audio = audio_data.copy() | |
| warnings = [] | |
| # Device-specific simulation | |
| if device == PlaybackDevice.PHONE_SPEAKER: | |
| # Apply phone speaker frequency response | |
| # High-pass filter to remove inaudible low frequencies | |
| simulated_audio = self._apply_highpass_filter( | |
| simulated_audio, | |
| freq_profile.phone_safe_low_hz | |
| ) | |
| # Simulate mono summing (most phone speakers are mono) | |
| if simulated_audio.ndim > 1: | |
| simulated_audio = np.mean(simulated_audio, axis=0) | |
| warnings.append("Phone speaker: Stereo collapsed to mono") | |
| # Simulate limited dynamic range | |
| simulated_audio = self._apply_phone_speaker_compression(simulated_audio) | |
| elif device == PlaybackDevice.EARBUDS: | |
| # Earbuds frequency response simulation | |
| simulated_audio = self._apply_earbuds_response( | |
| simulated_audio, | |
| freq_profile.earbuds_sweet_spot_hz | |
| ) | |
| elif device == PlaybackDevice.MONO_FALLBACK: | |
| # Worst-case mono compatibility check | |
| if simulated_audio.ndim > 1: | |
| simulated_audio = np.mean(simulated_audio, axis=0) | |
| warnings.append("Mono fallback: All stereo information lost") | |
| # Analyze intelligibility | |
| intelligibility_score = self._analyze_intelligibility(simulated_audio) | |
| if intelligibility_score < 0.7: | |
| warnings.append(f"Low intelligibility: {intelligibility_score:.2f}") | |
| # Analyze clarity | |
| clarity_score = self._analyze_clarity(simulated_audio) | |
| if clarity_score < 0.65: | |
| warnings.append(f"Clarity issues detected: {clarity_score:.2f}") | |
| diagnostic_report = { | |
| "device": device.value, | |
| "platform": platform.value, | |
| "intelligibility_score": intelligibility_score, | |
| "clarity_score": clarity_score, | |
| "warnings": warnings, | |
| "peak_level_db": 20 * np.log10(np.abs(simulated_audio).max() + 1e-10), | |
| "rms_level_db": 20 * np.log10(np.sqrt(np.mean(simulated_audio**2)) + 1e-10) | |
| } | |
| return simulated_audio, diagnostic_report | |
| def normalize_loudness( | |
| self, | |
| audio_data: np.ndarray, | |
| platform: Platform, | |
| preserve_dynamics: bool = True | |
| ) -> Tuple[np.ndarray, Dict[str, float]]: | |
| """ | |
| Normalize audio to platform target loudness. | |
| Args: | |
| audio_data: Input audio array | |
| platform: Target platform | |
| preserve_dynamics: Whether to preserve dynamic range | |
| Returns: | |
| Tuple of (normalized_audio, loudness_metrics) | |
| """ | |
| profile = self.profiles[platform] | |
| target_lufs = profile.loudness.target_lufs | |
| # Measure current loudness (simplified) | |
| current_rms = np.sqrt(np.mean(audio_data**2)) | |
| current_lufs = -23.0 + 20 * np.log10(current_rms + 1e-10) | |
| # Calculate gain adjustment | |
| gain_db = target_lufs - current_lufs | |
| gain_linear = 10 ** (gain_db / 20.0) | |
| normalized_audio = audio_data * gain_linear | |
| # Apply peak limiting to prevent clipping | |
| peak_limit = 10 ** (profile.loudness.true_peak_dbtp / 20.0) | |
| peak = np.abs(normalized_audio).max() | |
| if peak > peak_limit: | |
| normalized_audio *= (peak_limit / peak) | |
| # Apply compression if preserving dynamics | |
| if preserve_dynamics: | |
| normalized_audio = self._apply_dynamic_compression( | |
| normalized_audio, | |
| profile.compression | |
| ) | |
| metrics = { | |
| "input_lufs": current_lufs, | |
| "output_lufs": target_lufs, | |
| "gain_applied_db": gain_db, | |
| "peak_limited": peak > peak_limit | |
| } | |
| return normalized_audio, metrics | |
| def generate_candidate_variants( | |
| self, | |
| base_audio: np.ndarray, | |
| platform: Platform, | |
| num_variants: int = 5 | |
| ) -> List[Dict[str, Any]]: | |
| """ | |
| Generate multiple audio variants optimized for virality. | |
| Args: | |
| base_audio: Base audio to create variants from | |
| platform: Target platform | |
| num_variants: Number of variants to generate | |
| Returns: | |
| List of variant dictionaries with audio and predicted scores | |
| """ | |
| profile = self.profiles[platform] | |
| variants = [] | |
| for i in range(num_variants): | |
| variant_audio = base_audio.copy() | |
| # Apply different optimization strategies | |
| if i == 0: | |
| # Aggressive compression for maximum loudness | |
| variant_audio = self._apply_aggressive_compression(variant_audio) | |
| strategy = "max_loudness" | |
| elif i == 1: | |
| # Dynamic preservation for emotional impact | |
| variant_audio = self._preserve_dynamics(variant_audio) | |
| strategy = "dynamic_emotional" | |
| elif i == 2: | |
| # Beat-aligned enhancement | |
| variant_audio = self._enhance_beat_alignment(variant_audio, profile) | |
| strategy = "beat_optimized" | |
| elif i == 3: | |
| # Frequency sculpting for phone speakers | |
| variant_audio = self._optimize_for_phone_speakers(variant_audio, profile) | |
| strategy = "phone_optimized" | |
| else: | |
| # Experimental RL-guided variation | |
| variant_audio = self._apply_rl_exploration(variant_audio) | |
| strategy = "rl_experimental" | |
| # Predict engagement scores | |
| engagement_score = self._predict_engagement(variant_audio, profile) | |
| variants.append({ | |
| "variant_id": f"{platform.value}_v{i}_{strategy}", | |
| "audio": variant_audio, | |
| "strategy": strategy, | |
| "predicted_engagement": engagement_score, | |
| "predicted_ctr": engagement_score * profile.predicted_ctr, | |
| "predicted_watch_through": engagement_score * profile.predicted_watch_through, | |
| "loopability_score": self._calculate_loopability(variant_audio), | |
| "virality_tier": self._assign_virality_tier(engagement_score) | |
| }) | |
| # Sort by predicted engagement | |
| variants.sort(key=lambda x: x["predicted_engagement"], reverse=True) | |
| return variants | |
| def update_profile_with_feedback( | |
| self, | |
| platform: Platform, | |
| variant_id: str, | |
| engagement_metrics: Dict[str, float] | |
| ): | |
| """ | |
| Update platform profile based on real engagement feedback. | |
| Args: | |
| platform: Platform that was tested | |
| variant_id: ID of audio variant | |
| engagement_metrics: Actual engagement data from video | |
| """ | |
| profile = self.profiles[platform] | |
| # Update predicted metrics with weighted average | |
| alpha = 0.1 # Learning rate | |
| if "ctr" in engagement_metrics: | |
| profile.predicted_ctr = ( | |
| (1 - alpha) * profile.predicted_ctr + | |
| alpha * engagement_metrics["ctr"] | |
| ) | |
| if "watch_through_rate" in engagement_metrics: | |
| profile.predicted_watch_through = ( | |
| (1 - alpha) * profile.predicted_watch_through + | |
| alpha * engagement_metrics["watch_through_rate"] | |
| ) | |
| if "loop_rate" in engagement_metrics: | |
| profile.predicted_loop_rate = ( | |
| (1 - alpha) * profile.predicted_loop_rate + | |
| alpha * engagement_metrics["loop_rate"] | |
| ) | |
| # Store successful patterns for memory manager | |
| if self.memory_manager and engagement_metrics.get("views", 0) > 1000000: | |
| self.memory_manager.store_viral_pattern( | |
| platform=platform.value, | |
| variant_id=variant_id, | |
| metrics=engagement_metrics | |
| ) | |
| def get_cross_platform_optimization( | |
| self, | |
| audio_data: np.ndarray, | |
| platforms: List[Platform] | |
| ) -> Dict[Platform, np.ndarray]: | |
| """ | |
| Generate platform-specific optimized versions from single audio |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment