Пример #1
0
    def test_init_shape_error(self):

        m = 2
        n = 3
        shape = (m, n)
        time_axis, array_axes, power_axis = utils.create_signal_axes(shape)
        
        shapes = [
            (m * n,),
            (m + 1, n),
            (m, n + 1),
            (m, n, 1)
        ]
        
        for shape in shapes:
            
            samples = utils.create_samples(shape)
            
            # In practice the parent of an `ArraySignal` will be either a
            # `MultichannelSignal` or `None`. We use a string here for
            # simplicity.
            args = (samples, 'Signal', 'Parent', time_axis, array_axes,
                    power_axis)
            
            self._assert_raises(ValueError, ArraySignal, *args)
Пример #2
0
    def _test_reader(self, reader, file_path, file_type, num_channels, length,
                     sample_rate, dtype):

        self.assertEqual(reader.file_path, file_path)
        self.assertEqual(reader.file_type, WaveAudioFileType)
        self.assertEqual(reader.num_channels, num_channels)
        self.assertEqual(reader.length, length)
        self.assertEqual(reader.sample_rate, sample_rate)
        self.assertEqual(reader.dtype, dtype)

        expected = utils.create_samples((num_channels, length),
                                        factor=1000,
                                        dtype=dtype)

        # all samples
        samples = reader.read()
        utils.assert_arrays_equal(samples, expected, strict=True)

        # samples from frame 5 on
        samples = reader.read(start_index=5)
        utils.assert_arrays_equal(samples, expected[:, 5:], strict=True)

        # a couple of segments
        for start_index, length in [(0, 5), (5, 5)]:
            samples = reader.read(start_index, length)
            stop_index = start_index + length
            utils.assert_arrays_equal(samples,
                                      expected[:, start_index:stop_index],
                                      strict=True)
Пример #3
0
 def test_init(self):
     
     shapes = [
         (0,),
         (1,),
         (2,),
         (2, 0),
         (2, 1),
         (2, 3),
         (2, 3, 4)
     ]
     
     for shape in shapes:
         
         samples = utils.create_samples(shape)
         
         time_axis, array_axes, amplitude_axis = \
             utils.create_signal_axes(shape)
             
         # In practice the parent of a `ArraySignal` will be either a
         # `MultichannelSignal` or `None`. We use a string here for
         # simplicity.
         args = (samples, 'Signal', 'Parent', time_axis, array_axes,
                 amplitude_axis)
         
         s = ArraySignal(*args)
         
         self.assert_array_signal(s, *(args[1:] + args[:1]))
Пример #4
0
 def _test_reader(
             self, reader, file_path, file_type, num_channels, length,
             sample_rate, dtype):
     
         self.assertEqual(reader.file_path, file_path)
         self.assertEqual(reader.file_type, WaveAudioFileType)
         self.assertEqual(reader.num_channels, num_channels)
         self.assertEqual(reader.length, length)
         self.assertEqual(reader.sample_rate, sample_rate)
         self.assertEqual(reader.dtype, dtype)
         
         expected = utils.create_samples(
             (num_channels, length), factor=1000, dtype=dtype)
         
         # all samples
         samples = reader.read()
         utils.assert_arrays_equal(samples, expected, strict=True)
         
         # samples from frame 5 on
         samples = reader.read(start_index=5)
         utils.assert_arrays_equal(samples, expected[:, 5:], strict=True)
         
         # a couple of segments
         for start_index, length in [(0, 5), (5, 5)]:
             samples = reader.read(start_index, length)
             stop_index = start_index + length
             utils.assert_arrays_equal(
                 samples, expected[:, start_index:stop_index], strict=True)
Пример #5
0
    def test_init(self):

        cases = [
            ('A', 0, 0, (), 'int16'),
            ('B', 2, 0, (2, ), 'float'),
            ('Bobo', 0, 3, (3, 4), '<i2'),
            ('Bibi', 1, 2, (5, 6, 7), '<i4'),
        ]

        frame_rate = 24000

        for name, length, channel_count, array_shape, dtype in cases:

            time_axis = TimeAxis(length, frame_rate)
            shape = (channel_count, length) + array_shape
            samples = utils.create_samples(shape, dtype=dtype)
            sample_provider = _SampleProvider(samples)

            # Test initializer with specified name.

            s = Signal(time_axis, channel_count, array_shape, dtype,
                       sample_provider, name)

            self.assert_signal(s, name, time_axis, channel_count, array_shape,
                               dtype, samples)

            # Test initializer with default name.

            s = Signal(time_axis, channel_count, array_shape, dtype,
                       sample_provider)

            self.assert_signal(s, 'Signal', time_axis, channel_count,
                               array_shape, dtype, samples)
    def test_init(self):
        
        shapes = [
#            (0, 0),
#            (1, 0),
            (1, 2),
            (1, 2, 3),
            (2, 3),
            (2, 3, 4),
            (4, 2, 3)
        ]
        
        for shape in shapes:
            
            samples = utils.create_samples(shape)

            channel_names = tuple(str(i) for i in range(shape[0]))
            
            time_axis, array_axes, amplitude_axis = \
                utils.create_signal_axes(shape[1:])
            
            args = (samples, 'Signal', channel_names, time_axis, array_axes,
                    amplitude_axis)
            
            s = MultichannelArraySignal(*args)
            
            self.assert_multichannel_array_signal(s, *(args[1:] + args[:1]))
Пример #7
0
 def test_create_samples(self):
     
     cases = [
              
         ((), []),
         
         ((0,), []),
         
         ((2,), [0, 1]),
         
         ((2, 0), [[], []]),
         
         ((2, 3, 4), [
             [[0, 1, 2, 3],
              [100, 101, 102, 103],
              [200, 201, 202, 203]],
                              
             [[10000, 10001, 10002, 10003],
              [10100, 10101, 10102, 10103],
              [10200, 10201, 10202, 10203]]
          ])
         
     ]
     
     for shape, expected in cases:
         samples = utils.create_samples(shape)
         expected = np.array(expected)
         utils.assert_arrays_equal(samples, expected)
Пример #8
0
    def test_create_samples(self):

        cases = [((), []), ((0, ), []), ((2, ), [0, 1]), ((2, 0), [[], []]),
                 ((2, 3, 4), [[[0, 1, 2, 3], [100, 101, 102, 103],
                               [200, 201, 202, 203]],
                              [[10000, 10001, 10002, 10003],
                               [10100, 10101, 10102, 10103],
                               [10200, 10201, 10202, 10203]]])]

        for shape, expected in cases:
            samples = utils.create_samples(shape)
            expected = np.array(expected)
            utils.assert_arrays_equal(samples, expected)
Пример #9
0
 def _test_create_multichannel_array_signal(
         self, file_path, num_channels, length, sample_rate, dtype):
     
     audio = audio_file_utils.create_multichannel_array_signal(file_path)
     
     self.assertEqual(len(audio), num_channels)
     self.assertEqual(audio.time_axis.length, length)
     self.assertEqual(audio.time_axis.sample_rate, sample_rate)
     self.assertEqual(audio.dtype, dtype)
     
     expected = utils.create_samples(
         (num_channels, length), factor=1000, dtype=dtype)
     utils.assert_arrays_equal(audio[:], expected, strict=True)
Пример #10
0
    def _test_create_multichannel_array_signal(self, file_path, num_channels,
                                               length, sample_rate, dtype):

        audio = audio_file_utils.create_multichannel_array_signal(file_path)

        self.assertEqual(len(audio), num_channels)
        self.assertEqual(audio.time_axis.length, length)
        self.assertEqual(audio.time_axis.sample_rate, sample_rate)
        self.assertEqual(audio.dtype, dtype)

        expected = utils.create_samples((num_channels, length),
                                        factor=1000,
                                        dtype=dtype)
        utils.assert_arrays_equal(audio[:], expected, strict=True)
Пример #11
0
 def test_create_array_signal(self):
     
     cases = [
         ('One Channel.wav', 100, 22050, np.int16),
     ]
     
     for file_name, length, sample_rate, dtype in cases:
         
         file_path = utils.create_test_audio_file_path(file_name)
         
         audio = audio_file_utils.create_array_signal(file_path)
         
         self.assertIsInstance(audio, ArraySignal)
         self.assertEqual(len(audio), length)
         self.assertEqual(audio.time_axis.sample_rate, sample_rate)
         self.assertEqual(audio.dtype, dtype)
         
         expected = utils.create_samples((length,), factor=1000, dtype=dtype)
         utils.assert_arrays_equal(audio[:], expected, strict=True)
Пример #12
0
 def test_init(self):
     
     names = ['Bobo', None]
     
     shapes = [
         (0, 0),
         (2, 0),
         (0, 2),
         (1, 2),
         (1, 2, 3),
         (2, 3),
         (2, 3, 4),
         (4, 2, 3, 4)
     ]
     
     frame_rates = [24000, 44100]
     
     # We don't test with an int16 dtype since the int16 value range
     # is not large enough to accomodate some test sample values.
     dtypes = ['int32', np.dtype('float')]
     
     cases = itertools.product(names, shapes, frame_rates, dtypes)
     
     for name, shape, frame_rate, dtype in cases:
     
         expected_name = 'Signal' if name is None else name
         time_axis = TimeAxis(shape[0], frame_rate)
         channel_count = shape[1]
         array_shape = shape[2:]
         samples_f = utils.create_samples(shape, dtype=dtype)
         samples_c = np.swapaxes(samples_f, 0, 1)
         
         # Frame rate and frame-first samples.
         s = RamSignal(frame_rate, samples_f, True, name)
         SignalTests.assert_signal(
             s, expected_name, time_axis, channel_count, array_shape,
             dtype, samples_c)
 
         # Time axis and rame-first samples.
         s = RamSignal(time_axis, samples_f, True, name)
         SignalTests.assert_signal(
             s, expected_name, time_axis, channel_count, array_shape,
             dtype, samples_c)
Пример #13
0
    def test_init(self):

        cases = [('Header Only.wav', 0, 1, 24000, '<i2'),
                 ('One Channel.wav', 10, 1, 22050, '<i2'),
                 ('Two Channels.wav', 10, 2, 24000, '<i2')]

        for file_name, frame_count, channel_count, frame_rate, dtype in cases:

            file_path = _DATA_DIR_PATH / file_name
            time_axis = TimeAxis(frame_count, frame_rate)
            shape = (channel_count, frame_count)
            samples = utils.create_samples(shape, dtype='<i2')

            signal = WaveFileSignal(file_path, name=file_name)
            SignalTests.assert_signal(signal, file_name, time_axis,
                                      channel_count, (), dtype, samples)

            signal = WaveFileSignal(file_path)
            SignalTests.assert_signal(signal, 'Signal', time_axis,
                                      channel_count, (), dtype, samples)
Пример #14
0
    def test_create_array_signal(self):

        cases = [
            ('One Channel.wav', 100, 22050, np.int16),
        ]

        for file_name, length, sample_rate, dtype in cases:

            file_path = utils.create_test_audio_file_path(file_name)

            audio = audio_file_utils.create_array_signal(file_path)

            self.assertIsInstance(audio, ArraySignal)
            self.assertEqual(len(audio), length)
            self.assertEqual(audio.time_axis.sample_rate, sample_rate)
            self.assertEqual(audio.dtype, dtype)

            expected = utils.create_samples((length, ),
                                            factor=1000,
                                            dtype=dtype)
            utils.assert_arrays_equal(audio[:], expected, strict=True)
Пример #15
0
    def test_create_multichannel_array_signal(self):

        cases = [('One Channel.wav', 1, 100, 22050, np.int16),
                 ('Two Channels.wav', 2, 10, 24000, np.int16),
                 ('Four Channels.wav', 4, 100, 22050, np.int16)]

        for file_name, num_channels, length, sample_rate, dtype in cases:

            file_path = utils.create_test_audio_file_path(file_name)

            audio = audio_file_utils.create_multichannel_array_signal(
                file_path)

            self.assertIsInstance(audio, MultichannelArraySignal)
            self.assertEqual(len(audio), num_channels)
            self.assertEqual(audio.time_axis.length, length)
            self.assertEqual(audio.time_axis.sample_rate, sample_rate)
            self.assertEqual(audio.dtype, dtype)

            expected = utils.create_samples((num_channels, length),
                                            factor=1000,
                                            dtype=dtype)
            utils.assert_arrays_equal(audio[:], expected, strict=True)
Пример #16
0
 def test_create_multichannel_array_signal(self):
     
     cases = [
         ('One Channel.wav', 1, 100, 22050, np.int16),
         ('Two Channels.wav', 2, 10, 24000, np.int16),
         ('Four Channels.wav', 4, 100, 22050, np.int16)
     ]
     
     for file_name, num_channels, length, sample_rate, dtype in cases:
         
         file_path = utils.create_test_audio_file_path(file_name)
         
         audio = audio_file_utils.create_multichannel_array_signal(file_path)
         
         self.assertIsInstance(audio, MultichannelArraySignal)
         self.assertEqual(len(audio), num_channels)
         self.assertEqual(audio.time_axis.length, length)
         self.assertEqual(audio.time_axis.sample_rate, sample_rate)
         self.assertEqual(audio.dtype, dtype)
         
         expected = utils.create_samples(
             (num_channels, length), factor=1000, dtype=dtype)
         utils.assert_arrays_equal(audio[:], expected, strict=True)
    def test_init_shape_error(self):

        m = 2
        n = 3
        shape = (m, n)
        time_axis, array_axes, power_axis = utils.create_signal_axes(shape)
        
        shapes = [
            (2, m * n,),
            (2, m + 1, n),
            (2, m, n + 1),
            (2, m, n, 1)
        ]
        
        channel_names = ('Left', 'Right')
        
        for shape in shapes:
            
            samples = utils.create_samples(shape)
            
            args = (samples, 'Signal', channel_names, time_axis, array_axes,
                    power_axis)
            
            self._assert_raises(ValueError, MultichannelArraySignal, *args)