我很难解析PCM文件中的音频长度。
EfficientConformer使用LibriSpeechDataset,音频文件格式是flac,但在我的例子中,我使用的是pcm文件。EfficientConformer通过这样的torchaudio提取音频长度
audio_length = torchaudio.load(DATASET_PATH)[0].size(1)但就我的情况而言,它不适用于PCM文件,所以我尝试了不同的方法。
我所做的
先从下面的代码获取信号
signal = np.memmap(audio_path, dtype='h', mode='r').astype('float32')
if sum(abs(signal)) <= 80:
raise ValueError('[WARN] Silence file in {0}'.format(audio_path))
return signal / 32767 # normalize audio然后得到波形
waveform = Tensor(signal).unsqueeze(0).t()最后在dim(1)中得到大小。
audio_length = waveform.size(1)但它将打印1保存在终端中。
这是我的PCM数据集信息
files
如何在pcm文件中获取音频长度?
发布于 2022-10-20 14:07:18
如果您使用的是TorchAudio v0.12或更高版本,那么使用torchaudio.io.StreamReader允许直接加载PCM。
参考文献:https://pytorch.org/audio/main/tutorials/streamreader_basic_tutorial.html#headerless-media
s = StreamReader(src=PATH, format="s16le", option={"sample_rate": "16000"})
s.add_basic_audio_stream(frames_per_chunk=-1)
s.process_all_packets()
waveform, = s.pop_chunkshttps://stackoverflow.com/questions/73810750
复制相似问题