@@ -1384,3 +1384,78 @@ def freeze(self):
13841384 def unfreeze (self ):
13851385 """Resumes updates to the running mean/std"""
13861386 self .frozen = False
1387+
1388+ class MinLevelNorm (torch .nn .Module ):
1389+ """A normalization for the decibel scale
1390+
1391+ The scheme is as follows
1392+
1393+ x_norm = (x - min_level_db)/-min_level_db * 2 - 1
1394+
1395+ Arguments
1396+ ---------
1397+ min_level_db: float
1398+ the minimum level
1399+ """
1400+
1401+ def __init__ (self , min_level_db ):
1402+ super ().__init__ ()
1403+ self .min_level_db = min_level_db
1404+
1405+ def forward (self , x ):
1406+ """Normalizes audio features in decibels (usually spectrograms)
1407+
1408+ Arguments
1409+ ---------
1410+ x: torch.Tensor
1411+ input features
1412+ Returns
1413+ -------
1414+ normalized_features: torch.Tensor
1415+ the normalized features
1416+ """
1417+
1418+ x = (x - self .min_level_db ) / - self .min_level_db
1419+ x *= 2.0
1420+ x = x - 1.0
1421+ x = torch .clip (x , - 1 , 1 )
1422+ return x
1423+
1424+ def denormalize (self , x ):
1425+ """Reverses the min level normalization process
1426+
1427+ Arguments
1428+ ---------
1429+ x: torch.Tensor
1430+ the normalized tensor
1431+
1432+ Returns
1433+ -------
1434+ result: torch.Tensor
1435+ the denormalized tensor
1436+ """
1437+ x = torch .clip (x , - 1 , 1 )
1438+ x = (x + 1.0 ) / 2.0
1439+ x *= - self .min_level_db
1440+ x += self .min_level_db
1441+ return x
1442+
1443+
1444+ class DynamicRangeCompression (torch .nn .Module ):
1445+ """Dynamic range compression for audio signals
1446+ Arguments
1447+ ---------
1448+ multiplier: float
1449+ the multiplier constant
1450+ clip_val: float
1451+ the minimum accepted value (values below this
1452+ minimum will be clipped)
1453+ """
1454+
1455+ def __init__ (self , multiplier = 1 , clip_val = 1e-5 ):
1456+ super ().__init__ ()
1457+ self .multiplier = multiplier
1458+ self .clip_val = clip_val
1459+
1460+ def forward (self , x ):
1461+ return torch .log (torch .clamp (x , min = self .clip_val ) * self .multiplier )
0 commit comments