You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_flanger.py 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. import numpy as np
  16. import pytest
  17. import mindspore.dataset as ds
  18. import mindspore.dataset.audio.transforms as audio
  19. from mindspore import log as logger
  20. from mindspore.dataset.audio.utils import Modulation, Interpolation
  21. def count_unequal_element(data_expected, data_me, rtol, atol):
  22. assert data_expected.shape == data_me.shape
  23. total_count = len(data_expected.flatten())
  24. error = np.abs(data_expected - data_me)
  25. greater = np.greater(error, atol + np.abs(data_expected) * rtol)
  26. loss_count = np.count_nonzero(greater)
  27. assert (loss_count / total_count) < rtol, "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}".format(
  28. data_expected[greater], data_me[greater], error[greater])
  29. def test_flanger_eager_sinusoidal_linear_float64():
  30. """ mindspore eager mode normal testcase:flanger op"""
  31. # Original waveform
  32. waveform = np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float64)
  33. # Expect waveform
  34. expect_waveform = np.array([[0.10000000000, 0.19999999536, 0.29999998145],
  35. [0.23391812865, 0.29239766081, 0.35087719298]], dtype=np.float64)
  36. flanger_op = audio.Flanger(44100, 0.0, 2.0, 0.0, 71.0, 0.5, 25.0, Modulation.SINUSOIDAL, Interpolation.LINEAR)
  37. # Filtered waveform by flanger
  38. output = flanger_op(waveform)
  39. count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
  40. def test_flanger_eager_triangular_linear_float32():
  41. """ mindspore eager mode normal testcase:flanger op"""
  42. # Original waveform
  43. waveform = np.array([[-1.2, 2, -3.6], [1, 2.4, 3.7]], dtype=np.float32)
  44. # Expect waveform
  45. expect_waveform = np.array([[-1.0000000000, 1.0000000000, -1.0000000000],
  46. [0.58479529619, 1.0000000000, 1.0000000000]], dtype=np.float32)
  47. flanger_op = audio.Flanger(44100, 0.0, 2.0, 0.0, 71.0, 0.5, 25.0, Modulation.TRIANGULAR, Interpolation.LINEAR)
  48. # Filtered waveform by flanger
  49. output = flanger_op(waveform)
  50. count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
  51. def test_flanger_eager_triangular_linear_int():
  52. """ mindspore eager mode normal testcase:flanger op"""
  53. # Original waveform
  54. waveform = np.array([[-2, -3, 0], [2, 2, 3]], dtype=np.int)
  55. # Expect waveform
  56. expect_waveform = np.array([[-1, -1, 0],
  57. [1, 1, 1]], dtype=np.int)
  58. flanger_op = audio.Flanger(44100, 0.0, 2.0, 0.0, 71.0, 0.5, 25.0, Modulation.TRIANGULAR, Interpolation.LINEAR)
  59. # Filtered waveform by flanger
  60. output = flanger_op(waveform)
  61. count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
  62. def test_flanger_shape_221():
  63. """ mindspore eager mode normal testcase:flanger op"""
  64. # Original waveform
  65. waveform = np.array([[[1], [1.1]], [[0.9], [0.6]]], dtype=np.float64)
  66. # Expect waveform
  67. expect_waveform = np.array([[[1.00000000],
  68. [0.64327485]],
  69. [[0.90000000],
  70. [0.35087719]]], dtype=np.float64)
  71. flanger_op = audio.Flanger(44100)
  72. # Filtered waveform by flanger
  73. output = flanger_op(waveform)
  74. count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
  75. def test_flanger_shape_11211():
  76. """ mindspore eager mode normal testcase:flanger op"""
  77. # Original waveform
  78. waveform = np.array([[[[[0.44]], [[0.55]]]]], dtype=np.float64)
  79. # Expect waveform
  80. expect_waveform = np.array([[[[[0.44000000]], [[0.55000000]]]]], dtype=np.float64)
  81. flanger_op = audio.Flanger(44100)
  82. # Filtered waveform by flanger
  83. output = flanger_op(waveform)
  84. count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
  85. def test_flanger_pipeline():
  86. """ mindspore pipeline mode normal testcase:flanger op"""
  87. # Original waveform
  88. waveform = np.array([[[1.1, 1.2, 1.3], [1.4, 1.5, 1.6]]], dtype=np.float64)
  89. # Expect waveform
  90. expect_waveform = np.array([[[1.00000000000, 1.00000000000, 1.00000000000],
  91. [0.81871345029, 0.87719298245, 0.93567251461]]], dtype=np.float64)
  92. data = (waveform, np.random.sample((1, 2, 1)))
  93. dataset = ds.NumpySlicesDataset(data, ["channel", "sample"], shuffle=False)
  94. flanger_op = audio.Flanger(44100)
  95. # Filtered waveform by flanger
  96. dataset = dataset.map(
  97. input_columns=["channel"], operations=flanger_op, num_parallel_workers=1)
  98. i = 0
  99. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  100. count_unequal_element(expect_waveform[i, :],
  101. item['channel'], 0.0001, 0.0001)
  102. i += 1
  103. def test_invalid_flanger_input():
  104. def test_invalid_input(test_name, sample_rate, delay, depth, regen, width, speed, phase, modulation, interpolation,
  105. error, error_msg):
  106. logger.info("Test Flanger with bad input: {0}".format(test_name))
  107. with pytest.raises(error) as error_info:
  108. audio.Flanger(sample_rate, delay, depth, regen, width, speed, phase, modulation, interpolation)
  109. assert error_msg in str(error_info.value)
  110. test_invalid_input("invalid sample_rate parameter value", 0, 0.0, 2.0, 0.0, 71.0, 0.5, 25.0,
  111. Modulation.SINUSOIDAL, Interpolation.LINEAR, ValueError,
  112. "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].")
  113. test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, 0.0, 2.0, 0.0, 71.0, 0.5, 25.0,
  114. Modulation.SINUSOIDAL, Interpolation.LINEAR, TypeError,
  115. "Argument sample_rate with value 44100.5 is not of "
  116. "type [<class 'int'>], but got <class 'float'>.")
  117. test_invalid_input("invalid sample_rate parameter type as a String", "44100", 0.0, 2.0, 0.0, 71.0, 0.5, 25.0,
  118. Modulation.SINUSOIDAL, Interpolation.LINEAR, TypeError,
  119. "Argument sample_rate with value 44100 is not of "
  120. "type [<class 'int'>], but got <class 'str'>.")
  121. test_invalid_input("invalid delay parameter type as a String", 44100, "0.0", 2.0, 0.0, 71.0, 0.5, 25.0,
  122. Modulation.SINUSOIDAL, Interpolation.LINEAR, TypeError,
  123. "Argument delay with value 0.0 is not of type [<class 'float'>, <class 'int'>],"
  124. " but got <class 'str'>.")
  125. test_invalid_input("invalid delay parameter value", 44100, 50, 2.0, 0.0, 71.0, 0.5, 25.0,
  126. Modulation.SINUSOIDAL, Interpolation.LINEAR, ValueError,
  127. "Input delay is not within the required interval of [0, 30].")
  128. test_invalid_input("invalid depth parameter type as a String", 44100, 0.0, "2.0", 0.0, 71.0, 0.5, 25.0,
  129. Modulation.SINUSOIDAL, Interpolation.LINEAR, TypeError,
  130. "Argument depth with value 2.0 is not of type [<class 'float'>, <class 'int'>],"
  131. " but got <class 'str'>.")
  132. test_invalid_input("invalid depth parameter value", 44100, 0.0, 50.0, 0.0, 71.0, 0.5, 25.0,
  133. Modulation.SINUSOIDAL, Interpolation.LINEAR, ValueError,
  134. "Input depth is not within the required interval of [0, 10].")
  135. test_invalid_input("invalid regen parameter type as a String", 44100, 0.0, 2.0, "0.0", 71.0, 0.5, 25.0,
  136. Modulation.SINUSOIDAL, Interpolation.LINEAR, TypeError,
  137. "Argument regen with value 0.0 is not of type [<class 'float'>, <class 'int'>],"
  138. " but got <class 'str'>.")
  139. test_invalid_input("invalid regen parameter value", 44100, 0.0, 2.0, 100.0, 71.0, 0.5, 25.0,
  140. Modulation.SINUSOIDAL, Interpolation.LINEAR, ValueError,
  141. "Input regen is not within the required interval of [-95, 95].")
  142. test_invalid_input("invalid width parameter type as a String", 44100, 0.0, 2.0, 0.0, "71.0", 0.5, 25.0,
  143. Modulation.SINUSOIDAL, Interpolation.LINEAR, TypeError,
  144. "Argument width with value 71.0 is not of type [<class 'float'>, <class 'int'>],"
  145. " but got <class 'str'>.")
  146. test_invalid_input("invalid width parameter value", 44100, 0.0, 2.0, 0.0, 150.0, 0.5, 25.0,
  147. Modulation.SINUSOIDAL, Interpolation.LINEAR, ValueError,
  148. "Input width is not within the required interval of [0, 100].")
  149. test_invalid_input("invalid speed parameter type as a String", 44100, 0.0, 2.0, 0.0, 71.0, "0.5", 25.0,
  150. Modulation.SINUSOIDAL, Interpolation.LINEAR, TypeError,
  151. "Argument speed with value 0.5 is not of type [<class 'float'>, <class 'int'>],"
  152. " but got <class 'str'>.")
  153. test_invalid_input("invalid speed parameter value", 44100, 0.0, 2.0, 0.0, 71.0, 50, 25.0,
  154. Modulation.SINUSOIDAL, Interpolation.LINEAR, ValueError,
  155. "Input speed is not within the required interval of [0.1, 10].")
  156. test_invalid_input("invalid phase parameter type as a String", 44100, 0.0, 2.0, 0.0, 71.0, 0.5, "25.0",
  157. Modulation.SINUSOIDAL, Interpolation.LINEAR, TypeError,
  158. "Argument phase with value 25.0 is not of type [<class 'float'>, <class 'int'>],"
  159. " but got <class 'str'>.")
  160. test_invalid_input("invalid phase parameter value", 44100, 0.0, 2.0, 0.0, 71.0, 0.5, 150.0,
  161. Modulation.SINUSOIDAL, Interpolation.LINEAR, ValueError,
  162. "Input phase is not within the required interval of [0, 100].")
  163. test_invalid_input("invalid modulation parameter value", 44100, 0.0, 2.0, 0.0, 71.0, 0.5, 25.0, "test",
  164. Interpolation.LINEAR, TypeError,
  165. "Argument modulation with value test is not of type [<enum 'Modulation'>], "
  166. "but got <class 'str'>.")
  167. test_invalid_input("invalid modulation parameter value", 44100, 0.0, 2.0, 0.0, 71.0, 0.5, 25.0,
  168. Modulation.SINUSOIDAL, "test", TypeError,
  169. "Argument interpolation with value test is not of type [<enum 'Interpolation'>], "
  170. "but got <class 'str'>.")
  171. if __name__ == '__main__':
  172. test_flanger_eager_sinusoidal_linear_float64()
  173. test_flanger_eager_triangular_linear_float32()
  174. test_flanger_eager_triangular_linear_int()
  175. test_flanger_shape_221()
  176. test_flanger_shape_11211()
  177. test_flanger_pipeline()
  178. test_invalid_flanger_input()