internal static void Run(string processToDim, string priorityProcess, float loweredVolume) { Console.WriteLine($"Using {nameof(DimWhenPeakMeterRegisters)} algorithm"); try { using (var processToDimWatcher = new ProcessAudioWatcher(processToDim)) { Console.WriteLine($"Watching process to dim: {processToDim}"); using (var priorityProcessWatcher = new ProcessAudioWatcher(priorityProcess)) { Console.WriteLine($"Watching priority process: {priorityProcess}"); // Signal timer now and every 500ms var t = new Timer((state) => RecalculateProcessDimState(processToDimWatcher, priorityProcessWatcher, loweredVolume), null, 0, 500); Console.WriteLine("Press a key to stop."); Console.ReadKey(true); // Stop timer t.Change(Timeout.Infinite, Timeout.Infinite); processToDimWatcher.SetAllSessionsToVolume(1.0f); } } } catch (Exception ex) { Console.Error.WriteLine($"Error occurred: {ex.Message}"); } }
private static void RecalculateProcessDimState(ProcessAudioWatcher process, ProcessAudioWatcher priorityProcess) { var volumeLevel = 1.0f; if (priorityProcess.SessionList.Any(s => s.State == AudioSessionState.Active)) { volumeLevel = loweredVolume; } Console.WriteLine($"Setting {process.ProcessName} volume to {volumeLevel:P}"); process.SetAllSessionsToVolume(volumeLevel); }
private static void RecalculateProcessDimState(ProcessAudioWatcher process, ProcessAudioWatcher priorityProcess, float loweredVolume) { bool shouldDim = priorityProcess.SessionList.Any(s => s.PeakMeterValue > 0.01f); if (shouldDim != isProcessDimmed) { isProcessDimmed = shouldDim; var volumeLevel = shouldDim ? loweredVolume : 1.0f; Console.WriteLine($"Setting {process.ProcessName} volume to {volumeLevel:P}"); process.SetAllSessionsToVolume(volumeLevel); } }
internal static void Run(string processToDim, string priorityProcess, float loweredVolume) { Console.WriteLine($"Using {nameof(DimWhenActiveAlgorithm)} algorithm"); DimWhenActiveAlgorithm.loweredVolume = loweredVolume; try { using (processToDimWatcher = new ProcessAudioWatcher(processToDim)) { var c = (INotifyCollectionChanged)processToDimWatcher.SessionList; c.CollectionChanged += SessionListChanged; Console.WriteLine($"Watching process to dim: {processToDim}"); using (priorityProcessWatcher = new ProcessAudioWatcher(priorityProcess)) { Console.WriteLine($"Watching priority process: {priorityProcess}"); foreach (var existingSession in priorityProcessWatcher.SessionList) { existingSession.StateChanged += Session_StateChanged; existingSession.Disconnected += Session_Disconnected; } var c2 = (INotifyCollectionChanged)priorityProcessWatcher.SessionList; c2.CollectionChanged += SessionListChanged; // If an existing session is already playing, dim our process RecalculateProcessDimState(processToDimWatcher, priorityProcessWatcher); Console.WriteLine("Press a key to stop."); Console.ReadKey(true); processToDimWatcher.SetAllSessionsToVolume(1.0f); } } } catch (Exception ex) { Console.Error.WriteLine($"Error occurred: {ex.Message}"); } }