File size: 2,398 Bytes
e15bf7c
 
 
 
 
ce21c3a
 
e15bf7c
 
 
ce21c3a
e15bf7c
 
 
ce21c3a
 
e15bf7c
 
 
 
 
ce21c3a
e15bf7c
 
 
 
 
ce21c3a
 
 
 
 
e15bf7c
ce21c3a
 
 
e15bf7c
 
 
 
 
 
 
 
 
 
 
 
ce21c3a
 
e15bf7c
 
 
 
 
 
 
 
 
ce21c3a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import streamlit as st
from pydub import AudioSegment
import tempfile
import os

def merge_audio(kirtan_path, tts_path, output_path, fade_start=4000, kirtan_reduction=-15, tts_start_offset=0):
    """Merges kirtan and TTS audio with adjustable fade-in and offset."""
    kirtan = AudioSegment.from_file(kirtan_path)
    tts = AudioSegment.from_file(tts_path)
    
    # Reduce kirtan volume after specified fade_start seconds
    fade_point = min(fade_start, len(kirtan))
    kirtan_fade = kirtan[:fade_point] + kirtan[fade_point:].apply_gain(kirtan_reduction)
    
    # Overlay TTS over faded kirtan with offset adjustment
    combined = kirtan_fade.overlay(tts, position=fade_point + tts_start_offset)
    
    # Export merged file
    combined.export(output_path, format="mp3")

def main():
    st.title("Kirtan + TTS Audio Mixer with Adjustable Timing")
    
    # Upload section
    tts_file = st.file_uploader("Upload TTS Audio (MP3 or WAV)", type=["mp3", "wav"])
    kirtan_option = st.selectbox("Select a Kirtan:", ["Kirtan 1", "Kirtan 2", "Kirtan 3"])
    
    # Slider for user adjustment
    fade_start = st.slider("Kirtan Fade Start (ms)", min_value=1000, max_value=10000, value=4000, step=500)
    tts_start_offset = st.slider("TTS Start Offset (ms)", min_value=0, max_value=5000, value=0, step=100)
    kirtan_reduction = st.slider("Kirtan Volume Reduction (dB)", min_value=-30, max_value=0, value=-15, step=1)
    
    kirtan_files = {
        "Kirtan 1": "kirtan1.mp3",
        "Kirtan 2": "kirtan2.mp3",
        "Kirtan 3": "kirtan3.mp3"
    }
    
    if tts_file and kirtan_option:
        # Save uploaded TTS file
        with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_tts:
            temp_tts.write(tts_file.read())
            temp_tts_path = temp_tts.name
        
        # Set paths
        kirtan_path = kirtan_files[kirtan_option]
        output_path = "merged_audio.mp3"
        
        # Process audio with user-defined parameters
        merge_audio(kirtan_path, temp_tts_path, output_path, fade_start, kirtan_reduction, tts_start_offset)
        
        # Play and download merged file
        st.audio(output_path, format='audio/mp3')
        st.download_button("Download Merged Audio", output_path, file_name="final_audio.mp3")
        
        # Cleanup temp files
        os.remove(temp_tts_path)

if __name__ == "__main__":
    main()