Spaces:
Configuration error
Configuration error
Commit ยท
f8a1b4d
1
Parent(s): 0e896d0
loudness issues
Browse files196 unmodified lines197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229196 unmodified linesgenerated_audio_segment = AudioSegment.from_wav(filename_with_extension)file_paths_for_cleanup.append(filename_with_extension)# VOLUME MATCHING: Use RMS instead of peak normalization# 1. Remove prompt duration from original (no overlap)prompt_duration_ms = int(prompt_duration * 1000)original_minus_prompt = original_audio[:-prompt_duration_ms]# 2. Calculate RMS levels for perceived loudnessoriginal_rms = original_minus_prompt.rmsgenerated_rms = generated_audio_segment.rmsprint(f"๐ Volume analysis:")print(f" Original RMS: {original_rms}")print(f" Generated RMS: {generated_rms}")# 3. Match generated segment to original's RMS levelif generated_rms > 0: # Avoid division by zero# Calculate dB adjustment neededfrom pydub.utils import ratio_to_dbvolume_adjustment = ratio_to_db(original_rms / generated_rms)print(f" Applying {volume_adjustment:.1f}dB to generated audio")# Apply volume adjustmentgenerated_matched = generated_audio_segment + volume_adjustmentelse:generated_matched = generated_audio_segment# 4. Combine seamlesslycombined_audio = original_minus_prompt + generated_matched# Save final result196 unmodified lines197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232196 unmodified linesgenerated_audio_segment = AudioSegment.from_wav(filename_with_extension)file_paths_for_cleanup.append(filename_with_extension)# SMART VOLUME MATCHING: Only match the prompt portion# 1. Remove prompt duration from original (no overlap)prompt_duration_ms = int(prompt_duration * 1000)original_minus_prompt = original_audio[:-prompt_duration_ms]# 2. Extract JUST the prompt portion from generated audio for RMS analysisgenerated_prompt_portion = generated_audio_segment[:prompt_duration_ms]# 3. Calculate RMS of the transition pointsoriginal_rms = original_minus_prompt.rmsprompt_portion_rms = generated_prompt_portion.rmsprint(f"๐ Smart volume analysis:")print(f" Original ending RMS: {original_rms}")print(f" Generated prompt RMS: {prompt_portion_rms}")print(f" Generated full RMS: {generated_audio_segment.rms}")# 4. Match the prompt portion to original levelif prompt_portion_rms > 0:from pydub.utils import ratio_to_dbvolume_adjustment = ratio_to_db(original_rms / prompt_portion_rms)print(f" Applying {volume_adjustment:.1f}dB to entire generated segment")# Apply to entire segment (preserves the buildup)generated_matched = generated_audio_segment + volume_adjustmentelse:generated_matched = generated_audio_segment# 5. Combine seamlesslycombined_audio = original_minus_prompt + generated_matched# Save final result
app.py+16-13