[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Re: [BUG] mixer.init() mixer.get_init() inconsistancies 1.8.0
2008/6/8 Charlie Nolan <funnyman3595@xxxxxxxxx>:
> In case it's useful, here are my results, from pygame 1.8.0 on Gentoo Linux:
>
> Tried (11025, -16, 1), got (11025, -16, 0)
> Sample rate equal? True
> Sample width equal? True
> Sample number of output channels equal? False
> Tried (11025, -16, 2), got (11025, -16, 1)
> Sample rate equal? True
> Sample width equal? True
> Sample number of output channels equal? False
*snip*
Yeah, that's identical to what I got. Here's a patch against the head
(rev.1286) of the trunk which fixes all the problems except:
* 16 bit unsigned is not allowed as a sample format
* For sample width-- in: -8, out: +8
Index: src/mixer.c
===================================================================
--- src/mixer.c (revision 1286)
+++ src/mixer.c (working copy)
@@ -267,8 +267,9 @@
Py_RETURN_NONE;
//create a signed or unsigned number of bits per sample
+ // XXX: When mixer is init'd with a format of -8, this returns +8
realform = format&~0xff ? - (format&0xff) : format&0xff;
- return Py_BuildValue ("(iii)", freq, realform, channels > 1);
+ return Py_BuildValue ("(iii)", freq, realform, channels);
}
static PyObject*
Index: lib/_numpysndarray.py
===================================================================
--- lib/_numpysndarray.py (revision 1286)
+++ lib/_numpysndarray.py (working copy)
@@ -37,22 +37,17 @@
import pygame.mixer as mixer
import numpy
-def array (sound):
- """pygame._numpysndarray.array(Sound): return array
-
- Copy Sound samples into an array.
-
- Creates a new array for the sound data and copies the samples. The
- array will always be in the format returned from
- pygame.mixer.get_init().
- """
+def _array_samples(sound, raw):
# Info is a (freq, format, stereo) tuple
info = mixer.get_init ()
if not info:
raise pygame.error, "Mixer not initialized"
fmtbytes = (abs (info[1]) & 0xff) >> 3
- channels = mixer.get_num_channels ()
- data = sound.get_buffer ().raw
+ channels = info[2]
+ if raw:
+ data = sound.get_buffer ().raw
+ else:
+ data = sound.get_buffer ()
shape = (len (data) / channels * fmtbytes, )
if channels > 1:
@@ -70,6 +65,17 @@
array.shape = shape
return array
+def array (sound):
+ """pygame._numpysndarray.array(Sound): return array
+
+ Copy Sound samples into an array.
+
+ Creates a new array for the sound data and copies the samples. The
+ array will always be in the format returned from
+ pygame.mixer.get_init().
+ """
+ return _array_samples(sound, True)
+
def samples (sound):
"""pygame._numpysndarray.samples(Sound): return array
@@ -79,30 +85,8 @@
object. Modifying the array will change the Sound. The array will
always be in the format returned from pygame.mixer.get_init().
"""
- # Info is a (freq, format, stereo) tuple
- info = pygame.mixer.get_init ()
- if not info:
- raise pygame.error, "Mixer not initialized"
- fmtbytes = (abs (info[1]) & 0xff) >> 3
- channels = mixer.get_num_channels ()
- data = sound.get_buffer ()
+ return _array_samples(sound, False)
- shape = (data.length / channels * fmtbytes, )
- if channels > 1:
- shape = (shape[0], 2)
-
- # mixer.init () does not support different formats from the ones below,
- # so MSB/LSB stuff is silently ignored.
- typecode = { 8 : numpy.uint8, # AUDIO_U8
- 16 : numpy.uint16, # AUDIO_U16
- -8 : numpy.int8, # AUDIO_S8
- -16 : numpy.int16 # AUDUI_S16
- }[info[1]]
-
- array = numpy.frombuffer (data, typecode)
- array.shape = shape
- return array
-
def make_sound (array):
"""pygame._numpysndarray.make_sound(array): return Sound
>
> On 6/7/08, Frankie <Frankie.Robertson@xxxxxxxxxxxxxx> wrote:
>> On Jun 7, 3:59 pm, Frankie <Frankie.Robert...@xxxxxxxxxxxxxx> wrote:
>> *snip*
>>
*snip*
>>