Skip to content

Commit

Permalink
Merge pull request #2590 from gresm/channel-queue-sound-subclass
Browse files Browse the repository at this point in the history
Subclassing Sounds + Channels.
  • Loading branch information
Starbuck5 authored Dec 10, 2023
2 parents c86d247 + d504072 commit 5482b63
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions buildconfig/stubs/pygame/mixer.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Dict, Optional, Tuple, Union, final, overload
from typing import Any, Dict, Optional, Tuple, Union, overload

import numpy

Expand Down Expand Up @@ -70,7 +70,7 @@ class Sound:
def get_length(self) -> float: ...
def get_raw(self) -> bytes: ...

@final

class Channel:
def __init__(self, id: int) -> None: ...
@property
Expand Down
1 change: 1 addition & 0 deletions docs/reST/ref/mixer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ change the default buffer by calling :func:`pygame.mixer.pre_init` before

.. versionchanged:: 2.1.4 This class is also available through the ``pygame.Channel``
alias.
.. versionchanged:: 2.4.0 It is now possible to create subclasses of ``pygame.mixer.Channel``

.. attribute:: id

Expand Down
6 changes: 3 additions & 3 deletions src_c/include/pygame_mixer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ typedef struct {
PYGAMEAPI_DEFINE_SLOTS(mixer);

#define pgSound_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(mixer, 0))

#define pgSound_Check(x) ((x)->ob_type == &pgSound_Type)
#define pgSound_Check(x) (PyObject_IsInstance(x, (PyObject *)&pgSound_Type))

#define pgSound_New \
(*(PyObject * (*)(Mix_Chunk *)) PYGAMEAPI_GET_SLOT(mixer, 1))
Expand All @@ -60,7 +59,8 @@ PYGAMEAPI_DEFINE_SLOTS(mixer);
(*(PyObject * (*)(PyObject *, PyObject *)) PYGAMEAPI_GET_SLOT(mixer, 2))

#define pgChannel_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(mixer, 3))
#define pgChannel_Check(x) ((x)->ob_type == &pgChannel_Type)
#define pgChannel_Check(x) \
(PyObject_IsInstance(x, (PyObject *)&pgChannel_Type))

#define pgChannel_New (*(PyObject * (*)(int)) PYGAMEAPI_GET_SLOT(mixer, 4))

Expand Down
6 changes: 4 additions & 2 deletions src_c/mixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ static PyObject *
pgSound_New(Mix_Chunk *);
static PyObject *
pgChannel_New(int);
#define pgSound_Check(x) (Py_TYPE(x) == &pgSound_Type)
#define pgChannel_Check(x) (Py_TYPE(x) == &pgChannel_Type)
#define pgSound_Check(x) (PyObject_IsInstance(x, (PyObject *)&pgSound_Type))
#define pgChannel_Check(x) \
(PyObject_IsInstance(x, (PyObject *)&pgChannel_Type))

static int
snd_getbuffer(PyObject *, Py_buffer *, int);
Expand Down Expand Up @@ -1373,6 +1374,7 @@ static PyTypeObject pgChannel_Type = {
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "pygame.mixer.Channel",
.tp_basicsize = sizeof(pgChannelObject),
.tp_dealloc = channel_dealloc,
.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE),
.tp_doc = DOC_MIXER_CHANNEL,
.tp_methods = channel_methods,
.tp_init = (initproc)channel_init,
Expand Down
15 changes: 15 additions & 0 deletions test/mixer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,10 @@ def test_id_getter(self):
self.assertEqual(ch1.id, 1)
self.assertEqual(ch2.id, 2)

def test_subclass(self):
class MyChannel(mixer.Channel):
pass


class ChannelInteractiveTest(unittest.TestCase):
__tags__ = ["interactive"]
Expand Down Expand Up @@ -1264,6 +1268,17 @@ def __init__(self, file):
except Exception:
self.fail("This should not raise an exception.")

channel = mixer.Channel(0)
try:
channel.play(correct)
except Exception:
self.fail("This should not raise an exception.")

self.assertIsInstance(channel.get_sound(), CorrectSublass)
self.assertIs(channel.get_sound(), correct)

channel.stop()

def test_incorrect_subclassing(self):
class IncorrectSuclass(mixer.Sound):
def __init__(self):
Expand Down

0 comments on commit 5482b63

Please sign in to comment.