SHARE
TWEET

SimpleAmplifier.cs

a guest Jan 17th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #if UNITY_EDITOR
  2. using UnityEditor;
  3. #endif
  4. using UnityEngine;
  5.  
  6. namespace Photon.Voice.Unity.UtilityScripts
  7. {
  8.     [RequireComponent(typeof(Recorder))]
  9.     public class SimpleAmplifier : VoiceComponent
  10.     {
  11.         [SerializeField]
  12.         private int amplificationFactor = 1;
  13.  
  14.         public int AmplificationFactor
  15.         {
  16.             get { return this.amplificationFactor; }
  17.             set
  18.             {
  19.                 if (this.amplificationFactor == value)
  20.                 {
  21.                     return;
  22.                 }
  23.                 this.amplificationFactor = value;
  24.                 if (this.floatProcessor != null)
  25.                 {
  26.                     this.floatProcessor.AmplificationFactor = this.amplificationFactor;
  27.                 }
  28.                 if (this.shortProcessor != null)
  29.                 {
  30.                     this.shortProcessor.AmplificationFactor = (short)this.amplificationFactor;
  31.                 }
  32.             }
  33.         }
  34.  
  35.         private SimpleAmplifierFloatProcessor floatProcessor;
  36.         private SimpleAmplifierShortProcessor shortProcessor;
  37.  
  38.         // Message sent by Recorder
  39.         private void PhotonVoiceCreated(Recorder.PhotonVoiceCreatedParams p)
  40.         {
  41.             if (p.Voice is LocalVoiceAudioFloat)
  42.             {
  43.                 LocalVoiceAudioFloat v = p.Voice as LocalVoiceAudioFloat;
  44.                 this.floatProcessor = new SimpleAmplifierFloatProcessor(this.amplificationFactor);
  45.                 v.AddPostProcessor(this.floatProcessor);
  46.             }
  47.             else if (p.Voice is LocalVoiceAudioShort)
  48.             {
  49.                 LocalVoiceAudioShort v = p.Voice as LocalVoiceAudioShort;
  50.                 this.shortProcessor = new SimpleAmplifierShortProcessor((short)this.amplificationFactor);
  51.                 v.AddPostProcessor(this.shortProcessor);
  52.             }
  53.             else if (this.Logger.IsErrorEnabled)
  54.             {
  55.                 this.Logger.LogError("LocalVoice object has unexpected value/type: {0}", p.Voice == null ? "null" : p.Voice.GetType().ToString());
  56.             }
  57.         }
  58.     }
  59.  
  60.     public class SimpleAmplifierFloatProcessor : Voice.IProcessor<float>
  61.     {
  62.         public int AmplificationFactor { get; set; }
  63.  
  64.         public SimpleAmplifierFloatProcessor(int amplificationFactor)
  65.         {
  66.             this.AmplificationFactor = amplificationFactor;
  67.         }
  68.  
  69.         public float[] Process(float[] buf)
  70.         {
  71.             for (int i = 0; i < buf.Length; i++)
  72.             {
  73.                 buf[i] *= this.AmplificationFactor;
  74.             }
  75.             return buf;
  76.         }
  77.  
  78.         public void Dispose()
  79.         {
  80.            
  81.         }
  82.     }
  83.  
  84.     public class SimpleAmplifierShortProcessor : Voice.IProcessor<short>
  85.     {
  86.         public short AmplificationFactor { get; set; }
  87.  
  88.         public SimpleAmplifierShortProcessor(short amplificationFactor)
  89.         {
  90.             this.AmplificationFactor = amplificationFactor;
  91.         }
  92.  
  93.         public short[] Process(short[] buf)
  94.         {
  95.             for (int i = 0; i < buf.Length; i++)
  96.             {
  97.                 buf[i] *= this.AmplificationFactor;
  98.             }
  99.             return buf;
  100.         }
  101.  
  102.         public void Dispose()
  103.         {
  104.            
  105.         }
  106.     }
  107.  
  108.     #if UNITY_EDITOR
  109.     [CustomEditor(typeof(SimpleAmplifier))]
  110.     public class SimpleAmplifierEditor : Editor
  111.     {
  112.         private SimpleAmplifier simpleAmplifier;
  113.  
  114.         private void OnEnable()
  115.         {
  116.             this.simpleAmplifier = this.target as SimpleAmplifier;
  117.         }
  118.  
  119.         public override void OnInspectorGUI()
  120.         {
  121.             EditorGUI.BeginChangeCheck();
  122.             this.simpleAmplifier.AmplificationFactor = EditorGUILayout.IntField(new GUIContent("AmplificationFactor", "Amplification Factor"),
  123.                 this.simpleAmplifier.AmplificationFactor);
  124.             if (EditorGUI.EndChangeCheck())
  125.             {
  126.                 serializedObject.ApplyModifiedProperties();
  127.             }
  128.         }
  129.     }
  130.     #endif
  131. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top