musdfakoc commited on
Commit
86448a8
·
verified ·
1 Parent(s): 4970be7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -144,13 +144,14 @@ def magnitude_to_complex_spectrogram(magnitude_spectrogram):
144
 
145
 
146
  def spectrogram_to_audio(magnitude_spectrogram):
147
- # Perform inverse log scaling to undo any log scaling applied to the spectrogram
148
  magnitude_spectrogram = torch.expm1(magnitude_spectrogram)
149
 
150
- # Convert magnitude-only spectrogram to complex format
151
- complex_spectrogram = magnitude_to_complex_spectrogram(magnitude_spectrogram)
 
152
 
153
- # Use inverse STFT to convert the spectrogram back to time-domain audio
154
  audio = torch.istft(complex_spectrogram, n_fft=n_fft, hop_length=hop_length)
155
 
156
  # Handle NaNs or Infs in the audio and replace them with zeros
@@ -160,16 +161,16 @@ def spectrogram_to_audio(magnitude_spectrogram):
160
  if torch.max(torch.abs(audio)) != 0:
161
  audio = audio / torch.max(torch.abs(audio))
162
 
163
- # Clip the audio to the range [-1, 1] to avoid out-of-bounds values
164
  audio = torch.clamp(audio, min=-1, max=1)
165
 
166
- # Scale the audio to 16-bit PCM format and convert to int16
167
  audio = (audio * 32767).short()
168
 
169
- # Ensure the audio is clipped to the valid range for int16 [-32768, 32767]
170
  audio = torch.clamp(audio, min=-32768, max=32767)
171
 
172
- # Convert to a NumPy array and ensure it's in the correct format
173
  audio_numpy = audio.cpu().numpy().astype(np.int16)
174
 
175
  return audio_numpy
@@ -178,21 +179,20 @@ def spectrogram_to_audio(magnitude_spectrogram):
178
 
179
 
180
  def generate_audio_from_image(image):
181
- test_img = image_transform(image).unsqueeze(0).to(device) # Preprocess the input image
182
 
183
- # Generate a sound spectrogram from the image using the pre-trained GAN model
184
  with torch.no_grad():
185
  generated_spectrogram = generator(test_img)
186
 
187
  # Convert the generated spectrogram to time-domain audio
188
  generated_audio_numpy = spectrogram_to_audio(generated_spectrogram.squeeze(0).cpu())
189
 
190
- # Return the sample rate and the NumPy array containing the audio data
191
  return (sample_rate, generated_audio_numpy)
192
 
193
 
194
 
195
-
196
  # Gradio Interface
197
  def main():
198
  global generator # Declare the generator object globally
 
144
 
145
 
146
  def spectrogram_to_audio(magnitude_spectrogram):
147
+ # Perform inverse log scaling to undo any log scaling
148
  magnitude_spectrogram = torch.expm1(magnitude_spectrogram)
149
 
150
+ # Convert magnitude-only spectrogram to complex format (real part and zero imaginary)
151
+ zero_phase = torch.zeros_like(magnitude_spectrogram)
152
+ complex_spectrogram = torch.stack([magnitude_spectrogram, zero_phase], dim=-1)
153
 
154
+ # Inverse STFT to convert the spectrogram back to time-domain audio
155
  audio = torch.istft(complex_spectrogram, n_fft=n_fft, hop_length=hop_length)
156
 
157
  # Handle NaNs or Infs in the audio and replace them with zeros
 
161
  if torch.max(torch.abs(audio)) != 0:
162
  audio = audio / torch.max(torch.abs(audio))
163
 
164
+ # Clip the audio to ensure no values fall outside the range [-1, 1]
165
  audio = torch.clamp(audio, min=-1, max=1)
166
 
167
+ # Convert to 16-bit PCM format by scaling and casting to int16
168
  audio = (audio * 32767).short()
169
 
170
+ # Ensure the audio is in the valid range for int16 [-32768, 32767]
171
  audio = torch.clamp(audio, min=-32768, max=32767)
172
 
173
+ # Convert the audio to a NumPy array of int16
174
  audio_numpy = audio.cpu().numpy().astype(np.int16)
175
 
176
  return audio_numpy
 
179
 
180
 
181
  def generate_audio_from_image(image):
182
+ test_img = image_transform(image).unsqueeze(0).to(device) # Preprocess the image
183
 
184
+ # Generate a sound spectrogram from the image using the loaded generator
185
  with torch.no_grad():
186
  generated_spectrogram = generator(test_img)
187
 
188
  # Convert the generated spectrogram to time-domain audio
189
  generated_audio_numpy = spectrogram_to_audio(generated_spectrogram.squeeze(0).cpu())
190
 
191
+ # Return the sample rate and the audio in the correct format for Gradio
192
  return (sample_rate, generated_audio_numpy)
193
 
194
 
195
 
 
196
  # Gradio Interface
197
  def main():
198
  global generator # Declare the generator object globally