Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -54,6 +54,15 @@ ai = GoogleGenAI({
|
|
| 54 |
model = 'lyria-realtime-exp'
|
| 55 |
sample_rate = 48000
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
@app.route('/generate', methods=['POST'])
|
| 58 |
async def generate_music():
|
| 59 |
try:
|
|
@@ -90,16 +99,16 @@ async def generate_music():
|
|
| 90 |
slowed_factor = config.get('slowed_factor', 1.0)
|
| 91 |
chunk_duration = 2 * slowed_factor
|
| 92 |
samples_per_chunk = int(sample_rate * chunk_duration * 2) # Stereo, 16-bit
|
| 93 |
-
|
| 94 |
while total_duration < target_duration:
|
| 95 |
-
#
|
| 96 |
total_weight = sum(p['weight'] for p in weighted_prompts)
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
chunk = (amplitude * 32767 * np.sin(2 * np.pi *
|
| 103 |
encoded_chunk = base64.b64encode(chunk.tobytes()).decode('utf-8')
|
| 104 |
decoded_chunk = decode(encoded_chunk)
|
| 105 |
collected_chunks.append(decoded_chunk)
|
|
|
|
| 54 |
model = 'lyria-realtime-exp'
|
| 55 |
sample_rate = 48000
|
| 56 |
|
| 57 |
+
# Genre-specific parameters
|
| 58 |
+
GENRE_PARAMS = {
|
| 59 |
+
"Synthwave": {"base_freq": 220, "mod_freq": 2, "amplitude": 0.7}, # Lower freq, pulsing
|
| 60 |
+
"Dreamwave": {"base_freq": 110, "mod_freq": 0.5, "amplitude": 0.5}, # Softer, slower
|
| 61 |
+
"Chillsynth": {"base_freq": 165, "mod_freq": 1, "amplitude": 0.6}, # Mid-range, smooth
|
| 62 |
+
"Lovewave": {"base_freq": 130, "mod_freq": 0.8, "amplitude": 0.4}, # Warm, gentle
|
| 63 |
+
"slowed": {"base_freq": 55, "mod_freq": 0.2, "amplitude": 0.3} # Very low, slow
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
@app.route('/generate', methods=['POST'])
|
| 67 |
async def generate_music():
|
| 68 |
try:
|
|
|
|
| 99 |
slowed_factor = config.get('slowed_factor', 1.0)
|
| 100 |
chunk_duration = 2 * slowed_factor
|
| 101 |
samples_per_chunk = int(sample_rate * chunk_duration * 2) # Stereo, 16-bit
|
| 102 |
+
t = np.linspace(0, chunk_duration, samples_per_chunk, False)
|
| 103 |
while total_duration < target_duration:
|
| 104 |
+
# Weighted average of genre parameters
|
| 105 |
total_weight = sum(p['weight'] for p in weighted_prompts)
|
| 106 |
+
base_freq = sum(p['weight'] * GENRE_PARAMS.get(p['text'], GENRE_PARAMS["Synthwave"])['base_freq'] for p in weighted_prompts) / total_weight
|
| 107 |
+
mod_freq = sum(p['weight'] * GENRE_PARAMS.get(p['text'], GENRE_PARAMS["Synthwave"])['mod_freq'] for p in weighted_prompts) / total_weight
|
| 108 |
+
amplitude = sum(p['weight'] * GENRE_PARAMS.get(p['text'], GENRE_PARAMS["Synthwave"])['amplitude'] for p in weighted_prompts) / total_weight
|
| 109 |
+
amplitude *= 0.5 if slowed_factor < 1 else 1.0 # Reduce amplitude for slowed effect
|
| 110 |
+
# Generate modulated sine wave
|
| 111 |
+
chunk = (amplitude * 32767 * np.sin(2 * np.pi * (base_freq + mod_freq * np.sin(2 * np.pi * 0.1 * t)) * t / sample_rate)).astype(np.int16)
|
| 112 |
encoded_chunk = base64.b64encode(chunk.tobytes()).decode('utf-8')
|
| 113 |
decoded_chunk = decode(encoded_chunk)
|
| 114 |
collected_chunks.append(decoded_chunk)
|