Mercurial > pygame
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 |
| 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 | 21 """pygame module for accessing sound sample data using numpy |
| 22 | |
| 23 Functions to convert between numpy arrays and Sound objects. This module | |
| 24 will only be available when pygame can use the external numpy package. | |
| 25 | |
| 26 Sound data is made of thousands of samples per second, and each sample | |
| 27 is the amplitude of the wave at a particular moment in time. For | |
| 28 example, in 22-kHz format, element number 5 of the array is the | |
| 29 amplitude of the wave after 5/22000 seconds. | |
| 30 | |
| 31 Each sample is an 8-bit or 16-bit integer, depending on the data format. | |
| 32 A stereo sound file has two values per sample, while a mono sound file | |
| 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 | 55 |
| 56 # mixer.init () does not support different formats from the ones below, | |
| 57 # so MSB/LSB stuff is silently ignored. | |
| 58 typecode = { 8 : numpy.uint8, # AUDIO_U8 | |
| 901 | 59 16 : numpy.uint16, # AUDIO_U16 / AUDIO_U16SYS |
| 899 | 60 -8 : numpy.int8, # AUDIO_S8 |
| 901 | 61 -16 : numpy.int16 # AUDUI_S16 / AUDIO_S16SYS |
| 899 | 62 }[info[1]] |
| 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 | 80 """pygame._numpysndarray.samples(Sound): return array |
| 81 | |
| 82 Reference Sound samples into an array. | |
| 83 | |
| 84 Creates a new array that directly references the samples in a Sound | |
| 85 object. Modifying the array will change the Sound. The array will | |
| 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 | 100 # mixer.init () does not support different formats from the ones below, |
| 101 # so MSB/LSB stuff is silently ignored. | |
| 102 typecode = { 8 : numpy.uint8, # AUDIO_U8 | |
| 103 16 : numpy.uint16, # AUDIO_U16 | |
| 104 -8 : numpy.int8, # AUDIO_S8 | |
| 105 -16 : numpy.int16 # AUDUI_S16 | |
| 106 }[info[1]] | |
| 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 | 113 """pygame._numpysndarray.make_sound(array): return Sound |
| 114 | |
| 115 Convert an array into a Sound object. | |
| 116 | |
| 117 Create a new playable Sound object from an array. The mixer module | |
| 118 must be initialized and the array format must be similar to the mixer | |
| 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 | 122 info = pygame.mixer.get_init () |
| 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 | 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 | 128 if channels == 1: |
| 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 | 131 else: |
| 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 | 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) |
