Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
148 |
magnitude_spectrogram = torch.expm1(magnitude_spectrogram)
|
149 |
|
150 |
-
# Convert magnitude-only spectrogram to complex format
|
151 |
-
|
|
|
152 |
|
153 |
-
#
|
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]
|
164 |
audio = torch.clamp(audio, min=-1, max=1)
|
165 |
|
166 |
-
#
|
167 |
audio = (audio * 32767).short()
|
168 |
|
169 |
-
# Ensure the audio is
|
170 |
audio = torch.clamp(audio, min=-32768, max=32767)
|
171 |
|
172 |
-
# Convert to a NumPy array
|
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
|
182 |
|
183 |
-
# Generate a sound spectrogram from the image using the
|
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
|
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
|