Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: StillInThe90s on Mon 18/05/2015 16:41:47

Title: workaround for the "no digital sound" crash
Post by: StillInThe90s on Mon 18/05/2015 16:41:47
Without starting another discussion about what AGS should and should not do, I'm trying to find a workaround for the sound issue discussed in this thread (http://www.adventuregamestudio.co.uk/forums/index.php?topic=42186.0). I.e. if "no digital sound" option is chosen in the setup, the code below will make your game crash.
Code (ags) Select
AudioChannel *audio = aMusic.Play();
audio.Volume = 70;

...unless you wrap everything in a "if(audio){}" which is too much work for me at this point.

I suppose there is no way of checking if this option is chosen, from within an ags-application? Could I do this by raw reading acetup.cfg? (been unsuccessful so far) Changing the name of audio.vox with a custom setup could do the trick, since ags can check for files. Any obvious dangers there? (such as windows security)

Thanks.
Title: Re: workaround for the "no digital sound" crash
Post by: Wyz on Mon 18/05/2015 17:12:35
Without making any changes to the engine the best I can come up with is a wrapper object. This is structure that you assign an AudioChannel object and implements all functionality that AudioChannel objects already have but with the added null check.

For this example it would contain the following:
Code (ags) Select

struct SafeAudioChannel
{
  AudioChannel *audio;

  void SetVolume(int volume)
  {
    if (audio)
      audio.Volume = volume;
  }

  int GetVolume(int volume)
  {
    if (audio)
      return audio.Volume;
    return -1;
  }
 
  // ... many more potentially
};


Which you can use like this:
Code (ags) Select

SafeAudioChannel channel;
channel.audio = aMusic.Play();
channel.SetVolume(70);


Would be nice to have this as a module I guess. :)
Title: Re: workaround for the "no digital sound" crash
Post by: Snarky on Mon 18/05/2015 17:17:09
Should be AudioChannel* audio; â€" but yeah.
Title: Re: workaround for the "no digital sound" crash
Post by: StillInThe90s on Mon 18/05/2015 21:23:55
Interesting.
Could this be done with already defined global AudioChannels as well?
Title: Re: workaround for the "no digital sound" crash
Post by: Wyz on Tue 19/05/2015 01:24:42
I believe so yes. It would need the usual im/exporting and setting up like other global variables but I can't think of a reason why not.
Title: Re: workaround for the "no digital sound" crash
Post by: StillInThe90s on Tue 19/05/2015 17:29:07
Thanks Wyz.
Unfortunately, it seems that this solution was horrible to implement in retrospect. I will have to go for bracket wrapping this time, but any new project will have safe audio structs for sure. ...that is, if nobody makes it even easier and writes a module -COUGH!-
Title: Re: workaround for the "no digital sound" crash
Post by: Snarky on Tue 19/05/2015 17:51:19
Quote from: StillInThe90s on Tue 19/05/2015 17:29:07
Unfortunately, it seems that this solution was horrible to implement in retrospect.

How so? Seems to me that there should only be two or three steps to it:

1. Create the struct type like Wyz outlines, wrapping all the API methods you need.
2. If you have any AudioChannel* globalvars, change them to regular variables exported by a script and imported in the header.
3. Change every AudioChannel* declaration to a SafeAudioChannel declaration.

The only possible problem I can see is if you've been passing AudioChannel* variables as arguments to functions, or had them as members of other structs.
Title: Re: workaround for the "no digital sound" crash
Post by: StillInThe90s on Tue 19/05/2015 22:03:52
It's hard because my script is a mess, but I think I got it. :)
QuoteThe only possible problem I can see is if you've been passing AudioChannel* variables as arguments to functions...
Well, sort of. At least now I know this is bad practice.
Title: Re: workaround for the "no digital sound" crash
Post by: Snarky on Tue 19/05/2015 22:36:29
No, not necessarily. It's just that it interferes with this particular workaround because of one of the most frustrating limitations in the AGS scripting language.
Title: Re: workaround for the "no digital sound" crash
Post by: Crimson Wizard on Tue 02/06/2015 01:09:43
You can also use helper functions like
Code (ags) Select

AudioChannel *SafePlayAtVolume(AudioClip *clip, int volume)
{
    AudioChannel *ch = clip.Play();
    if (ch)
        ch.Volume = volume;
    return ch;
}
Title: Re: workaround for the "no digital sound" crash
Post by: StillInThe90s on Wed 03/06/2015 23:41:36
Well that looks simple enough.
Will definitely try it out.