sam12345324 commited on
Commit
2a35cbb
·
verified ·
1 Parent(s): 9b1cdf1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -2
app.py CHANGED
@@ -5,7 +5,7 @@ import base64
5
  import numpy as np
6
  from flask import Flask, request, Response, jsonify, stream_with_context
7
  from io import BytesIO
8
- from utils import decode, pcm_to_wav
9
  from datetime import datetime
10
 
11
  app = Flask(__name__)
@@ -61,7 +61,7 @@ bits_per_sample = 16
61
 
62
  # Genre-specific parameters
63
  GENRE_PARAMS = {
64
- "Synthwave": {"base_freq": 220, "mod_freq": 2, "amplitude": 0.7},
65
  "Dreamwave": {"base_freq": 110, "mod_freq": 0.5, "amplitude": 0.5},
66
  "Chillsynth": {"base_freq": 165, "mod_freq": 1, "amplitude": 0.6},
67
  "Lovewave": {"base_freq": 130, "mod_freq": 0.8, "amplitude": 0.4},
@@ -90,6 +90,14 @@ def generate_audio_chunk(prompts, config, total_duration):
90
  chunk = np.clip(chunk * 32768, -32768, 32767).astype(np.int16) # Convert to 16-bit
91
  return chunk.tobytes()
92
 
 
 
 
 
 
 
 
 
93
  @app.route('/generate', methods=['POST'])
94
  def generate_music():
95
  try:
@@ -151,5 +159,57 @@ def generate_music():
151
  except Exception as e:
152
  return jsonify({'error': str(e)}), 500
153
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  if __name__ == '__main__':
155
  app.run(host='0.0.0.0', port=7860)
 
5
  import numpy as np
6
  from flask import Flask, request, Response, jsonify, stream_with_context
7
  from io import BytesIO
8
+ import wave
9
  from datetime import datetime
10
 
11
  app = Flask(__name__)
 
61
 
62
  # Genre-specific parameters
63
  GENRE_PARAMS = {
64
+ "Synthwave": {"base_freq": 220, "mod_freq": 2, "amplitude": telescopes},
65
  "Dreamwave": {"base_freq": 110, "mod_freq": 0.5, "amplitude": 0.5},
66
  "Chillsynth": {"base_freq": 165, "mod_freq": 1, "amplitude": 0.6},
67
  "Lovewave": {"base_freq": 130, "mod_freq": 0.8, "amplitude": 0.4},
 
90
  chunk = np.clip(chunk * 32768, -32768, 32767).astype(np.int16) # Convert to 16-bit
91
  return chunk.tobytes()
92
 
93
+ def pcm_to_wav(pcm_data, filename, sample_rate=48000, channels=2, bits_per_sample=16):
94
+ """Convert PCM data to WAV file."""
95
+ with wave.open(filename, 'wb') as wav_file:
96
+ wav_file.setnchannels(channels)
97
+ wav_file.setsampwidth(bits_per_sample // 8)
98
+ wav_file.setframerate(sample_rate)
99
+ wav_file.writeframes(pcm_data)
100
+
101
  @app.route('/generate', methods=['POST'])
102
  def generate_music():
103
  try:
 
159
  except Exception as e:
160
  return jsonify({'error': str(e)}), 500
161
 
162
+ @app.route('/generate_file', methods=['POST'])
163
+ def generate_music_file():
164
+ try:
165
+ data = request.get_json()
166
+ if not data:
167
+ return jsonify({'error': 'No JSON data provided'}), 400
168
+
169
+ prompts = data.get('prompts', [])
170
+ config = data.get('config', {
171
+ 'temperature': 1.1,
172
+ 'topK': 40,
173
+ 'guidance': 4.0,
174
+ 'slowed_factor': 1.0
175
+ })
176
+
177
+ if not prompts:
178
+ return jsonify({'error': 'At least one prompt is required'}), 400
179
+
180
+ weighted_prompts = [
181
+ {
182
+ 'promptId': f"prompt-{i}",
183
+ 'text': prompt['text'],
184
+ 'weight': prompt.get('weight', 1.0),
185
+ 'color': prompt.get('color', '#9900ff')
186
+ } for i, prompt in enumerate(prompts)
187
+ ]
188
+
189
+ # Collect all audio chunks
190
+ total_duration = 0
191
+ target_duration = 60 # 1 minute
192
+ audio_chunks = []
193
+ session = MockLiveMusicSession(model)
194
+ session.is_playing = True
195
+
196
+ while total_duration < target_duration and session.is_playing:
197
+ chunk_data = generate_audio_chunk(weighted_prompts, config, total_duration)
198
+ audio_chunks.append(chunk_data)
199
+ total_duration += 5 * config.get('slowed_factor', 1.0)
200
+
201
+ session.close()
202
+
203
+ # Combine chunks and save to WAV
204
+ pcm_data = b''.join(audio_chunks)
205
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
206
+ output_filename = f"generated_music_{timestamp}.wav"
207
+ pcm_to_wav(pcm_data, output_filename, sample_rate, channels, bits_per_sample)
208
+
209
+ return jsonify({'message': f'Audio file saved as {output_filename}'})
210
+
211
+ except Exception as e:
212
+ return jsonify({'error': str(e)}), 500
213
+
214
  if __name__ == '__main__':
215
  app.run(host='0.0.0.0', port=7860)