Encode audio files in input path to .dac format. Parameters ---------- input : str Path to input audio file or directory output : str, optional Path to output directory, by default "". If `input` is a directory, the directory sub-tree relative to `input` is re-create
(
input: str,
output: str = "",
weights_path: str = "",
model_tag: str = "latest",
model_bitrate: str = "8kbps",
n_quantizers: int = None,
device: str = "cuda",
model_type: str = "44khz",
win_duration: float = 5.0,
verbose: bool = False,
)
| 18 | @torch.inference_mode() |
| 19 | @torch.no_grad() |
| 20 | def encode( |
| 21 | input: str, |
| 22 | output: str = "", |
| 23 | weights_path: str = "", |
| 24 | model_tag: str = "latest", |
| 25 | model_bitrate: str = "8kbps", |
| 26 | n_quantizers: int = None, |
| 27 | device: str = "cuda", |
| 28 | model_type: str = "44khz", |
| 29 | win_duration: float = 5.0, |
| 30 | verbose: bool = False, |
| 31 | ): |
| 32 | """Encode audio files in input path to .dac format. |
| 33 | |
| 34 | Parameters |
| 35 | ---------- |
| 36 | input : str |
| 37 | Path to input audio file or directory |
| 38 | output : str, optional |
| 39 | Path to output directory, by default "". If `input` is a directory, the directory sub-tree relative to `input` is re-created in `output`. |
| 40 | weights_path : str, optional |
| 41 | Path to weights file, by default "". If not specified, the weights file will be downloaded from the internet using the |
| 42 | model_tag and model_type. |
| 43 | model_tag : str, optional |
| 44 | Tag of the model to use, by default "latest". Ignored if `weights_path` is specified. |
| 45 | model_bitrate: str |
| 46 | Bitrate of the model. Must be one of "8kbps", or "16kbps". Defaults to "8kbps". |
| 47 | n_quantizers : int, optional |
| 48 | Number of quantizers to use, by default None. If not specified, all the quantizers will be used and the model will compress at maximum bitrate. |
| 49 | device : str, optional |
| 50 | Device to use, by default "cuda" |
| 51 | model_type : str, optional |
| 52 | The type of model to use. Must be one of "44khz", "24khz", or "16khz". Defaults to "44khz". Ignored if `weights_path` is specified. |
| 53 | """ |
| 54 | generator = load_model( |
| 55 | model_type=model_type, |
| 56 | model_bitrate=model_bitrate, |
| 57 | tag=model_tag, |
| 58 | load_path=weights_path, |
| 59 | ) |
| 60 | generator.to(device) |
| 61 | generator.eval() |
| 62 | kwargs = {"n_quantizers": n_quantizers} |
| 63 | |
| 64 | # Find all audio files in input path |
| 65 | input = Path(input) |
| 66 | audio_files = util.find_audio(input) |
| 67 | |
| 68 | output = Path(output) |
| 69 | output.mkdir(parents=True, exist_ok=True) |
| 70 | |
| 71 | for i in tqdm(range(len(audio_files)), desc="Encoding files"): |
| 72 | # Load file |
| 73 | signal = AudioSignal(audio_files[i]) |
| 74 | |
| 75 | # Encode audio to .dac format |
| 76 | artifact = generator.compress(signal, win_duration, verbose=verbose, **kwargs) |
| 77 |
no test coverage detected