From 91a532aa255a8ba35874f02f9249a38b389697fa Mon Sep 17 00:00:00 2001 From: Page David Date: Tue, 11 Apr 2017 08:20:15 +0800 Subject: [PATCH 1/8] fix noiseprof output check. --- sox/transform.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sox/transform.py b/sox/transform.py index f286d2e..b41ce64 100644 --- a/sox/transform.py +++ b/sox/transform.py @@ -1875,9 +1875,10 @@ def noiseprof(self, input_filepath, profile_path): ''' if os.path.isdir(profile_path): - raise ValueError("profile_path {} is a directory, but should be a file") + raise ValueError("profile_path {} is a directory, but filename should be specified.") - if not os.access(os.path.dirname(profile_path), os.W_OK): + if (bool(os.path.dirname(profile_path)) and + not os.access(os.path.dirname(profile_path), os.W_OK)): raise IOError("profile_path {} is not writeable.".format(profile_path)) effect_args = ['noiseprof', profile_path] From 529c5bb8e9200409af5dcff684d78cdae2a80bd6 Mon Sep 17 00:00:00 2001 From: Page David Date: Tue, 11 Apr 2017 10:32:21 +0800 Subject: [PATCH 2/8] docs for noiseprof and noisered --- docs/example.rst | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/docs/example.rst b/docs/example.rst index 5d7cc4b..eab4e95 100644 --- a/docs/example.rst +++ b/docs/example.rst @@ -39,3 +39,30 @@ Combiner Example cbn.build( ['input1.wav', 'input2.wav', 'input3.wav'], output.wav, 'concatenate' ) + + +Advanced Usage +============== + +noiseprof / noisered +-------------------- + +.. code-block:: python + :linenos: + + import sox + # create transformer + tfm = sox.Transformer() + # create a prof file with noise data + # you can record an "empty" file contains noise from environment + # so sox will identify noise data without do harm to what you need + tfm.noiseprof('noise.wav', 'noise.prof') + # now noise.prof is in your working directory + # it is time to fire up noisered and get noise rid + # amount parameter means how much noise should be remove + # high amount may cause detail losing + tfm.noisered('noise.prof', amount=0.3) + # preview the effect before output + tfm.preview('sing.wav') + # create the output + tfm.build('sing.wav') From e420806fa5f916d09d77664e1a20990110b892da Mon Sep 17 00:00:00 2001 From: Page David Date: Tue, 11 Apr 2017 11:11:46 +0800 Subject: [PATCH 3/8] add description for advanced usage --- docs/example.rst | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/example.rst b/docs/example.rst index eab4e95..d02b993 100644 --- a/docs/example.rst +++ b/docs/example.rst @@ -44,6 +44,10 @@ Combiner Example Advanced Usage ============== +The following functions behave differently than the rest in `Transformer`. +the `build` function is not need to be call after them, instead the result +will be output as soon as they are called. + noiseprof / noisered -------------------- @@ -66,3 +70,20 @@ noiseprof / noisered tfm.preview('sing.wav') # create the output tfm.build('sing.wav') + +power spectrum +-------------- + +.. code-block:: python + :linenos: + + import sox + # create transformer + tfm = sox.Transformer() + # get power spectrum data + # by default, it analyse sound in channel 1 + power = tfm.power_spectrum('talk.wav') + # the result is a list contain lists where [0] is frequency + # and [1] is amplitude + # you can split amplitude data for further analyse / display for gui + amp = [pair[1] for pair in power] From d2324bcdebcd9005e45c7790e08cf7bdf1f8f0e6 Mon Sep 17 00:00:00 2001 From: Page David Date: Tue, 11 Apr 2017 12:12:46 +0800 Subject: [PATCH 4/8] fix output check --- sox/file_info.py | 3 ++- sox/transform.py | 7 ++++--- tests/test_transform.py | 8 ++++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/sox/file_info.py b/sox/file_info.py index a66d9aa..4efa129 100644 --- a/sox/file_info.py +++ b/sox/file_info.py @@ -258,7 +258,8 @@ def validate_output_file(output_filepath): ''' nowrite_conditions = [ - bool(os.path.dirname(output_filepath)), + bool(os.path.dirname(output_filepath)) or\ + not os.access(os.path.join(os.getcwd(), output_filepath)), not os.access(os.path.dirname(output_filepath), os.W_OK)] if all(nowrite_conditions): diff --git a/sox/transform.py b/sox/transform.py index b41ce64..bb57f3d 100644 --- a/sox/transform.py +++ b/sox/transform.py @@ -1877,9 +1877,10 @@ def noiseprof(self, input_filepath, profile_path): if os.path.isdir(profile_path): raise ValueError("profile_path {} is a directory, but filename should be specified.") - if (bool(os.path.dirname(profile_path)) and - not os.access(os.path.dirname(profile_path), os.W_OK)): - raise IOError("profile_path {} is not writeable.".format(profile_path)) + _abs_profile_path = os.path.dirname(profile_path) and\ + profile_path or os.path.join(os.getcwd(), profile_path) + if not os.access(os.path.dirname(_abs_profile_path), os.W_OK): + raise IOError("profile_path {} is not writeable.".format(_abs_profile_path)) effect_args = ['noiseprof', profile_path] self.build(input_filepath, None, extra_args=effect_args) diff --git a/tests/test_transform.py b/tests/test_transform.py index 5110992..9a3ceb9 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -2679,6 +2679,14 @@ def test_noise_prof_invalid_write(self): with self.assertRaises(IOError): tfm.noiseprof(INPUT_FILE, '/usr/noise.prof') + def test_noise_prof_invalid_cwd(self): + tfm = new_transformer() + _cwd = os.getcwd() + os.chdir('/') + with self.assertRaises(IOError): + tfm.noiseprof(INPUT_FILE, 'noise.prof') + os.chdir(_cwd) + class TestTransformerNoisered(unittest.TestCase): From 391b79979b5d0c65b95f384a23abe21b2d7ca0c3 Mon Sep 17 00:00:00 2001 From: Page David Date: Tue, 11 Apr 2017 12:28:52 +0800 Subject: [PATCH 5/8] fix wrong arg for os.access --- sox/file_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sox/file_info.py b/sox/file_info.py index 4efa129..bcf368c 100644 --- a/sox/file_info.py +++ b/sox/file_info.py @@ -259,7 +259,7 @@ def validate_output_file(output_filepath): nowrite_conditions = [ bool(os.path.dirname(output_filepath)) or\ - not os.access(os.path.join(os.getcwd(), output_filepath)), + not os.access(os.getcwd(), os.W_OK), not os.access(os.path.dirname(output_filepath), os.W_OK)] if all(nowrite_conditions): From b11ef8f3ff134797ed8b5cb58f306553b6a73fbc Mon Sep 17 00:00:00 2001 From: Page David Date: Tue, 11 Apr 2017 13:44:19 +0800 Subject: [PATCH 6/8] advance usage in doc --- docs/example.rst | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/docs/example.rst b/docs/example.rst index d02b993..a32dced 100644 --- a/docs/example.rst +++ b/docs/example.rst @@ -46,7 +46,7 @@ Advanced Usage The following functions behave differently than the rest in `Transformer`. the `build` function is not need to be call after them, instead the result -will be output as soon as they are called. +will be output or returned as soon as they are called. noiseprof / noisered -------------------- @@ -87,3 +87,23 @@ power spectrum # and [1] is amplitude # you can split amplitude data for further analyse / display for gui amp = [pair[1] for pair in power] + +stat / stats +------------ + +Both `stat` and `stats` provides some domain statistical information +about an audio. Here we will show how to get these data, for the meaning +of output information, please read `man sox`. + +.. code-block:: python + :linenos: + + import sox + # create transformer + tfm = sox.Transformer() + # get stat data + stat_data = tfm.stat('input.wav') + # now for the stats data + stats_data = tfm.stat('input.wav') + type(stat_data) + > From fcf0e8953cdd8a6d51b092f0b404a2c1bc71eceb Mon Sep 17 00:00:00 2001 From: Page David Date: Tue, 11 Apr 2017 13:53:58 +0800 Subject: [PATCH 7/8] reset unneed comment --- sox/file_info.py | 3 +-- sox/transform.py | 8 +++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/sox/file_info.py b/sox/file_info.py index bcf368c..a66d9aa 100644 --- a/sox/file_info.py +++ b/sox/file_info.py @@ -258,8 +258,7 @@ def validate_output_file(output_filepath): ''' nowrite_conditions = [ - bool(os.path.dirname(output_filepath)) or\ - not os.access(os.getcwd(), os.W_OK), + bool(os.path.dirname(output_filepath)), not os.access(os.path.dirname(output_filepath), os.W_OK)] if all(nowrite_conditions): diff --git a/sox/transform.py b/sox/transform.py index bb57f3d..f286d2e 100644 --- a/sox/transform.py +++ b/sox/transform.py @@ -1875,12 +1875,10 @@ def noiseprof(self, input_filepath, profile_path): ''' if os.path.isdir(profile_path): - raise ValueError("profile_path {} is a directory, but filename should be specified.") + raise ValueError("profile_path {} is a directory, but should be a file") - _abs_profile_path = os.path.dirname(profile_path) and\ - profile_path or os.path.join(os.getcwd(), profile_path) - if not os.access(os.path.dirname(_abs_profile_path), os.W_OK): - raise IOError("profile_path {} is not writeable.".format(_abs_profile_path)) + if not os.access(os.path.dirname(profile_path), os.W_OK): + raise IOError("profile_path {} is not writeable.".format(profile_path)) effect_args = ['noiseprof', profile_path] self.build(input_filepath, None, extra_args=effect_args) From 36ab2c84e4f1299252ca1c2bee8913724d64a39a Mon Sep 17 00:00:00 2001 From: Page David Date: Tue, 11 Apr 2017 13:54:50 +0800 Subject: [PATCH 8/8] reset unneed comment --- tests/test_transform.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/test_transform.py b/tests/test_transform.py index 9a3ceb9..5110992 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -2679,14 +2679,6 @@ def test_noise_prof_invalid_write(self): with self.assertRaises(IOError): tfm.noiseprof(INPUT_FILE, '/usr/noise.prof') - def test_noise_prof_invalid_cwd(self): - tfm = new_transformer() - _cwd = os.getcwd() - os.chdir('/') - with self.assertRaises(IOError): - tfm.noiseprof(INPUT_FILE, 'noise.prof') - os.chdir(_cwd) - class TestTransformerNoisered(unittest.TestCase):