test_audio_backend.py 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
14
import os
15 16 17
import unittest

import numpy as np
18 19
import soundfile

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
import paddle.audio


class TestAudioBackends(unittest.TestCase):
    def setUp(self):
        self.initParmas()

    def initParmas(self):
        def get_wav_data(dtype: str, num_channels: int, num_frames: int):
            dtype_ = getattr(paddle, dtype)
            base = paddle.linspace(-1.0, 1.0, num_frames, dtype=dtype_) * 0.1
            data = base.tile([num_channels, 1])
            return data

        self.duration = 0.5
        self.num_channels = 1
        self.sr = 16000
        self.dtype = "float32"
        self.window_size = 1024
39 40 41
        waveform_tensor = get_wav_data(
            self.dtype, self.num_channels, num_frames=self.duration * self.sr
        )
42 43 44 45 46 47
        # shape (1, 8000)
        self.waveform = waveform_tensor.numpy()

    def test_backend(self):
        base_dir = os.getcwd()
        wave_wav_path = os.path.join(base_dir, "wave_test.wav")
48 49 50 51 52 53
        paddle.audio.save(
            wave_wav_path,
            paddle.to_tensor(self.waveform),
            self.sr,
            channels_first=True,
        )
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

        # test backends(wave)(wave_backend) info
        wav_info = paddle.audio.info(wave_wav_path)
        self.assertTrue(wav_info.sample_rate, self.sr)
        self.assertTrue(wav_info.num_channels, self.num_channels)
        self.assertTrue(wav_info.bits_per_sample, 16)

        with open(wave_wav_path, 'rb') as file_:
            wav_info = paddle.audio.info(file_)
            self.assertTrue(wav_info.sample_rate, self.sr)
            self.assertTrue(wav_info.num_channels, self.num_channels)
            self.assertTrue(wav_info.bits_per_sample, 16)

        # test backends(wave_backend) load & save
        wav_data, sr = paddle.audio.load(wave_wav_path)
        np.testing.assert_array_almost_equal(wav_data, self.waveform, decimal=4)
        with soundfile.SoundFile(wave_wav_path, "r") as file_:
            dtype = "float32"
            frames = file_._prepare_read(0, None, -1)
            waveform = file_.read(frames, dtype, always_2d=True)
            waveform = waveform.T
            np.testing.assert_array_almost_equal(wav_data, waveform)

        with open(wave_wav_path, 'rb') as file_:
78 79 80
            wav_data, sr = paddle.audio.load(
                file_, normalize=False, num_frames=10000
            )
81 82 83 84 85 86 87 88
        with soundfile.SoundFile(wave_wav_path, "r") as file_:
            dtype = "int16"
            frames = file_._prepare_read(0, None, -1)
            waveform = file_.read(frames, dtype, always_2d=True)
            waveform = waveform.T
            np.testing.assert_array_almost_equal(wav_data, waveform)

        current_backend = paddle.audio.backends.get_current_backend()
Y
YangZhou 已提交
89 90 91
        self.assertTrue(
            current_backend in ["wave_backend", "soundfile", "sox_io"]
        )
92 93 94 95 96

        paddle.audio.backends.set_backend("wave_backend")

        backends = paddle.audio.backends.list_available_backends()
        for backend in backends:
Y
YangZhou 已提交
97
            self.assertTrue(backend in ["wave_backend", "soundfile", "sox_io"])
98 99 100 101 102 103 104 105 106

        # Test error
        try:
            paddle.audio.backends.set_backend("jfiji")
        except NotImplementedError:
            pass

        try:
            import paddleaudio
107

108 109
            backends = paddle.audio.backends.list_available_backends()
            for backend in backends:
Y
YangZhou 已提交
110 111 112
                self.assertTrue(
                    backend in ["wave_backend", "soundfile", "sox_io"]
                )
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
            current_backend = paddle.audio.backends.get_current_backend()
            self.assertTrue(current_backend, "wave_backend")
            paddleaudio.backends.set_audio_backend("soundfile")
            paddle.audio.backends.set_backend("soundfile")
            current_backend = paddle.audio.backends.get_current_backend()
            self.assertTrue(current_backend, "soundfile")
            wav_info = paddle.audio.info(wave_wav_path)
            self.assertTrue(wav_info.sample_rate, self.sr)
            self.assertTrue(wav_info.num_channels, self.num_channels)
            self.assertTrue(wav_info.bits_per_sample, 16)
            paddle.audio.backends.set_backend("wave_backend")
        except ImportError:
            pass

        try:
128 129 130 131 132 133 134
            paddle.audio.save(
                wave_wav_path,
                paddle.to_tensor(self.waveform),
                self.sr,
                bits_per_sample=24,
                channels_first=True,
            )
135 136 137 138
        except ValueError:
            pass

        try:
139 140 141 142 143
            paddle.audio.save(
                wave_wav_path,
                paddle.to_tensor(self.waveform).unsqueeze(0),
                self.sr,
            )
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
        except AssertionError:
            pass

        fake_data = np.array([0, 1, 2, 3, 4, 6], np.float32)
        soundfile.write(wave_wav_path, fake_data, 1, subtype="DOUBLE")
        try:
            wav_info = paddle.audio.info(wave_wav_path)
        except NotImplementedError:
            pass
        try:
            wav_data = paddle.audio.load(wave_wav_path)
        except NotImplementedError:
            pass

        if os.path.exists(wave_wav_path):
            os.remove(wave_wav_path)


if __name__ == '__main__':
    unittest.main()