Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -60,6 +60,50 @@ def analyze_audio_files(files, folder_path):
|
|
| 60 |
S_mean = np.mean(S, axis=1)
|
| 61 |
freqs = np.linspace(0, sr / 2, len(S_mean))
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
# Plot the frequency spectrum
|
| 64 |
fig1 = plt.figure(figsize=(8, 4))
|
| 65 |
plt.semilogx(freqs, 20 * np.log10(S_mean + 1e-10)) # Avoid log(0)
|
|
@@ -160,6 +204,10 @@ def analyze_audio_files(files, folder_path):
|
|
| 160 |
# Combine text and images into HTML
|
| 161 |
output_html += f"""
|
| 162 |
{output_text}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
<h4 style="font-size:20px;">Frequency Spectrum</h4>
|
| 164 |
{spectrum_html}
|
| 165 |
<h4 style="font-size:20px;">Spectrogram</h4>
|
|
|
|
| 60 |
S_mean = np.mean(S, axis=1)
|
| 61 |
freqs = np.linspace(0, sr / 2, len(S_mean))
|
| 62 |
|
| 63 |
+
# Plot the waveform
|
| 64 |
+
fig_waveform = plt.figure(figsize=(8, 4))
|
| 65 |
+
librosa.display.waveshow(y, sr=sr, alpha=0.5)
|
| 66 |
+
plt.title('Waveform', fontsize=14)
|
| 67 |
+
plt.xlabel('Time (s)', fontsize=12)
|
| 68 |
+
plt.ylabel('Amplitude', fontsize=12)
|
| 69 |
+
plt.tight_layout()
|
| 70 |
+
waveform_image = io.BytesIO()
|
| 71 |
+
plt.savefig(waveform_image, format='png', bbox_inches='tight')
|
| 72 |
+
plt.close(fig_waveform)
|
| 73 |
+
waveform_image.seek(0)
|
| 74 |
+
waveform_base64 = base64.b64encode(
|
| 75 |
+
waveform_image.read()).decode('utf-8')
|
| 76 |
+
waveform_html = f'<img src="data:image/png;base64,{waveform_base64}" alt="Waveform">'
|
| 77 |
+
|
| 78 |
+
# Calculate spectral features: spectral centroid, spectral bandwidth, and spectral rolloff
|
| 79 |
+
spectral_centroids = librosa.feature.spectral_centroid(y=y, sr=sr)[
|
| 80 |
+
0]
|
| 81 |
+
spectral_bandwidth = librosa.feature.spectral_bandwidth(y=y, sr=sr)[
|
| 82 |
+
0]
|
| 83 |
+
spectral_rolloff = librosa.feature.spectral_rolloff(
|
| 84 |
+
y=y, sr=sr, roll_percent=0.85)[0]
|
| 85 |
+
times = librosa.times_like(spectral_centroids)
|
| 86 |
+
|
| 87 |
+
# Plot the spectral features
|
| 88 |
+
fig_spectral_features = plt.figure(figsize=(8, 4))
|
| 89 |
+
plt.semilogy(times, spectral_centroids, label='Spectral Centroid')
|
| 90 |
+
plt.semilogy(times, spectral_bandwidth, label='Spectral Bandwidth')
|
| 91 |
+
plt.semilogy(times, spectral_rolloff,
|
| 92 |
+
label='Spectral Rolloff', linestyle='--')
|
| 93 |
+
plt.title('Spectral Features', fontsize=14)
|
| 94 |
+
plt.xlabel('Time (s)', fontsize=12)
|
| 95 |
+
plt.ylabel('Hz', fontsize=12)
|
| 96 |
+
plt.legend(loc='upper right')
|
| 97 |
+
plt.tight_layout()
|
| 98 |
+
spectral_features_image = io.BytesIO()
|
| 99 |
+
plt.savefig(spectral_features_image,
|
| 100 |
+
format='png', bbox_inches='tight')
|
| 101 |
+
plt.close(fig_spectral_features)
|
| 102 |
+
spectral_features_image.seek(0)
|
| 103 |
+
spectral_features_base64 = base64.b64encode(
|
| 104 |
+
spectral_features_image.read()).decode('utf-8')
|
| 105 |
+
spectral_features_html = f'<img src="data:image/png;base64,{spectral_features_base64}" alt="Spectral Features">'
|
| 106 |
+
|
| 107 |
# Plot the frequency spectrum
|
| 108 |
fig1 = plt.figure(figsize=(8, 4))
|
| 109 |
plt.semilogx(freqs, 20 * np.log10(S_mean + 1e-10)) # Avoid log(0)
|
|
|
|
| 204 |
# Combine text and images into HTML
|
| 205 |
output_html += f"""
|
| 206 |
{output_text}
|
| 207 |
+
<h4 style="font-size:20px;">Waveform</h4>
|
| 208 |
+
{waveform_html}
|
| 209 |
+
<h4 style="font-size:20px;">Spectral Features</h4>
|
| 210 |
+
{spectral_features_html}
|
| 211 |
<h4 style="font-size:20px;">Frequency Spectrum</h4>
|
| 212 |
{spectrum_html}
|
| 213 |
<h4 style="font-size:20px;">Spectrogram</h4>
|