-
Notifications
You must be signed in to change notification settings - Fork 121
Open
Description
sf.read(filepath) works as intended, it successfully finds the file. However, when trying to write to the file (replace its content), this error triggers
File "C:\Users\User\Documents\someFolder\src\util\files.py", line 92, in set_fixed_duration
sf.write(file_path, audio_trunc, SAMPLE_RATE, format='FLAC')
File "C:\Users\User\miniforge3\envs\speech\lib\site-packages\soundfile.py", line 363, in write
with SoundFile(file, 'w', samplerate, channels,
File "C:\Users\User\miniforge3\envs\speech\lib\site-packages\soundfile.py", line 690, in __init__
self._file = self._open(file, mode_int, closefd)
File "C:\Users\User\miniforge3\envs\speech\lib\site-packages\soundfile.py", line 1265, in _open
raise LibsndfileError(err, prefix="Error opening {0!r}: ".format(self.name))
soundfile.LibsndfileError: Error opening 'data/train/speech/103-1240-0000.flac': System error.
This is my code
def set_fixed_duration(directory):
"""
Set all audio files in the specified directory to a fixed duration by truncating or padding with silence.
Replace the original files with the adjusted ones.
"""
target_len = int(SAMPLE_RATE * DURATION)
for filename in tqdm(os.listdir(directory)):
if filename.endswith('.flac'):
file_path = os.path.join(directory, filename)
# Load the audio file
audio, sr = sf.read(file_path)
if sr != SAMPLE_RATE:
raise ValueError(f"When setting duration | Expected sample rate {SAMPLE_RATE}, but got {sr}")
# Convert to mono if needed
if audio.ndim > 1:
print(f"Converting stereo to mono for file: {filename}")
audio = np.mean(audio, axis=1)
# Print first 10 samples for debugging
# print(f"Original audio samples (first 10) for {filename}: {audio[:10]}")
L = len(audio)
# Adjust duration
if L > target_len:
# Truncate
audio_trunc = audio[:target_len]
else:
# Pad with silence (zeros)
pad_width = target_len - L
audio_trunc = np.pad(audio, (0, pad_width), mode='constant', constant_values=EPSILON)
# Replace the original file with the adjusted one
sf.write(file_path, audio_trunc, SAMPLE_RATE, format='FLAC')I have soundfile v0.13.1 and libsndfile v.1.2.2
I am on windows 11
Metadata
Metadata
Assignees
Labels
No labels