1# Copyright 2021 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15""" 16Testing fade op in DE 17""" 18import numpy as np 19 20import mindspore.dataset as ds 21import mindspore.dataset.audio.transforms as audio 22from mindspore.dataset.audio.utils import FadeShape 23from mindspore import log as logger 24 25 26def test_fade_linear(): 27 """ 28 Test Fade, fade shape is linear. 29 """ 30 logger.info("test fade, fade shape is 'linear'") 31 32 waveform = [[[9.1553e-05, 6.1035e-05, 6.1035e-05, 6.1035e-05, 1.2207e-04, 1.2207e-04, 33 9.1553e-05, 9.1553e-05, 9.1553e-05, 9.1553e-05, 9.1553e-05, 6.1035e-05, 34 1.2207e-04, 1.2207e-04, 1.2207e-04, 9.1553e-05, 9.1553e-05, 9.1553e-05, 35 6.1035e-05, 9.1553e-05]]] 36 dataset = ds.NumpySlicesDataset(data=waveform, column_names='audio', shuffle=False) 37 transforms = [audio.Fade(fade_in_len=10, fade_out_len=5, fade_shape=FadeShape.LINEAR)] 38 dataset = dataset.map(operations=transforms, input_columns=["audio"]) 39 40 for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True): 41 out_put = item["audio"] 42 # The result of the reference operator 43 expected_output = np.array([[0.0000000000000000000, 6.781666797905927e-06, 1.356333359581185e-05, 44 2.034499993897043e-05, 5.425333438324742e-05, 6.781666888855398e-05, 45 6.103533087298274e-05, 7.120789086911827e-05, 8.138045086525380e-05, 46 9.155300358543172e-05, 9.155300358543172e-05, 6.103499981691129e-05, 47 0.0001220699996338225, 0.0001220699996338225, 0.0001220699996338225, 48 9.155300358543172e-05, 6.866475450806320e-05, 4.577650179271586e-05, 49 1.525874995422782e-05, 0.0000000000000000000]], dtype=np.float32) 50 assert np.mean(out_put - expected_output) < 0.0001 51 52 53def test_fade_exponential(): 54 """ 55 Test Fade, fade shape is exponential. 56 """ 57 logger.info("test fade, fade shape is 'exponential'") 58 59 waveform = [[[1, 2, 3, 4, 5, 6], 60 [5, 7, 3, 78, 8, 4]]] 61 dataset = ds.NumpySlicesDataset(data=waveform, column_names='audio', shuffle=False) 62 transforms = [audio.Fade(fade_in_len=5, fade_out_len=6, fade_shape=FadeShape.EXPONENTIAL)] 63 dataset = dataset.map(operations=transforms, input_columns=["audio"]) 64 65 for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True): 66 out_put = item["audio"] 67 # The result of the reference operator 68 expected_output = np.array([[0.0000, 0.2071, 0.4823, 0.6657, 0.5743, 0.0000], 69 [0.0000, 0.7247, 0.4823, 12.9820, 0.9190, 0.0000]], dtype=np.float32) 70 assert np.mean(out_put - expected_output) < 0.0001 71 72 73def test_fade_logarithmic(): 74 """ 75 Test Fade, fade shape is logarithmic. 76 """ 77 logger.info("test fade, fade shape is 'logarithmic'") 78 79 waveform = [[[0.03424072265625, 0.01476832226565, 0.04995727590625, 80 -0.0205993652375, -0.0356467868775, 0.01290893546875]]] 81 dataset = ds.NumpySlicesDataset(data=waveform, column_names='audio', shuffle=False) 82 transforms = [audio.Fade(fade_in_len=4, fade_out_len=2, fade_shape=FadeShape.LOGARITHMIC)] 83 dataset = dataset.map(operations=transforms, input_columns=["audio"]) 84 85 for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True): 86 out_put = item["audio"] 87 # The result of the reference operator 88 expected_output = np.array([[0.0000e+00, 9.4048e-03, 4.4193e-02, 89 -2.0599e-02, -3.5647e-02, 1.5389e-09]], 90 dtype=np.float32) 91 assert np.mean(out_put - expected_output) < 0.0001 92 93 94def test_fade_quarter_sine(): 95 """ 96 Test Fade, fade shape is quarter_sine. 97 """ 98 logger.info("test fade, fade shape is 'quarter sine'") 99 100 waveform = np.array([[[1, 2, 3, 4, 5, 6], 101 [5, 7, 3, 78, 8, 4], 102 [1, 2, 3, 4, 5, 6]]], dtype=np.float64) 103 dataset = ds.NumpySlicesDataset(data=waveform, column_names='audio', shuffle=False) 104 transforms = [audio.Fade(fade_in_len=6, fade_out_len=6, fade_shape=FadeShape.QUARTERSINE)] 105 dataset = dataset.map(operations=transforms, input_columns=["audio"]) 106 107 for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True): 108 out_put = item["audio"] 109 # The result of the reference operator 110 expected_output = np.array([[0.0000, 0.5878, 1.4266, 1.9021, 1.4695, 0.0000], 111 [0.0000, 2.0572, 1.4266, 37.091, 2.3511, 0.0000], 112 [0.0000, 0.5878, 1.4266, 1.9021, 1.4695, 0.0000]], dtype=np.float64) 113 assert np.mean(out_put - expected_output) < 0.0001 114 115 116def test_fade_half_sine(): 117 """ 118 Test Fade, fade shape is half_sine. 119 """ 120 logger.info("test fade, fade shape is 'half sine'") 121 122 waveform = [[[0.03424072265625, 0.013580322265625, -0.011871337890625, 123 -0.0205993652343, -0.01049804687500, 0.0129089355468750], 124 [0.04125976562500, 0.060577392578125, 0.0499572753906250, 125 0.01306152343750, -0.019683837890625, -0.018829345703125]]] 126 dataset = ds.NumpySlicesDataset(data=waveform, column_names='audio', shuffle=False) 127 transforms = [audio.Fade(fade_in_len=3, fade_out_len=3, fade_shape=FadeShape.HALFSINE)] 128 dataset = dataset.map(operations=transforms, input_columns=["audio"]) 129 130 for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True): 131 out_put = item["audio"] 132 # The result of the reference operator 133 expected_output = np.array([[0.0000, 0.0068, -0.0119, -0.0206, -0.0052, 0.0000], 134 [0.0000, 0.0303, 0.0500, 0.0131, -0.0098, -0.0000]], dtype=np.float32) 135 assert np.mean(out_put - expected_output) < 0.0001 136 137 138def test_fade_wrong_arguments(): 139 """ 140 Test Fade with invalid arguments 141 """ 142 logger.info("test fade with invalid arguments") 143 try: 144 _ = audio.Fade(-1, 0) 145 except ValueError as e: 146 logger.info("Got an exception in Fade: {}".format(str(e))) 147 assert "fade_in_len is not within the required interval of [0, 2147483647]" in str(e) 148 try: 149 _ = audio.Fade(0, -1) 150 except ValueError as e: 151 logger.info("Got an exception in Fade: {}".format(str(e))) 152 assert "fade_out_len is not within the required interval of [0, 2147483647]" in str(e) 153 try: 154 _ = audio.Fade(fade_shape='123') 155 except TypeError as e: 156 logger.info("Got an exception in Fade: {}".format(str(e))) 157 assert "is not of type [<enum 'FadeShape'>]" in str(e) 158 159 160def test_fade_eager(): 161 """ 162 Test Fade eager. 163 """ 164 logger.info("test fade eager") 165 166 data = np.array([[9.1553e-05, 6.1035e-05, 6.1035e-05, 6.1035e-05, 1.2207e-04, 1.2207e-04, 167 9.1553e-05, 9.1553e-05, 9.1553e-05, 9.1553e-05, 9.1553e-05, 6.1035e-05, 168 1.2207e-04, 1.2207e-04, 1.2207e-04, 9.1553e-05, 9.1553e-05, 9.1553e-05, 169 6.1035e-05, 9.1553e-05]]).astype(np.float32) 170 expected_output = np.array([0.0000000000000000000, 6.781666797905927e-06, 1.356333359581185e-05, 171 2.034499993897043e-05, 5.425333438324742e-05, 6.781666888855398e-05, 172 6.103533087298274e-05, 7.120789086911827e-05, 8.138045086525380e-05, 173 9.155300358543172e-05, 9.155300358543172e-05, 6.103499981691129e-05, 174 0.0001220699996338225, 0.0001220699996338225, 0.0001220699996338225, 175 9.155300358543172e-05, 6.866475450806320e-05, 4.577650179271586e-05, 176 1.525874995422782e-05, 0.0000000000000000000], dtype=np.float32) 177 fade = audio.Fade(10, 5, fade_shape=FadeShape.LINEAR) 178 out_put = fade(data) 179 assert np.mean(out_put - expected_output) < 0.0001 180 181 182if __name__ == '__main__': 183 test_fade_linear() 184 test_fade_exponential() 185 test_fade_logarithmic() 186 test_fade_quarter_sine() 187 test_fade_half_sine() 188 test_fade_wrong_arguments() 189 test_fade_eager() 190