Skip to content

Commit

Permalink
Add scripts and main
Browse files Browse the repository at this point in the history
  • Loading branch information
synesthesiam committed Jul 5, 2024
1 parent 9f28750 commit 4290c32
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ __pycache__/

/.venv/
*.whl

/*.so
/*.wav
21 changes: 21 additions & 0 deletions script/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python3
import subprocess
import venv
from pathlib import Path

_DIR = Path(__file__).parent
_PROGRAM_DIR = _DIR.parent
_VENV_DIR = _PROGRAM_DIR / ".venv"

# Create virtual environment
builder = venv.EnvBuilder(with_pip=True)
context = builder.ensure_directories(_VENV_DIR)
builder.create(_VENV_DIR)

# Upgrade dependencies
pip = [context.env_exe, "-m", "pip"]
subprocess.check_call(pip + ["install", "--upgrade", "pip"])
subprocess.check_call(pip + ["install", "--upgrade", "setuptools", "wheel"])

# Install requirements
subprocess.check_call(pip + ["install", "-r", str(_PROGRAM_DIR / "requirements_dev.txt")])
13 changes: 13 additions & 0 deletions script/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env python3
import subprocess
import sys
import venv
from pathlib import Path

_DIR = Path(__file__).parent
_PROGRAM_DIR = _DIR.parent
_VENV_DIR = _PROGRAM_DIR / ".venv"
_TEST_DIR = _PROGRAM_DIR / "tests"

context = venv.EnvBuilder().ensure_directories(_VENV_DIR)
subprocess.check_call([context.env_exe, "-m", "pytest", _TEST_DIR] + sys.argv[1:])
46 changes: 46 additions & 0 deletions webrtc_noise_gain/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""Processes a WAV file using the audio processor."""

import argparse
import wave

from webrtc_noise_gain import AudioProcessor

SAMPLES_10MS = 160


def main() -> None:
"""Main entry point."""
parser = argparse.ArgumentParser()
parser.add_argument("wav_in", help="WAV input file (16Khz 16-bit mono)")
parser.add_argument("wav_out", help="WAV output file")
parser.add_argument("--auto-gain", type=int, default=0, choices=range(32))
parser.add_argument("--noise-suppression", type=int, default=0, choices=range(5))
args = parser.parse_args()

audio_processor = AudioProcessor(args.auto_gain, args.noise_suppression)

wav_in: wave.Wave_read = wave.open(args.wav_in, "rb")
with wav_in:
assert wav_in.getframerate() == 16000, "Sample rate must be 16Khz"
assert wav_in.getsampwidth() == 2, "Sample width must be 16-bit"
assert wav_in.getnchannels() == 1, "Only mono audio is supported"

wav_out: wave.Wave_write = wave.open(args.wav_out, "wb")
with wav_out:
wav_out.setframerate(16000)
wav_out.setsampwidth(2)
wav_out.setnchannels(1)

# Process in 10ms chunks
chunk = wav_in.readframes(SAMPLES_10MS)
while chunk:
if len(chunk) < SAMPLES_10MS:
wav_out.writeframes(chunk)
break

wav_out.writeframes(audio_processor.Process10ms(chunk).audio)
chunk = wav_in.readframes(SAMPLES_10MS)


if __name__ == "__main__":
main()

0 comments on commit 4290c32

Please sign in to comment.