Skip to content

Commit

Permalink
Add additional functions for amplitude and coefficient conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyolicoris committed Apr 2, 2024
1 parent 7999d1c commit 52fc9f0
Showing 1 changed file with 62 additions and 5 deletions.
67 changes: 62 additions & 5 deletions torchcomp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,69 @@

from .core import compressor_core

__all__ = ["compexp_gain", "limiter_gain", "ms2coef", "avg"]
__all__ = [
"compexp_gain",
"limiter_gain",
"ms2coef",
"avg",
"amp2db",
"db2amp",
"coef2ms",
]

amp2db = lambda x: 20 * torch.log10(x)
db2amp = lambda x: 10 ** (x / 20)
ms2coef = lambda ms, sr: (1 - torch.exp(-2200 / ms / sr))
coef2ms = lambda coef, sr: -2200 / (sr * torch.log(1 - coef))

def amp2db(x: torch.Tensor) -> torch.Tensor:
"""Convert amplitude to decibels.
Args:
x (torch.Tensor): Input amplitude.
Returns:
torch.Tensor: Output decibels.
"""
return 20 * torch.log10(x)


def db2amp(x: torch.Tensor) -> torch.Tensor:
"""Convert decibels to amplitude.
Args:
x (torch.Tensor): Input decibels.
Returns:
torch.Tensor: Output amplitude.
"""
return 10 ** (x / 20)


def ms2coef(ms: torch.Tensor, sr: int) -> torch.Tensor:
"""Convert milliseconds to coefficient.
Args:
ms (torch.Tensor): Input milliseconds.
sr (int): Sample rate.
Returns:
torch.Tensor: Output coefficient.
"""
return 1 - torch.exp(-2200 / ms / sr)


def coef2ms(coef: torch.Tensor, sr: int) -> torch.Tensor:
"""Convert coefficient to milliseconds.
Args:
coef (torch.Tensor): Input coefficient.
sr (int): Sample rate.
Returns:
torch.Tensor: Output milliseconds.
"""
return -2200 / (sr * torch.log(1 - coef))


def avg(rms: torch.Tensor, avg_coef: Union[torch.Tensor, float]):
Expand Down

0 comments on commit 52fc9f0

Please sign in to comment.