sndarray_test.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import unittest
  2. from numpy import int8, int16, uint8, uint16, float32, array, alltrue
  3. import pygame
  4. from pygame.compat import as_bytes
  5. import pygame.sndarray
  6. SDL2 = pygame.get_sdl_version()[0] >= 2
  7. class SndarrayTest (unittest.TestCase):
  8. array_dtypes = {8: uint8, -8: int8, 16: uint16, -16: int16, 32: float32}
  9. def _assert_compatible(self, arr, size):
  10. dtype = self.array_dtypes[size]
  11. self.assertEqual(arr.dtype, dtype)
  12. def test_array(self):
  13. def check_array(size, channels, test_data):
  14. try:
  15. pygame.mixer.init(22050, size, channels, allowedchanges=0)
  16. except pygame.error:
  17. # Not all sizes are supported on all systems.
  18. return
  19. try:
  20. __, sz, __ = pygame.mixer.get_init()
  21. if sz == size:
  22. srcarr = array(test_data, self.array_dtypes[size])
  23. snd = pygame.sndarray.make_sound(srcarr)
  24. arr = pygame.sndarray.array(snd)
  25. self._assert_compatible(arr, size)
  26. self.assertTrue(alltrue(arr == srcarr),
  27. "size: %i\n%s\n%s" % (
  28. size, arr, test_data))
  29. finally:
  30. pygame.mixer.quit()
  31. check_array(8, 1, [0, 0x0f, 0xf0, 0xff])
  32. check_array(8, 2,
  33. [[0, 0x80], [0x2D, 0x41], [0x64, 0xA1], [0xff, 0x40]])
  34. check_array(16, 1, [0, 0x00ff, 0xff00, 0xffff])
  35. check_array(16, 2, [[0, 0xffff], [0xffff, 0],
  36. [0x00ff, 0xff00], [0x0f0f, 0xf0f0]])
  37. check_array(-8, 1, [0, -0x80, 0x7f, 0x64])
  38. check_array(-8, 2,
  39. [[0, -0x80], [-0x64, 0x64], [0x25, -0x50], [0xff, 0]])
  40. check_array(-16, 1, [0, 0x7fff, -0x7fff, -1])
  41. check_array(-16, 2, [[0, -0x7fff], [-0x7fff, 0],
  42. [0x7fff, 0], [0, 0x7fff]])
  43. def test_get_arraytype(self):
  44. array_type = pygame.sndarray.get_arraytype()
  45. self.assertEqual(array_type, 'numpy',
  46. "unknown array type %s" % array_type)
  47. def test_get_arraytypes(self):
  48. arraytypes = pygame.sndarray.get_arraytypes()
  49. self.assertIn('numpy', arraytypes)
  50. for atype in arraytypes:
  51. self.assertEqual(atype, 'numpy', "unknown array type %s" % atype)
  52. def test_make_sound(self):
  53. def check_sound(size, channels, test_data):
  54. try:
  55. pygame.mixer.init(22050, size, channels, allowedchanges=0)
  56. except pygame.error:
  57. # Not all sizes are supported on all systems.
  58. return
  59. try:
  60. __, sz, __ = pygame.mixer.get_init()
  61. if sz == size:
  62. srcarr = array(test_data, self.array_dtypes[size])
  63. snd = pygame.sndarray.make_sound(srcarr)
  64. arr = pygame.sndarray.samples(snd)
  65. self.assertTrue(alltrue(arr == srcarr),
  66. "size: %i\n%s\n%s" % (
  67. size, arr, test_data))
  68. finally:
  69. pygame.mixer.quit()
  70. check_sound(8, 1, [0, 0x0f, 0xf0, 0xff])
  71. check_sound(8, 2,
  72. [[0, 0x80], [0x2D, 0x41], [0x64, 0xA1], [0xff, 0x40]])
  73. check_sound(16, 1, [0, 0x00ff, 0xff00, 0xffff])
  74. check_sound(16, 2, [[0, 0xffff], [0xffff, 0],
  75. [0x00ff, 0xff00], [0x0f0f, 0xf0f0]])
  76. check_sound(-8, 1, [0, -0x80, 0x7f, 0x64])
  77. check_sound(-8, 2,
  78. [[0, -0x80], [-0x64, 0x64], [0x25, -0x50], [0xff, 0]])
  79. check_sound(-16, 1, [0, 0x7fff, -0x7fff, -1])
  80. check_sound(-16, 2, [[0, -0x7fff], [-0x7fff, 0],
  81. [0x7fff, 0], [0, 0x7fff]])
  82. if SDL2:
  83. check_sound(32, 2, [[0.0, -1.0], [-1.0, 0], [1.0, 0], [0, 1.0]])
  84. def test_samples(self):
  85. null_byte = as_bytes('\x00')
  86. def check_sample(size, channels, test_data):
  87. try:
  88. pygame.mixer.init(22050, size, channels, allowedchanges=0)
  89. except pygame.error:
  90. # Not all sizes are supported on all systems.
  91. return
  92. try:
  93. __, sz, __ = pygame.mixer.get_init()
  94. if sz == size:
  95. zeroed = null_byte * ((abs(size) // 8) *
  96. len(test_data) *
  97. channels)
  98. snd = pygame.mixer.Sound(buffer=zeroed)
  99. samples = pygame.sndarray.samples(snd)
  100. self._assert_compatible(samples, size)
  101. ##print ('X %s' % (samples.shape,))
  102. ##print ('Y %s' % (test_data,))
  103. samples[...] = test_data
  104. arr = pygame.sndarray.array(snd)
  105. self.assertTrue(alltrue(samples == arr),
  106. "size: %i\n%s\n%s" % (
  107. size, arr, test_data))
  108. finally:
  109. pygame.mixer.quit()
  110. check_sample(8, 1, [0, 0x0f, 0xf0, 0xff])
  111. check_sample(8, 2,
  112. [[0, 0x80], [0x2D, 0x41], [0x64, 0xA1], [0xff, 0x40]])
  113. check_sample(16, 1, [0, 0x00ff, 0xff00, 0xffff])
  114. check_sample(16, 2, [[0, 0xffff], [0xffff, 0],
  115. [0x00ff, 0xff00], [0x0f0f, 0xf0f0]])
  116. check_sample(-8, 1, [0, -0x80, 0x7f, 0x64])
  117. check_sample(-8, 2,
  118. [[0, -0x80], [-0x64, 0x64], [0x25, -0x50], [0xff, 0]])
  119. check_sample(-16, 1, [0, 0x7fff, -0x7fff, -1])
  120. check_sample(-16, 2, [[0, -0x7fff], [-0x7fff, 0],
  121. [0x7fff, 0], [0, 0x7fff]])
  122. if SDL2:
  123. check_sample(32, 2, [[0.0, -1.0], [-1.0, 0], [1.0, 0], [0, 1.0]])
  124. def test_use_arraytype(self):
  125. def do_use_arraytype(atype):
  126. pygame.sndarray.use_arraytype(atype)
  127. pygame.sndarray.use_arraytype('numpy')
  128. self.assertEqual(pygame.sndarray.get_arraytype(), 'numpy')
  129. self.assertRaises(ValueError, do_use_arraytype, 'not an option')
  130. @unittest.skipIf(not SDL2, 'requires SDL2')
  131. def test_float32(self):
  132. """ sized arrays work with Sounds and 32bit float arrays.
  133. """
  134. try:
  135. pygame.mixer.init(22050, 32, 2, allowedchanges=0)
  136. except pygame.error:
  137. # Not all sizes are supported on all systems.
  138. self.skipTest("unsupported mixer configuration")
  139. arr = array([[0.0, -1.0], [-1.0, 0], [1.0, 0], [0, 1.0]], float32)
  140. newsound = pygame.mixer.Sound(array=arr)
  141. pygame.mixer.quit()
  142. if __name__ == '__main__':
  143. unittest.main()