Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -154,24 +154,48 @@ def resample_waveform(waveform, original_sample_rate, target_sample_rate):
|
|
| 154 |
return resampler(waveform), target_sample_rate
|
| 155 |
return waveform, original_sample_rate
|
| 156 |
|
|
|
|
|
|
|
|
|
|
| 157 |
def split_audio(waveform, sample_rate):
|
| 158 |
segment_samples = segment_duration * sample_rate
|
| 159 |
total_samples = waveform.size(0)
|
| 160 |
|
| 161 |
segments = []
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
segment = waveform[start:end]
|
| 166 |
segments.append(segment)
|
| 167 |
|
| 168 |
-
#
|
| 169 |
-
if len(segments) == 0:
|
| 170 |
-
|
| 171 |
-
segments.append(segment)
|
| 172 |
|
| 173 |
return segments
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
def safe_remove_dir(directory):
|
| 177 |
"""
|
|
|
|
| 154 |
return resampler(waveform), target_sample_rate
|
| 155 |
return waveform, original_sample_rate
|
| 156 |
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
|
| 160 |
def split_audio(waveform, sample_rate):
|
| 161 |
segment_samples = segment_duration * sample_rate
|
| 162 |
total_samples = waveform.size(0)
|
| 163 |
|
| 164 |
segments = []
|
| 165 |
+
# If the audio is shorter than the segment duration, just use the entire audio
|
| 166 |
+
if total_samples <= segment_samples:
|
| 167 |
+
segments.append(waveform)
|
| 168 |
+
else:
|
| 169 |
+
# Split the audio into segments of the specified duration
|
| 170 |
+
for start in range(0, total_samples, segment_samples):
|
| 171 |
+
end = min(start + segment_samples, total_samples)
|
| 172 |
segment = waveform[start:end]
|
| 173 |
segments.append(segment)
|
| 174 |
|
| 175 |
+
# Ensure we have at least one segment
|
| 176 |
+
if len(segments) == 0:
|
| 177 |
+
segments.append(waveform)
|
|
|
|
| 178 |
|
| 179 |
return segments
|
| 180 |
|
| 181 |
+
# def split_audio(waveform, sample_rate):
|
| 182 |
+
# segment_samples = segment_duration * sample_rate
|
| 183 |
+
# total_samples = waveform.size(0)
|
| 184 |
+
|
| 185 |
+
# segments = []
|
| 186 |
+
# for start in range(0, total_samples, segment_samples):
|
| 187 |
+
# end = start + segment_samples
|
| 188 |
+
# if end <= total_samples:
|
| 189 |
+
# segment = waveform[start:end]
|
| 190 |
+
# segments.append(segment)
|
| 191 |
+
|
| 192 |
+
# # In case audio length is shorter than segment length.
|
| 193 |
+
# if len(segments) == 0:
|
| 194 |
+
# segment = waveform
|
| 195 |
+
# segments.append(segment)
|
| 196 |
+
|
| 197 |
+
# return segments
|
| 198 |
+
|
| 199 |
|
| 200 |
def safe_remove_dir(directory):
|
| 201 |
"""
|