sndarray.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ## pygame - Python Game Library
  2. ## Copyright (C) 2008 Marcus von Appen
  3. ##
  4. ## This library is free software; you can redistribute it and/or
  5. ## modify it under the terms of the GNU Library General Public
  6. ## License as published by the Free Software Foundation; either
  7. ## version 2 of the License, or (at your option) any later version.
  8. ##
  9. ## This library is distributed in the hope that it will be useful,
  10. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ## Library General Public License for more details.
  13. ##
  14. ## You should have received a copy of the GNU Library General Public
  15. ## License along with this library; if not, write to the Free
  16. ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. ##
  18. ## Marcus von Appen
  19. ## mva@sysfault.org
  20. """pygame module for accessing sound sample data
  21. Functions to convert between numpy arrays and Sound
  22. objects. This module will only be available when pygame can use the
  23. external numpy package.
  24. Sound data is made of thousands of samples per second, and each sample
  25. is the amplitude of the wave at a particular moment in time. For
  26. example, in 22-kHz format, element number 5 of the array is the
  27. amplitude of the wave after 5/22000 seconds.
  28. Each sample is an 8-bit or 16-bit integer, depending on the data format.
  29. A stereo sound file has two values per sample, while a mono sound file
  30. only has one.
  31. Supported array systems are
  32. numpy
  33. The array type to use can be changed at runtime using the use_arraytype()
  34. method, which requires one of the above types as string.
  35. Sounds with 16-bit data will be treated as unsigned integers,
  36. if the sound sample type requests this.
  37. """
  38. import pygame._numpysndarray as numpysnd
  39. def array (sound):
  40. """pygame.sndarray.array(Sound): return array
  41. Copy Sound samples into an array.
  42. Creates a new array for the sound data and copies the samples. The
  43. array will always be in the format returned from
  44. pygame.mixer.get_init().
  45. """
  46. return numpysnd.array (sound)
  47. def samples (sound):
  48. """pygame.sndarray.samples(Sound): return array
  49. Reference Sound samples into an array.
  50. Creates a new array that directly references the samples in a Sound
  51. object. Modifying the array will change the Sound. The array will
  52. always be in the format returned from pygame.mixer.get_init().
  53. """
  54. return numpysnd.samples (sound)
  55. def make_sound (array):
  56. """pygame.sndarray.make_sound(array): return Sound
  57. Convert an array into a Sound object.
  58. Create a new playable Sound object from an array. The mixer module
  59. must be initialized and the array format must be similar to the mixer
  60. audio format.
  61. """
  62. return numpysnd.make_sound (array)
  63. def use_arraytype (arraytype):
  64. """pygame.sndarray.use_arraytype (arraytype): return None
  65. DEPRECATED - only numpy arrays are now supported.
  66. """
  67. arraytype = arraytype.lower ()
  68. if arraytype != 'numpy':
  69. raise ValueError("invalid array type")
  70. def get_arraytype ():
  71. """pygame.sndarray.get_arraytype (): return str
  72. DEPRECATED - only numpy arrays are now supported.
  73. """
  74. return 'numpy'
  75. def get_arraytypes ():
  76. """pygame.sndarray.get_arraytypes (): return tuple
  77. DEPRECATED - only numpy arrays are now supported.
  78. """
  79. return ('numpy',)