ageraustine commited on
Commit
01fc4a7
1 Parent(s): 722acb9
Files changed (1) hide show
  1. utils.py +106 -0
utils.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import numpy as np
4
+ import os
5
+ import base64
6
+ from streamlit import session_state as st_state
7
+ import librosa
8
+ import soundfile as sf
9
+ from scipy.signal import butter, sosfilt
10
+
11
+
12
+ def get_api_url_and_bearer_token():
13
+ """
14
+ Tries to get API_URL and BEARER_TOKEN from environment variables.
15
+ If not found, sets them to default values and throws an error.
16
+ """
17
+ try:
18
+ API_URL = os.environ["API_URL"]
19
+ except KeyError:
20
+ st.error("API_URL environment variable is not set.")
21
+ st.stop()
22
+
23
+ try:
24
+ BEARER_TOKEN = os.environ["BEARER_TOKEN"]
25
+ except KeyError:
26
+ st.error("BEARER_TOKEN environment variable is not set.")
27
+ st.stop()
28
+
29
+ return API_URL, BEARER_TOKEN
30
+
31
+
32
+ def initialize_session_state():
33
+ """
34
+ Initializes session state variables for audio data and user inputs.
35
+ """
36
+ if 'audio' not in st_state:
37
+ st_state.audio = None
38
+ if 'augmented_audio' not in st_state:
39
+ st_state.augmented_audio = None
40
+ if 'vocal_audio' not in st_state:
41
+ st_state.vocal_audio = None
42
+
43
+
44
+ def create_headers(bearer_token):
45
+ """
46
+ Creates headers for API requests with Bearer token authorization.
47
+ """
48
+ return {
49
+ "Authorization": f"Bearer {bearer_token}",
50
+ "Content-Type": "application/json"
51
+ }
52
+
53
+
54
+ def upload_and_get_file_bytes():
55
+ """
56
+ Uploads a music file and returns its bytes if uploaded, otherwise None.
57
+ """
58
+ uploaded_file = st.file_uploader("Upload Music File", type=["mp3", "wav", "ogg", "flac", "aac"])
59
+ if uploaded_file:
60
+ return uploaded_file.read()
61
+ else:
62
+ return None
63
+
64
+
65
+ def generate_audio(api_url, headers, prompt, duration, audio_bytes=None):
66
+ """
67
+ Generates audio based on user prompt, duration and optional uploaded audio.
68
+ """
69
+ payload = {"inputs": {"prompt": prompt, "duration": duration}}
70
+ if audio_bytes:
71
+ audio_base64 = base64.b64encode(audio_bytes).decode('utf-8')
72
+ payload["inputs"]["track"] = audio_base64
73
+ st.text("Generating audio...")
74
+ response = requests.post(api_url, headers=headers, json=payload)
75
+ generated_audio = np.array(response.json()[0]['generated_audio'], dtype=np.float32)
76
+ sample_rate = response.json()[0]['sample_rate']
77
+ st.audio(generated_audio, format="audio/wav", sample_rate=sample_rate, start_time=0)
78
+ return generated_audio, sample_rate
79
+
80
+
81
+ def mix_vocals(audio, vocal_audio, sample_rate):
82
+ """
83
+ Mixes uploaded vocal audio with the generated audio.
84
+ """
85
+ vocal_audio, _ = librosa.load(vocal_audio, sr=sample_rate, mono=False)
86
+ vocal_audio = librosa.util.fix_length(vocal_audio, len(audio))
87
+ return (audio + vocal_audio) / 2
88
+
89
+
90
+ def apply_volume_balance(audio, balance):
91
+ """
92
+ Applies volume balance to the audio.
93
+ """
94
+ return audio * 10 ** (balance / 20)
95
+
96
+
97
+ def apply_compression(audio, threshold, ratio, knee, max_gain):
98
+ """
99
+ Applies simple soft-knee compression to the audio.
100
+ """
101
+ def compress(x):
102
+ over = np.maximum(x - threshold, 0)
103
+ gain = over / (over + knee) * (1 - (1 / ratio)) + 1
104
+ gain = np.maximum(gain, 1 - max_gain)
105
+ return x * gain
106
+ return compress(audio)