Skip to content

Support formats option in sdl2 mixer #24158

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

Merged
merged 8 commits into from
Apr 30, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -2470,6 +2470,7 @@ def test_sdl_get_key_name(self):
def test_sdl2_mixer_wav(self):
self.emcc(test_file('browser/test_sdl2_mixer_wav.c'), ['-sUSE_SDL_MIXER=2'], output_filename='a.out.js')
self.emcc(test_file('browser/test_sdl2_mixer_wav.c'), ['--use-port=sdl2_mixer'], output_filename='a.out.js')
self.emcc(test_file('browser/test_sdl2_mixer_wav.c'), ['--use-port=sdl2_mixer:formats=ogg'], output_filename='a.out.js')
Copy link
Collaborator

Choose a reason for hiding this comment

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

How does this test (which plays a wav file) work if only ogg is selected? Is this because wav is simply always available maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I think so. wav is already available:

flags = [
'-sUSE_SDL=2',
'-DMUSIC_WAV',
]


def test_sdl2_linkable(self):
# Ensure that SDL2 can be built with LINKABLE. This implies there are no undefined
Expand Down
2 changes: 1 addition & 1 deletion tools/ports/sdl2_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def needed(settings):


def get_formats(settings):
return set(settings.SDL2_IMAGE_FORMATS).union(opts['formats'])
return opts['formats'].union(settings.SDL2_IMAGE_FORMATS)


def get_lib_name(settings):
Expand Down
47 changes: 38 additions & 9 deletions tools/ports/sdl2_mixer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# found in the LICENSE file.

import os
from typing import Dict, Set

TAG = 'release-2.8.0'
HASH = '494ccd74540f74e717f7e4f1dc7f96398c0f4b1883ab00c4a76b0c7239bd2c185cb4358a35ef47819c49e7c14dac7c37b98a29c7b5237478121571f5e7ac4dfc'
Expand All @@ -16,14 +17,28 @@
'sdl2_mixer-none-mt': {'SDL2_MIXER_FORMATS': [], 'PTHREADS': 1},
}

OPTIONS = {
'formats': 'A comma separated list of formats (ex: --use-port=sdl2_mixer:formats=ogg,mp3)'
}

SUPPORTED_FORMATS = {'ogg', 'mp3', 'mod', 'mid'}

# user options (from --use-port)
opts: Dict[str, Set] = {
'formats': set()
}


def needed(settings):
return settings.USE_SDL_MIXER == 2


def get_formats(settings):
return opts['formats'].union(settings.SDL2_MIXER_FORMATS)


def get_lib_name(settings):
settings.SDL2_MIXER_FORMATS.sort()
formats = '-'.join(settings.SDL2_MIXER_FORMATS)
formats = '-'.join(sorted(get_formats(settings)))

libname = 'libSDL2_mixer'
if formats != '':
Expand All @@ -41,30 +56,33 @@ def get(ports, settings, shared):

def create(final):
source_path = ports.get_dir('sdl2_mixer', 'SDL_mixer-' + TAG)

formats = get_formats(settings)

flags = [
'-sUSE_SDL=2',
'-DMUSIC_WAV',
]

if "ogg" in settings.SDL2_MIXER_FORMATS:
if "ogg" in formats:
flags += [
'-sUSE_VORBIS',
'-DMUSIC_OGG',
]

if "mp3" in settings.SDL2_MIXER_FORMATS:
if "mp3" in formats:
flags += [
'-sUSE_MPG123',
'-DMUSIC_MP3_MPG123',
]

if "mod" in settings.SDL2_MIXER_FORMATS:
if "mod" in formats:
flags += [
'-sUSE_MODPLUG',
'-DMUSIC_MOD_MODPLUG',
]

if "mid" in settings.SDL2_MIXER_FORMATS:
if "mid" in formats:
flags += [
'-DMUSIC_MID_TIMIDITY',
]
Expand Down Expand Up @@ -107,16 +125,27 @@ def clear(ports, settings, shared):

def process_dependencies(settings):
settings.USE_SDL = 2
if "ogg" in settings.SDL2_MIXER_FORMATS:
formats = get_formats(settings)
if "ogg" in formats:
deps.append('vorbis')
settings.USE_VORBIS = 1
if "mp3" in settings.SDL2_MIXER_FORMATS:
if "mp3" in formats:
deps.append('mpg123')
settings.USE_MPG123 = 1
if "mod" in settings.SDL2_MIXER_FORMATS:
if "mod" in formats:
deps.append('libmodplug')
settings.USE_MODPLUG = 1


def handle_options(options, error_handler):
formats = options['formats'].split(',')
for format in formats:
format = format.lower().strip()
if format not in SUPPORTED_FORMATS:
error_handler(f'{format} is not a supported format')
else:
opts['formats'].add(format)


def show():
return 'sdl2_mixer (-sUSE_SDL_MIXER=2 or --use-port=sdl2_mixer; zlib license)'