public void StopSound(Sound s) { if (s == null) { return; } bool Match(PlayingSample p) => !p.Finished && p.AudioSample == s; if (playingSamples != null && playingSamples.Exists(Match)) { int index = playingSamples.FindIndex(Match); PlayingSample ps = playingSamples[index]; ps.Finished = true; playingSamples[index] = ps; } }
private float GetMixerOutput(int channel, float globalTime, float timeStep) { const float MaxValue = 1f / short.MaxValue; float mixerSample = 0.0f; if (playingSamples != null) { for (int i = 0; i < playingSamples.Count; i++) { PlayingSample ps = playingSamples[i]; float increment = ps.AudioSample.WavHeader.SamplesPerSec * timeStep; ps.SamplePosition += (long)Math.Ceiling(increment); if (ps.SamplePosition < ps.AudioSample.SampleCount) { mixerSample += ps.AudioSample.Samples[(ps.SamplePosition * ps.AudioSample.Channels) + channel] * MaxValue * ps.AudioSample.Volume; } else { if (ps.Loop) { ps.SamplePosition = 0; } else { ps.Finished = true; } } playingSamples[i] = ps; playingSamples.RemoveAll(s => s.Finished); } } mixerSample += OnSoundCreate(channel, globalTime, timeStep); mixerSample = OnSoundFilter(channel, globalTime, mixerSample); mixerSample *= MasterVolume; return(mixerSample); }
/// <summary> Play a given sound object. </summary> /// <param name="s"> Sound to play </param> public void PlaySound(Sound s) { if (s == null) { return; } if (playingSamples == null) { playingSamples = new List <PlayingSample>(); } PlayingSample ps = new PlayingSample { AudioSample = s, SamplePosition = 0, Finished = false, Loop = s.Loop }; playingSamples.Add(ps); }