From 75bbaa521c43b370d1680a4836b15768a206f4b6 Mon Sep 17 00:00:00 2001 From: Weiqi Gao Date: Thu, 25 May 2023 01:48:36 +0800 Subject: [PATCH] flac output & max_allowed_segment only for HTDemucs (#500) --- Makefile | 1 + demucs/apply.py | 3 ++- demucs/audio.py | 2 ++ demucs/separate.py | 11 ++++++++--- docs/release.md | 4 ++++ 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index a60eeee9..5b58bf45 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,7 @@ test_eval: python3 -m demucs -n demucs_unittest test.mp3 python3 -m demucs -n demucs_unittest --two-stems=vocals test.mp3 python3 -m demucs -n demucs_unittest --mp3 test.mp3 + python3 -m demucs -n demucs_unittest --flac --int24 test.mp3 python3 -m demucs -n demucs_unittest --int24 --clip-mode clamp test.mp3 python3 -m demucs -n demucs_unittest --segment 8 test.mp3 diff --git a/demucs/apply.py b/demucs/apply.py index 24360510..54ad9256 100644 --- a/demucs/apply.py +++ b/demucs/apply.py @@ -68,7 +68,8 @@ def __init__(self, models: tp.List[Model], def max_allowed_segment(self) -> float: max_allowed_segment = float('inf') for model in self.models: - max_allowed_segment = min(max_allowed_segment, float(model.segment)) + if isinstance(model, HTDemucs): + max_allowed_segment = min(max_allowed_segment, float(model.segment)) return max_allowed_segment def forward(self, x): diff --git a/demucs/audio.py b/demucs/audio.py index 2172b28d..2b1af82e 100644 --- a/demucs/audio.py +++ b/demucs/audio.py @@ -257,5 +257,7 @@ def save_audio(wav: torch.Tensor, encoding = 'PCM_S' ta.save(str(path), wav, sample_rate=samplerate, encoding=encoding, bits_per_sample=bits_per_sample) + elif suffix == ".flac": + ta.save(str(path), wav, sample_rate=samplerate, bits_per_sample=bits_per_sample) else: raise ValueError(f"Invalid suffix for path: {suffix}") diff --git a/demucs/separate.py b/demucs/separate.py index 1f93dab5..985ad2d8 100644 --- a/demucs/separate.py +++ b/demucs/separate.py @@ -104,8 +104,11 @@ def get_parser(): parser.add_argument("--clip-mode", default="rescale", choices=["rescale", "clamp"], help="Strategy for avoiding clipping: rescaling entire signal " "if necessary (rescale) or hard clipping (clamp).") - parser.add_argument("--mp3", action="store_true", - help="Convert the output wavs to mp3.") + format_group = parser.add_mutually_exclusive_group() + format_group.add_argument("--flac", action="store_true", + help="Convert the output wavs to flac.") + format_group.add_argument("--mp3", action="store_true", + help="Convert the output wavs to mp3.") parser.add_argument("--mp3-bitrate", default=320, type=int, @@ -128,7 +131,7 @@ def main(opts=None): except ModelLoadingError as error: fatal(error.args[0]) - max_allowed_segment: float = float('inf') + max_allowed_segment = float('inf') if isinstance(model, HTDemucs): max_allowed_segment = float(model.segment) elif isinstance(model, BagOfModels): @@ -170,6 +173,8 @@ def main(opts=None): if args.mp3: ext = "mp3" + elif args.flac: + ext = "flac" else: ext = "wav" kwargs = { diff --git a/docs/release.md b/docs/release.md index 005055d7..579d09df 100644 --- a/docs/release.md +++ b/docs/release.md @@ -2,11 +2,15 @@ ## V4.0.1a1, TBD +**From this version, Python 3.7 is no longer supported. This is not a problem since the latest PyTorch 2.0.0 no longer support it either.** + Various improvements by @CarlGao4. Support for `segment` param inside of HTDemucs model. Made diffq an optional dependency, with an error message if not installed. +Added output format flac (Free Lossless Audio Codec) + ## V4.0.0, 7th of December 2022 Adding hybrid transformer Demucs model.