Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format PicoAudio and move into tta bin #378

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

5,000 changes: 0 additions & 5,000 deletions models/temporally_controllable_tta/data/meta_data/train.json

This file was deleted.

Binary file not shown.

This file was deleted.

24 changes: 0 additions & 24 deletions models/temporally_controllable_tta/picoaudio/data/filter_data.py

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

5,000 changes: 0 additions & 5,000 deletions models/temporally_controllable_tta/picoaudio/data/meta_data/train.json

This file was deleted.

This file was deleted.

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
from .ldm import LatentDiffusion
from .utils import seed_everything, save_wave, get_time, get_duration
from .pipeline import *





Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
#!/usr/bin/python3
import os
from audioldm import text_to_audio, style_transfer, build_model, save_wave, get_time, round_up_duration, get_duration
from audioldm import (
text_to_audio,
style_transfer,
build_model,
save_wave,
get_time,
round_up_duration,
get_duration,
)
import argparse

CACHE_DIR = os.getenv(
"AUDIOLDM_CACHE_DIR",
os.path.join(os.path.expanduser("~"), ".cache/audioldm"))
"AUDIOLDM_CACHE_DIR", os.path.join(os.path.expanduser("~"), ".cache/audioldm")
)

parser = argparse.ArgumentParser()

Expand All @@ -15,7 +23,7 @@
required=False,
default="generation",
help="generation: text-to-audio generation; transfer: style transfer",
choices=["generation", "transfer"]
choices=["generation", "transfer"],
)

parser.add_argument(
Expand Down Expand Up @@ -59,7 +67,7 @@
required=False,
help="The checkpoint you gonna use",
default="audioldm-s-full",
choices=["audioldm-s-full", "audioldm-l-full", "audioldm-s-full-v2"]
choices=["audioldm-s-full", "audioldm-l-full", "audioldm-s-full-v2"],
)

parser.add_argument(
Expand Down Expand Up @@ -125,21 +133,21 @@

args = parser.parse_args()

if(args.ckpt_path is not None):
if args.ckpt_path is not None:
print("Warning: ckpt_path has no effect after version 0.0.20.")

assert args.duration % 2.5 == 0, "Duration must be a multiple of 2.5"

mode = args.mode
if(mode == "generation" and args.file_path is not None):
if mode == "generation" and args.file_path is not None:
mode = "generation_audio_to_audio"
if(len(args.text) > 0):
if len(args.text) > 0:
print("Warning: You have specified the --file_path. --text will be ignored")
args.text = ""

save_path = os.path.join(args.save_path, mode)

if(args.file_path is not None):
if args.file_path is not None:
save_path = os.path.join(save_path, os.path.basename(args.file_path.split(".")[0]))

text = args.text
Expand All @@ -151,7 +159,7 @@
os.makedirs(save_path, exist_ok=True)
audioldm = build_model(model_name=args.model_name)

if(args.mode == "generation"):
if args.mode == "generation":
waveform = text_to_audio(
audioldm,
text,
Expand All @@ -163,10 +171,13 @@
n_candidate_gen_per_text=n_candidate_gen_per_text,
batchsize=args.batchsize,
)
elif(args.mode == "transfer"):

elif args.mode == "transfer":
assert args.file_path is not None
assert os.path.exists(args.file_path), "The original audio file \'%s\' for style transfer does not exist." % args.file_path
assert os.path.exists(args.file_path), (
"The original audio file '%s' for style transfer does not exist."
% args.file_path
)
waveform = style_transfer(
audioldm,
text,
Expand All @@ -178,6 +189,6 @@
ddim_steps=args.ddim_steps,
batchsize=args.batchsize,
)
waveform = waveform[:,None,:]
waveform = waveform[:, None, :]

save_wave(waveform, save_path, name="%s_%s" % (get_time(), text))
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(self, filter_length, hop_length, win_length, window="hann"):
def transform(self, input_data):
device = self.forward_basis.device
input_data = input_data.to(device)

num_batches = input_data.size(0)
num_samples = input_data.size(1)

Expand All @@ -72,7 +72,7 @@ def transform(self, input_data):
torch.autograd.Variable(self.forward_basis, requires_grad=False),
stride=self.hop_length,
padding=0,
)#.cpu()
) # .cpu()

cutoff = int((self.filter_length / 2) + 1)
real_part = forward_transform[:, :cutoff, :]
Expand All @@ -86,7 +86,7 @@ def transform(self, input_data):
def inverse(self, magnitude, phase):
device = self.forward_basis.device
magnitude, phase = magnitude.to(device), phase.to(device)

recombine_magnitude_phase = torch.cat(
[magnitude * torch.cos(phase), magnitude * torch.sin(phase)], dim=1
)
Expand Down Expand Up @@ -149,7 +149,11 @@ def __init__(
self.sampling_rate = sampling_rate
self.stft_fn = STFT(filter_length, hop_length, win_length)
mel_basis = librosa_mel_fn(
sr=sampling_rate, n_fft=filter_length, n_mels=n_mel_channels, fmin=mel_fmin, fmax=mel_fmax
sr=sampling_rate,
n_fft=filter_length,
n_mels=n_mel_channels,
fmin=mel_fmin,
fmax=mel_fmax,
)
mel_basis = torch.from_numpy(mel_basis).float()
self.register_buffer("mel_basis", mel_basis)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def pad_wav(waveform, segment_length):
temp_wav[:, :waveform_length] = waveform
return temp_wav


def normalize_wav(waveform):
waveform = waveform - np.mean(waveform)
waveform = waveform / (np.max(np.abs(waveform)) + 1e-8)
Expand All @@ -57,10 +58,10 @@ def read_wav_file(filename, segment_length):
waveform = normalize_wav(waveform)
waveform = waveform[None, ...]
waveform = pad_wav(waveform, segment_length)

waveform = waveform / np.max(np.abs(waveform))
waveform = 0.5 * waveform

return waveform


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def __init__(
key="class",
sampling_rate=16000,
embed_mode="audio",
amodel = "HTSAT-tiny",
amodel="HTSAT-tiny",
unconditional_prob=0.1,
random_mute=False,
max_random_mute_portion=0.5,
Expand Down Expand Up @@ -92,7 +92,11 @@ def cos_similarity(self, waveform, text):
audio_emb = self(waveform.cuda())
self.embed_mode = "text"
text_emb = self(text)
similarity = F.cosine_similarity(audio_emb, text_emb, dim=2), audio_emb, text_emb
similarity = (
F.cosine_similarity(audio_emb, text_emb, dim=2),
audio_emb,
text_emb,
)
return similarity.squeeze()

def forward(self, batch, key=None):
Expand Down Expand Up @@ -167,4 +171,4 @@ def tokenizer(self, text):
max_length=512,
return_tensors="pt",
)
return {k: v.squeeze(0) for k, v in result.items()}
return {k: v.squeeze(0) for k, v in result.items()}
Loading
Loading