Exemple #1
0
    def test_pts_missing_time_base(self):

        resampler = AudioResampler("s16", "mono", 44100)

        # resample one frame
        iframe = AudioFrame("s16", "stereo", 1024)
        iframe.sample_rate = 48000
        iframe.pts = 0

        oframes = resampler.resample(iframe)
        self.assertEqual(len(oframes), 1)

        oframe = oframes[0]
        self.assertEqual(oframe.pts, 0)
        self.assertEqual(oframe.time_base, Fraction(1, 44100))
        self.assertEqual(oframe.sample_rate, 44100)

        # flush
        oframes = resampler.resample(None)
        self.assertEqual(len(oframes), 1)

        oframe = oframes[0]
        self.assertEqual(oframe.pts, 925)
        self.assertEqual(oframe.time_base, Fraction(1, 44100))
        self.assertEqual(oframe.sample_rate, 44100)
        self.assertEqual(oframe.samples, 16)
Exemple #2
0
    def test_pts_assertion_same_rate(self):

        resampler = AudioResampler("s16", "mono")

        # resample one frame
        iframe = AudioFrame("s16", "stereo", 1024)
        iframe.sample_rate = 48000
        iframe.time_base = "1/48000"
        iframe.pts = 0

        oframes = resampler.resample(iframe)
        self.assertEqual(len(oframes), 1)

        oframe = oframes[0]
        self.assertEqual(oframe.pts, 0)
        self.assertEqual(oframe.time_base, iframe.time_base)
        self.assertEqual(oframe.sample_rate, iframe.sample_rate)
        self.assertEqual(oframe.samples, iframe.samples)

        # resample another frame
        iframe.pts = 1024

        oframes = resampler.resample(iframe)
        self.assertEqual(len(oframes), 1)

        oframe = oframes[0]
        self.assertEqual(oframe.pts, 1024)
        self.assertEqual(oframe.time_base, iframe.time_base)
        self.assertEqual(oframe.sample_rate, iframe.sample_rate)
        self.assertEqual(oframe.samples, iframe.samples)

        # resample another frame with a pts gap, do not raise exception
        iframe.pts = 9999
        oframes = resampler.resample(iframe)
        self.assertEqual(len(oframes), 1)

        oframe = oframes[0]
        self.assertEqual(oframe.pts, 9999)
        self.assertEqual(oframe.time_base, iframe.time_base)
        self.assertEqual(oframe.sample_rate, iframe.sample_rate)
        self.assertEqual(oframe.samples, iframe.samples)

        # flush
        oframes = resampler.resample(None)
        self.assertEqual(len(oframes), 0)
Exemple #3
0
    def test_mismatched_input(self):
        """
        Consecutive frames must have the same layout, sample format and sample rate.
        """
        resampler = AudioResampler("s16", "mono", 44100)

        # resample one frame
        iframe = AudioFrame("s16", "stereo", 1024)
        iframe.sample_rate = 48000
        resampler.resample(iframe)

        # resample another frame with a sample format
        iframe = AudioFrame("s16", "mono", 1024)
        iframe.sample_rate = 48000
        with self.assertRaises(ValueError) as cm:
            resampler.resample(iframe)
        self.assertEqual(str(cm.exception),
                         "Frame does not match AudioResampler setup.")
    def test_identity_passthrough(self):

        # If we don't ask it to do anything, it won't.

        resampler = AudioResampler()

        iframe = AudioFrame('s16', 'stereo', 1024)
        oframe = resampler.resample(iframe)

        self.assertIs(iframe, oframe)
    def test_matching_passthrough(self):

        # If the frames match, it won't do anything.

        resampler = AudioResampler('s16', 'stereo')

        iframe = AudioFrame('s16', 'stereo', 1024)
        oframe = resampler.resample(iframe)

        self.assertIs(iframe, oframe)
    def test_identity_passthrough(self):

        # If we don't ask it to do anything, it won't.

        resampler = AudioResampler()

        iframe = AudioFrame('s16', 'stereo', 1024)
        oframe = resampler.resample(iframe)

        self.assertIs(iframe, oframe)
    def test_matching_passthrough(self):

        # If the frames match, it won't do anything.

        resampler = AudioResampler('s16', 'stereo')

        iframe = AudioFrame('s16', 'stereo', 1024)
        oframe = resampler.resample(iframe)

        self.assertIs(iframe, oframe)
Exemple #8
0
    def test_flush_immediately(self):
        """
        If we flush the resampler before passing any input, it returns
        a `None` frame without setting up the graph.
        """

        resampler = AudioResampler()

        # flush
        oframes = resampler.resample(None)
        self.assertEqual(len(oframes), 0)
    def test_pts_missing_time_base(self):

        resampler = AudioResampler('s16', 'mono', 44100)

        iframe = AudioFrame('s16', 'stereo', 1024)
        iframe.sample_rate = 48000
        iframe.pts = 0

        oframe = resampler.resample(iframe)
        self.assertIs(oframe.pts, None)
        self.assertIs(oframe.time_base, None)
        self.assertEqual(oframe.sample_rate, 44100)
    def test_pts_missing_time_base(self):

        resampler = AudioResampler('s16', 'mono', 44100)

        iframe = AudioFrame('s16', 'stereo', 1024)
        iframe.sample_rate = 48000
        iframe.pts = 0

        oframe = resampler.resample(iframe)
        self.assertIs(oframe.pts, None)
        self.assertIs(oframe.time_base, None)
        self.assertEqual(oframe.sample_rate, 44100)
Exemple #11
0
    def test_pts_assertion_new_rate(self):

        resampler = AudioResampler('s16', 'mono', 44100)

        iframe = AudioFrame('s16', 'stereo', 1024)
        iframe.sample_rate = 48000
        iframe.time_base = '1/48000'
        iframe.pts = 0

        oframe = resampler.resample(iframe)
        self.assertEqual(oframe.pts, 0)
        self.assertEqual(str(oframe.time_base), '1/44100')
        self.assertEqual(oframe.sample_rate, 44100)

        samples_out = resampler.samples_out
        self.assertTrue(samples_out > 0)

        iframe.pts = 1024
        oframe = resampler.resample(iframe)
        self.assertEqual(oframe.pts, samples_out)
        self.assertEqual(str(oframe.time_base), '1/44100')
        self.assertEqual(oframe.sample_rate, 44100)
    def test_pts_assertion_new_rate(self):

        resampler = AudioResampler('s16', 'mono', 44100)

        iframe = AudioFrame('s16', 'stereo', 1024)
        iframe.sample_rate = 48000
        iframe.time_base = '1/48000'
        iframe.pts = 0

        oframe = resampler.resample(iframe)
        self.assertEqual(oframe.pts, 0)
        self.assertEqual(str(oframe.time_base), '1/44100')
        self.assertEqual(oframe.sample_rate, 44100)

        samples_out = resampler.samples_out
        self.assertTrue(samples_out > 0)

        iframe.pts = 1024
        oframe = resampler.resample(iframe)
        self.assertEqual(oframe.pts, samples_out)
        self.assertEqual(str(oframe.time_base), '1/44100')
        self.assertEqual(oframe.sample_rate, 44100)
Exemple #13
0
    def test_pts_assertion_same_rate(self):

        resampler = AudioResampler('s16', 'mono')

        iframe = AudioFrame('s16', 'stereo', 1024)
        iframe.sample_rate = 48000
        iframe.time_base = '1/48000'
        iframe.pts = 0

        oframe = resampler.resample(iframe)

        self.assertEqual(oframe.pts, 0)
        self.assertEqual(oframe.time_base, iframe.time_base)
        self.assertEqual(oframe.sample_rate, iframe.sample_rate)

        iframe.pts = 1024
        oframe = resampler.resample(iframe)

        self.assertEqual(oframe.pts, 1024)
        self.assertEqual(oframe.time_base, iframe.time_base)
        self.assertEqual(oframe.sample_rate, iframe.sample_rate)

        iframe.pts = 9999
        self.assertRaises(ValueError, resampler.resample, iframe)
Exemple #14
0
    def test_identity_passthrough(self):
        """
        If we don't ask it to do anything, it won't.
        """

        resampler = AudioResampler()

        # resample one frame
        iframe = AudioFrame("s16", "stereo", 1024)

        oframes = resampler.resample(iframe)
        self.assertEqual(len(oframes), 1)
        self.assertIs(iframe, oframes[0])

        # resample another frame
        iframe.pts = 1024

        oframes = resampler.resample(iframe)
        self.assertEqual(len(oframes), 1)
        self.assertIs(iframe, oframes[0])

        # flush
        oframes = resampler.resample(None)
        self.assertEqual(len(oframes), 0)
    def test_pts_assertion_same_rate(self):

        resampler = AudioResampler('s16', 'mono')

        iframe = AudioFrame('s16', 'stereo', 1024)
        iframe.sample_rate = 48000
        iframe.time_base = '1/48000'
        iframe.pts = 0

        oframe = resampler.resample(iframe)

        self.assertEqual(oframe.pts, 0)
        self.assertEqual(oframe.time_base, iframe.time_base)
        self.assertEqual(oframe.sample_rate, iframe.sample_rate)

        iframe.pts = 1024
        oframe = resampler.resample(iframe)

        self.assertEqual(oframe.pts, 1024)
        self.assertEqual(oframe.time_base, iframe.time_base)
        self.assertEqual(oframe.sample_rate, iframe.sample_rate)

        iframe.pts = 9999
        self.assertRaises(ValueError, resampler.resample, iframe)
Exemple #16
0
    def test_matching_passthrough(self):
        """
        If the frames match, it won't do anything.
        """

        resampler = AudioResampler("s16", "stereo")

        # resample one frame
        iframe = AudioFrame("s16", "stereo", 1024)

        oframes = resampler.resample(iframe)
        self.assertEqual(len(oframes), 1)
        self.assertIs(iframe, oframes[0])

        # resample another frame
        iframe.pts = 1024

        oframes = resampler.resample(iframe)
        self.assertEqual(len(oframes), 1)
        self.assertIs(iframe, oframes[0])

        # flush
        oframes = resampler.resample(None)
        self.assertEqual(len(oframes), 0)
Exemple #17
0
    def audio_encoding(self, codec_name):

        try:
            codec = Codec(codec_name, "w")
        except UnknownCodecError:
            raise SkipTest()

        ctx = codec.create()
        if ctx.codec.experimental:
            raise SkipTest()

        sample_fmt = ctx.codec.audio_formats[-1].name
        sample_rate = 48000
        channel_layout = "stereo"
        channels = 2

        ctx.time_base = Fraction(1) / sample_rate
        ctx.sample_rate = sample_rate
        ctx.format = sample_fmt
        ctx.layout = channel_layout
        ctx.channels = channels

        ctx.open()

        resampler = AudioResampler(sample_fmt, channel_layout, sample_rate)

        container = av.open(
            fate_suite("audio-reference/chorusnoise_2ch_44kHz_s16.wav"))
        audio_stream = container.streams.audio[0]

        path = self.sandboxed("encoder.%s" % codec_name)

        samples = 0
        packet_sizes = []

        with open(path, "wb") as f:
            for frame in iter_frames(container, audio_stream):

                resampled_frames = resampler.resample(frame)
                for resampled_frame in resampled_frames:
                    samples += resampled_frame.samples

                    for packet in ctx.encode(resampled_frame):
                        packet_sizes.append(packet.size)
                        f.write(packet)

            for packet in ctx.encode(None):
                packet_sizes.append(packet.size)
                f.write(packet)

        ctx = Codec(codec_name, "r").create()
        ctx.sample_rate = sample_rate
        ctx.format = sample_fmt
        ctx.layout = channel_layout
        ctx.channels = channels
        ctx.open()

        result_samples = 0

        # should have more asserts but not sure what to check
        # libav and ffmpeg give different results
        # so can really use checksums
        for frame in iter_raw_frames(path, packet_sizes, ctx):
            result_samples += frame.samples
            self.assertEqual(frame.rate, sample_rate)
            self.assertEqual(len(frame.layout.channels), channels)
Exemple #18
0
    def audio_encoding(self, codec_name):

        try:
            codec = Codec(codec_name, 'w')
        except UnknownCodecError:
            raise SkipTest()

        ctx = codec.create()
        if ctx.codec.experimental:
            raise SkipTest()

        sample_fmt = ctx.codec.audio_formats[-1].name
        sample_rate = 48000
        channel_layout = "stereo"
        channels = 2

        ctx.time_base = Fraction(1) / sample_rate
        ctx.sample_rate = sample_rate
        ctx.format = sample_fmt
        ctx.layout = channel_layout
        ctx.channels = channels

        ctx.open()

        resampler = AudioResampler(sample_fmt, channel_layout, sample_rate)

        container = av.open(
            fate_suite('audio-reference/chorusnoise_2ch_44kHz_s16.wav'))
        audio_stream = container.streams.audio[0]

        path = self.sandboxed('encoder.%s' % codec_name)

        samples = 0
        packet_sizes = []

        with open(path, 'wb') as f:
            for frame in iter_frames(container, audio_stream):

                # We need to let the encoder retime.
                frame.pts = None
                """
                bad_resampler = AudioResampler(sample_fmt, "mono", sample_rate)
                bad_frame = bad_resampler.resample(frame)
                with self.assertRaises(ValueError):
                    next(encoder.encode(bad_frame))

                bad_resampler = AudioResampler(sample_fmt, channel_layout, 3000)
                bad_frame = bad_resampler.resample(frame)

                with self.assertRaises(ValueError):
                    next(encoder.encode(bad_frame))

                bad_resampler = AudioResampler('u8', channel_layout, 3000)
                bad_frame = bad_resampler.resample(frame)

                with self.assertRaises(ValueError):
                    next(encoder.encode(bad_frame))
                """

                resampled_frame = resampler.resample(frame)
                samples += resampled_frame.samples

                for packet in ctx.encode(resampled_frame):
                    # bytearray because python can
                    # freaks out if the first byte is NULL
                    f.write(bytearray(packet))
                    packet_sizes.append(packet.size)

            for packet in ctx.encode(None):
                packet_sizes.append(packet.size)
                f.write(bytearray(packet))

        ctx = Codec(codec_name, 'r').create()
        ctx.time_base = Fraction(1) / sample_rate
        ctx.sample_rate = sample_rate
        ctx.format = sample_fmt
        ctx.layout = channel_layout
        ctx.channels = channels
        ctx.open()

        result_samples = 0

        # should have more asserts but not sure what to check
        # libav and ffmpeg give different results
        # so can really use checksums
        for frame in iter_raw_frames(path, packet_sizes, ctx):
            result_samples += frame.samples
            self.assertEqual(frame.rate, sample_rate)
            self.assertEqual(len(frame.layout.channels), channels)
Exemple #19
0
    def audio_encoding(self, codec_name):

        try:
            codec = Codec(codec_name, 'w')
        except UnknownCodecError:
            raise SkipTest()

        ctx = codec.create()
        if ctx.codec.experimental:
            raise SkipTest()

        sample_fmt = ctx.codec.audio_formats[-1].name
        sample_rate = 48000
        channel_layout = "stereo"
        channels = 2

        ctx.time_base = Fraction(1) / sample_rate
        ctx.sample_rate = sample_rate
        ctx.format = sample_fmt
        ctx.layout = channel_layout
        ctx.channels = channels

        ctx.open()

        resampler = AudioResampler(sample_fmt, channel_layout, sample_rate)

        container = av.open(fate_suite('audio-reference/chorusnoise_2ch_44kHz_s16.wav'))
        audio_stream = container.streams.audio[0]

        path = self.sandboxed('encoder.%s' % codec_name)

        samples = 0
        packet_sizes = []

        with open(path, 'wb') as f:
            for frame in iter_frames(container, audio_stream):

                # We need to let the encoder retime.
                frame.pts = None

                """
                bad_resampler = AudioResampler(sample_fmt, "mono", sample_rate)
                bad_frame = bad_resampler.resample(frame)
                with self.assertRaises(ValueError):
                    next(encoder.encode(bad_frame))

                bad_resampler = AudioResampler(sample_fmt, channel_layout, 3000)
                bad_frame = bad_resampler.resample(frame)

                with self.assertRaises(ValueError):
                    next(encoder.encode(bad_frame))

                bad_resampler = AudioResampler('u8', channel_layout, 3000)
                bad_frame = bad_resampler.resample(frame)

                with self.assertRaises(ValueError):
                    next(encoder.encode(bad_frame))
                """

                resampled_frame = resampler.resample(frame)
                samples += resampled_frame.samples

                for packet in ctx.encode(resampled_frame):
                    # bytearray because python can
                    # freaks out if the first byte is NULL
                    f.write(bytearray(packet))
                    packet_sizes.append(packet.size)

            for packet in ctx.encode(None):
                packet_sizes.append(packet.size)
                f.write(bytearray(packet))

        ctx = Codec(codec_name, 'r').create()
        ctx.time_base = Fraction(1) / sample_rate
        ctx.sample_rate = sample_rate
        ctx.format = sample_fmt
        ctx.layout = channel_layout
        ctx.channels = channels
        ctx.open()

        result_samples = 0

        # should have more asserts but not sure what to check
        # libav and ffmpeg give different results
        # so can really use checksums
        for frame in iter_raw_frames(path, packet_sizes, ctx):
            result_samples += frame.samples
            self.assertEqual(frame.rate, sample_rate)
            self.assertEqual(len(frame.layout.channels), channels)