annotate src/_numericsndarray.c @ 3194:9fcb7abf3185 default tip

Remove some gcc 4.7.2 compiler warnings
author Lenard Lindstrom <len-l@telus.net>
date Tue, 23 Apr 2013 19:53:36 -0700
parents 0d661c00c1aa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
372
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
21 */
88501955fada add initial sndarray
pygame
parents:
diff changeset
22
627
18b5fa9180bf new reference documentation system
pygame
parents: 519
diff changeset
23 #include "pygame.h"
1937
0d661c00c1aa Merged FreeType branch into trunk.
tanoku
parents: 1499
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
e12ce508da5b better Numeric support for python2.3
pygame
parents: 448
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
b9eed62703cd Fixed format typecodes for numpy sndarray.
marcus
parents: 898
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
76 }
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
91 }
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
95 {
88501955fada add initial sndarray
pygame
parents:
diff changeset
96 PyObject *arrayobj;
88501955fada add initial sndarray
pygame
parents:
diff changeset
97 PyArrayObject *array;
88501955fada add initial sndarray
pygame
parents:
diff changeset
98 Mix_Chunk *chunk;
88501955fada add initial sndarray
pygame
parents:
diff changeset
99 Uint16 format;
374
cf628b75b7fe win32 warning cleanups
pygame
parents: 373
diff changeset
100 int numchannels, mixerbytes;
448
be89f62cdb9a compiler warning cleanup
pygame
parents: 374
diff changeset
101 int loop1, loop2, step1, step2, length, length2=0;
372
88501955fada add initial sndarray
pygame
parents:
diff changeset
102 Uint8 *src, *dst;
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
114 mixerbytes = 1;
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
115 else
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
124 }
88501955fada add initial sndarray
pygame
parents:
diff changeset
125 else
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
133 }
373
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
134 length = array->dimensions[0];
519
4a882200a8e9 Chad Lester's work on sndarray
pygame
parents: 479
diff changeset
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
4a882200a8e9 Chad Lester's work on sndarray
pygame
parents: 479
diff changeset
137 {
373
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
138 length2 = array->dimensions[1];
519
4a882200a8e9 Chad Lester's work on sndarray
pygame
parents: 479
diff changeset
139 step2 = array->strides[1];
4a882200a8e9 Chad Lester's work on sndarray
pygame
parents: 479
diff changeset
140 }
4a882200a8e9 Chad Lester's work on sndarray
pygame
parents: 479
diff changeset
141 else
4a882200a8e9 Chad Lester's work on sndarray
pygame
parents: 479
diff changeset
142 {
4a882200a8e9 Chad Lester's work on sndarray
pygame
parents: 479
diff changeset
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
4a882200a8e9 Chad Lester's work on sndarray
pygame
parents: 479
diff changeset
146 }
479
e12ce508da5b better Numeric support for python2.3
pygame
parents: 448
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
153 /*let's hope Mix_Chunk never changes also*/
519
4a882200a8e9 Chad Lester's work on sndarray
pygame
parents: 479
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
156 chunk->allocated = 1;
88501955fada add initial sndarray
pygame
parents:
diff changeset
157 chunk->volume = 128;
373
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
158
519
4a882200a8e9 Chad Lester's work on sndarray
pygame
parents: 479
diff changeset
159 if (step1 == mixerbytes * numchannels && step2 == mixerbytes)
372
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
165 }
88501955fada add initial sndarray
pygame
parents:
diff changeset
166 else
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
175 {
373
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
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
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
178 *(Uint8*)dst = (Uint8)*((Uint8*)src);
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
179 break;
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
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
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
182 *(Uint8*)dst = (Uint8)*((Uint16*)src);
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
183 break;
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
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
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
186 *(Uint8*)dst = (Uint8)*((Uint32*)src);
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
187 break;
372
88501955fada add initial sndarray
pygame
parents:
diff changeset
188 }
88501955fada add initial sndarray
pygame
parents:
diff changeset
189 }
88501955fada add initial sndarray
pygame
parents:
diff changeset
190 }
88501955fada add initial sndarray
pygame
parents:
diff changeset
191 else
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
197 {
373
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
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
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
200 *(Uint16*)dst = (Uint16)(*((Uint8*)src)<<8);
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
201 break;
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
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
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
204 *(Uint16*)dst = (Uint16)*((Uint16*)src);
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
205 break;
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
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
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
208 *(Uint16*)dst = (Uint16)*((Uint32*)src);
68a41ca8b17c array and sound work
pygame
parents: 372
diff changeset
209 break;
372
88501955fada add initial sndarray
pygame
parents:
diff changeset
210 }
88501955fada add initial sndarray
pygame
parents:
diff changeset
211 }
88501955fada add initial sndarray
pygame
parents:
diff changeset
212 }
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
216 }
88501955fada add initial sndarray
pygame
parents:
diff changeset
217
88501955fada add initial sndarray
pygame
parents:
diff changeset
218 static PyMethodDef sndarray_builtins[] =
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
225 };
88501955fada add initial sndarray
pygame
parents:
diff changeset
226
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
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
88501955fada add initial sndarray
pygame
parents:
diff changeset
248 }