我正在尝试将bytes
音频流转换为PyTorch张量,以作为PyTorch的forward()
函数的输入。
(更具体地说,我正在使用Nemo
说话人模型进行说话人识别:
audio_signal,audio_signal_length = torch.tensor([audio]),torch.tensor([audio_length])
)
有人知道如何做吗?
我正在尝试将bytes
音频流转换为PyTorch张量,以作为PyTorch的forward()
函数的输入。
(更具体地说,我正在使用Nemo
说话人模型进行说话人识别:
audio_signal,audio_signal_length = torch.tensor([audio]),torch.tensor([audio_length])
)
有人知道如何做吗?
import torch
import numpy as np
def audio_to_tensor(audio_frame: bytes):
buffer = np.frombuffer(audio_frame, dtype=np.int16).astype(np.float32) / 32767.0
return torch.from_numpy(buffer)
这个对我来说是可行的,虽然我相信肯定有更简单的方法
import io
import numpy as np
import scipy as sc
import torch
import torchaudio
SAMPLE_RATE = 16000
def bytes_to_audio_tensor(audio_bytes:bytes) -> torch.Tensor:
bytes_io = io.BytesIO()
raw_data = np.frombuffer(
buffer=audio_bytes, dtype=np.int32
)
sc.io.wavfile.write(bytes_io, SAMPLE_RATE, raw_data)
audio, _ = torchaudio.load(bytes_io)
return audio.squeeze(0)