I am trying to get the audio chunk of audio file between specific start time and end time
Consider a audio of duration 10 seconds. Now i need to get chunk from 4 sec to 7 sec
torchaudio.info doesn’t give correct num_frames
for io.BytesIO flac audio file. So there is no way to find the total number of frames in the given audio to check for out of bounds start offset Ref: [v0.12] torchaudio.info reports num_frames=0 for MP3 · Issue #2524 · pytorch/audio · GitHub
What I did to get the chunk of audio with start and end offsets.
def read_audio(audio_file, start, end):
audio_file.seek(0)
reader = StreamReader(audio_file)
sample_rate = int(reader.get_src_stream_info(reader.default_audio_stream).sample_rate)
reader.seek(start * sample_rate)
reader.add_basic_audio_stream(frames_per_chunk=(end - start) * sample_rate)
return list(reader.stream())[0].pop()
This is working as intended for start time less than the duration of audio file. But when we give the start time more than the duration of audio file, It doesn’t throw error or return empty tensor. Is there any way to know the given offsets are out of bounds.