forked from facebookresearch/audiocraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencodec_encode.py
27 lines (21 loc) · 1.12 KB
/
encodec_encode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from audiocraft.models import CompressionModel
import torchaudio
import torch
from collections import OrderedDict
from tqdm import tqdm
import os
model = CompressionModel.get_pretrained('facebook/encodec_32khz', device='cuda')
for dirpath, dirnames, filenames in os.walk("/home/jongmin/userdata/MAESTRO/maestro-v3.0.0/"):
for filename in tqdm([f for f in filenames if f.endswith(".wav")]):
print("Tokeninzing: ", os.path.join(dirpath, filename))
x, sr = torchaudio.load(str(os.path.join(dirpath, filename)))
x = torchaudio.functional.resample(x.cuda(), orig_freq=sr, new_freq=32000)
x = x.mean(0, keepdim=True)
token_dict = {}
token_dict['len']=int(x.shape[-1]/(32000*30))+1
for i in range(int(x.shape[-1]/(32000*30))):
a, b = model.encode(x[...,i*32000*30:(i+1)*32000*30].unsqueeze(0))
token_dict[i*30]=a.cpu().detach().to(torch.int16)
a, b = model.encode(x[...,-32000*30:].unsqueeze(0))
token_dict[-1]=a.cpu().detach().to(torch.int16)
torch.save(token_dict, str(os.path.join(dirpath, filename)).rsplit('.', 1)[0]+'_encodec.pt')