Page 1 of 1

Retriggering an already playing sound

Posted: Sun Feb 09, 2020 3:33 am
by rrTea
I'm trying to retrigger a certain sound effect, so I dug out an old example of how this should be done.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnUpdate>
    <Group Comment="Sound 1 (works)">
      <Children>
        <Condition Expression="return trigger_sound1;">
          <OnTrue>
            <ZExpression>
              <Expression>
<![CDATA[trigger_sound1 = 0;
sfx_1.Length = 1;]]>
              </Expression>
            </ZExpression>
            <PlaySound Sound="sfx_1" NoteNr="60" ByReference="255"/>
          </OnTrue>
        </Condition>
        <KeyPress Keys="1" RepeatDelay="0.1">
          <OnPressed>
            <ZExpression>
              <Expression>
<![CDATA[sfx_1.Length = 0;
trigger_sound1 = 1;]]>
              </Expression>
            </ZExpression>
          </OnPressed>
        </KeyPress>
      </Children>
    </Group>
    <Group Comment="Sound 2">
      <Children>
        <Condition Expression="return trigger_sound2;">
          <OnTrue>
            <ZExpression>
              <Expression>
<![CDATA[trigger_sound2 = 0;
sfx_2.Length = 0.2;]]>
              </Expression>
            </ZExpression>
            <PlaySound Sound="sfx_2" NoteNr="60" ByReference="255"/>
          </OnTrue>
        </Condition>
        <KeyPress Keys="2" RepeatDelay="0.1">
          <OnPressed>
            <ZExpression>
              <Expression>
<![CDATA[sfx_2.Length = 0;
trigger_sound2 = 1;]]>
              </Expression>
            </ZExpression>
          </OnPressed>
        </KeyPress>
      </Children>
    </Group>
  </OnUpdate>
  <Content>
    <Sound Name="sfx_1" Volume="0.88" Osc1Waveform="1" Mod0Active="1" Mod0Destination="11" Mod0Amount="1" Mod1Active="1" Mod1Source="1" Mod1Destination="2" Mod1Amount="1" Env0Active="1" Env0DecayTime="0.38" Env0SustainLevel="0.328" Env0ReleaseTime="1.38" Env1Active="1" Env1AttackTime="0.22" Env1DecayTime="0.7" Env1SustainLevel="0.276"/>
    <Variable Name="trigger_sound1" Type="4"/>
    <Sound Name="sfx_2" Length="0.3" Volume="0.2" Osc1Waveform="3" Osc1PW="-0.87" UseOsc2="1" Osc2Volume="0.38" UseFilter="1" FilterCutoff="0.9091" FilterQ="0.0999" Mod0Active="1" Mod0Source="2" Mod0Destination="2" Mod0Amount="0.36" Mod1Active="1" Mod1Source="3" Mod1Destination="11" Mod1Amount="1" Mod2Active="1" Mod2Amount="1" Mod3Destination="11" Mod3Amount="1" Env0Active="1" Env0AttackTime="0.22" Env0ReleaseTime="0.22" Lfo0Active="1" Lfo0Style="2" Lfo0Speed="0.06" Lfo1Active="1" Lfo1IsBipolar="1" Lfo1Style="2" Lfo1Speed="0.24"/>
    <Variable Name="trigger_sound2"/>
  </Content>
</ZApplication>
Sound 1 in Group 1 is from the original example and it works as expected, but sound 2 (from my project) breaks and sounds different every time it's triggered. Why?

If you want to hear what the sound 2 really sounds like, play it in the Sound preview and don't click Start / press Alt+Enter (the sound will get glitchy as soon as it is played the first time in Preview).

Re: Retriggering an already playing sound

Posted: Sun Feb 09, 2020 12:21 pm
by Kjell
Hi rrTea,
rrTea wrote: Sun Feb 09, 2020 3:33 amSound 1 in Group 1 is from the original example and it works as expected, but sound 2 (from my project) breaks and sounds different every time it's triggered. Why?
It's because the LFOs are driven globally, so they don't get reset every time you re-trigger the sound.

K

Re: Retriggering an already playing sound

Posted: Mon Feb 10, 2020 8:01 am
by VilleK
Although in this case it is not the "GlobalLFO" that is used but the LFO that is local to the sound, so could perhaps be a bug here. The "ByReference" feature was never tested much. Let me know if this is something you are stuck with and then I'll investigate more.

Re: Retriggering an already playing sound

Posted: Mon Feb 10, 2020 9:51 am
by Kjell
Hej Ville,
VilleK wrote: Mon Feb 10, 2020 8:01 amAlthough in this case it is not the "GlobalLFO" that is used but the LFO that is local to the sound, so could perhaps be a bug here.
I didn't mean the global LFOs that are part of the AudioMixer, i meant the way that they are driven seemed "global". But perhaps the fact that they aren't reset / re-initialized by EmitNote ( unlike the Envelopes ) is simply a oversight?

K

Re: Retriggering an already playing sound

Posted: Mon Feb 10, 2020 10:42 am
by rrTea
I can also try to make the sound effect without using any LFOs (the first sound effect from the example doesn't use them)… I'll try some other things tomorrow.

Re: Retriggering an already playing sound

Posted: Mon Feb 10, 2020 1:53 pm
by VilleK
I think I solved the issue. Please download ZGE again and try.

Re: Retriggering an already playing sound

Posted: Thu Feb 13, 2020 2:06 am
by rrTea
It sounds like it works! Although… looking at it, it seems a bit strange that, to achieve this, I need to introduce a Condition, ZExpression and a variable dedicated to keep track of all this. (And maybe even make the ZExpression into a Component too.) (Edit: And once the sound is triggered by models, the actual playing of the sound probably has to be completely split from it and go to a some remote part of the ProjectTree, AppState or somewhere else like that). With this approach even the simplest event looks like this:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnUpdate>
    <Condition Expression="return trigger_sound2;">
      <OnTrue>
        <ZExpression>
          <Expression>
<![CDATA[trigger_sound2 = 0;
sfx_2.Length = 0.2;]]>
          </Expression>
        </ZExpression>
        <PlaySound Sound="sfx_2" NoteNr="60" ByReference="255"/>
      </OnTrue>
    </Condition>
    <KeyPress Comment="Trigger 1" Keys="1">
      <OnPressed>
        <CallComponent Component="play_Sound2"/>
      </OnPressed>
    </KeyPress>
    <KeyPress Comment="Trigger 2" Keys="2">
      <OnPressed>
        <CallComponent Component="play_Sound2"/>
      </OnPressed>
    </KeyPress>
  </OnUpdate>
  <Content>
    <Sound Name="sfx_2" Length="0.2" Volume="0.2" Osc1Waveform="3" Osc1PW="-0.87" UseOsc2="1" Osc2Volume="0.38" UseFilter="1" FilterCutoff="0.9091" FilterQ="0.0999" Mod0Active="1" Mod0Source="2" Mod0Destination="2" Mod0Amount="0.36" Mod1Active="1" Mod1Source="3" Mod1Destination="11" Mod1Amount="1" Mod2Active="1" Mod2Amount="1" Mod3Destination="11" Mod3Amount="1" Env0Active="1" Env0AttackTime="0.22" Env0ReleaseTime="0.22" Lfo0Active="1" Lfo0Style="2" Lfo0Speed="0.06" Lfo1Active="1" Lfo1IsBipolar="1" Lfo1Style="2" Lfo1Speed="0.24"/>
    <Variable Name="trigger_sound2"/>
    <ZExpression Name="play_Sound2">
      <Expression>
<![CDATA[sfx_2.Length = 0;
trigger_sound2 = 1;]]>
      </Expression>
    </ZExpression>
  </Content>
</ZApplication>
(+ the sound always plays with one frame of delay, nothing horrible but it is a bit strange as a concept)

If the PlaySound component ??? itself automagically (I guess it'd need another tickbox to control that?) the whole thing could work something like

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnUpdate>
    <KeyPress Comment="Triggered by Model 1" Keys="1">
      <OnPressed>
        <CallComponent Component="play_sfx1"/>
      </OnPressed>
    </KeyPress>
    <KeyPress Comment="Triggered by Model 2" Keys="2">
      <OnPressed>
        <CallComponent Component="play_sfx1"/>
      </OnPressed>
    </KeyPress>
  </OnUpdate>
  <Content>
    <Sound Name="sfx_2" Volume="0.2" Osc1Waveform="3" Osc1PW="-0.87" UseOsc2="1" Osc2Volume="0.38" UseFilter="1" FilterCutoff="0.9091" FilterQ="0.0999" Mod0Active="1" Mod0Source="2" Mod0Destination="2" Mod0Amount="0.47" Mod1Source="3" Mod1Destination="11" Mod1Amount="1" Mod2Active="1" Mod2Amount="1" Mod3Destination="11" Mod3Amount="1" Env0Active="1" Env0AttackTime="0.22" Env0ReleaseTime="0.22" Lfo0Active="1" Lfo0Style="2" Lfo0Speed="0.02" Lfo1Active="1" Lfo1IsBipolar="1" Lfo1Style="2" Lfo1Speed="0.24"/>
    <PlaySound Name="play_sfx1" Sound="sfx_2" NoteNr="60" ByReference="255"/>
  </Content>
</ZApplication>
(just as an example / this code doesn't actually retrigger the playing sound)

Re: Retriggering an already playing sound

Posted: Thu Feb 13, 2020 2:07 am
by rrTea
Anyway it works now & I'll implement it where needed! Thanks :D