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_fade.py 8.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. """
  16. Testing fade op in DE
  17. """
  18. import numpy as np
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.audio.transforms as audio
  21. from mindspore.dataset.audio.utils import FadeShape
  22. from mindspore import log as logger
  23. def test_fade_linear():
  24. """
  25. Test Fade, fade shape is linear.
  26. """
  27. logger.info("test fade, fade shape is 'linear'")
  28. waveform = [[[9.1553e-05, 6.1035e-05, 6.1035e-05, 6.1035e-05, 1.2207e-04, 1.2207e-04,
  29. 9.1553e-05, 9.1553e-05, 9.1553e-05, 9.1553e-05, 9.1553e-05, 6.1035e-05,
  30. 1.2207e-04, 1.2207e-04, 1.2207e-04, 9.1553e-05, 9.1553e-05, 9.1553e-05,
  31. 6.1035e-05, 9.1553e-05]]]
  32. dataset = ds.NumpySlicesDataset(data=waveform, column_names='audio', shuffle=False)
  33. transforms = [audio.Fade(fade_in_len=10, fade_out_len=5, fade_shape=FadeShape.LINEAR)]
  34. dataset = dataset.map(operations=transforms, input_columns=["audio"])
  35. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  36. out_put = item["audio"]
  37. # The result of the reference operator
  38. expected_output = np.array([[0.0000000000000000000, 6.781666797905927e-06, 1.356333359581185e-05,
  39. 2.034499993897043e-05, 5.425333438324742e-05, 6.781666888855398e-05,
  40. 6.103533087298274e-05, 7.120789086911827e-05, 8.138045086525380e-05,
  41. 9.155300358543172e-05, 9.155300358543172e-05, 6.103499981691129e-05,
  42. 0.0001220699996338225, 0.0001220699996338225, 0.0001220699996338225,
  43. 9.155300358543172e-05, 6.866475450806320e-05, 4.577650179271586e-05,
  44. 1.525874995422782e-05, 0.0000000000000000000]], dtype=np.float32)
  45. assert np.mean(out_put - expected_output) < 0.0001
  46. def test_fade_exponential():
  47. """
  48. Test Fade, fade shape is exponential.
  49. """
  50. logger.info("test fade, fade shape is 'exponential'")
  51. waveform = [[[1, 2, 3, 4, 5, 6],
  52. [5, 7, 3, 78, 8, 4]]]
  53. dataset = ds.NumpySlicesDataset(data=waveform, column_names='audio', shuffle=False)
  54. transforms = [audio.Fade(fade_in_len=5, fade_out_len=6, fade_shape=FadeShape.EXPONENTIAL)]
  55. dataset = dataset.map(operations=transforms, input_columns=["audio"])
  56. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  57. out_put = item["audio"]
  58. # The result of the reference operator
  59. expected_output = np.array([[0.0000, 0.2071, 0.4823, 0.6657, 0.5743, 0.0000],
  60. [0.0000, 0.7247, 0.4823, 12.9820, 0.9190, 0.0000]], dtype=np.float32)
  61. assert np.mean(out_put - expected_output) < 0.0001
  62. def test_fade_logarithmic():
  63. """
  64. Test Fade, fade shape is logarithmic.
  65. """
  66. logger.info("test fade, fade shape is 'logarithmic'")
  67. waveform = [[[0.03424072265625, 0.01476832226565, 0.04995727590625,
  68. -0.0205993652375, -0.0356467868775, 0.01290893546875]]]
  69. dataset = ds.NumpySlicesDataset(data=waveform, column_names='audio', shuffle=False)
  70. transforms = [audio.Fade(fade_in_len=4, fade_out_len=2, fade_shape=FadeShape.LOGARITHMIC)]
  71. dataset = dataset.map(operations=transforms, input_columns=["audio"])
  72. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  73. out_put = item["audio"]
  74. # The result of the reference operator
  75. expected_output = np.array([[0.0000e+00, 9.4048e-03, 4.4193e-02,
  76. -2.0599e-02, -3.5647e-02, 1.5389e-09]],
  77. dtype=np.float32)
  78. assert np.mean(out_put - expected_output) < 0.0001
  79. def test_fade_quarter_sine():
  80. """
  81. Test Fade, fade shape is quarter_sine.
  82. """
  83. logger.info("test fade, fade shape is 'quarter sine'")
  84. waveform = np.array([[[1, 2, 3, 4, 5, 6],
  85. [5, 7, 3, 78, 8, 4],
  86. [1, 2, 3, 4, 5, 6]]], dtype=np.float64)
  87. dataset = ds.NumpySlicesDataset(data=waveform, column_names='audio', shuffle=False)
  88. transforms = [audio.Fade(fade_in_len=6, fade_out_len=6, fade_shape=FadeShape.QUARTERSINE)]
  89. dataset = dataset.map(operations=transforms, input_columns=["audio"])
  90. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  91. out_put = item["audio"]
  92. # The result of the reference operator
  93. expected_output = np.array([[0.0000, 0.5878, 1.4266, 1.9021, 1.4695, 0.0000],
  94. [0.0000, 2.0572, 1.4266, 37.091, 2.3511, 0.0000],
  95. [0.0000, 0.5878, 1.4266, 1.9021, 1.4695, 0.0000]], dtype=np.float64)
  96. assert np.mean(out_put - expected_output) < 0.0001
  97. def test_fade_half_sine():
  98. """
  99. Test Fade, fade shape is half_sine.
  100. """
  101. logger.info("test fade, fade shape is 'half sine'")
  102. waveform = [[[0.03424072265625, 0.013580322265625, -0.011871337890625,
  103. -0.0205993652343, -0.01049804687500, 0.0129089355468750],
  104. [0.04125976562500, 0.060577392578125, 0.0499572753906250,
  105. 0.01306152343750, -0.019683837890625, -0.018829345703125]]]
  106. dataset = ds.NumpySlicesDataset(data=waveform, column_names='audio', shuffle=False)
  107. transforms = [audio.Fade(fade_in_len=3, fade_out_len=3, fade_shape=FadeShape.HALFSINE)]
  108. dataset = dataset.map(operations=transforms, input_columns=["audio"])
  109. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  110. out_put = item["audio"]
  111. # The result of the reference operator
  112. expected_output = np.array([[0.0000, 0.0068, -0.0119, -0.0206, -0.0052, 0.0000],
  113. [0.0000, 0.0303, 0.0500, 0.0131, -0.0098, -0.0000]], dtype=np.float32)
  114. assert np.mean(out_put - expected_output) < 0.0001
  115. def test_fade_wrong_arguments():
  116. """
  117. Test Fade with invalid arguments
  118. """
  119. logger.info("test fade with invalid arguments")
  120. try:
  121. _ = audio.Fade(-1, 0)
  122. except ValueError as e:
  123. logger.info("Got an exception in Fade: {}".format(str(e)))
  124. assert "fade_in_len is not within the required interval of [0, 2147483647]" in str(e)
  125. try:
  126. _ = audio.Fade(0, -1)
  127. except ValueError as e:
  128. logger.info("Got an exception in Fade: {}".format(str(e)))
  129. assert "fade_out_len is not within the required interval of [0, 2147483647]" in str(e)
  130. try:
  131. _ = audio.Fade(fade_shape='123')
  132. except TypeError as e:
  133. logger.info("Got an exception in Fade: {}".format(str(e)))
  134. assert "is not of type [<enum 'FadeShape'>]" in str(e)
  135. def test_fade_eager():
  136. """
  137. Test Fade eager.
  138. """
  139. logger.info("test fade eager")
  140. data = np.array([[9.1553e-05, 6.1035e-05, 6.1035e-05, 6.1035e-05, 1.2207e-04, 1.2207e-04,
  141. 9.1553e-05, 9.1553e-05, 9.1553e-05, 9.1553e-05, 9.1553e-05, 6.1035e-05,
  142. 1.2207e-04, 1.2207e-04, 1.2207e-04, 9.1553e-05, 9.1553e-05, 9.1553e-05,
  143. 6.1035e-05, 9.1553e-05]]).astype(np.float32)
  144. expected_output = np.array([0.0000000000000000000, 6.781666797905927e-06, 1.356333359581185e-05,
  145. 2.034499993897043e-05, 5.425333438324742e-05, 6.781666888855398e-05,
  146. 6.103533087298274e-05, 7.120789086911827e-05, 8.138045086525380e-05,
  147. 9.155300358543172e-05, 9.155300358543172e-05, 6.103499981691129e-05,
  148. 0.0001220699996338225, 0.0001220699996338225, 0.0001220699996338225,
  149. 9.155300358543172e-05, 6.866475450806320e-05, 4.577650179271586e-05,
  150. 1.525874995422782e-05, 0.0000000000000000000], dtype=np.float32)
  151. fade = audio.Fade(10, 5, fade_shape=FadeShape.LINEAR)
  152. out_put = fade(data)
  153. assert np.mean(out_put - expected_output) < 0.0001
  154. if __name__ == '__main__':
  155. test_fade_linear()
  156. test_fade_exponential()
  157. test_fade_logarithmic()
  158. test_fade_quarter_sine()
  159. test_fade_half_sine()
  160. test_fade_wrong_arguments()
  161. test_fade_eager()