Spaces:
Runtime error
Runtime error
File size: 8,275 Bytes
6f6fd13 ac059f4 3815be3 6f6fd13 93b48cb 6f6fd13 9fbfaa6 6f6fd13 3815be3 6f6fd13 3815be3 9fbfaa6 6f6fd13 84d4ed6 6f6fd13 9fbfaa6 3815be3 6f6fd13 3815be3 6f6fd13 3815be3 99122c4 3815be3 93b48cb 3815be3 6f6fd13 3815be3 ac059f4 3815be3 6f6fd13 93b48cb 99122c4 93b48cb 99122c4 93b48cb 6f6fd13 93b48cb 99122c4 93b48cb 99122c4 93b48cb 6f6fd13 ac059f4 3815be3 93b48cb 3815be3 ac059f4 93b48cb 3815be3 6f6fd13 93b48cb 9fbfaa6 6f6fd13 93b48cb 322cc3a 93b48cb 6f6fd13 9fbfaa6 6f6fd13 9fbfaa6 6f6fd13 ac059f4 6f6fd13 93b48cb 3815be3 9fbfaa6 ac059f4 93b48cb ac059f4 6f6fd13 57047e5 6f6fd13 57047e5 6f6fd13 57047e5 6f6fd13 |
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
from pathlib import Path
import random
from typing import List
import tempfile
import subprocess
import argbind
from tqdm import tqdm
import torch
from vampnet.interface import Interface
import audiotools as at
Interface: Interface = argbind.bind(Interface)
def calculate_bitrate(
interface, num_codebooks,
downsample_factor
):
bit_width = 10
sr = interface.codec.sample_rate
hop = interface.codec.hop_size
rate = (sr / hop) * ((bit_width * num_codebooks) / downsample_factor)
return rate
def baseline(sig, interface):
return interface.preprocess(sig)
def reconstructed(sig, interface):
return interface.to_signal(
interface.encode(sig)
)
def coarse2fine(sig, interface):
z = interface.encode(sig)
z = z[:, :interface.c2f.n_conditioning_codebooks, :]
z = interface.coarse_to_fine(z)
return interface.to_signal(z)
def coarse2fine_argmax(sig, interface):
z = interface.encode(sig)
z = z[:, :interface.c2f.n_conditioning_codebooks, :]
z = interface.coarse_to_fine(z,
sample="argmax", sampling_steps=1,
temperature=1.0
)
return interface.to_signal(z)
class CoarseCond:
def __init__(self, num_codebooks, downsample_factor):
self.num_codebooks = num_codebooks
self.downsample_factor = downsample_factor
def __call__(self, sig, interface):
n_conditioning_codebooks = interface.coarse.n_codebooks - self.num_codebooks
zv = interface.coarse_vamp(sig,
n_conditioning_codebooks=n_conditioning_codebooks,
downsample_factor=self.downsample_factor,
)
zv = interface.coarse_to_fine(zv)
return interface.to_signal(zv)
def opus(sig, interface, bitrate=128):
sig = interface.preprocess(sig)
with tempfile.NamedTemporaryFile(suffix=".wav") as f:
sig.write(f.name)
opus_name = Path(f.name).with_suffix(".opus")
# convert to opus
cmd = [
"ffmpeg", "-y", "-i", f.name,
"-c:a", "libopus",
"-b:a", f"{bitrate}",
opus_name
]
subprocess.run(cmd, check=True)
# convert back to wav
output_name = Path(f"{f.name}-opus").with_suffix(".wav")
cmd = [
"ffmpeg", "-y", "-i", opus_name,
output_name
]
subprocess.run(cmd, check=True)
sig = at.AudioSignal(
output_name,
sample_rate=sig.sample_rate
)
return sig
def token_noise(ratio=1.0):
def wrapper(sig, interface):
z = interface.encode(sig)
r = interface.coarse.invgamma(ratio).to(interface.device)
print(f'adding noise with ratio {ratio}')
z, mask = interface.coarse.add_noise(
z,
r,
noise_mode="random"
)
return interface.to_signal(z)
return wrapper
def mask_ratio_1_step(ratio=1.0):
def wrapper(sig, interface):
r = interface.coarse.invgamma(ratio).to(interface.device)
intensity = 1-r
zv = interface.coarse_vamp(
sig,
sample='argmax',
sampling_steps=1,
intensity=intensity
)
return interface.to_signal(zv)
return wrapper
def num_sampling_steps(num_steps=1):
def wrapper(sig, interface):
zv = interface.coarse_vamp(
sig,
downsample_factor=16,
sampling_steps=num_steps,
)
zv = interface.coarse_to_fine(zv)
return interface.to_signal(zv)
return wrapper
def beat_mask(ctx_time):
def wrapper(sig, interface):
beat_mask = interface.make_beat_mask(
sig,
before_beat_s=0.0,
after_beat_s=ctx_time,
invert=True
)
zv = interface.coarse_vamp(
sig,
ext_mask=beat_mask,
)
zv = interface.coarse_to_fine(zv)
return interface.to_signal(zv)
return wrapper
def inpaint(ctx_time):
def wrapper(sig, interface):
zv = interface.coarse_vamp(
sig,
prefix_dur_s=ctx_time,
suffix_dur_s=ctx_time,
)
zv = interface.coarse_to_fine(zv)
return interface.to_signal(zv)
return wrapper
EXP_REGISTRY = {}
EXP_REGISTRY["gen-compression"] = {
"baseline": baseline,
"reconstructed": reconstructed,
"coarse2fine": coarse2fine,
**{
f"{n}_codebooks_downsampled_{x}x": CoarseCond(num_codebooks=n, downsample_factor=x)
for (n, x) in (
(4, 2), # 4 codebooks, downsampled 2x,
(2, 2), # 2 codebooks, downsampled 2x
(1, None), # 1 codebook, no downsampling
(4, 4), # 4 codebooks, downsampled 4x
(1, 2), # 1 codebook, downsampled 2x,
(4, 6), # 4 codebooks, downsampled 6x
(4, 8), # 4 codebooks, downsampled 8x
(4, 16), # 4 codebooks, downsampled 16x
(4, 32), # 4 codebooks, downsampled 16x
)
},
}
EXP_REGISTRY["opus-jazzpop"] = {
f"opus_{bitrate}": lambda sig, interface: opus(sig, interface, bitrate=bitrate)
for bitrate in [5620, 1875, 1250, 625]
}
EXP_REGISTRY["opus-spotdl"] = {
f"opus_{bitrate}": lambda sig, interface: opus(sig, interface, bitrate=bitrate)
for bitrate in [8036, 2296, 1148, 574]
}
EXP_REGISTRY["opus-baseline"] = {
f"opus_{bitrate}": lambda sig, interface: opus(sig, interface, bitrate=bitrate)
for bitrate in [8000, 12000, 16000]
}
EXP_REGISTRY["c2f"] = {
"baseline": baseline,
"reconstructed": reconstructed,
"coarse2fine": coarse2fine,
"coarse2fine_argmax": coarse2fine_argmax,
}
EXP_REGISTRY["token-noise"] = {
f"token_noise_{r}": token_noise(r) for r in [0.25, 0.5, 0.75, 1.0]
}
EXP_REGISTRY["mask-ratio"] = {
"codec": reconstructed,
**{f"mask_ratio_{r}": mask_ratio_1_step(r) for r in [0.25, 0.5, 0.75, 0.9]}
}
EXP_REGISTRY["sampling-steps"] = {
"codec": reconstructed,
**{f"steps_{n}": num_sampling_steps(n) for n in [1, 4, 12, 24, 36, 64, 72]},
}
EXP_REGISTRY["baseline"] = {
"baseline": baseline,
"codec": reconstructed,
}
EXP_REGISTRY["musical-sampling"] = {
"baseline": baseline,
"codec": reconstructed,
**{f"downsample_{x}x": CoarseCond(4, downsample_factor=x) for x in [16, 32]},
**{f"beat_mask_{t}": beat_mask(t) for t in [0.075]},
**{f"inpaint_{t}": inpaint(t) for t in [0.5, 1.0,]}, # multiply these by 2 (they go left and right)
}
@argbind.bind(without_prefix=True)
def main(
sources=[
"/data/spotdl/audio/val", "/data/spotdl/audio/test"
],
output_dir: str = "./samples",
max_excerpts: int = 5000,
exp_type: str = "coarse",
seed: int = 0,
):
at.util.seed(seed)
interface = Interface()
output_dir = Path(output_dir)
output_dir.mkdir(exist_ok=True, parents=True)
from audiotools.data.datasets import AudioLoader, AudioDataset
loader = AudioLoader(sources=sources, shuffle_state=seed)
dataset = AudioDataset(loader,
sample_rate=interface.codec.sample_rate,
duration=interface.coarse.chunk_size_s,
n_examples=max_excerpts,
without_replacement=True,
)
if exp_type in EXP_REGISTRY:
SAMPLE_CONDS = EXP_REGISTRY[exp_type]
else:
raise ValueError(f"Unknown exp_type {exp_type}")
indices = list(range(max_excerpts))
random.shuffle(indices)
for i in tqdm(indices):
# if all our files are already there, skip
done = []
for name in SAMPLE_CONDS:
o_dir = Path(output_dir) / name
done.append((o_dir / f"{i}.wav").exists())
if all(done):
continue
sig = dataset[i]["signal"]
results = {
name: cond(sig, interface).cpu()
for name, cond in SAMPLE_CONDS.items()
}
for name, sig in results.items():
o_dir = Path(output_dir) / name
o_dir.mkdir(exist_ok=True, parents=True)
sig.write(o_dir / f"{i}.wav")
if __name__ == "__main__":
args = argbind.parse_args()
with argbind.scope(args):
main()
|