Changeset dfe6ab6 for python


Ignore:
Timestamp:
Mar 31, 2019, 9:58:02 PM (6 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/constantq
Children:
9492313a
Parents:
868c6b8 (diff), f55630c (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into feature/constantq

Location:
python
Files:
1 added
2 deleted
33 edited

Legend:

Unmodified
Added
Removed
  • python/demos/demo_wav2midi.py

    r868c6b8 rdfe6ab6  
    6464        if new_note[2] > 0:
    6565            track.append(Message('note_off', note=int(new_note[2]),
    66                 velocity=127, time=0)
     66                velocity=127, time=delta)
    6767                )
    6868        track.append(Message('note_on',
  • python/ext/py-filter.c

    r868c6b8 rdfe6ab6  
    110110{
    111111  Py_XDECREF(self->out);
    112   del_aubio_filter (self->o);
     112  if (self->o)
     113    del_aubio_filter (self->o);
    113114  Py_TYPE(self)->tp_free ((PyObject *) self);
    114115}
  • python/ext/py-sink.c

    r868c6b8 rdfe6ab6  
    151151Py_sink_del (Py_sink *self, PyObject *unused)
    152152{
    153   del_aubio_sink(self->o);
    154   free(self->mwrite_data.data);
     153  if (self->o) {
     154    del_aubio_sink(self->o);
     155    free(self->mwrite_data.data);
     156  }
    155157  if (self->uri) {
    156158    free(self->uri);
  • python/ext/py-source.c

    r868c6b8 rdfe6ab6  
    437437  aubio_source_do (self->o, &(self->c_read_to), &read);
    438438
     439  if (PyErr_Occurred() != NULL) {
     440    return NULL;
     441  }
     442
    439443  outputs = PyTuple_New(2);
    440444  PyTuple_SetItem( outputs, 0, self->read_to );
     
    457461  /* compute _do function */
    458462  aubio_source_do_multi (self->o, &(self->c_mread_to), &read);
     463
     464  if (PyErr_Occurred() != NULL) {
     465    return NULL;
     466  }
    459467
    460468  outputs = PyTuple_New(2);
     
    574582    } else if (PyLong_AsLong(size) > 0) {
    575583      // short read, return a shorter array
    576       PyArrayObject *shortread = (PyArrayObject*)PyTuple_GetItem(done, 0);
     584      PyObject *vec = PyTuple_GetItem(done, 0);
     585      // take a copy to prevent resizing internal arrays
     586      PyArrayObject *shortread = (PyArrayObject*)PyArray_FROM_OTF(vec,
     587          NPY_NOTYPE, NPY_ARRAY_ENSURECOPY);
    577588      PyArray_Dims newdims;
    578589      PyObject *reshaped;
     
    587598      reshaped = PyArray_Newshape(shortread, &newdims, NPY_CORDER);
    588599      Py_DECREF(shortread);
     600      Py_DECREF(vec);
    589601      return reshaped;
    590602    } else {
  • python/lib/moresetuptools.py

    r868c6b8 rdfe6ab6  
    6969    for define_macro in ['HAVE_STDLIB_H', 'HAVE_STDIO_H',
    7070                         'HAVE_MATH_H', 'HAVE_STRING_H',
    71                          'HAVE_C99_VARARGS_MACROS',
     71                         'HAVE_ERRNO_H', 'HAVE_C99_VARARGS_MACROS',
    7272                         'HAVE_LIMITS_H', 'HAVE_STDARG_H',
    7373                         'HAVE_MEMCPY_HACKS']:
  • python/tests/test_aubio.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from unittest import main
    43from numpy.testing import TestCase
    54
     
    1615
    1716if __name__ == '__main__':
     17    from unittest import main
    1818    main()
    19 
  • python/tests/test_aubio_cmd.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
     3from numpy.testing import TestCase
    34import aubio.cmd
    4 from nose2 import main
    5 from numpy.testing import TestCase
    65
    76class aubio_cmd(TestCase):
     
    3231
    3332if __name__ == '__main__':
     33    from unittest import main
    3434    main()
  • python/tests/test_aubio_cut.py

    r868c6b8 rdfe6ab6  
    22
    33import aubio.cut
    4 from nose2 import main
    54from numpy.testing import TestCase
    65
     
    1413
    1514if __name__ == '__main__':
     15    from unittest import main
    1616    main()
  • python/tests/test_cvec.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from unittest import main
    43import numpy as np
    54from numpy.testing import TestCase, assert_equal
     
    142141
    143142if __name__ == '__main__':
     143    from unittest import main
    144144    main()
  • python/tests/test_dct.py

    r868c6b8 rdfe6ab6  
    6767        except AssertionError:
    6868            self.skipTest('creating aubio.dct with size %d did not fail' % size)
     69
     70if __name__ == '__main__':
     71    from unittest import main
     72    main()
  • python/tests/test_fft.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from unittest import main
    43from numpy.testing import TestCase
    54from numpy.testing import assert_equal, assert_almost_equal
     
    213212
    214213if __name__ == '__main__':
     214    from unittest import main
    215215    main()
  • python/tests/test_filter.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from unittest import main
    43from numpy.testing import TestCase, assert_equal, assert_almost_equal
    54from aubio import fvec, digital_filter
    6 from .utils import array_from_text_file
     5from utils import array_from_text_file
    76
    87class aubio_filter_test_case(TestCase):
     
    9594
    9695if __name__ == '__main__':
     96    from unittest import main
    9797    main()
  • python/tests/test_filterbank.py

    r868c6b8 rdfe6ab6  
    55
    66from aubio import cvec, filterbank, float_type
    7 from .utils import array_from_text_file
     7from utils import array_from_text_file
    88
    99class aubio_filterbank_test_case(TestCase):
     
    8888
    8989if __name__ == '__main__':
    90     import nose2
    91     nose2.main()
     90    from unittest import main
     91    main()
  • python/tests/test_filterbank_mel.py

    r868c6b8 rdfe6ab6  
    33import numpy as np
    44from numpy.testing import TestCase, assert_equal, assert_almost_equal
     5from _tools import assert_warns
    56
    67from aubio import fvec, cvec, filterbank, float_type
    7 
    8 import warnings
    9 warnings.filterwarnings('ignore', category=UserWarning, append=True)
    108
    119class aubio_filterbank_mel_test_case(TestCase):
     
    7674        freq_list = [0, samplerate//4, samplerate // 2 + 1]
    7775        f = filterbank(len(freq_list)-2, 1024)
    78         # TODO add assert_warns
    79         f.set_triangle_bands(fvec(freq_list), samplerate)
     76        with assert_warns(UserWarning):
     77            f.set_triangle_bands(fvec(freq_list), samplerate)
    8078
    8179    def test_triangle_freqs_with_not_enough_filters(self):
     
    8482        freq_list = [0, 100, 1000, 4000, 8000, 10000]
    8583        f = filterbank(len(freq_list)-3, 1024)
    86         # TODO add assert_warns
    87         f.set_triangle_bands(fvec(freq_list), samplerate)
     84        with assert_warns(UserWarning):
     85            f.set_triangle_bands(fvec(freq_list), samplerate)
    8886
    8987    def test_triangle_freqs_with_too_many_filters(self):
     
    9290        freq_list = [0, 100, 1000, 4000, 8000, 10000]
    9391        f = filterbank(len(freq_list)-1, 1024)
    94         # TODO add assert_warns
    95         f.set_triangle_bands(fvec(freq_list), samplerate)
     92        with assert_warns(UserWarning):
     93            f.set_triangle_bands(fvec(freq_list), samplerate)
    9694
    9795    def test_triangle_freqs_with_double_value(self):
     
    10098        freq_list = [0, 100, 1000, 4000, 4000, 4000, 10000]
    10199        f = filterbank(len(freq_list)-2, 1024)
    102         # TODO add assert_warns
    103         f.set_triangle_bands(fvec(freq_list), samplerate)
     100        with assert_warns(UserWarning):
     101            f.set_triangle_bands(fvec(freq_list), samplerate)
    104102
    105103    def test_triangle_freqs_with_triple(self):
     
    108106        freq_list = [0, 100, 1000, 4000, 4000, 4000, 10000]
    109107        f = filterbank(len(freq_list)-2, 1024)
    110         # TODO add assert_warns
    111         f.set_triangle_bands(fvec(freq_list), samplerate)
     108        with assert_warns(UserWarning):
     109            f.set_triangle_bands(fvec(freq_list), samplerate)
     110
    112111
    113112    def test_triangle_freqs_without_norm(self):
     
    169168
    170169if __name__ == '__main__':
    171     import nose2
    172     nose2.main()
     170    from unittest import main
     171    main()
  • python/tests/test_fvec.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from unittest import main
    43import numpy as np
    54from numpy.testing import TestCase, assert_equal, assert_almost_equal
     
    149148
    150149if __name__ == '__main__':
     150    from unittest import main
    151151    main()
  • python/tests/test_fvec_shift.py

    r868c6b8 rdfe6ab6  
    3131        self.run_shift_ishift(7)
    3232
    33 from unittest import main
    3433if __name__ == '__main__':
     34    from unittest import main
    3535    main()
  • python/tests/test_hztomel.py

    r868c6b8 rdfe6ab6  
    44from numpy.testing import TestCase
    55from numpy.testing import assert_equal, assert_almost_equal
     6from _tools import assert_warns
    67import numpy as np
    78import aubio
     
    3839
    3940    def test_meltohz_negative(self):
    40         # TODO add assert_warns
    41         assert_equal(meltohz(-1), 0)
     41        with assert_warns(UserWarning):
     42            assert_equal(meltohz(-1), 0)
    4243
    4344    def test_hztomel_negative(self):
    44         # TODO add assert_warns
    45         assert_equal(hztomel(-1), 0)
     45        with assert_warns(UserWarning):
     46            assert_equal(hztomel(-1), 0)
    4647
    4748
     
    5859
    5960    def test_meltohz_negative(self):
    60         # TODO add assert_warns
    61         assert_equal(meltohz(-1, htk=True), 0)
     61        with assert_warns(UserWarning):
     62            assert_equal(meltohz(-1, htk=True), 0)
    6263        assert_almost_equal(meltohz(2000, htk=True), 3428.7, decimal=1)
    6364        assert_almost_equal(meltohz(1000, htk=True), 1000., decimal=1)
    6465
    6566    def test_hztomel_negative(self):
    66         # TODO add assert_warns
    67         assert_equal(hztomel(-1, htk=True), 0)
     67        with assert_warns(UserWarning):
     68            assert_equal(meltohz(-1, htk=True), 0)
     69        with assert_warns(UserWarning):
     70            assert_equal(hztomel(-1, htk=True), 0)
    6871        assert_almost_equal(hztomel(1000, htk=True), 1000., decimal=1)
    6972
  • python/tests/test_mathutils.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from unittest import main
    43from numpy.testing import TestCase, assert_equal
    54from numpy import array, arange, isnan, isinf
     
    102101
    103102if __name__ == '__main__':
     103    from unittest import main
    104104    main()
  • python/tests/test_mfcc.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from nose2 import main
    4 from nose2.tools import params
     3from _tools import parametrize, assert_raises
    54from numpy import random, count_nonzero
    65from numpy.testing import TestCase
     
    1615new_deflts = [1024, 40, 13, 44100]
    1716
    18 class aubio_mfcc(TestCase):
     17class Test_aubio_mfcc(object):
    1918
    20     def setUp(self):
    21         self.o = mfcc()
     19    members_args = 'name'
    2220
    23     def test_default_creation(self):
    24         pass
    25 
    26     def test_delete(self):
    27         del self.o
    28 
    29     @params(*new_params)
     21    @parametrize(members_args, new_params)
    3022    def test_read_only_member(self, name):
    31         o = self.o
    32         with self.assertRaises((TypeError, AttributeError)):
     23        o = mfcc()
     24        with assert_raises((TypeError, AttributeError)):
    3325            setattr(o, name, 0)
    3426
    35     @params(*zip(new_params, new_deflts))
     27    @parametrize('name, expected', zip(new_params, new_deflts))
    3628    def test_default_param(self, name, expected):
    3729        """ test mfcc.{:s} = {:d} """.format(name, expected)
    38         o = self.o
    39         self.assertEqual( getattr(o, name), expected)
     30        o = mfcc()
     31        assert getattr(o, name) == expected
    4032
    4133class aubio_mfcc_wrong_params(TestCase):
     
    8375
    8476
    85 class aubio_mfcc_all_parameters(TestCase):
     77class Test_aubio_mfcc_all_parameters(object):
    8678
    87     @params(
     79    run_values = [
    8880            (2048, 40, 13, 44100),
    8981            (1024, 40, 13, 44100),
     
    10193            (1024, 40, 40, 44100),
    10294            (1024, 40, 3, 44100),
    103             )
     95            ]
     96    run_args = ['buf_size', 'n_filters', 'n_coeffs', 'samplerate']
     97
     98    @parametrize(run_args, run_values)
    10499    def test_run_with_params(self, buf_size, n_filters, n_coeffs, samplerate):
    105100        " check mfcc can run with reasonable parameters "
     
    149144
    150145if __name__ == '__main__':
    151     main()
     146    from _tools import run_module_suite
     147    run_module_suite()
  • python/tests/test_midi2note.py

    r868c6b8 rdfe6ab6  
    33
    44from aubio import midi2note
    5 from nose2.tools import params
    6 import unittest
     5from _tools import parametrize, assert_raises
    76
    87list_of_known_midis = (
     
    1615        )
    1716
    18 class midi2note_good_values(unittest.TestCase):
     17class Test_midi2note_good_values(object):
    1918
    20     @params(*list_of_known_midis)
     19    @parametrize('midi, note', list_of_known_midis)
    2120    def test_midi2note_known_values(self, midi, note):
    2221        " known values are correctly converted "
    23         self.assertEqual ( midi2note(midi), note )
     22        assert midi2note(midi) == (note)
    2423
    25 class midi2note_wrong_values(unittest.TestCase):
     24class Test_midi2note_wrong_values(object):
    2625
    2726    def test_midi2note_negative_value(self):
    2827        " fails when passed a negative value "
    29         self.assertRaises(ValueError, midi2note, -2)
     28        assert_raises(ValueError, midi2note, -2)
    3029
    3130    def test_midi2note_large(self):
    3231        " fails when passed a value greater than 127 "
    33         self.assertRaises(ValueError, midi2note, 128)
     32        assert_raises(ValueError, midi2note, 128)
    3433
    3534    def test_midi2note_floating_value(self):
    3635        " fails when passed a floating point "
    37         self.assertRaises(TypeError, midi2note, 69.2)
     36        assert_raises(TypeError, midi2note, 69.2)
    3837
    3938    def test_midi2note_character_value(self):
    4039        " fails when passed a value that can not be transformed to integer "
    41         self.assertRaises(TypeError, midi2note, "a")
     40        assert_raises(TypeError, midi2note, "a")
    4241
    4342if __name__ == '__main__':
    44     import nose2
    45     nose2.main()
     43    from _tools import run_module_suite
     44    run_module_suite()
  • python/tests/test_musicutils.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from unittest import main
    43import numpy as np
    54from numpy.testing import TestCase
    6 from numpy.testing.utils import assert_equal, assert_almost_equal
     5from numpy.testing import assert_equal, assert_almost_equal
    76from aubio import window, level_lin, db_spl, silence_detection, level_detection
    87from aubio import fvec, float_type
     
    8685
    8786if __name__ == '__main__':
     87    from unittest import main
    8888    main()
  • python/tests/test_note2midi.py

    r868c6b8 rdfe6ab6  
    55
    66from aubio import note2midi, freq2note, note2freq, float_type
    7 from nose2.tools import params
    8 import unittest
     7from numpy.testing import TestCase
     8from _tools import parametrize, assert_raises, skipTest
    99
    1010list_of_known_notes = (
     
    4545        )
    4646
    47 class note2midi_good_values(unittest.TestCase):
     47class Test_note2midi_good_values(object):
    4848
    49     @params(*list_of_known_notes)
     49    @parametrize('note, midi', list_of_known_notes)
    5050    def test_note2midi_known_values(self, note, midi):
    5151        " known values are correctly converted "
    52         self.assertEqual ( note2midi(note), midi )
     52        assert note2midi(note) == midi
    5353
    54     @params(*list_of_known_notes_with_unicode_issues)
     54    @parametrize('note, midi', list_of_known_notes_with_unicode_issues)
    5555    def test_note2midi_known_values_with_unicode_issues(self, note, midi):
    56         " known values are correctly converted, unless decoding is expected to fail"
     56        " difficult values are correctly converted unless expected failure "
    5757        try:
    58             self.assertEqual ( note2midi(note), midi )
     58            assert note2midi(note) == midi
    5959        except UnicodeEncodeError as e:
     60            # platforms with decoding failures include:
     61            # - osx: python <= 2.7.10
     62            # - win: python <= 2.7.12
    6063            import sys
    61             strfmt = "len(u'\\U0001D12A') != 1, excpected decoding failure | {:s} | {:s} {:s}"
    62             strres = strfmt.format(e, sys.platform, sys.version)
    63             # happens with: darwin 2.7.10, windows 2.7.12
     64            strmsg = "len(u'\\U0001D12A') != 1, expected decoding failure"
     65            strmsg += " | upgrade to Python 3 to fix"
     66            strmsg += " | {:s} | {:s} {:s}"
    6467            if len('\U0001D12A') != 1 and sys.version[0] == '2':
    65                 self.skipTest(strres + " | upgrade to Python 3 to fix")
     68                skipTest(strmsg.format(repr(e), sys.platform, sys.version))
    6669            else:
    6770                raise
    6871
    69 class note2midi_wrong_values(unittest.TestCase):
     72class note2midi_wrong_values(TestCase):
    7073
    7174    def test_note2midi_missing_octave(self):
     
    105108        self.assertRaises(ValueError, note2midi, 'CB+-3')
    106109
    107     @params(*list_of_unknown_notes)
     110class Test_note2midi_unknown_values(object):
     111
     112    @parametrize('note', list_of_unknown_notes)
    108113    def test_note2midi_unknown_values(self, note):
    109114        " unknown values throw out an error "
    110         self.assertRaises(ValueError, note2midi, note)
     115        assert_raises(ValueError, note2midi, note)
    111116
    112 class freq2note_simple_test(unittest.TestCase):
     117class freq2note_simple_test(TestCase):
    113118
    114119    def test_freq2note_above(self):
     
    120125        self.assertEqual("A4", freq2note(439))
    121126
    122 class note2freq_simple_test(unittest.TestCase):
     127class note2freq_simple_test(TestCase):
    123128
    124129    def test_note2freq(self):
     
    134139
    135140if __name__ == '__main__':
    136     import nose2
    137     nose2.main()
     141    from _tools import run_module_suite
     142    run_module_suite()
  • python/tests/test_notes.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from unittest import main
    43from numpy.testing import TestCase, assert_equal, assert_almost_equal
    5 from aubio import notes
     4from aubio import notes, source
     5import numpy as np
     6from utils import list_all_sounds
     7
     8list_of_sounds = list_all_sounds('sounds')
    69
    710AUBIO_DEFAULT_NOTES_SILENCE = -70.
     
    5356            self.o.set_release_drop(val)
    5457
    55 from .utils import list_all_sounds
    56 list_of_sounds = list_all_sounds('sounds')
    57 
    5858class aubio_notes_sinewave(TestCase):
    5959
    6060    def analyze_file(self, filepath, samplerate=0):
    61         from aubio import source
    62         import numpy as np
    6361        win_s = 512 # fft size
    6462        hop_s = 256 # hop size
     
    9391
    9492if __name__ == '__main__':
     93    from unittest import main
    9594    main()
  • python/tests/test_onset.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from unittest import main
    43from numpy.testing import TestCase, assert_equal, assert_almost_equal
    54from aubio import onset, fvec
     
    117116
    118117if __name__ == '__main__':
     118    from unittest import main
    119119    main()
  • python/tests/test_phasevoc.py

    r868c6b8 rdfe6ab6  
    22
    33from numpy.testing import TestCase, assert_equal, assert_array_less
     4from _tools import parametrize
    45from aubio import fvec, cvec, pvoc, float_type
    5 from nose2 import main
    6 from nose2.tools import params
    76import numpy as np
    87
     
    1918    return np.random.rand(hop_s).astype(float_type) * 2. - 1.
    2019
    21 class aubio_pvoc_test_case(TestCase):
     20class Test_aubio_pvoc_test_case(object):
    2221    """ pvoc object test case """
    2322
     
    6665            assert_equal ( t, 0.)
    6766
    68     @params(
     67    resynth_noise_args = "hop_s, ratio"
     68    resynth_noise_values = [
    6969            ( 256, 8),
    7070            ( 256, 4),
     
    8888            (8192, 4),
    8989            (8192, 2),
    90             )
     90            ]
     91
     92    @parametrize(resynth_noise_args, resynth_noise_values)
    9193    def test_resynth_steps_noise(self, hop_s, ratio):
    9294        """ check the resynthesis of a random signal is correct """
     
    9496        self.reconstruction(sigin, hop_s, ratio)
    9597
    96     @params(
     98    resynth_sine_args = "samplerate, hop_s, ratio, freq"
     99    resynth_sine_values = [
    97100            (44100,  256, 8,   441),
    98101            (44100,  256, 4,  1203),
     
    109112            (96000, 1024, 8, 47000),
    110113            (96000, 1024, 8,    20),
    111             )
     114            ]
     115
     116    @parametrize(resynth_sine_args, resynth_sine_values)
    112117    def test_resynth_steps_sine(self, samplerate, hop_s, ratio, freq):
    113118        """ check the resynthesis of a sine is correct """
     
    200205
    201206if __name__ == '__main__':
     207    from unittest import main
    202208    main()
    203 
  • python/tests/test_pitch.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from unittest import TestCase, main
    4 from numpy.testing import assert_equal
     3from numpy.testing import TestCase, assert_equal
    54from numpy import sin, arange, mean, median, isnan, pi
    65from aubio import fvec, pitch, freqtomidi, float_type
     
    117116for algo in pitch_algorithms:
    118117    for mode in signal_modes:
    119         test_method = create_test (algo, mode)
    120         test_method.__name__ = 'test_pitch_%s_%d_%d_%dHz_sin_%.0f' % ( algo,
     118        _test_method = create_test (algo, mode)
     119        _test_method.__name__ = 'test_pitch_%s_%d_%d_%dHz_sin_%.0f' % ( algo,
    121120                mode[0], mode[1], mode[2], mode[3] )
    122         setattr (aubio_pitch_Sinusoid, test_method.__name__, test_method)
     121        setattr (aubio_pitch_Sinusoid, _test_method.__name__, _test_method)
    123122
    124123if __name__ == '__main__':
     124    from unittest import main
    125125    main()
  • python/tests/test_sink.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from nose2 import main
    4 from nose2.tools import params
    53from numpy.testing import TestCase
    64from aubio import fvec, source, sink
    7 from .utils import list_all_sounds, get_tmp_sink_path, del_tmp_sink_path
    8 
    9 import warnings
    10 warnings.filterwarnings('ignore', category=UserWarning, append=True)
     5from utils import list_all_sounds, get_tmp_sink_path, del_tmp_sink_path
     6from utils import parse_file_samplerate
     7from _tools import parametrize, skipTest, assert_raises, assert_warns
    118
    129list_of_sounds = list_all_sounds('sounds')
     
    2421            all_params.append((hop_size, samplerate, soundfile))
    2522
    26 class aubio_sink_test_case(TestCase):
    27 
    28     def setUp(self):
    29         if not len(list_of_sounds):
    30             self.skipTest('add some sound files in \'python/tests/sounds\'')
     23class Test_aubio_sink(object):
    3124
    3225    def test_wrong_filename(self):
    33         with self.assertRaises(RuntimeError):
     26        with assert_raises(RuntimeError):
    3427            sink('')
    3528
    3629    def test_wrong_samplerate(self):
    37         with self.assertRaises(RuntimeError):
     30        with assert_raises(RuntimeError):
    3831            sink(get_tmp_sink_path(), -1)
    3932
    4033    def test_wrong_samplerate_too_large(self):
    41         with self.assertRaises(RuntimeError):
     34        with assert_raises(RuntimeError):
    4235            sink(get_tmp_sink_path(), 1536001, 2)
    4336
    4437    def test_wrong_channels(self):
    45         with self.assertRaises(RuntimeError):
     38        with assert_raises(RuntimeError):
    4639            sink(get_tmp_sink_path(), 44100, -1)
    4740
    4841    def test_wrong_channels_too_large(self):
    49         with self.assertRaises(RuntimeError):
     42        with assert_raises(RuntimeError):
    5043            sink(get_tmp_sink_path(), 44100, 202020)
    5144
     
    6760        shutil.rmtree(tmpdir)
    6861
    69     @params(*all_params)
     62    @parametrize('hop_size, samplerate, path', all_params)
    7063    def test_read_and_write(self, hop_size, samplerate, path):
    71 
     64        orig_samplerate = parse_file_samplerate(soundfile)
    7265        try:
    73             f = source(path, samplerate, hop_size)
     66            if orig_samplerate is not None and orig_samplerate < samplerate:
     67                # upsampling should emit a warning
     68                with assert_warns(UserWarning):
     69                    f = source(soundfile, samplerate, hop_size)
     70            else:
     71                f = source(soundfile, samplerate, hop_size)
    7472        except RuntimeError as e:
    75             self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e)))
     73            err_msg = '{:s} (hop_s = {:d}, samplerate = {:d})'
     74            skipTest(err_msg.format(str(e), hop_size, samplerate))
    7675        if samplerate == 0: samplerate = f.samplerate
    7776        sink_path = get_tmp_sink_path()
     
    8584        del_tmp_sink_path(sink_path)
    8685
    87     @params(*all_params)
     86    @parametrize('hop_size, samplerate, path', all_params)
    8887    def test_read_and_write_multi(self, hop_size, samplerate, path):
     88        orig_samplerate = parse_file_samplerate(soundfile)
    8989        try:
    90             f = source(path, samplerate, hop_size)
     90            if orig_samplerate is not None and orig_samplerate < samplerate:
     91                # upsampling should emit a warning
     92                with assert_warns(UserWarning):
     93                    f = source(soundfile, samplerate, hop_size)
     94            else:
     95                f = source(soundfile, samplerate, hop_size)
    9196        except RuntimeError as e:
    92             self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e)))
     97            err_msg = '{:s} (hop_s = {:d}, samplerate = {:d})'
     98            skipTest(err_msg.format(str(e), hop_size, samplerate))
    9399        if samplerate == 0: samplerate = f.samplerate
    94100        sink_path = get_tmp_sink_path()
     
    126132
    127133if __name__ == '__main__':
    128     main()
     134    from _tools import run_module_suite
     135    run_module_suite()
  • python/tests/test_slicing.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from unittest import main
    43from numpy.testing import TestCase, assert_equal
    54from aubio import slice_source_at_stamps
    6 from .utils import count_files_in_directory, get_default_test_sound
    7 from .utils import count_samples_in_directory, count_samples_in_file
     5from utils import count_files_in_directory, get_default_test_sound
     6from utils import count_samples_in_directory, count_samples_in_file
    87
    98import tempfile
     
    168167
    169168if __name__ == '__main__':
     169    from unittest import main
    170170    main()
  • python/tests/test_source.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from nose2 import main
    4 from nose2.tools import params
     3
    54from numpy.testing import TestCase, assert_equal
    65from aubio import source
    7 from .utils import list_all_sounds
    8 
    9 import warnings
    10 warnings.filterwarnings('ignore', category=UserWarning, append=True)
     6from utils import list_all_sounds, parse_file_samplerate
     7import unittest
     8from _tools import assert_raises, assert_equal, assert_warns
     9from _tools import parametrize, skipTest
    1110
    1211list_of_sounds = list_all_sounds('sounds')
     
    1413hop_sizes = [512, 1024, 64]
    1514
    16 path = None
     15default_test_sound = len(list_of_sounds) and list_of_sounds[0] or None
    1716
    1817all_params = []
     
    2221            all_params.append((hop_size, samplerate, soundfile))
    2322
    24 
    25 class aubio_source_test_case_base(TestCase):
     23no_sounds_msg = "no test sounds, add some in 'python/tests/sounds/'!"
     24
     25_debug = False
     26
     27
     28class Test_aubio_source_test_case(TestCase):
    2629
    2730    def setUp(self):
    28         if not len(list_of_sounds):
    29             self.skipTest('add some sound files in \'python/tests/sounds\'')
    30         self.default_test_sound = list_of_sounds[0]
    31 
    32 class aubio_source_test_case(aubio_source_test_case_base):
    33 
    34     @params(*list_of_sounds)
    35     def test_close_file(self, filename):
     31        if not default_test_sound:
     32            skipTest(no_sounds_msg)
     33
     34    def test_close_file(self):
    3635        samplerate = 0 # use native samplerate
    3736        hop_size = 256
    38         f = source(filename, samplerate, hop_size)
    39         f.close()
    40 
    41     @params(*list_of_sounds)
    42     def test_close_file_twice(self, filename):
     37        f = source(default_test_sound, samplerate, hop_size)
     38        f.close()
     39
     40    def test_close_file_twice(self):
    4341        samplerate = 0 # use native samplerate
    4442        hop_size = 256
    45         f = source(filename, samplerate, hop_size)
    46         f.close()
    47         f.close()
    48 
    49 class aubio_source_read_test_case(aubio_source_test_case_base):
     43        f = source(default_test_sound, samplerate, hop_size)
     44        f.close()
     45        f.close()
     46
     47    def test_read_after_close(self):
     48        samplerate = 0 # use native samplerate
     49        hop_size = 256
     50        f = source(default_test_sound, samplerate, hop_size)
     51        read, frames = f()
     52        f.close()
     53        with assert_raises(RuntimeError):
     54            read, frames = f()
     55        with assert_raises(RuntimeError):
     56            read, frames = f.do_multi()
     57
     58
     59class Test_aubio_source_read(object):
    5060
    5161    def read_from_source(self, f):
     
    5767                assert_equal(samples[read:], 0)
    5868                break
    59         #result_str = "read {:.2f}s ({:d} frames in {:d} blocks at {:d}Hz) from {:s}"
    60         #result_params = total_frames / float(f.samplerate), total_frames, total_frames//f.hop_size, f.samplerate, f.uri
    61         #print (result_str.format(*result_params))
     69        if _debug:
     70            result_str = "read {:.2f}s ({:d} frames"
     71            result_str += " in {:d} blocks at {:d}Hz) from {:s}"
     72            result_params = total_frames / float(f.samplerate), total_frames, \
     73                    total_frames//f.hop_size, f.samplerate, f.uri
     74            print (result_str.format(*result_params))
    6275        return total_frames
    6376
    64     @params(*all_params)
     77    @parametrize('hop_size, samplerate, soundfile', all_params)
    6578    def test_samplerate_hopsize(self, hop_size, samplerate, soundfile):
     79        orig_samplerate = parse_file_samplerate(soundfile)
    6680        try:
    67             f = source(soundfile, samplerate, hop_size)
     81            if orig_samplerate is not None and orig_samplerate < samplerate:
     82                # upsampling should emit a warning
     83                with assert_warns(UserWarning):
     84                    f = source(soundfile, samplerate, hop_size)
     85            else:
     86                f = source(soundfile, samplerate, hop_size)
    6887        except RuntimeError as e:
    69             self.skipTest('failed opening with hop_s = {:d}, samplerate = {:d} ({:s})'.format(hop_size, samplerate, str(e)))
     88            err_msg = 'failed opening with hop_s={:d}, samplerate={:d} ({:s})'
     89            skipTest(err_msg.format(hop_size, samplerate, str(e)))
    7090        assert f.samplerate != 0
    7191        read_frames = self.read_from_source(f)
    7292        if 'f_' in soundfile and samplerate == 0:
    7393            import re
    74             f = re.compile('.*_\([0:9]*f\)_.*')
     94            f = re.compile(r'.*_\([0:9]*f\)_.*')
    7595            match_f = re.findall('([0-9]*)f_', soundfile)
    7696            if len(match_f) == 1:
    7797                expected_frames = int(match_f[0])
    78                 self.assertEqual(expected_frames, read_frames)
    79 
    80     @params(*list_of_sounds)
     98                assert_equal(expected_frames, read_frames)
     99
     100    @parametrize('p', list_of_sounds)
    81101    def test_samplerate_none(self, p):
    82102        f = source(p)
     
    84104        self.read_from_source(f)
    85105
    86     @params(*list_of_sounds)
     106    @parametrize('p', list_of_sounds)
    87107    def test_samplerate_0(self, p):
    88108        f = source(p, 0)
     
    90110        self.read_from_source(f)
    91111
    92     @params(*list_of_sounds)
     112    @parametrize('p', list_of_sounds)
    93113    def test_zero_hop_size(self, p):
    94114        f = source(p, 0, 0)
     
    97117        self.read_from_source(f)
    98118
    99     @params(*list_of_sounds)
     119    @parametrize('p', list_of_sounds)
    100120    def test_seek_to_half(self, p):
    101121        from random import randint
     
    109129        assert a == b + c
    110130
    111     @params(*list_of_sounds)
     131    @parametrize('p', list_of_sounds)
    112132    def test_duration(self, p):
    113133        total_frames = 0
     
    118138            total_frames += read
    119139            if read < f.hop_size: break
    120         self.assertEqual(duration, total_frames)
    121 
    122 
    123 class aubio_source_test_wrong_params(TestCase):
     140        assert_equal (duration, total_frames)
     141
     142
     143class Test_aubio_source_wrong_params(object):
    124144
    125145    def test_wrong_file(self):
    126         with self.assertRaises(RuntimeError):
     146        with assert_raises(RuntimeError):
    127147            source('path_to/unexisting file.mp3')
    128148
    129 class aubio_source_test_wrong_params_with_file(aubio_source_test_case_base):
     149@unittest.skipIf(default_test_sound is None, no_sounds_msg)
     150class Test_aubio_source_wrong_params_with_file(TestCase):
    130151
    131152    def test_wrong_samplerate(self):
    132         with self.assertRaises(ValueError):
    133             source(self.default_test_sound, -1)
     153        with assert_raises(ValueError):
     154            source(default_test_sound, -1)
    134155
    135156    def test_wrong_hop_size(self):
    136         with self.assertRaises(ValueError):
    137             source(self.default_test_sound, 0, -1)
     157        with assert_raises(ValueError):
     158            source(default_test_sound, 0, -1)
    138159
    139160    def test_wrong_channels(self):
    140         with self.assertRaises(ValueError):
    141             source(self.default_test_sound, 0, 0, -1)
     161        with assert_raises(ValueError):
     162            source(default_test_sound, 0, 0, -1)
    142163
    143164    def test_wrong_seek(self):
    144         f = source(self.default_test_sound)
    145         with self.assertRaises(ValueError):
     165        f = source(default_test_sound)
     166        with assert_raises(ValueError):
    146167            f.seek(-1)
    147168
    148169    def test_wrong_seek_too_large(self):
    149         f = source(self.default_test_sound)
     170        f = source(default_test_sound)
    150171        try:
    151             with self.assertRaises(ValueError):
     172            with assert_raises(ValueError):
    152173                f.seek(f.duration + f.samplerate * 10)
    153         except AssertionError:
    154             self.skipTest('seeking after end of stream failed raising ValueError')
    155 
    156 class aubio_source_readmulti_test_case(aubio_source_read_test_case):
     174        except:
     175            skipTest('seeking after end of stream failed raising ValueError')
     176
     177class Test_aubio_source_readmulti(Test_aubio_source_read):
    157178
    158179    def read_from_source(self, f):
     
    164185                assert_equal(samples[:,read:], 0)
    165186                break
    166         #result_str = "read {:.2f}s ({:d} frames in {:d} channels and {:d} blocks at {:d}Hz) from {:s}"
    167         #result_params = total_frames / float(f.samplerate), total_frames, f.channels, int(total_frames/f.hop_size), f.samplerate, f.uri
    168         #print (result_str.format(*result_params))
     187        if _debug:
     188            result_str = "read {:.2f}s ({:d} frames in {:d} channels"
     189            result_str += " and {:d} blocks at {:d}Hz) from {:s}"
     190            result_params = total_frames / float(f.samplerate), total_frames, \
     191                    f.channels, int(total_frames/f.hop_size), \
     192                    f.samplerate, f.uri
     193            print (result_str.format(*result_params))
    169194        return total_frames
    170195
    171 class aubio_source_with(aubio_source_test_case_base):
    172 
    173     #@params(*list_of_sounds)
    174     @params(*list_of_sounds)
     196class Test_aubio_source_with(object):
     197
     198    @parametrize('filename', list_of_sounds)
    175199    def test_read_from_mono(self, filename):
    176200        total_frames = 0
     
    186210
    187211if __name__ == '__main__':
    188     main()
     212    from _tools import run_module_suite
     213    run_module_suite()
  • python/tests/test_source_channels.py

    r868c6b8 rdfe6ab6  
    99import numpy as np
    1010from numpy.testing import assert_equal
    11 from .utils import get_tmp_sink_path
     11from utils import get_tmp_sink_path
    1212
    1313class aubio_source_test_case(unittest.TestCase):
  • python/tests/test_specdesc.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from unittest import main
    43from numpy.testing import TestCase, assert_equal, assert_almost_equal
    54from numpy import random, arange, log, zeros
     
    230229
    231230if __name__ == '__main__':
     231    from unittest import main
    232232    main()
  • python/tests/test_zero_crossing_rate.py

    r868c6b8 rdfe6ab6  
    11#! /usr/bin/env python
    22
    3 from unittest import main
    43from numpy.testing import TestCase
    54from aubio import fvec, zero_crossing_rate
     
    4443
    4544if __name__ == '__main__':
     45    from unittest import main
    4646    main()
  • python/tests/utils.py

    r868c6b8 rdfe6ab6  
    22
    33import os
     4import re
    45import glob
    56import numpy as np
     
    910
    1011def array_from_text_file(filename, dtype = 'float'):
    11     filename = os.path.join(os.path.dirname(__file__), filename)
    12     with open(filename) as f:
    13         lines = f.readlines()
    14     return np.array([line.split() for line in lines],
    15             dtype = dtype)
     12    realpathname = os.path.join(os.path.dirname(__file__), filename)
     13    return np.loadtxt(realpathname, dtype = dtype)
    1614
    1715def list_all_sounds(rel_dir):
     
    3937        os.unlink(path)
    4038    except WindowsError as e:
    41         print("deleting {:s} failed ({:s}), reopening".format(path, repr(e)))
    42         with open(path, 'wb') as f:
    43             f.close()
    44         try:
    45             os.unlink(path)
    46         except WindowsError as f:
    47             print("deleting {:s} failed ({:s}), aborting".format(path, repr(e)))
     39        # removing the temporary directory sometimes fails on windows
     40        import warnings
     41        errmsg = "failed deleting temporary file {:s} ({:s})"
     42        warnings.warn(UserWarning(errmsg.format(path, repr(e))))
    4843
    4944def array_from_yaml_file(filename):
     
    8479                    total_files += 1
    8580    return total_files
     81
     82def parse_file_samplerate(soundfile):
     83    samplerate = None
     84    # parse samplerate
     85    re_sr = re.compile(r'/([0-9]{4,})Hz_.*')
     86    match_samplerate = re_sr.findall(soundfile)
     87    if match_samplerate:
     88        samplerate = int(match_samplerate[0])
     89    else:
     90        import warnings
     91        warnings.warn(UserWarning("could not parse samplerate for {:s}"
     92            .format(soundfile)))
     93    return samplerate
Note: See TracChangeset for help on using the changeset viewer.