marl / pysox

Python wrapper around sox.
BSD 3-Clause "New" or "Revised" License
517 stars 80 forks source link

vol() method of the Transformer class should be able to handle negative gain values #133

Open semins opened 3 years ago

semins commented 3 years ago

Python package: sox==1.4.1 Python version: 3.6 OS: Windows 10 sox cmd line utility version: 14.4.2

The current implementation of the vol() method from the Transformer class throws an exception if the gain argument is set to a negative value. According to sox documentation from https://linux.die.net/man/1/sox it should be possible to set a negative gain value when is the gain type is selected as "amplitude" or "power".

One useful application of using a negative gain value is signal inversion. When I changed line 3558 as below and set the gain argument to -1 I was able to invert the input audio signal:

if gain_type in ['amplitude', 'power'] and gain < 0 and gain != -1: raise ValueError( "If gain_type = amplitude or power, gain must be positive." )

Unless there is another way of performing signal inversion using another Transformer method, the above conditional statement can be modified to accept negative values.

rabitt commented 3 years ago

@semins Yep, you're right, it should accept negative values. Feel free to make a PR if you'd like!