How do I interrupt sound_library sounds?

I have a function that calls several commands from sound_library.h, like so:

#include ../sound/sound_library.h

  RefPtr<BufferedWavPlayer> wav_player_;

  void Loop() override {
    PropBase::Loop();
    sound_library_.Poll(wav_player_);
  }

  void CheckBattery() {
    SaberBase::DoEffect(EFFECT_BATTERY_LEVEL, 0);
    if (SFX_mnum) {
      sound_library_.SaySelect();
      sound_library_.SayTheBatteryLevelIs();
      sound_library_.SayNumber(battery_monitor.battery_percent(), SAY_WHOLE);
      sound_library_.SayPercent();
    } else {
      talkie.Say(spBATTERYLEVEL);
      talkie.SayNumber((int)floorf(battery_monitor.battery_percent()));
      talkie.Say(spPERCENT);
    }
  }

I want to add a test to see whether something is already playing via sound_library, and immediately stop it if it is, before playing the battery level sounds. That is, to reset the sound queue and start over.

I tried this:

if (wav_player_->isPlaying()) wav_player_->Stop();
sound_library_.SaySelect();
sound_library_.SayTheBatteryLevelIs();
sound_library_.SayNumber(battery_monitor.battery_percent(), SAY_WHOLE);
sound_library_.SayPercent();

However, the Stop function seems to break the sound_library queue completely. Is there a way to do this correctly?

If you want to reset the queue of sounds, then you’ll need to do it from inside the sound library. You would also need to add a clear method to the SoundQueue class. (Which lives in props/prop_base.h for some reason)

Not sure what you mean by “break the sound_library queue completely” though.

I mean once it receives a Stop() it doesn’t play any further sounds that are triggered. Normal effects can be triggered, although sometimes those don’t work either. I haven’t checked the serial monitor as to why.

It might be crashing because you’re trying to do wav_player_->Stop() when wav_player_ is null?

Does it work better if you do:

  if (wav_player_) wav_player_->Stop();

or maybe:

  if (wav_player_ && wav_player->isPlaying()) wav_player_->Stop();

?

Tried your second suggestion, doesn’t seem to work. Sound library sounds play just fine until I trigger that Stop. After that, Sound library sounds won’t play at all until I change to a different preset. Oddly, in the serial monitor, I see “Playing mselect.wav” and other sounds listed even though the sounds aren’t actually playing. No errors in the serial monitor:

EVENT: Aux-HeldMedium#2 ON millis=182134
Enter Volume Menu
Playing mselect.wav, channels: 1 rate: 44100 bits: 16
Playing vmbegin.wav, channels: 1 rate: 44100 bits: 16
EVENT: Aux-Released#2 ON millis=182321
EVENT: Aux-Released ON millis=182321
EVENT: Aux-Pressed#1 ON millis=182436
EVENT: Aux-Pressed ON millis=182436
unit = 3 vol = 0.00, Playing Caiwyn/swingl/swingl02.wav
channels: 1 rate: 44100 bits: 16
unit = 4 vol = 0.00, Playing Caiwyn/swingh/swingh02.wav
channels: 1 rate: 44100 bits: 16
EVENT: Aux-Released#1 ON millis=182550
EVENT: Aux-Released ON millis=182550
EVENT: Aux-Shortclick#1 ON millis=182550
EVENT: Aux-Shortclick ON millis=182550
Volume Reset
Current Volume: 2000
Playing mselect.wav, channels: 1 rate: 44100 bits: 16
Playing volmax.wav, channels: 1 rate: 44100 bits: 16

Hmm, it occurs to me that stop() now leaves the volume at zero for the player.
That’s probably not expected, so maybe I should fix that…

Sync and try again?

Sure enough, that fixed it. Not only that, but it seems I don’t need to do anything special to empty the sound queue. Either the sound queue already clears when wav_player_ stops, or the next time sound_library_.Play() is invoked, it overwrites the existing queue. Whatever the case, I’m getting the desired result now. Thanks!

Most of the time there is only one sound playing, and nothing in the queue.
However, when you do something that plays a number, then multiple sounds are stringed together, and that’s when you would need to clear the queue, as calling Stop would only end the currently playing sound.

I see. So I’ll have to edit the sound_library and the SoundQueue class after all…

If you want to interrupt the sound library properly, then yes.