Lyxica

LPD8 Volume Script

May 1st, 2021 (edited)
1,691
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.07 KB | None | 0 0
  1. using System.Collections.Concurrent;
  2.  
  3. namespace SetAppVolumne
  4. {
  5.     static class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             //Console.WriteLine(MidiIn.DeviceInfo(0).ProductName);
  10.             var x = new LDP();
  11.         }
  12.     }
  13.    
  14.     public class MidiEvent
  15.     {
  16.         public int controller;
  17.         public float value;
  18.         public DateTime time;
  19.        
  20.         public MidiEvent(int controller, float value) {
  21.             this.controller = controller;
  22.             this.value = getNormalizedValue(value);
  23.             this.time = DateTime.Now.AddMilliseconds(8);
  24.         }
  25.        
  26.         private float getNormalizedValue(float value)
  27.         {
  28.             // Max = 127
  29.             float range = Math.Clamp(value, 5f, 120f) - 5f;
  30.             float norm = range / 115.0f;
  31.             return norm;
  32.         }
  33.     }
  34.  
  35.     public class LDP
  36.     {
  37.         [DllImport("user32.dll")]
  38.         public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId);
  39.  
  40.         [DllImport("user32.dll")]
  41.         private static extern IntPtr GetForegroundWindow();
  42.  
  43.         Dictionary<int, string> lut = new Dictionary<int, string>()
  44.         {
  45.             {51, "System"},
  46.             {52, "ActiveProcess"},
  47.             {55, "foobar2000"},
  48.             {57, "chrome"},
  49.             {58, "Discord"}
  50.         };
  51.  
  52.         ConcurrentDictionary<int, MidiEvent> event_list = new ConcurrentDictionary<int, SetAppVolumne.MidiEvent>();
  53.  
  54.         public LDP()
  55.         {
  56.             printProcessNames();
  57.            
  58.             var inmidi = new MidiIn(0);
  59.             inmidi.MessageReceived += midiIn_messageReceive;
  60.  
  61.             inmidi.Start();
  62.            
  63.             while (true)
  64.             {
  65.                 var now = DateTime.Now;
  66.                 foreach(var key in this.event_list.Keys)
  67.                 {
  68.                     var event_item = this.event_list[key];
  69.                     if (now >= event_item.time)
  70.                     {
  71.                         setVolumeForMatchingProcessNames(this.lut[event_item.controller], event_item.value);
  72.                         while (!this.event_list.TryRemove(key, out _)) {}
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.  
  78.         private void midiIn_messageReceive(object sender, MidiInMessageEventArgs e)
  79.         {
  80.             var concrete_event = ((NAudio.Midi.ControlChangeEvent)e.MidiEvent);
  81.             int controller = (int)concrete_event.Controller;
  82.             if (!lut.ContainsKey(controller) && !lut.ContainsKey(controller - 40))
  83.             {
  84.                 return;
  85.             }
  86.            
  87.             if (controller > 90)
  88.             {
  89.                 setMuteForMatchingProcessNames(lut[controller - 40], concrete_event.ControllerValue > 0);
  90.             }
  91.             else
  92.             {
  93.                 var pod_event = new MidiEvent(controller, concrete_event.ControllerValue);
  94.                 this.event_list[controller] = pod_event;
  95.             }
  96.         }
  97.  
  98.         private void setVolumeForMatchingProcessNames(string proc_name, float volume)
  99.         {
  100.             var x = new MMDeviceEnumerator();
  101.             var device = x.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
  102.             if (proc_name == "System")
  103.             {
  104.                 device.AudioEndpointVolume.MasterVolumeLevelScalar = volume;
  105.                 return;
  106.             }
  107.            
  108.             if (proc_name == "ActiveProcess")
  109.             {
  110.                 uint active_process;
  111.                 GetWindowThreadProcessId(GetForegroundWindow(), out active_process);
  112.                 var proc_name_ = Process.GetProcessById((int)active_process).ProcessName;
  113.                
  114.                 for (var i = 0; i < device.AudioSessionManager.Sessions.Count; i++)
  115.                 {
  116.                     var session = device.AudioSessionManager.Sessions[i];
  117.                     if (Process.GetProcessById((int)session.GetProcessID).ProcessName ==  proc_name_)
  118.                     {
  119.                         session.SimpleAudioVolume.Volume = volume;
  120.                     }
  121.                 }
  122.  
  123.                 return;
  124.             }
  125.            
  126.             for (var i = 0; i < device.AudioSessionManager.Sessions.Count; i++)
  127.             {
  128.                 var session = device.AudioSessionManager.Sessions[i];
  129.                 var process = Process.GetProcessById((int)session.GetProcessID);
  130.                 if (process.ProcessName.Contains(proc_name))
  131.                 {
  132.                     session.SimpleAudioVolume.Volume = volume;
  133.                 }
  134.             }
  135.         }
  136.  
  137.         private void setMuteForMatchingProcessNames(string proc_name, bool mute)
  138.         {
  139.             var x = new MMDeviceEnumerator();
  140.             var device = x.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
  141.             if (proc_name == "System")
  142.             {
  143.                 device.AudioEndpointVolume.Mute = mute;
  144.                 return;
  145.             }
  146.  
  147.             if (proc_name == "ActiveProcess")
  148.             {
  149.                 uint active_process;
  150.                 GetWindowThreadProcessId(GetForegroundWindow(), out active_process);
  151.                 var proc_name_ = Process.GetProcessById((int)active_process).ProcessName;
  152.  
  153.                 for (var i = 0; i < device.AudioSessionManager.Sessions.Count; i++)
  154.                 {
  155.                     var session = device.AudioSessionManager.Sessions[i];
  156.                     if (Process.GetProcessById((int)session.GetProcessID).ProcessName == proc_name_)
  157.                     {
  158.                         session.SimpleAudioVolume.Mute = mute;
  159.                     }
  160.                 }
  161.  
  162.                 return;
  163.             }
  164.  
  165.             for (var i = 0; i < device.AudioSessionManager.Sessions.Count; i++)
  166.             {
  167.                 var session = device.AudioSessionManager.Sessions[i];
  168.                 var process = Process.GetProcessById((int)session.GetProcessID);
  169.                 if (process.ProcessName.Contains(proc_name))
  170.                 {
  171.                     session.SimpleAudioVolume.Mute = mute;
  172.                 }
  173.             }
  174.         }
  175.  
  176.         private void printProcessNames()
  177.         {
  178.             var x = new MMDeviceEnumerator();
  179.             var device = x.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
  180.  
  181.             for (var i = 0; i < device.AudioSessionManager.Sessions.Count; i++)
  182.             {
  183.                 var session = device.AudioSessionManager.Sessions[i];
  184.                 var process = Process.GetProcessById((int)session.GetProcessID);
  185.                 Console.WriteLine(process.ProcessName);
  186.             }
  187.         }
  188.     }
  189. }
  190.  
Advertisement
Add Comment
Please, Sign In to add comment