Mercurial > pygame
annotate src/_numericsndarray.c @ 1849:301bc61643a9 math_module
merged changes from trunk
| author | lorenz |
|---|---|
| date | Mon, 16 Nov 2009 18:31:40 +0000 |
| parents | 65e001d30448 |
| children |
| rev | line source |
|---|---|
| 372 | 1 /* |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
2 pygame - Python Game Library |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
3 Copyright (C) 2000-2001 Pete Shinners |
| 372 | 4 |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
5 This library is free software; you can redistribute it and/or |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
6 modify it under the terms of the GNU Library General Public |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
7 License as published by the Free Software Foundation; either |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
8 version 2 of the License, or (at your option) any later version. |
| 372 | 9 |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
10 This library is distributed in the hope that it will be useful, |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
13 Library General Public License for more details. |
| 372 | 14 |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
15 You should have received a copy of the GNU Library General Public |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
16 License along with this library; if not, write to the Free |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 372 | 18 |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
19 Pete Shinners |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
20 pete@shinners.org |
| 372 | 21 */ |
| 22 | |
| 627 | 23 #include "pygame.h" |
| 1849 | 24 #include "doc/sndarray_doc.h" |
|
871
62e182c1c769
Added numpy surface array module. Removed Numeric dependency and imported Numeric/arrayobject header.
marcus
parents:
831
diff
changeset
|
25 #include "mixer.h" |
|
62e182c1c769
Added numpy surface array module. Removed Numeric dependency and imported Numeric/arrayobject header.
marcus
parents:
831
diff
changeset
|
26 #include "numeric_arrayobject.h" |
|
62e182c1c769
Added numpy surface array module. Removed Numeric dependency and imported Numeric/arrayobject header.
marcus
parents:
831
diff
changeset
|
27 #include <SDL_byteorder.h> |
| 372 | 28 |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
29 static PyObject* |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
30 sndarray_samples (PyObject* self, PyObject* arg) |
| 372 | 31 { |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
32 int dim[2], numdims, type, formatbytes; |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
33 PyObject *array, *chunkobj; |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
34 Mix_Chunk* chunk; |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
35 Uint16 format; |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
36 int numchannels; |
| 479 | 37 |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
38 if (!PyArg_ParseTuple (arg, "O!", &PySound_Type, &chunkobj)) |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
39 return NULL; |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
40 chunk = PySound_AsChunk (chunkobj); |
| 372 | 41 |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
42 if (!Mix_QuerySpec (NULL, &format, &numchannels)) |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
43 return RAISE (PyExc_SDLError, "Mixer not initialized"); |
| 372 | 44 |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
45 formatbytes = (abs (format) & 0xff) / 8; |
| 899 | 46 switch (format) |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
47 { |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
48 case AUDIO_S8: |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
49 type = PyArray_CHAR; |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
50 break; |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
51 case AUDIO_U8: |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
52 type = PyArray_UBYTE; |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
53 break; |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
54 case AUDIO_S16SYS: |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
55 type = PyArray_SHORT; |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
56 break; |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
57 case AUDIO_U16SYS: |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
58 type = PyArray_USHORT; |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
59 break; |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
60 default: |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
61 return RAISE (PyExc_TypeError, "Unpresentable audio format"); |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
62 } |
| 372 | 63 |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
64 numdims = (numchannels > 1) ? 2 : 1; |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
65 dim[0] = chunk->alen / (numchannels*formatbytes); |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
66 dim[1] = numchannels; |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
67 |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
68 array = PyArray_FromDimsAndData (numdims, dim, type, (char*)chunk->abuf); |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
69 if(array) |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
70 { |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
71 Py_INCREF (chunkobj); |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
72 ((PyArrayObject*) array)->base = chunkobj; |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
73 ((PyArrayObject*) array)->flags |= SAVESPACE; |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
74 } |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
75 return array; |
| 372 | 76 } |
| 77 | |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
78 PyObject* |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
79 sndarray_array (PyObject* self, PyObject* arg) |
| 372 | 80 { |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
81 PyObject *array, *arraycopy=NULL; |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
82 |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
83 /*we'll let numeric do the copying for us*/ |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
84 array = sndarray_samples (self, arg); |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
85 if(array) |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
86 { |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
87 arraycopy = PyArray_Copy ((PyArrayObject*) array); |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
88 Py_DECREF (array); |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
89 } |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
90 return arraycopy; |
| 372 | 91 } |
| 92 | |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
93 PyObject* |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
94 sndarray_make_sound (PyObject* self, PyObject* arg) |
| 372 | 95 { |
| 96 PyObject *arrayobj; | |
| 97 PyArrayObject *array; | |
| 98 Mix_Chunk *chunk; | |
| 99 Uint16 format; | |
| 374 | 100 int numchannels, mixerbytes; |
| 448 | 101 int loop1, loop2, step1, step2, length, length2=0; |
| 372 | 102 Uint8 *src, *dst; |
| 103 | |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
104 if (!PyArg_ParseTuple (arg, "O!", &PyArray_Type, &arrayobj)) |
| 372 | 105 return NULL; |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
106 array = (PyArrayObject*) arrayobj; |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
107 |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
108 if (!Mix_QuerySpec (NULL, &format, &numchannels)) |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
109 return RAISE (PyExc_SDLError, "Mixer not initialized"); |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
110 if (array->descr->type_num > PyArray_LONG) |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
111 return RAISE (PyExc_ValueError, "Invalid array datatype for sound"); |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
112 |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
113 if (format==AUDIO_S8 || format==AUDIO_U8) |
| 373 | 114 mixerbytes = 1; |
| 115 else | |
| 116 mixerbytes = 2; | |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
117 |
| 372 | 118 /*test array dimensions*/ |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
119 if (numchannels == 1) |
| 372 | 120 { |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
121 if (array->nd != 1) |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
122 return RAISE (PyExc_ValueError, |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
123 "Array must be 1-dimensional for mono mixer"); |
| 372 | 124 } |
| 125 else | |
| 126 { | |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
127 if (array->nd != 2) |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
128 return RAISE (PyExc_ValueError, |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
129 "Array must be 2-dimensional for stereo mixer"); |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
130 if (array->dimensions[1] != numchannels) |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
131 return RAISE (PyExc_ValueError, |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
132 "Array depth must match number of mixer channels"); |
| 372 | 133 } |
| 373 | 134 length = array->dimensions[0]; |
| 519 | 135 step1 = array->strides[0]; |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
136 if (array->nd == 2) |
| 519 | 137 { |
| 373 | 138 length2 = array->dimensions[1]; |
| 519 | 139 step2 = array->strides[1]; |
| 140 } | |
| 141 else | |
| 142 { | |
| 143 length2 = 1; | |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
144 /*since length2 == 1, this won't be used for looping*/ |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
145 step2 = mixerbytes; |
| 519 | 146 } |
| 479 | 147 |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
148 /*create chunk, we are screwed if SDL_mixer ever does more than |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
149 * malloc/free*/ |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
150 chunk = (Mix_Chunk *)malloc (sizeof (Mix_Chunk)); |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
151 if (chunk == NULL) |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
152 return RAISE (PyExc_MemoryError, "Cannot allocate chunk\n"); |
| 372 | 153 /*let's hope Mix_Chunk never changes also*/ |
| 519 | 154 chunk->alen = length * numchannels * mixerbytes; |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
155 chunk->abuf = (Uint8*) malloc (chunk->alen); |
| 372 | 156 chunk->allocated = 1; |
| 157 chunk->volume = 128; | |
| 373 | 158 |
| 519 | 159 if (step1 == mixerbytes * numchannels && step2 == mixerbytes) |
| 372 | 160 { |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
161 /*OPTIMIZATION: in these cases, we don't need to loop through |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
162 *the samples individually, because the bytes are already layed |
|
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
163 *out correctly*/ |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
164 memcpy (chunk->abuf, array->data, chunk->alen); |
| 372 | 165 } |
| 166 else | |
| 167 { | |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
168 dst = (Uint8*) chunk->abuf; |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
169 if (mixerbytes == 1) |
| 372 | 170 { |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
171 for (loop1 = 0; loop1 < length; loop1++) |
| 372 | 172 { |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
173 src = (Uint8*) array->data + loop1*step1; |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
174 switch (array->descr->elsize) |
| 372 | 175 { |
| 373 | 176 case 1: |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
177 for (loop2=0; loop2<length2; loop2++, dst+=1, src+=step2) |
| 373 | 178 *(Uint8*)dst = (Uint8)*((Uint8*)src); |
| 179 break; | |
| 180 case 2: | |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
181 for (loop2=0; loop2<length2; loop2++, dst+=1, src+=step2) |
| 373 | 182 *(Uint8*)dst = (Uint8)*((Uint16*)src); |
| 183 break; | |
| 184 case 4: | |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
185 for (loop2=0; loop2<length2; loop2++, dst+=1, src+=step2) |
| 373 | 186 *(Uint8*)dst = (Uint8)*((Uint32*)src); |
| 187 break; | |
| 372 | 188 } |
| 189 } | |
| 190 } | |
| 191 else | |
| 192 { | |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
193 for (loop1 = 0; loop1 < length; loop1++) |
| 372 | 194 { |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
195 src = (Uint8*) array->data + loop1*step1; |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
196 switch (array->descr->elsize) |
| 372 | 197 { |
| 373 | 198 case 1: |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
199 for (loop2=0; loop2<length2; loop2++, dst+=2, src+=step2) |
| 373 | 200 *(Uint16*)dst = (Uint16)(*((Uint8*)src)<<8); |
| 201 break; | |
| 202 case 2: | |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
203 for (loop2=0; loop2<length2; loop2++, dst+=2, src+=step2) |
| 373 | 204 *(Uint16*)dst = (Uint16)*((Uint16*)src); |
| 205 break; | |
| 206 case 4: | |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
207 for (loop2=0; loop2<length2; loop2++, dst+=2, src+=step2) |
| 373 | 208 *(Uint16*)dst = (Uint16)*((Uint32*)src); |
| 209 break; | |
| 372 | 210 } |
| 211 } | |
| 212 } | |
| 213 } | |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
214 |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
215 return PySound_New (chunk); |
| 372 | 216 } |
| 217 | |
| 218 static PyMethodDef sndarray_builtins[] = | |
| 219 { | |
|
831
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
220 { "samples", sndarray_samples, METH_VARARGS, DOC_PYGAMESNDARRAYSAMPLES }, |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
221 { "array", sndarray_array, METH_VARARGS, DOC_PYGAMESNDARRAYARRAY }, |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
222 { "make_sound", sndarray_make_sound, METH_VARARGS, |
|
8749d7c235f8
Changes from Marcus for ref counting bug fixes and
Rene Dudfield <renesd@gmail.com>
parents:
794
diff
changeset
|
223 DOC_PYGAMESNDARRAYMAKESOUND }, |
|
794
1633c4302f48
Some code cleanups from Marcus von Appen. min->MIN, indentation, other.
Rene Dudfield <renesd@gmail.com>
parents:
627
diff
changeset
|
224 { NULL, NULL, 0, NULL} |
| 372 | 225 }; |
| 226 | |
| 227 PYGAME_EXPORT | |
|
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:
871
diff
changeset
|
228 void init_numericsndarray (void) |
| 372 | 229 { |
|
1499
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
230 /* imported needed apis; Do this first so if there is an error |
|
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
231 the module is not loaded. |
|
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
232 */ |
|
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
233 import_pygame_base (); |
|
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
234 if (PyErr_Occurred ()) { |
|
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
235 return; |
|
1487
5e5efccec42c
make _numericsndarray fail to import if Numeric not installed
Lenard Lindstrom <len-l@telus.net>
parents:
899
diff
changeset
|
236 } |
|
1499
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
237 import_pygame_mixer (); |
|
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
238 if (PyErr_Occurred ()) { |
|
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
239 return; |
|
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
240 } |
|
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
241 import_array (); |
|
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
242 if (PyErr_Occurred ()) { |
|
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
243 return; |
|
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
244 } |
|
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
245 |
|
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
246 /* create the module */ |
|
65e001d30448
bug fix - required modules not raising error when failing to import.
Lenard Lindstrom <len-l@telus.net>
parents:
1487
diff
changeset
|
247 Py_InitModule3 ("_numericsndarray", sndarray_builtins, DOC_PYGAMESNDARRAY); |
| 372 | 248 } |
