Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions sox/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -3131,7 +3131,9 @@ def speed(self, factor: float):
return self

def stat(self,
input_filepath: Union[str, Path],
input_filepath: Optional[Union[str, Path]] = None,
input_array: Optional[np.ndarray] = None,
sample_rate_in: Optional[float] = None,
scale: Optional[float] = None,
rms: Optional[bool] = False):
'''Display time and frequency domain statistical information about the
Expand All @@ -3145,8 +3147,13 @@ def stat(self,

Parameters
----------
input_filepath : str
input_filepath : str or None
Path to input file to compute stats on.
input_array : np.ndarray or None
A np.ndarray of an waveform with shape (n_samples, n_channels)
or None
sample_rate_in : int or None
Sample rate of input_array or None
scale : float or None, default=None
If not None, scales the input by the given scale factor.
rms : bool, default=False
Expand All @@ -3171,7 +3178,8 @@ def stat(self,
effect_args.append('-rms')

_, _, stat_output = self.build(
input_filepath, '-n', extra_args=effect_args, return_output=True
input_filepath=input_filepath, input_array=input_array, sample_rate_in=sample_rate_in,
output_filepath='-n', extra_args=effect_args, return_output=True
)

stat_dict = {}
Expand All @@ -3186,16 +3194,24 @@ def stat(self,

return stat_dict

def power_spectrum(self, input_filepath: Union[str, Path]):
def power_spectrum(self,
input_filepath: Optional[Union[str, Path]] = None,
input_array: Optional[np.ndarray] = None,
sample_rate_in: Optional[float] = None):
'''Calculates the power spectrum (4096 point DFT). This method
internally invokes the stat command with the -freq option.

Note: The file is downmixed to mono prior to computation.

Parameters
----------
input_filepath : str
input_filepath : str or None
Path to input file to compute stats on.
input_array : np.ndarray or None
A np.ndarray of an waveform with shape (n_samples, n_channels)
or None
sample_rate_in : int or None
Sample rate of input_array or None

Returns
-------
Expand All @@ -3209,7 +3225,8 @@ def power_spectrum(self, input_filepath: Union[str, Path]):
effect_args = ['channels', '1', 'stat', '-freq']

_, _, stat_output = self.build(
input_filepath, '-n', extra_args=effect_args, return_output=True
input_filepath=input_filepath, input_array=input_array, sample_rate_in=sample_rate_in,
output_filepath='-n', extra_args=effect_args, return_output=True
)

power_spectrum = []
Expand All @@ -3224,7 +3241,10 @@ def power_spectrum(self, input_filepath: Union[str, Path]):

return power_spectrum

def stats(self, input_filepath: Union[str, Path]):
def stats(self,
input_filepath: Optional[Union[str, Path]] = None,
input_array: Optional[np.ndarray] = None,
sample_rate_in: Optional[float] = None):
'''Display time domain statistical information about the audio
channels. Audio is passed unmodified through the SoX processing chain.
Statistics are calculated and displayed for each audio channel
Expand All @@ -3237,8 +3257,13 @@ def stats(self, input_filepath: Union[str, Path]):

Parameters
----------
input_filepath : str
input_filepath : str or None
Path to input file to compute stats on.
input_array : np.ndarray or None
A np.ndarray of an waveform with shape (n_samples, n_channels)
or None
sample_rate_in : int or None
Sample rate of input_array or None

Returns
-------
Expand All @@ -3252,7 +3277,8 @@ def stats(self, input_filepath: Union[str, Path]):
effect_args = ['channels', '1', 'stats']

_, _, stats_output = self.build(
input_filepath, '-n', extra_args=effect_args, return_output=True
input_filepath=input_filepath, input_array=input_array, sample_rate_in=sample_rate_in,
output_filepath='-n', extra_args=effect_args, return_output=True
)

stats_dict = {}
Expand Down