Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loop and fade options to pygame.mixer.channel.queue() #3252

Closed
4 changes: 4 additions & 0 deletions buildconfig/stubs/pygame/math.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ class Vector2(_GenericVector):
xy: Vector2
yx: Vector2
yy: Vector2
@property
def angle(self) -> float: ...
@property
def angle_rad(self) -> float: ...
@overload
def __init__(
self: _TVec,
Expand Down
17 changes: 17 additions & 0 deletions docs/reST/ref/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,23 @@ Multiple coordinates can be set using slices or swizzling
find that either the margin is too large or too small, in which case changing ``epsilon`` slightly
might help you out.

.. attribute:: angle

| :sl:`Gives the angle of the vector in degrees, relative to the X-axis, normalized to the interval [-180, 180].`

Read-only attribute representing the angle of the vector in degrees relative to the X-axis. This angle is normalized to
the interval [-180, 180].

Usage: Accessing `angle` provides the current angle of the vector in degrees within the predefined range of [-180, 180].

.. attribute:: angle_rad

| :sl:`Gives the angle of the vector in radians, relative to the X-axis, normalized to the interval [-π, π].`

Read-only attribute representing the angle of the vector in radians relative to the X-axis. This value is equivalent
to the `angle` attribute converted to radians and is normalized to the interval [-π, π].

Usage: Accessing `angle_rad` provides the current angle of the vector in radians within the predefined range of [-π, π].

.. ## pygame.math.Vector2 ##

Expand Down
9 changes: 9 additions & 0 deletions docs/reST/ref/mixer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,15 @@ The following file formats are supported
If there is no sound actively playing on the Channel then the Sound will
begin playing immediately.

The optional parameters ``loops`` and ``fade_ms`` control how the queued
Sound is played:

- ``loops``: The number of times the queued Sound will loop. A value of 0
means the sound will play once, 1 means it will play twice, and so on.
- ``fade_ms``: The duration of the fade-in effect in milliseconds when the
queued Sound starts playing. If set to 0 (default), the Sound will play
without a fade.

.. ## Channel.queue ##

.. method:: get_queue
Expand Down
2 changes: 2 additions & 0 deletions src_c/doc/math_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#define DOC_MATH_VECTOR2_CLAMPMAGNITUDEIP "clamp_magnitude_ip(max_length, /) -> None\nclamp_magnitude_ip(min_length, max_length, /) -> None\nClamps the vector's magnitude between max_length and min_length"
#define DOC_MATH_VECTOR2_UPDATE "update() -> None\nupdate(int) -> None\nupdate(float) -> None\nupdate(Vector2) -> None\nupdate(x, y) -> None\nupdate((x, y)) -> None\nSets the coordinates of the vector."
#define DOC_MATH_VECTOR2_EPSILON "Determines the tolerance of vector calculations."
#define DOC_MATH_VECTOR2_ANGLE "Gives the angle of the vector in degrees, relative to the X-axis, normalized to the interval [-180, 180]."
#define DOC_MATH_VECTOR2_ANGLERAD "Gives the angle of the vector in radians, relative to the X-axis, normalized to the interval [-π, π]."
#define DOC_MATH_VECTOR3 "Vector3() -> Vector3(0, 0, 0)\nVector3(int) -> Vector3\nVector3(float) -> Vector3\nVector3(Vector3) -> Vector3\nVector3(x, y, z) -> Vector3\nVector3((x, y, z)) -> Vector3\na 3-Dimensional Vector"
#define DOC_MATH_VECTOR3_DOT "dot(Vector3, /) -> float\ncalculates the dot- or scalar-product with the other vector"
#define DOC_MATH_VECTOR3_CROSS "cross(Vector3, /) -> Vector3\ncalculates the cross- or vector-product"
Expand Down
63 changes: 63 additions & 0 deletions src_c/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@

#define TWO_PI (2. * M_PI)

#define RAD_TO_DEG (180.0 / M_PI)
#define DEG_TO_RAD (M_PI / 180.0)

#ifndef M_PI_2
#define M_PI_2 (M_PI / 2.0)
#endif /* M_PI_2 */
Expand Down Expand Up @@ -142,6 +145,8 @@ _vector_coords_from_string(PyObject *str, char **delimiter, double *coords,
static void
_vector_move_towards_helper(Py_ssize_t dim, double *origin_coords,
double *target_coords, double max_distance);
static double
_pg_atan2(double y, double x);

/* generic vector functions */
static PyObject *
Expand Down Expand Up @@ -202,6 +207,10 @@ vector_sety(pgVector *self, PyObject *value, void *closure);
static int
vector_setz(pgVector *self, PyObject *value, void *closure);
static PyObject *
vector_get_angle(pgVector *self, void *closure);
static PyObject *
vector_get_angle_rad(pgVector *self, void *closure);
static PyObject *
vector_richcompare(PyObject *o1, PyObject *o2, int op);
static PyObject *
vector_length(pgVector *self, PyObject *args);
Expand Down Expand Up @@ -631,6 +640,40 @@ vector_dealloc(pgVector *self)
Py_TYPE(self)->tp_free((PyObject *)self);
}

/*
*Returns rhe arctangent of the quotient y / x, in radians, considering the
*following special cases: atan2((anything), NaN ) is NaN; atan2(NAN ,
*(anything) ) is NaN; atan2(+-0, +(anything but NaN)) is +-0 ; atan2(+-0,
*-(anything but NaN)) is +-pi ; atan2(+-(anything but 0 and NaN), 0) is
*+-pi/2; atan2(+-(anything but INF and NaN), +INF) is +-0 ; atan2(+-(anything
*but INF and NaN), -INF) is +-pi; atan2(+-INF,+INF ) is +-pi/4 ;
* atan2(+-INF,-INF ) is +-3pi/4;
* atan2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2;
*
*/
static double
_pg_atan2(double y, double x)
{
if (Py_IS_NAN(x) || Py_IS_NAN(y)) {
return Py_NAN;
}

if (Py_IS_INFINITY(y)) {
if (Py_IS_INFINITY(x)) {
return copysign((copysign(1., x) == 1.) ? 0.25 * Py_MATH_PI
: 0.75 * Py_MATH_PI,
y);
}
return copysign(0.5 * Py_MATH_PI, y);
}

if (Py_IS_INFINITY(x) || y == 0.) {
return copysign((copysign(1., x) == 1.) ? 0. : Py_MATH_PI, y);
}

return atan2(y, x);
}

/**********************************************
* Generic vector PyNumber emulation routines
**********************************************/
Expand Down Expand Up @@ -1269,6 +1312,23 @@ vector_setz(pgVector *self, PyObject *value, void *closure)
return vector_set_component(self, value, 2);
}

static PyObject *
vector_get_angle_rad(pgVector *self, void *closure)
{
double angle_rad = _pg_atan2(self->coords[1], self->coords[0]);

return PyFloat_FromDouble(angle_rad);
}

static PyObject *
vector_get_angle(pgVector *self, void *closure)
{
double angle_rad = _pg_atan2(self->coords[1], self->coords[0]);
double angle_deg = angle_rad * RAD_TO_DEG;

return PyFloat_FromDouble(angle_deg);
}

static PyObject *
vector_richcompare(PyObject *o1, PyObject *o2, int op)
{
Expand Down Expand Up @@ -2585,6 +2645,9 @@ static PyMethodDef vector2_methods[] = {
static PyGetSetDef vector2_getsets[] = {
{"x", (getter)vector_getx, (setter)vector_setx, NULL, NULL},
{"y", (getter)vector_gety, (setter)vector_sety, NULL, NULL},
{"angle", (getter)vector_get_angle, NULL, DOC_MATH_VECTOR2_ANGLE, NULL},
{"angle_rad", (getter)vector_get_angle_rad, NULL,
DOC_MATH_VECTOR2_ANGLERAD, NULL},
{NULL, 0, NULL, NULL, NULL} /* Sentinel */
};

Expand Down
55 changes: 47 additions & 8 deletions src_c/mixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ static char *request_devicename = NULL;
struct ChannelData {
PyObject *sound;
PyObject *queue;
int queue_loop;
int queue_fade_ms;
int endevent;
};
static struct ChannelData *channeldata = NULL;
Expand Down Expand Up @@ -294,15 +296,35 @@ endsound_callback(int channel)

if (channeldata[channel].queue) {
PyGILState_STATE gstate = PyGILState_Ensure();
int channelnum;
Mix_Chunk *sound = pgSound_AsChunk(channeldata[channel].queue);

// Récupérer les paramètres de la queue
int queue_loop = channeldata[channel].queue_loop;
int queue_fade_ms = channeldata[channel].queue_fade_ms;

// Libérer le son actuel et déplacer le son de la queue dans
// "sound"
Py_XDECREF(channeldata[channel].sound);
channeldata[channel].sound = channeldata[channel].queue;
channeldata[channel].queue = NULL;
PyGILState_Release(gstate);
channelnum = Mix_PlayChannelTimed(channel, sound, 0, -1);
if (channelnum != -1)

// Jouer le son avec les paramètres de fade-in ou directement
int channelnum;
if (queue_fade_ms > 0) {
channelnum = Mix_FadeInChannelTimed(channel, sound, queue_loop,
queue_fade_ms, -1);
}
else {
channelnum =
Mix_PlayChannelTimed(channel, sound, queue_loop, -1);
}

// Associer le canal au groupe
if (channelnum != -1) {
Mix_GroupChannel(channelnum, (int)(intptr_t)sound);
}

PyGILState_Release(gstate);
}
else {
PyGILState_STATE gstate = PyGILState_Ensure();
Expand Down Expand Up @@ -1074,22 +1096,35 @@ chan_get_id(PyObject *self, PyObject *empty_args)
}

static PyObject *
chan_queue(PyObject *self, PyObject *sound)
chan_queue(PyObject *self, PyObject *args, PyObject *kwargs)
{
int channelnum = pgChannel_AsInt(self);
Mix_Chunk *chunk;
PyObject *sound;
int loop = 0;
int fade_ms = 0;

static char *kwlist[] = {"sound", "loop", "fade_ms", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ii", kwlist, &sound,
&loop, &fade_ms)) {
return RAISE(PyExc_TypeError,
"Expected arguments: sound, optional int loop, optional "
"int fade_ms");
}

if (!pgSound_Check(sound)) {
return RAISE(PyExc_TypeError,
"The argument must be an instance of Sound");
"The first argument must be an instance of Sound");
}

chunk = pgSound_AsChunk(sound);
CHECK_CHUNK_VALID(chunk, NULL);

if (!channeldata[channelnum].sound) /*nothing playing*/
{
Py_BEGIN_ALLOW_THREADS;
channelnum = Mix_PlayChannelTimed(channelnum, chunk, 0, -1);
channelnum = Mix_PlayChannelTimed(channelnum, chunk, loop, -1);
if (channelnum != -1)
Mix_GroupChannel(channelnum, (int)(intptr_t)chunk);
Py_END_ALLOW_THREADS;
Expand All @@ -1100,8 +1135,11 @@ chan_queue(PyObject *self, PyObject *sound)
else {
Py_XDECREF(channeldata[channelnum].queue);
channeldata[channelnum].queue = sound;
channeldata[channelnum].queue_loop = loop;
channeldata[channelnum].queue_fade_ms = fade_ms;
Py_INCREF(sound);
}

Py_RETURN_NONE;
}

Expand Down Expand Up @@ -1321,7 +1359,8 @@ static PyGetSetDef _channel_getsets[] = {
static PyMethodDef channel_methods[] = {
{"play", (PyCFunction)chan_play, METH_VARARGS | METH_KEYWORDS,
DOC_MIXER_CHANNEL_PLAY},
{"queue", chan_queue, METH_O, DOC_MIXER_CHANNEL_QUEUE},
{"queue", (PyCFunction)chan_queue, METH_VARARGS | METH_KEYWORDS,
DOC_MIXER_CHANNEL_QUEUE},
{"get_busy", (PyCFunction)chan_get_busy, METH_NOARGS,
DOC_MIXER_CHANNEL_GETBUSY},
{"fadeout", chan_fadeout, METH_VARARGS, DOC_MIXER_CHANNEL_FADEOUT},
Expand Down
98 changes: 98 additions & 0 deletions test/math_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,104 @@ def test_del_y(self):
exception = ctx.exception
self.assertEqual(str(exception), "Cannot delete the y attribute")

def test_angle_rad_property(self):
v0 = Vector2(1, 0)
self.assertEqual(v0.angle_rad, 0.0)

v1 = Vector2(0, 1)
self.assertEqual(v1.angle_rad, math.pi / 2)

v2 = Vector2(-1, 0)
self.assertEqual(v2.angle_rad, math.pi)

v3 = Vector2(0, -1)
self.assertEqual(v3.angle_rad, -math.pi / 2)

v4 = Vector2(1, 1)
self.assertEqual(v4.angle_rad, math.pi / 4)

v5 = Vector2(-1, 1)
self.assertEqual(v5.angle_rad, 3 * math.pi / 4)

v6 = Vector2(-1, -1)
self.assertEqual(v6.angle_rad, -3 * math.pi / 4)

v7 = Vector2(1, -1)
self.assertEqual(v7.angle_rad, -math.pi / 4)

v8 = Vector2(float('inf'), float('inf'))
self.assertEqual(v8.angle_rad, math.pi / 4)

v9 = Vector2(float('-inf'), float('inf'))
self.assertEqual(v9.angle_rad, 3 * math.pi / 4)

v10 = Vector2(float('-inf'), float('-inf'))
self.assertEqual(v10.angle_rad, -3 * math.pi / 4)

v11 = Vector2(float('inf'), float('-inf'))
self.assertEqual(v11.angle_rad, -math.pi / 4)

v12 = Vector2(0, 0)
self.assertEqual(v12.angle_rad, 0.0)

v13 = Vector2(float('nan'), 1)
self.assertTrue(math.isnan(v13.angle_rad))

v14 = Vector2(1, float('nan'))
self.assertTrue(math.isnan(v14.angle_rad))

v15 = Vector2(float('nan'), float('nan'))
self.assertTrue(math.isnan(v15.angle_rad))

def test_angle_property(self):
v0 = pygame.math.Vector2(1, 0)
self.assertEqual(v0.angle, 0.0)

v1 = pygame.math.Vector2(0, 1)
self.assertEqual(v1.angle, 90.0)

v2 = pygame.math.Vector2(-1, 0)
self.assertEqual(v2.angle, 180.0)

v3 = pygame.math.Vector2(0, -1)
self.assertEqual(v3.angle, -90.0)

v4 = pygame.math.Vector2(1, 1)
self.assertEqual(v4.angle, 45.0)

v5 = pygame.math.Vector2(-1, 1)
self.assertEqual(v5.angle, 135.0)

v6 = pygame.math.Vector2(-1, -1)
self.assertEqual(v6.angle, -135.0)

v7 = pygame.math.Vector2(1, -1)
self.assertEqual(v7.angle, -45.0)

v8 = pygame.math.Vector2(float('inf'), float('inf'))
self.assertEqual(v8.angle, 45.0)

v9 = pygame.math.Vector2(float('-inf'), float('inf'))
self.assertEqual(v9.angle, 135.0)

v10 = pygame.math.Vector2(float('-inf'), float('-inf'))
self.assertEqual(v10.angle, -135.0)

v11 = pygame.math.Vector2(float('inf'), float('-inf'))
self.assertEqual(v11.angle, -45.0)

v12 = pygame.math.Vector2(0, 0)
self.assertEqual(v12.angle, 0.0)

v13 = pygame.math.Vector2(float('nan'), 1)
self.assertTrue(math.isnan(v13.angle))

v14 = pygame.math.Vector2(1, float('nan'))
self.assertTrue(math.isnan(v14.angle))

v15 = pygame.math.Vector2(float('nan'), float('nan'))
self.assertTrue(math.isnan(v15.angle))


class Vector3TypeTest(unittest.TestCase):
def setUp(self):
Expand Down
Loading