annotate lib/_numpysndarray.py @ 1849:301bc61643a9 math_module

merged changes from trunk
author lorenz
date Mon, 16 Nov 2009 18:31:40 +0000
parents c35441d4f466
children 1983347f0ab7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
1 ## pygame - Python Game Library
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
2 ## Copyright (C) 2008 Marcus von Appen
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
3 ##
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
4 ## This library is free software; you can redistribute it and/or
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
5 ## modify it under the terms of the GNU Library General Public
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
6 ## License as published by the Free Software Foundation; either
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
7 ## version 2 of the License, or (at your option) any later version.
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
8 ##
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
9 ## This library is distributed in the hope that it will be useful,
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
12 ## Library General Public License for more details.
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
13 ##
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
14 ## You should have received a copy of the GNU Library General Public
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
15 ## License along with this library; if not, write to the Free
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
16 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
17 ##
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
18 ## Marcus von Appen
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
19 ## mva@sysfault.org
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
20
899
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
21 """pygame module for accessing sound sample data using numpy
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
22
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
23 Functions to convert between numpy arrays and Sound objects. This module
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
24 will only be available when pygame can use the external numpy package.
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
25
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
26 Sound data is made of thousands of samples per second, and each sample
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
27 is the amplitude of the wave at a particular moment in time. For
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
28 example, in 22-kHz format, element number 5 of the array is the
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
29 amplitude of the wave after 5/22000 seconds.
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
30
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
31 Each sample is an 8-bit or 16-bit integer, depending on the data format.
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
32 A stereo sound file has two values per sample, while a mono sound file
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
33 only has one.
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
34 """
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
35
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
36 import pygame
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
37 import pygame.mixer as mixer
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
38 import numpy
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
39
1109
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
40 def _array_samples(sound, raw):
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
41 # Info is a (freq, format, stereo) tuple
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
42 info = mixer.get_init ()
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
43 if not info:
1568
c35441d4f466 allow all Python modules to install with Python 3.1, though some may not work correctly
Lenard Lindstrom <len-l@telus.net>
parents: 1484
diff changeset
44 raise pygame.error("Mixer not initialized")
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
45 fmtbytes = (abs (info[1]) & 0xff) >> 3
1109
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
46 channels = info[2]
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
47 if raw:
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
48 data = sound.get_buffer ().raw
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
49 else:
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
50 data = sound.get_buffer ()
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
51
1484
9c8d62c17e5e fill in sndarray tests and fix uncovered bugs
Lenard Lindstrom <len-l@telus.net>
parents: 1482
diff changeset
52 shape = (len (data) // fmtbytes, )
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
53 if channels > 1:
1484
9c8d62c17e5e fill in sndarray tests and fix uncovered bugs
Lenard Lindstrom <len-l@telus.net>
parents: 1482
diff changeset
54 shape = (shape[0] // channels, channels)
899
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
55
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
56 # mixer.init () does not support different formats from the ones below,
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
57 # so MSB/LSB stuff is silently ignored.
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
58 typecode = { 8 : numpy.uint8, # AUDIO_U8
901
f9932c5e0e2a Fixed typo in sndarray.
marcus
parents: 899
diff changeset
59 16 : numpy.uint16, # AUDIO_U16 / AUDIO_U16SYS
899
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
60 -8 : numpy.int8, # AUDIO_S8
901
f9932c5e0e2a Fixed typo in sndarray.
marcus
parents: 899
diff changeset
61 -16 : numpy.int16 # AUDUI_S16 / AUDIO_S16SYS
899
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
62 }[info[1]]
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
63
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
64 array = numpy.fromstring (data, typecode)
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
65 array.shape = shape
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
66 return array
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
67
1109
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
68 def array (sound):
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
69 """pygame._numpysndarray.array(Sound): return array
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
70
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
71 Copy Sound samples into an array.
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
72
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
73 Creates a new array for the sound data and copies the samples. The
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
74 array will always be in the format returned from
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
75 pygame.mixer.get_init().
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
76 """
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
77 return _array_samples(sound, True)
438669b899b0 Fixed bug with mixer.get_init() Thanks Frankie Robertson
Rene Dudfield <renesd@gmail.com>
parents: 903
diff changeset
78
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
79 def samples (sound):
899
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
80 """pygame._numpysndarray.samples(Sound): return array
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
81
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
82 Reference Sound samples into an array.
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
83
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
84 Creates a new array that directly references the samples in a Sound
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
85 object. Modifying the array will change the Sound. The array will
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
86 always be in the format returned from pygame.mixer.get_init().
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
87 """
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
88 # Info is a (freq, format, stereo) tuple
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
89 info = pygame.mixer.get_init ()
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
90 if not info:
1568
c35441d4f466 allow all Python modules to install with Python 3.1, though some may not work correctly
Lenard Lindstrom <len-l@telus.net>
parents: 1484
diff changeset
91 raise pygame.error("Mixer not initialized")
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
92 fmtbytes = (abs (info[1]) & 0xff) >> 3
1484
9c8d62c17e5e fill in sndarray tests and fix uncovered bugs
Lenard Lindstrom <len-l@telus.net>
parents: 1482
diff changeset
93 channels = info[2]
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
94 data = sound.get_buffer ()
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
95
1484
9c8d62c17e5e fill in sndarray tests and fix uncovered bugs
Lenard Lindstrom <len-l@telus.net>
parents: 1482
diff changeset
96 shape = (data.length // fmtbytes, )
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
97 if channels > 1:
1484
9c8d62c17e5e fill in sndarray tests and fix uncovered bugs
Lenard Lindstrom <len-l@telus.net>
parents: 1482
diff changeset
98 shape = (shape[0] // channels, channels)
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
99
899
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
100 # mixer.init () does not support different formats from the ones below,
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
101 # so MSB/LSB stuff is silently ignored.
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
102 typecode = { 8 : numpy.uint8, # AUDIO_U8
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
103 16 : numpy.uint16, # AUDIO_U16
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
104 -8 : numpy.int8, # AUDIO_S8
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
105 -16 : numpy.int16 # AUDUI_S16
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
106 }[info[1]]
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
107
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
108 array = numpy.frombuffer (data, typecode)
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
109 array.shape = shape
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
110 return array
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
111
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
112 def make_sound (array):
899
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
113 """pygame._numpysndarray.make_sound(array): return Sound
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
114
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
115 Convert an array into a Sound object.
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
116
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
117 Create a new playable Sound object from an array. The mixer module
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
118 must be initialized and the array format must be similar to the mixer
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
119 audio format.
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
120 """
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
121 # Info is a (freq, format, stereo) tuple
903
72fad71f7a5e Fixed numpy sndarray.make_sound.
marcus
parents: 901
diff changeset
122 info = pygame.mixer.get_init ()
72fad71f7a5e Fixed numpy sndarray.make_sound.
marcus
parents: 901
diff changeset
123 if not info:
1568
c35441d4f466 allow all Python modules to install with Python 3.1, though some may not work correctly
Lenard Lindstrom <len-l@telus.net>
parents: 1484
diff changeset
124 raise pygame.error("Mixer not initialized")
903
72fad71f7a5e Fixed numpy sndarray.make_sound.
marcus
parents: 901
diff changeset
125 channels = info[2]
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
126
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
127 shape = array.shape
903
72fad71f7a5e Fixed numpy sndarray.make_sound.
marcus
parents: 901
diff changeset
128 if channels == 1:
72fad71f7a5e Fixed numpy sndarray.make_sound.
marcus
parents: 901
diff changeset
129 if len (shape) != 1:
1568
c35441d4f466 allow all Python modules to install with Python 3.1, though some may not work correctly
Lenard Lindstrom <len-l@telus.net>
parents: 1484
diff changeset
130 raise ValueError("Array must be 1-dimensional for mono mixer")
903
72fad71f7a5e Fixed numpy sndarray.make_sound.
marcus
parents: 901
diff changeset
131 else:
72fad71f7a5e Fixed numpy sndarray.make_sound.
marcus
parents: 901
diff changeset
132 if len (shape) != 2:
1568
c35441d4f466 allow all Python modules to install with Python 3.1, though some may not work correctly
Lenard Lindstrom <len-l@telus.net>
parents: 1484
diff changeset
133 raise ValueError("Array must be 2-dimensional for stereo mixer")
903
72fad71f7a5e Fixed numpy sndarray.make_sound.
marcus
parents: 901
diff changeset
134 elif shape[1] != channels:
1568
c35441d4f466 allow all Python modules to install with Python 3.1, though some may not work correctly
Lenard Lindstrom <len-l@telus.net>
parents: 1484
diff changeset
135 raise ValueError("Array depth must match number of mixer channels")
898
424ef30fa1ea Added numpy support for the sndarray module. BufferProxy now contains a length property, which indicates the length of the buffer in bytes.
marcus
parents:
diff changeset
136 return mixer.Sound (array)