Skip to content

Commit

Permalink
Merge branch 'facebookresearch:main' into language_id
Browse files Browse the repository at this point in the history
  • Loading branch information
am831 authored May 10, 2024
2 parents d2a65f8 + fed73b6 commit 4c65310
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/seamless_communication/denoise/demucs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@
from dataclasses import dataclass
from typing import Optional
import os
import logging

SAMPLING_RATE = 16000

logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s -- %(name)s: %(message)s",
)

logger = logging.getLogger("demucs")

@dataclass
class DenoisingConfig:
def __init__(
Expand Down Expand Up @@ -45,17 +53,17 @@ def run_command_with_temp_file(self, cmd):
with tempfile.NamedTemporaryFile(mode='w+', delete=False) as temp:
self.temp_files.append(temp.name)
result = sp.run(cmd, stdout=temp, stderr=temp, text=True)
# If there was an error, print the content of the file
# If there was an error, log the content of the file
if result.returncode != 0:
temp.seek(0)
print(temp.read())
logger.info(temp.read())

def cleanup_temp_files(self):
for temp_file in self.temp_files:
try:
os.remove(temp_file)
except Exception as e:
print(f"Failed to remove temporary file: {temp_file}. Error: {e}")
logger.info(f"Failed to remove temporary file: {temp_file}. Error: {e}")

def denoise(self, audio: Union[str, Tensor]):

Expand All @@ -69,7 +77,7 @@ def denoise(self, audio: Union[str, Tensor]):
audio = temp_wav.name

if not Path(audio).exists():
print("Input file does not exist.")
logger.info("Input file does not exist.")
return None

with tempfile.TemporaryDirectory() as temp_dir:
Expand All @@ -85,13 +93,13 @@ def denoise(self, audio: Union[str, Tensor]):
audio_name = audio_path.stem
audio = [str(audio)]

print("Executing command:", " ".join(cmd))
logger.info("Executing command:", " ".join(cmd))
self.run_command_with_temp_file(cmd + audio)

separated_files = list(Path(temp_dir + "/htdemucs/" + audio_name).glob("*vocals.wav*"))

if not separated_files:
print("Separated vocals file not found.")
logger.info("Separated vocals file not found.")
return None

waveform, sample_rate = torchaudio.load(separated_files[0])
Expand Down

0 comments on commit 4c65310

Please sign in to comment.