public SoundInstance PlayAttachedSound(string url, Entity parent, float pitch = 1f, float volume = 1f, float distanceScale = 1f, bool looped = false) { Vector3 pos = parent.Transform.WorldPosition(); float sqrDist = (pos - AudioEngine.DefaultListener.Position).LengthSquared(); if (MaxSoundDistance > 0f && sqrDist >= MaxSoundDistance * MaxSoundDistance) { return(null); } SoundInstance s = getFreeInstance(url, true); if (s == null) { return(null); } s.Pitch = pitch < 0f ? RandomPitch() : pitch; s.Volume = volume * MasterVolume; s.IsLooping = looped; s.Pan = 0f; s.Apply3D(pos, null, null, distanceScale); s.Play(); var posSnd = new PositionalSound() { pos = pos, soundInstance = s, entity = parent, distance_scale = distanceScale }; lock (currentAttached) { currentAttached.Add(posSnd); } return(s); }
public void UpdatePlayingSoundPositions() { for (int i = 0; i < currentAttached.Count; i++) { PositionalSound ps = currentAttached[i]; if (ps.entity.Scene == null) { ps.soundInstance.Stop(); currentAttached.RemoveAt(i); i--; continue; } else if (ps.soundInstance.PlayState == Media.PlayState.Stopped) { currentAttached.RemoveAt(i); i--; continue; } Vector3 newpos = ps.entity.Transform.WorldPosition(); ps.soundInstance.Apply3D(newpos, newpos - ps.pos, null, ps.distance_scale); ps.pos = newpos; } }
public void UpdatePlayingSoundPositions(float?overrideTimePerFrame = null) { for (int i = 0; i < currentAttached.Count; i++) { PositionalSound ps = currentAttached[i]; if (ps.entity.Scene == null) { ps.soundInstance.Stop(); currentAttached.RemoveAt(i); i--; continue; } else if (ps.soundInstance.PlayState == Media.PlayState.Stopped) { currentAttached.RemoveAt(i); i--; continue; } Vector3 newpos = ps.entity.Transform.WorldPosition(); float timePerFrame = overrideTimePerFrame ?? ((float)internalGame.UpdateTime.Elapsed.Ticks / TimeSpan.TicksPerSecond); ps.soundInstance.Apply3D(newpos, (newpos - ps.pos) / timePerFrame, null, ps.distance_scale); ps.pos = newpos; } }