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

Enabling users to set the soundfont on midi playback #2274

Merged
merged 46 commits into from
Jul 29, 2023

Conversation

AlexanderGroeger
Copy link
Contributor

@AlexanderGroeger AlexanderGroeger commented Jun 27, 2023

This PR adds a new set_soundfont function to the pygame.mixer.music module. This new feature allows game developers to leverage to pay a lower cost for storing music in their game by using smaller midi-formatted music tied with any soundfont they choose.

Technical Notice
This PR relies on SDL_mixer's Mix_SetSoundFonts function. At the initial commit, tests showed that setting the soundfont before loading and playing a MID file did not yield using the specified soundfont but instead fell back on the system default soundfont. This needs to be addressed before a merge will contribute anything.

UPDATE
A new get_soundfont function was added to pygame.mixer.music to verify whether SDL_mixer's internal soundfonts variable was correctly being updated. It turns out both functions work correctly, but there is still something in the SDL_mixer that is not immediately using the updated soundfont variable.

UPDATE 2
It appears the feature works on Raspbian, but not Windows. This means the compilation of SDL_Mixer needs to be considered in the compilation of pygame-ce because it is not compiled to support soundfonts for all operation systems. At this time, the only known working platform is Raspbian 64-bit.

UPDATE 3
set_soundfont now allows passing an empty string or no argument to clear the soundfont_paths variable in the SDL_Mixer. A stub has been added for both functions. I didn't see a MIDI used in the tests, so I wasn't sure if one existed, so both tests are left as todo tests. The docs now reflect this update too.

UPDATE 4
The todo tests have now been replaced with real tests that validate setting and getting soundfont paths will yield expected behavior.

UPDATE 5
More quick fixes. Some extra testing has been reported.
@oddbookworm and @rethanon have done some testing. On main (for both manjaro linux and mac), loading a midi yields a pygame error Couldn't open timidity.cfg which is the alternative to fluidsynth. Installing timidity with yay removes the error, but does not produce music. On this PR, (for both manjaro linux and mac), setting the soundfont before loading a midi works as intended. It seems Windows is still an issue.

UPDATE 6
Moved API to pygame.mixer, fixed some typos, and allowed set_soundfont to accept a None argument.

@AlexanderGroeger AlexanderGroeger requested a review from a team as a code owner June 27, 2023 16:27
@Starbuck5
Copy link
Member

For the two failing runs you asked about clarification about:

  • The code needs to be linted. Run python setup.py format after installing clang-format and black.
  • Type stubs need to be added for this new API. See buildconfig/stubs/pygame/

src_c/music.c Outdated Show resolved Hide resolved
src_c/music.c Outdated Show resolved Hide resolved
docs/reST/ref/music.rst Outdated Show resolved Hide resolved
@Starbuck5
Copy link
Member

As well as the type stubs the CI complained about, this PR also needs an entry into the pygame-ce test suite. See the tests/ directory.

@Starbuck5 Starbuck5 added mixer.music pygame.mixer.music New API This pull request may need extra debate as it adds a new class or function to pygame labels Jun 29, 2023
docs/reST/ref/music.rst Outdated Show resolved Hide resolved
test/mixer_music_test.py Outdated Show resolved Hide resolved
test/mixer_music_test.py Outdated Show resolved Hide resolved
Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, thanks for contributing to pygame-ce! 🍰

Left a couple of typing related reviews. Also I'd like to discuss the API a bit more? Should we go with SDL's semicolon-separated style? I wouldn't mind making the API more pythonic and having the users deal with sequences of strings

buildconfig/stubs/pygame/mixer_music.pyi Outdated Show resolved Hide resolved
buildconfig/stubs/pygame/mixer_music.pyi Outdated Show resolved Hide resolved
AlexanderGroeger and others added 3 commits July 5, 2023 20:16
Co-authored-by: Ankith <46915066+ankith26@users.noreply.github.com>
Co-authored-by: Ankith <46915066+ankith26@users.noreply.github.com>
@Starbuck5
Copy link
Member

Starbuck5 commented Jul 11, 2023

Question: why is this in mixer.music? Does this not apply to midi added as sounds (I'm on Windows so I can't test)? If it applies to all midi I'd think it should go in the mixer music.

@AlexanderGroeger
Copy link
Contributor Author

Question: why is this in mixer.music? Does this not apply to midi added as sounds (I'm on Windows so I can't test)? If it applies to all midi I'd think it should go in the mixer music.

I'm not sure what you're asking. It is in mixer.music, and you're suggesting it should go in mixer music....

@oddbookworm
Copy link
Member

Starbuck5 is probably referring to pygame.midi

@Starbuck5
Copy link
Member

Sorry I mistyped. I meant why does it not go in mixer.

@AlexanderGroeger
Copy link
Contributor Author

AlexanderGroeger commented Jul 11, 2023

I hadn't considered this. I tested mixer.Sound with the soundfont selected and confirmed it works on Raspbian. I can move the new API over. Which would be better to move to? pygame.mixer or pygame.midi.

src_c/mixer.c Outdated Show resolved Hide resolved
Copy link
Member

@oddbookworm oddbookworm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, looks good. I just have a few (mostly minor) nitpicks

src_c/doc/music_doc.h Outdated Show resolved Hide resolved
docs/reST/ref/mixer.rst Outdated Show resolved Hide resolved
docs/reST/ref/music.rst Outdated Show resolved Hide resolved
src_c/mixer.c Outdated Show resolved Hide resolved
src_c/mixer.c Outdated Show resolved Hide resolved
src_c/mixer.c Outdated Show resolved Hide resolved
src_c/mixer.c Outdated Show resolved Hide resolved
test/mixer_test.py Outdated Show resolved Hide resolved
src_c/mixer.c Outdated Show resolved Hide resolved
@AlexanderGroeger
Copy link
Contributor Author

One final note here. Perhaps we can learn something from this issue to get soundfonts working on windows.

@AlexanderGroeger
Copy link
Contributor Author

I tried including fluidsynth in the windows build config but didn't have the compiling experience or intuition to make the SDL_Mixer use soundfonts on windows, so I'd prefer to wrap up this PR and make an issue regarding this.

docs/reST/ref/mixer.rst Outdated Show resolved Hide resolved
Co-authored-by: Ankith <46915066+ankith26@users.noreply.github.com>
Copy link
Member

@ankith26 ankith26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the PR 🎉

PS: to the person merging this, please squash and merge this PR (you may remove any extra co-author tags that github adds)

Copy link
Member

@Starbuck5 Starbuck5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work getting this up to be fully ready!

@Starbuck5 Starbuck5 dismissed oddbookworm’s stale review July 29, 2023 08:55

Changes addressed.

@Starbuck5 Starbuck5 merged commit c36788a into pygame-community:main Jul 29, 2023
31 of 32 checks passed
@Starbuck5 Starbuck5 added this to the 2.3.1 milestone Jul 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mixer.music pygame.mixer.music New API This pull request may need extra debate as it adds a new class or function to pygame
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants