-
-
Notifications
You must be signed in to change notification settings - Fork 158
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
pygame.Sound.copy
implementation
#3238
Draft
Borishkof
wants to merge
9
commits into
pygame-community:main
Choose a base branch
from
thomasgrgi:sound-copy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+123
−0
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
532482a
sound copy part 1
bilhox 40bacbb
error message
bilhox 1fd41e2
snd_cpoy tests
Borishkof aec7da7
sound copy first fix and better tests
Borishkof 3be9a1c
Documentation for pygame.Sound.copy.
Borishkof 726244d
Better documentation for pygame.sound.copy
Borishkof 4bfd1e8
stubs addition and fixing mp3 test
Borishkof 446c2f1
"Add xm and opus to tested formats
Borishkof cc67e0d
safe loading files format
Borishkof File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -831,6 +831,72 @@ snd_get_samples_address(PyObject *self, PyObject *closure) | |
#endif | ||
} | ||
|
||
static PyObject * | ||
snd_copy(PyObject *self, PyObject *_null) | ||
{ | ||
Mix_Chunk *chunk = pgSound_AsChunk(self); | ||
pgSoundObject *new_sound; | ||
Mix_Chunk *new_chunk; | ||
|
||
// Validate the input chunk | ||
CHECK_CHUNK_VALID(chunk, NULL); | ||
|
||
// Create a new sound object | ||
new_sound = | ||
(pgSoundObject *)pgSound_Type.tp_new(Py_TYPE(self), NULL, NULL); | ||
if (!new_sound) { | ||
PyErr_SetString(PyExc_MemoryError, | ||
"Failed to allocate memory for new sound object"); | ||
return NULL; | ||
} | ||
|
||
// Handle chunk allocation type | ||
if (chunk->allocated) { | ||
// Create a deep copy of the audio buffer for allocated chunks | ||
Uint8 *buffer_copy = (Uint8 *)malloc(chunk->alen); | ||
if (!buffer_copy) { | ||
Py_DECREF(new_sound); | ||
PyErr_SetString(PyExc_MemoryError, | ||
"Failed to allocate memory for sound buffer"); | ||
return NULL; | ||
} | ||
memcpy(buffer_copy, chunk->abuf, chunk->alen); | ||
|
||
// Create a new Mix_Chunk | ||
new_chunk = Mix_QuickLoad_RAW(buffer_copy, chunk->alen); | ||
if (!new_chunk) { | ||
free(buffer_copy); | ||
Py_DECREF(new_sound); | ||
PyErr_SetString(pgExc_SDLError, | ||
"Failed to create new sound chunk"); | ||
return NULL; | ||
} | ||
new_chunk->volume = chunk->volume; | ||
new_sound->chunk = new_chunk; | ||
} | ||
else { | ||
// For non-allocated chunks (e.g., formats like .xm), create a full | ||
// copy | ||
new_chunk = (Mix_Chunk *)malloc(sizeof(Mix_Chunk)); | ||
if (!new_chunk) { | ||
Py_DECREF(new_sound); | ||
PyErr_SetString(PyExc_MemoryError, | ||
"Failed to allocate memory for sound chunk"); | ||
return NULL; | ||
} | ||
*new_chunk = *chunk; // Copy the entire structure | ||
Comment on lines
+880
to
+887
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copying an entire struct like this may work in this scenario but in my opinion is not something that is safe in general. From SDL's perspective these are all internal fields and they can do whatever they want with it. They're barely documented. |
||
|
||
// For safety, ensure the copied chunk doesn't share pointers | ||
new_chunk->abuf = | ||
NULL; // Prevent double-free if original gets deallocated | ||
new_chunk->allocated = 0; | ||
|
||
new_sound->chunk = new_chunk; | ||
} | ||
|
||
return (PyObject *)new_sound; | ||
} | ||
|
||
PyMethodDef sound_methods[] = { | ||
{"play", (PyCFunction)pgSound_Play, METH_VARARGS | METH_KEYWORDS, | ||
DOC_MIXER_SOUND_PLAY}, | ||
|
@@ -842,6 +908,8 @@ PyMethodDef sound_methods[] = { | |
{"get_volume", snd_get_volume, METH_NOARGS, DOC_MIXER_SOUND_GETVOLUME}, | ||
{"get_length", snd_get_length, METH_NOARGS, DOC_MIXER_SOUND_GETLENGTH}, | ||
{"get_raw", snd_get_raw, METH_NOARGS, DOC_MIXER_SOUND_GETRAW}, | ||
{"copy", snd_copy, METH_NOARGS, DOC_MIXER_SOUND_COPY}, | ||
bilhox marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{"__copy__", snd_copy, METH_NOARGS, DOC_MIXER_SOUND_COPY}, | ||
{NULL, NULL, 0, NULL}}; | ||
|
||
static PyGetSetDef sound_getset[] = { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is a memory leak. This is allocated but never freed, and it can't be freed here because it needs to live as long as the chunk does.