Advertisement
jedijosh920

Staticc Emitter Custom 3D Audio

Aug 26th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1. using GTA;
  2. using GTA.Native;
  3. using GTA.Math;
  4. using System;
  5. using System.Windows.Forms;
  6. using System.Collections.Generic;
  7. using IrrKlang;
  8.  
  9. public class StaticEmitterHandler : Script
  10. {
  11.     public static List<StaticEmitter> emitters = new List<StaticEmitter>();
  12.  
  13.     public StaticEmitterHandler()
  14.     {
  15.         Tick += OnTick;
  16.         Aborted += OnAborted;
  17.     }
  18.  
  19.     private void OnAborted(object sender, EventArgs e)
  20.     {
  21.         StaticEmitterAudio.SoundEngine.StopAllSounds();
  22.         StaticEmitterAudio.SoundEngine.Dispose();
  23.  
  24.         foreach (var emitter in emitters.ToArray())
  25.         {
  26.             if (emitter != null)
  27.             {
  28.                 emitter.Dispose(true);
  29.             }
  30.         }
  31.     }
  32.  
  33.     private void OnTick(object sender, EventArgs e)
  34.     {
  35.         foreach (var emitter in emitters.ToArray())
  36.         {
  37.             emitter.Tick();
  38.         }
  39.  
  40.         StaticEmitterAudio.ManageSoundEngine();
  41.     }
  42. }
  43.  
  44. public class StaticEmitter
  45. {
  46.     public Vector3 Position;
  47.     public StaticEmitterAudio Audio;
  48.     public float Range = 1f;
  49.  
  50.     public StaticEmitter(Vector3 pos, string filePath)
  51.     {
  52.         Audio = new StaticEmitterAudio(filePath);
  53.         Position = pos;
  54.         Audio.Play3DSound(Position, true, false, true);
  55.         StaticEmitterHandler.emitters.Add(this);
  56.     }
  57.  
  58.     public void Tick()
  59.     {
  60.         Audio.SetDistances(0f, Range);
  61.         Audio.ProcessSound(Position);
  62.     }
  63.  
  64.     public void Dispose(bool delete = false)
  65.     {
  66.         Audio.StopSound();
  67.         StaticEmitterHandler.emitters.Remove(this);
  68.     }
  69. }
  70.  
  71. public class StaticEmitterAudio
  72. {
  73.     public string FilePath;
  74.     public ISound Sound;
  75.     public ISoundEffectControl SoundEffect;
  76.     public float MaximumDistance = 20f;
  77.     public float MinimumDistance = 1f;
  78.  
  79.     public StaticEmitterAudio(string filepath)
  80.     {
  81.         FilePath = filepath;
  82.     }
  83.  
  84.     public void Play3DSound(Vector3 sourcePosition, bool playLooped, bool allowMultipleInstances = false, bool allowSoundEffects = false)
  85.     {
  86.         if (allowMultipleInstances || (!allowMultipleInstances && (Sound == null || Sound != null && Sound.Finished)))
  87.         {
  88.             Vector3D sourcePos = SoundHelperIK.Vector3ToVector3D(GameplayCamera.GetOffsetFromWorldCoords(sourcePosition));
  89.  
  90.             Sound = SoundEngine.Play3D(FilePath, sourcePos.X, sourcePos.Y, sourcePos.Z, playLooped, false, StreamMode.AutoDetect, allowSoundEffects);
  91.             if (allowSoundEffects)
  92.             {
  93.                 SoundEffect = Sound.SoundEffectControl;
  94.             }
  95.         }
  96.     }
  97.  
  98.     public void StopSound()
  99.     {
  100.         if (Sound == null || Sound.Finished) return;
  101.         Sound.Stop();
  102.     }
  103.  
  104.     public bool IsPlaying()
  105.     {
  106.         return Sound != null && !Sound.Finished;
  107.     }
  108.  
  109.     public void ProcessSound(Vector3 sourcePosition)
  110.     {
  111.         if (Sound != null && !Sound.Finished)
  112.         {
  113.             Sound.MaxDistance = MaximumDistance;
  114.             Sound.MinDistance = MinimumDistance;
  115.             Vector3D sourcePos = SoundHelperIK.Vector3ToVector3D(GameplayCamera.GetOffsetFromWorldCoords(sourcePosition));
  116.             Sound.Position = sourcePos;
  117.         }
  118.     }
  119.  
  120.     public void SetDistances(float max, float min)
  121.     {
  122.         MaximumDistance = max;
  123.         MinimumDistance = min;
  124.     }
  125.  
  126.     public void Dispose()
  127.     {
  128.         Sound.Stop();
  129.         Sound.Dispose();
  130.     }
  131.  
  132.     public static ISoundEngine SoundEngine = new ISoundEngine();
  133.  
  134.     public static void ManageSoundEngine()
  135.     {
  136.         SoundEngine.SetListenerPosition(new Vector3D(0, 0, 0), new Vector3D(0, 0, 1), new Vector3D(0, 0, 0), new Vector3D(0, 1, 0));
  137.         SoundEngine.Update();
  138.     }
  139. }
  140.  
  141. static class SoundHelperIK
  142. {
  143.     public static Vector3D Vector3ToVector3D(Vector3 vec)
  144.     {
  145.         return new Vector3D(vec.X, vec.Z, vec.Y);
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement