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

Updated SDL Sound code #336

Merged
merged 1 commit into from Nov 17, 2019
Merged

Updated SDL Sound code #336

merged 1 commit into from Nov 17, 2019

Conversation

angstsmurf
Copy link
Contributor

@angstsmurf angstsmurf commented Oct 21, 2018

This implements the extended GLK sound functions:

glk_schannel_create_ext()
glk_schannel_play_multi()
glk_schannel_set_volume_ext()
glk_schannel_pause()
glk_schannel_unpause()

What is still missing is working sound and volume notifications. They won't fire until some other event, such as the player pressing a key or moving the mouse, comes along, as reported in #204. This is implemented in #337, if only for Windows and macOS at present.

These changes only touch the SDL sound backend, and I have not been able to test whether FMOD sound still works.

I've made a test program here.

garglk/sndsdl.c Outdated
Comment on lines 652 to 654
if (chan == NULL)
gli_strict_warning("MOD player called with an invalid channel!");
Copy link
Contributor

Choose a reason for hiding this comment

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

Since chan is dereferenced below, I presume the function should return here instead of continuing.

garglk/sndsdl.c Outdated
if (chan->timer)
SDL_RemoveTimer((SDL_TimerID)chan->timer);

chan->timer = (int)SDL_AddTimer((Uint32)(duration / FADE_GRANULARITY), volume_timer_callback, (void *)chan);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is indented with a tab instead of 4 spaces.

Copy link
Contributor

Choose a reason for hiding this comment

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

In addition, chan->timer is an int and SDL_AddTimer returns a pointer. On many 64-bit systems this could result in the truncation of the pointer. timer should probably just be made an SDL_TimerID instead of int. I think you'd have to add a macro, e.g. -DGARGLK_USESDL when building with SDL, so you can conditionally include SDL/SDL_timer.h in garglk/garglk.h and conditionally define timer. In fact, you could probably conditionally include most/all of struct glk_schannel_struct so the members don't exist unless SDL is being used for sound.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review! I actually think SDL_TimerID changes from a pointer to an int between SDL versions, which is why the code ended up this way, but not really an excuse. I'll see if I can work out how to fix it properly.

garglk/sndsdl.c Outdated
{
gli_strict_warning("sound callback failed");

if (!sound_channel)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is indented too far.

garglk/sndsdl.c Outdated

if (!sound_channel)
{
fprintf(stderr, "sound_completion_callback called with invalid sound_channel %d\n", chan);
Copy link
Contributor

Choose a reason for hiding this comment

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

This ought to be done with gli_strict_warning, building up a message first with snprintf. Ideally gli_strict_warning would take a format string and variable number of arguments. I've added #355 to do this.

garglk/sndsdl.c Outdated
if (result && paused)
{
if (paused)
fprintf(stderr, "glk_schannel_play_ext: pausing channel again\n");
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this message necessary? If so, presumably it should be done via gli_strict_warning.

@angstsmurf angstsmurf force-pushed the new-sound branch 2 times, most recently from 0676261 to 91604ef Compare November 11, 2019 20:26
@angstsmurf
Copy link
Contributor Author

Right, I think I've done all the requested changes, and it seems to build fine on my Mac at least. Someone should test if it still works with FMOD sound.

@angstsmurf
Copy link
Contributor Author

angstsmurf commented Nov 11, 2019

A weird thing is that if I try to make the other SDL-specific members of glk_schannel_struct (sdl_rwops, sdl_memory and sdl_channel) also conditional on SDL sound, most games will break and only show a blank screen.

@angstsmurf angstsmurf force-pushed the new-sound branch 2 times, most recently from f9435a2 to 144a5fa Compare November 14, 2019 21:24
@angstsmurf
Copy link
Contributor Author

Squashed all commits into one. Hopefully this makes the changes more readable.

garglk/garglk.h Outdated
@@ -32,6 +32,10 @@

#include "gi_dispa.h"

#ifdef GARGLK_USESDL
#include "SDL/SDL_timer.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

One last thing here: I just tried a Windows build, and it fails because the include path points directly to the SDL directory, meaning that SDL_timer.h has to be included directly, not using the SDL/ prefix. This seems to be the proper way to do it on Unix systems too, as pkg-config sdl --cflags spits out -I/usr/include/SDL.

I'd also request using angle brackets instead of quotes since SDL is nominally a system library, even though on Windows it's being included directly, e.g.:

#include <SDL_timer.h>

The test program looked good, so with this one change, I'll merge it in.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed and squashed again.

This implements the extended GLK sound functions:

glk_schannel_create_ext()
glk_schannel_play_multi()
glk_schannel_set_volume_ext()
glk_schannel_pause()
glk_schannel_unpause()

Sound and volume notifications are still not working properly.
See bug report garglk#204 and pull request garglk#337.

These changes only touch the SDL sound backend.

I've made a test program here:
 https://github.com/angstsmurf/soundtest/releases
@angstsmurf
Copy link
Contributor Author

Rewrote the PR description and commit message.

@cspiegel cspiegel merged commit 2d098b0 into garglk:master Nov 17, 2019
@cspiegel
Copy link
Contributor

Thanks for implementing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants