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 namehint argument to music.queue #2750

Merged
merged 2 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions buildconfig/pygame-stubs/music.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
from typing import Optional
from typing import Optional, Union, IO

def load(filename: str) -> None: ...
if sys.version_info >= (3, 6):
from os import PathLike
AnyPath = Union[str, bytes, PathLike[str], PathLike[bytes]]
else:
AnyPath = Union[Text, bytes]

def load(filename: Union[AnyPath, IO], namehint: Optional[str] = "") -> None: ...
def unload() -> None: ...
def play(loops: int = 0, start: float = 0.0, fade_ms: int = 0) -> None: ...
def rewind() -> None: ...
Expand All @@ -13,6 +19,6 @@ def get_volume() -> float: ...
def get_busy() -> bool: ...
def set_pos(pos: float) -> None: ...
def get_pos() -> int: ...
def queue(filename: str) -> None: ...
def queue(filename: Union[AnyPath, IO], namehint: Optional[str] = "") -> None: ...
def set_endevent(event_type: int) -> None: ...
def get_endevent() -> int: ...
12 changes: 10 additions & 2 deletions docs/reST/ref/music.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ can crash the program, ``e.g``. Debian Linux. Consider using ``OGG`` instead.
a music stream is already playing it will be stopped. This does not start
the music playing.

If you are loading from a file object, the ``namehint`` parameter can be used to specify
the type of music data in the object. For example: :code:`load(fileobj, "mp3")`.
If you are loading from a file object, the namehint parameter can be used to specify
the type of music data in the object. For example: :code:`load(fileobj, "ogg")`.

.. versionchanged:: 2.0.2 Added optional ``namehint`` argument

.. ## pygame.mixer.music.load ##

Expand Down Expand Up @@ -218,13 +220,17 @@ can crash the program, ``e.g``. Debian Linux. Consider using ``OGG`` instead.

| :sl:`queue a sound file to follow the current`
| :sg:`queue(filename) -> None`
| :sg:`queue(fileobj, namehint="") -> None`

This will load a sound file and queue it. A queued sound file will begin as
soon as the current sound naturally ends. Only one sound can be queued at a
time. Queuing a new sound while another sound is queued will result in the
new sound becoming the queued sound. Also, if the current sound is ever
stopped or changed, the queued sound will be lost.

If you are loading from a file object, the namehint parameter can be used to specify
the type of music data in the object. For example: :code:`queue(fileobj, "ogg")`.

The following example will play music by Bach six times, then play music by
Mozart once:

Expand All @@ -234,6 +240,8 @@ can crash the program, ``e.g``. Debian Linux. Consider using ``OGG`` instead.
pygame.mixer.music.play(5) # Plays six times, not five!
pygame.mixer.music.queue('mozart.ogg')

.. versionchanged:: 2.0.2 Added optional ``namehint`` argument

.. ## pygame.mixer.music.queue ##

.. function:: set_endevent
Expand Down
3 changes: 2 additions & 1 deletion src_c/doc/music_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define DOC_PYGAMEMIXERMUSICGETBUSY "get_busy() -> bool\ncheck if the music stream is playing"
#define DOC_PYGAMEMIXERMUSICSETPOS "set_pos(pos) -> None\nset position to play from"
#define DOC_PYGAMEMIXERMUSICGETPOS "get_pos() -> time\nget the music play time"
#define DOC_PYGAMEMIXERMUSICQUEUE "queue(filename) -> None\nqueue a sound file to follow the current"
#define DOC_PYGAMEMIXERMUSICQUEUE "queue(filename) -> None\nqueue(fileobj, namehint="") -> None\nqueue a sound file to follow the current"
#define DOC_PYGAMEMIXERMUSICSETENDEVENT "set_endevent() -> None\nset_endevent(type) -> None\nhave the music send an event when playback stops"
#define DOC_PYGAMEMIXERMUSICGETENDEVENT "get_endevent() -> type\nget the event a channel sends when playback stops"

Expand Down Expand Up @@ -80,6 +80,7 @@ get the music play time

pygame.mixer.music.queue
queue(filename) -> None
queue(fileobj, namehint="") -> None
queue a sound file to follow the current

pygame.mixer.music.set_endevent
Expand Down
87 changes: 31 additions & 56 deletions src_c/music.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,8 @@ _get_type_from_hint(char *namehint)
return type;
}

static PyObject *
music_load(PyObject *self, PyObject *args)
{
Mix_Music *
_load_music(PyObject *args) {
PyObject *obj;
PyObject *oencoded;
Mix_Music *new_music = NULL;
Expand All @@ -375,8 +374,6 @@ music_load(PyObject *self, PyObject *args)
return NULL;
}

MIXER_INIT_CHECK();

oencoded = pg_EncodeString(obj, "UTF-8", NULL, pgExc_SDLError);
if (oencoded == Py_None) {
SDL_RWops *rw;
Expand All @@ -401,19 +398,36 @@ music_load(PyObject *self, PyObject *args)
}
else if (oencoded != NULL) {
name = Bytes_AS_STRING(oencoded);
Py_BEGIN_ALLOW_THREADS new_music = Mix_LoadMUS(name);
Py_END_ALLOW_THREADS Py_DECREF(oencoded);

Py_BEGIN_ALLOW_THREADS
new_music = Mix_LoadMUS(name);
Py_END_ALLOW_THREADS

Py_DECREF(oencoded);
}
else {
return NULL;
}

if (new_music == NULL) {
return RAISE(pgExc_SDLError, SDL_GetError());
if (!new_music) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return NULL;
}

return new_music;
}

static PyObject *
music_load(PyObject *self, PyObject *args)
{
Mix_Music *new_music = NULL;
MIXER_INIT_CHECK();

new_music = _load_music(args);
if (new_music == NULL) // meaning it has an error to return
return NULL;

Py_BEGIN_ALLOW_THREADS if (current_music != NULL)
{
Py_BEGIN_ALLOW_THREADS if (current_music != NULL) {
Mix_FreeMusic(current_music);
current_music = NULL;
}
Expand All @@ -423,7 +437,8 @@ music_load(PyObject *self, PyObject *args)
}
Py_END_ALLOW_THREADS

current_music = new_music;
current_music = new_music;

Py_RETURN_NONE;
}

Expand All @@ -442,59 +457,19 @@ music_unload(PyObject *self, PyObject *noarg)
queue_music = NULL;
}
Py_END_ALLOW_THREADS

Py_RETURN_NONE;
}

static PyObject *
music_queue(PyObject *self, PyObject *args)
{
PyObject *obj;
PyObject *oencoded;
Mix_Music *local_queue_music = NULL;
const char *name;

if (!PyArg_ParseTuple(args, "O", &obj)) {
return NULL;
}

MIXER_INIT_CHECK();

oencoded = pg_EncodeString(obj, "UTF-8", NULL, pgExc_SDLError);
if (oencoded == Py_None) {
SDL_RWops *rw;

Py_DECREF(oencoded);
if (!PG_CHECK_THREADS())
return NULL;
rw = pgRWops_FromFileObject(obj);
if (rw == NULL) {
return NULL;
}

Py_BEGIN_ALLOW_THREADS
#if IS_SDLv1
local_queue_music = Mix_LoadMUS_RW(rw);
#else /* IS_SDLv2 */
local_queue_music = Mix_LoadMUS_RW(rw, SDL_TRUE);
#endif /* IS_SDLv2 */
Py_END_ALLOW_THREADS
}
else if (oencoded != NULL) {
name = Bytes_AS_STRING(oencoded);

Py_BEGIN_ALLOW_THREADS
local_queue_music = Mix_LoadMUS(name);
Py_END_ALLOW_THREADS

Py_DECREF(oencoded);
}
else {

local_queue_music = _load_music(args);
if (local_queue_music == NULL) // meaning it has an error to return
return NULL;
}

if (local_queue_music == NULL) {
return RAISE(pgExc_SDLError, SDL_GetError());
}

Py_BEGIN_ALLOW_THREADS
/* Free any existing queued music. */
Expand Down
17 changes: 17 additions & 0 deletions test/mixer_music_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ def test_load_object(self):
with open(bmusfn, "rb") as musf:
pygame.mixer.music.load(musf)

def test_object_namehint(self):
"""test loading & queuing music from file-like objects with namehint argument."""
formats = ["wav", "ogg"]
data_fname = example_path("data")
for f in formats:
path = os.path.join(data_fname, "house_lo.%s" % f)
if os.sep == "\\":
path = path.replace("\\", "\\\\")
bmusfn = filesystem_encode(path)

# these two "with open" blocks need to be separate, which is kinda weird
with open(bmusfn, "rb") as musf:
pygame.mixer.music.load(musf, f)

with open(bmusfn, "rb") as musf:
pygame.mixer.music.queue(musf, f)

def test_load_unicode(self):
"""test non-ASCII unicode path"""
import shutil
Expand Down