예제 #1
0
    def run(self, channels=None, samplerate=None, blocksize=None, stack=None):
        """Setup/reset all processors in cascade and stream audio data along
        the pipe. Also returns the pipe itself."""

        source = self.processors[0]
        items = self.processors[1:]
        source.setup(channels=channels, samplerate=samplerate,
                     blocksize=blocksize)

        if stack is None:
                self.stack = False
        else:
            self.stack = stack

        if self.stack:
            self.frames_stack = []

        last = source

        # setup/reset processors and configure properties throughout the pipe
        for item in items:
            item.source_mediainfo = source.mediainfo()
            item.setup(channels=last.channels(),
                       samplerate=last.samplerate(),
                       blocksize=last.blocksize(),
                       totalframes=last.totalframes())
            last = item

        # now stream audio data along the pipe
        eod = False
        while not eod:
            frames, eod = source.process()
            if self.stack:
                self.frames_stack.append(frames)
            for item in items:
                frames, eod = item.process(frames, eod)

        # Post-processing
        for item in items:
            item.post_process()

        # Release processors
        if self.stack:
            if not isinstance(self.frames_stack, numpy.ndarray):
                self.frames_stack = numpy.vstack(self.frames_stack)
            from timeside.decoder.core import ArrayDecoder
            new_source = ArrayDecoder(samples=self.frames_stack,
                                      samplerate=source.samplerate())
            new_source.setup(channels=source.channels(),
                             samplerate=source.samplerate(),
                             blocksize=source.blocksize())
            self.processors[0] = new_source

        for item in items:
            item.release()
            self.processors.remove(item)
예제 #2
0
    def tearDown(self):
        decoder = ArrayDecoder(samples=self.source,
                               samplerate=self.source_samplerate,
                               start=self.start,
                               duration=self.duration)

        decoder.setup(samplerate=self.samplerate, channels=self.channels,
                      blocksize=self.blocksize)


        # Check input
        self.assertEqual(self.source_samplerate, decoder.input_samplerate)
        self.assertEqual(self.expected_is_segment, decoder.is_segment)
        self.assertEqual(self.expected_duration, decoder.input_duration)
        self.assertEqual(self.source_channels, decoder.input_channels)
        # Check output
        self.assertEqual(self.source_samplerate, decoder.samplerate())
        self.assertEqual(self.source_channels, decoder.channels())

        # Check Idecoder interface
        self.assertIsInstance(decoder.mediainfo(), dict)
        self.assertIsInstance(decoder.format(), str)
        self.assertIsInstance(decoder.encoding(), str)
        self.assertIsInstance(decoder.resolution(), int)
        self.assertIsNone(decoder.metadata())


        totalframes = 0

        while True:
            frames, eod = decoder.process()
            totalframes += frames.shape[0]
            if eod:
                break
            self.assertEqual(frames.shape[0], decoder.blocksize())
            self.assertEqual(frames.shape[1], decoder.channels())

        if self.channels:
            # when specified, check that the channels are the ones requested
            self.assertEqual(self.channels, decoder.output_channels)
        else:
            # otherwise check that the channels are preserved, if not specified
            self.assertEqual(decoder.input_channels, decoder.output_channels)
            # and if we know the expected channels, check the output match
            if self.source_channels:
                self.assertEqual(
                    self.source_channels, decoder.output_channels)
        # do the same with the sampling rate
        if self.samplerate:
            self.assertEqual(self.samplerate, decoder.output_samplerate)
        else:
            self.assertEqual(
                decoder.input_samplerate, decoder.output_samplerate)


        self.assertEqual(totalframes, self.expected_duration * decoder.output_samplerate)
        self.assertEquals(totalframes, decoder.totalframes())