Beispiel #1
0
        public virtual void midiIn_MessageReceived(object sender, MidiInMessageEventArgs e)
        {
            WindowTools.Dump(e.MidiEvent);

            foreach (var fader in faders)
            {
                fader.HandleEvent(e);
            }

            foreach (var button in buttons)
            {
                button.HandleEvent(e);
            }
        }
Beispiel #2
0
        public bool ToggleMute()
        {
            if (sessionType == SessionType.Application)
            {
                var muted = !audioSessions.First().SimpleAudioVolume.Mute;
                foreach (var session in audioSessions)
                {
                    session.SimpleAudioVolume.Mute = muted;
                }
                return(muted);
            }
            else if (sessionType == SessionType.Master)
            {
                try
                {
                    var muted = !deviceVolume.Mute;
                    deviceVolume.Mute = muted;
                    return(muted);
                }
                catch (System.Runtime.InteropServices.InvalidComObjectException e)
                {
                    // This catch handles the "COM object that has been separated from its underlying RCW cannot be used" issue.
                    // I believe this happens when we refresh the device when opening the menu, but this is fine for now.
                    Console.WriteLine($@"Error when toggling mute on master volume: {e.Message}");
                    deviceVolume = parent.GetDeviceVolumeObject();
                    var muted = !deviceVolume.Mute;
                    deviceVolume.Mute = muted;

                    Console.WriteLine("RETRY: toggling mute on master volume");
                    return(muted);
                }
            }
            else if (sessionType == SessionType.Focus)
            {
                var pid          = WindowTools.GetForegroundPID();
                var mixerSession = parent.FindMixerSessions(pid);
                if (mixerSession != null)
                {
                    var muted = !mixerSession.audioSessions.First().SimpleAudioVolume.Mute;
                    foreach (var session in mixerSession.audioSessions)
                    {
                        session.SimpleAudioVolume.Mute = muted;
                    }
                    return(muted);
                }
            }
            return(false);
        }
Beispiel #3
0
 public void SetVolume(float volume)
 {
     if (sessionType == SessionType.Application)
     {
         foreach (var session in audioSessions)
         {
             session.SimpleAudioVolume.Volume = volume;
         }
     }
     else if (sessionType == SessionType.Master)
     {
         try
         {
             devices.GetDeviceByIdentifier(parentDeviceIdentifier).AudioEndpointVolume.MasterVolumeLevelScalar = volume;
         }
         catch (System.Runtime.InteropServices.InvalidComObjectException e)
         {
             // This catch handles the "COM object that has been separated from its underlying RCW cannot be used" issue.
             // I believe this happens when we refresh the device when opening the menu, but this is fine for now.
             Console.WriteLine($@"Error when setting master volume: {e.Message}");
             AudioEndpointVolume deviceVolume = devices.GetDeviceByIdentifier(parentDeviceIdentifier).AudioEndpointVolume;
             deviceVolume.MasterVolumeLevelScalar = volume;
             Console.WriteLine($@"RETRY: Setting master volume to {volume}");
         }
         catch (System.Runtime.InteropServices.COMException e)
         {
             //TODO find out where this exception comes from and actually fix it
             Console.WriteLine("COM Execption" + e);
         }
         catch (System.InvalidCastException e)
         {
             Console.WriteLine("InvalidCastException Exeception" + e);
         }
     }
     else if (sessionType == SessionType.Focus)
     {
         var pid          = WindowTools.GetForegroundPID();
         var mixerSession = devices.FindMixerSession(pid);
         // Check if null since mixer session might not exist for currently focused window
         if (mixerSession != null)
         {
             foreach (var session in mixerSession.audioSessions)
             {
                 session.SimpleAudioVolume.Volume = volume;
             }
         }
     }
 }
Beispiel #4
0
        public bool GetMute()
        {
            if (sessionType == SessionType.Master)
            {
                return(devices.GetDeviceByIdentifier(parentDeviceIdentifier).AudioEndpointVolume.Mute);
            }

            var targetAudioSessions = audioSessions;

            if (sessionType == SessionType.Focus)
            {
                var pid          = WindowTools.GetForegroundPID();
                var mixerSession = devices.FindMixerSession(pid);
                if (mixerSession == null)
                {
                    return(false);
                }
                targetAudioSessions = mixerSession.audioSessions;
            }

            return(targetAudioSessions.First().SimpleAudioVolume.Mute);
        }
Beispiel #5
0
        public float ChangeVolume(float change)
        {
            float retVol = -1;

            if (sessionType == SessionType.Application)
            {
                foreach (var session in audioSessions)
                {
                    if (retVol < 0)
                    {
                        var curVol = session.SimpleAudioVolume.Volume;
                        curVol += change;
                        if (curVol < 0)
                        {
                            curVol = 0;
                        }
                        if (curVol > 1)
                        {
                            curVol = 1;
                        }
                        session.SimpleAudioVolume.Volume = curVol;
                        retVol = curVol;
                    }
                    {
                        session.SimpleAudioVolume.Volume = retVol;
                    }
                }
            }
            else if (sessionType == SessionType.Master)
            {
                try
                {
                    var curVol = deviceVolume.MasterVolumeLevelScalar;
                    curVol += change;
                    if (curVol < 0)
                    {
                        curVol = 0;
                    }
                    if (curVol > 1)
                    {
                        curVol = 1;
                    }
                    deviceVolume.MasterVolumeLevelScalar = curVol;
                    retVol = curVol;
                }
                catch (System.Runtime.InteropServices.InvalidComObjectException e)
                {
                    // This catch handles the "COM object that has been separated from its underlying RCW cannot be used" issue.
                    // I believe this happens when we refresh the device when opening the menu, but this is fine for now.
                    Console.WriteLine($@"Error when setting master volume: {e.Message}");
                    deviceVolume = parent.GetDeviceVolumeObject();
                    var curVol = deviceVolume.MasterVolumeLevelScalar;
                    curVol += change;
                    if (curVol < 0)
                    {
                        curVol = 0;
                    }
                    if (curVol > 1)
                    {
                        curVol = 1;
                    }
                    deviceVolume.MasterVolumeLevelScalar = curVol;
                    Console.WriteLine($@"RETRY: Setting master volume by {change}");
                }
            }
            else if (sessionType == SessionType.Focus)
            {
                var pid          = WindowTools.GetForegroundPID();
                var mixerSession = parent.FindMixerSessions(pid);
                if (mixerSession != null)
                {
                    foreach (var session in mixerSession.audioSessions)
                    {
                        var curVol = session.SimpleAudioVolume.Volume;
                        curVol += change;
                        if (curVol < 0)
                        {
                            curVol = 0;
                        }
                        if (curVol > 1)
                        {
                            curVol = 1;
                        }
                        session.SimpleAudioVolume.Volume = curVol;
                        retVol = curVol;
                    }
                }
            }
            return(retVol);
        }
Beispiel #6
0
        public bool HandleEvent(MidiInMessageEventArgs e)
        {
            if (!IsHandling())
            {
                SetHandling(true);

                //if loaded inactive, search again
                if (!assigned && identifier != null && !identifier.Equals(""))
                {
                    assignment = parent.audioDevices.FindMixerSession(identifier);
                    if (assignment != null)
                    {
                        assigned = true;
                    }
                }

                // Fader match
                if (assigned && Match(faderNumber, e.MidiEvent, faderDef.faderCode, faderDef.faderOffset, faderDef.faderChannelOverride))
                {
                    if (assignment.sessionType == SessionType.Application)
                    {
                        MixerSession newAssignment = assignment.devices.FindMixerSession(assignment.sessionIdentifier); //update list for re-routered app, but only overrides
                        if (newAssignment == null)                                                                      //if there is new assignments, otherwise, there is no more a inactive
                        {                                                                                               //MixerSession to hold the label
                            SetRecordLight(true);
                            return(true);
                        }
                        assignment = newAssignment;
                    }

                    if (faderDef.delta)
                    {
                        float curVol;
                        var   val = GetValue(e.MidiEvent);
                        if (val > faderDef.range / 2)
                        {
                            curVol = assignment.ChangeVolume((faderDef.range - val) / faderDef.range);
                        }
                        else
                        {
                            curVol = assignment.ChangeVolume(val / faderDef.range);
                        }
                        parent.SetVolumeIndicator(faderNumber, curVol);
                    }
                    else
                    {
                        assignment.SetVolume(GetValue(e.MidiEvent) / faderDef.range);
                    }

                    if (assignment.IsDead())
                    {
                        SetRecordLight(true);
                    }

                    return(true);
                }

                // Select match
                if (Match(faderNumber, e.MidiEvent, faderDef.selectCode, faderDef.selectOffset, faderDef.selectChannelOverride))
                {
                    if (GetValue(e.MidiEvent) != 127) // Only on button-down
                    {
                        return(true);
                    }

                    Console.WriteLine($@"Attempting to assign current window to fader {faderNumber}");
                    if (assigned)
                    {
                        Unassign();
                        parent.SaveAssignments();
                    }
                    else
                    {
                        var pid          = WindowTools.GetForegroundPID();
                        var mixerSession = parent.audioDevices.FindMixerSession(pid);
                        if (mixerSession != null)
                        {
                            Assign(mixerSession);
                            parent.SaveAssignments();
                        }
                        else
                        {
                            Console.WriteLine($@"MixerSession not found for pid {pid}");
                        }
                    }
                    return(true);
                }

                // Mute match
                if (assigned && Match(faderNumber, e.MidiEvent, faderDef.muteCode, faderDef.muteOffset, faderDef.muteChannelOverride))
                {
                    if (GetValue(e.MidiEvent) != 127) // Only on button-down
                    {
                        return(true);
                    }

                    SetMuteLight(assignment.ToggleMute());
                    if (assignment.IsDead())
                    {
                        SetRecordLight(true);
                    }
                    return(true);
                }

                // Record match
                if (assigned && Match(faderNumber, e.MidiEvent, faderDef.recordCode, faderDef.recordOffset, faderDef.recordChannelOverride))
                {
                    if (GetValue(e.MidiEvent) != 127) // Only on button-down
                    {
                        return(true);
                    }

                    if (WindowTools.IsProcessByNameRunning(applicationName))
                    {
                        SetRecordLight(false);
                    }
                    else
                    {
                        WindowTools.StartApplication(applicationPath);
                    }

                    return(true);
                }
            }
            return(false);
        }
Beispiel #7
0
        public bool HandleEvent(MidiInMessageEventArgs e)
        {
            // Fader match
            if (assigned && Match(faderNumber, e.MidiEvent, faderDef.faderCode, faderDef.faderOffset, faderDef.faderChannelOverride))
            {
                if (faderDef.delta)
                {
                    float curVol;
                    var   val = GetValue(e.MidiEvent);
                    if (val > faderDef.range / 2)
                    {
                        curVol = assignment.ChangeVolume((faderDef.range - val) / faderDef.range);
                    }
                    else
                    {
                        curVol = assignment.ChangeVolume(val / faderDef.range);
                    }
                    parent.SetVolumeIndicator(faderNumber, curVol);
                }
                else
                {
                    assignment.SetVolume(GetValue(e.MidiEvent) / faderDef.range);
                }

                if (assignment.IsDead())
                {
                    SetRecordLight(true);
                }

                return(true);
            }

            // Select match
            if (Match(faderNumber, e.MidiEvent, faderDef.selectCode, faderDef.selectOffset, faderDef.selectChannelOverride))
            {
                if (GetValue(e.MidiEvent) != 127) // Only on button-down
                {
                    return(true);
                }

                Console.WriteLine($@"Attempting to assign current window to fader {faderNumber}");
                if (assigned)
                {
                    Unassign();
                    parent.SaveAssignments();
                }
                else
                {
                    var pid          = WindowTools.GetForegroundPID();
                    var mixerSession = parent.audioDevice.FindMixerSessions(pid);
                    if (mixerSession != null)
                    {
                        Assign(mixerSession);
                        parent.SaveAssignments();
                    }
                    else
                    {
                        Console.WriteLine($@"MixerSession not found for pid {pid}");
                    }
                }
                return(true);
            }

            // Mute match
            if (assigned && Match(faderNumber, e.MidiEvent, faderDef.muteCode, faderDef.muteOffset, faderDef.muteChannelOverride))
            {
                if (GetValue(e.MidiEvent) != 127) // Only on button-down
                {
                    return(true);
                }

                SetMuteLight(assignment.ToggleMute());
                if (assignment.IsDead())
                {
                    SetRecordLight(true);
                }
                return(true);
            }

            // Record match
            if (assigned && Match(faderNumber, e.MidiEvent, faderDef.recordCode, faderDef.recordOffset, faderDef.recordChannelOverride))
            {
                SetRecordLight(assignment.IsDead());
                return(true);
            }
            return(false);
        }
Beispiel #8
0
        public bool HandleEvent(MidiInMessageEventArgs e)
        {
            if (!IsHandling())
            {
                SetHandling(true);

                //if loaded inactive, search again
                if (!assigned && identifier != null && !identifier.Equals(""))
                {
                    assignment = parent.audioDevices.FindMixerSession(identifier);
                    if (assignment != null)
                    {
                        assigned = true;
                    }
                }

                // Fader match
                if (assigned && Match(faderNumber, e.MidiEvent, faderDef.faderCode, faderDef.faderOffset, faderDef.faderChannelOverride))
                {
                    if (assignment.sessionType == SessionType.Application)
                    {
                        MixerSession newAssignment = assignment.devices.FindMixerSession(assignment.sessionIdentifier); //update list for re-routered app, but only overrides
                        if (newAssignment == null)                                                                      //if there is new assignments, otherwise, there is no more a inactive
                        {                                                                                               //MixerSession to hold the label
                            SetRecordLight(true);
                            return(true);
                        }
                        assignment = newAssignment;
                    }

                    float newVol;

                    if (faderDef.delta)
                    {
                        var val         = GetValue(e.MidiEvent);
                        var volNow      = assignment.GetVolume();
                        var nearestStep = steps.Select((x, i) => new { Index = i, Distance = Math.Abs(volNow - x) }).OrderBy(x => x.Distance).First().Index;
                        int nextStepIndex;

                        var volumeGoingDown = val > faderDef.range / 2;

                        if (volumeGoingDown)
                        {
                            nextStepIndex = Math.Max(nearestStep - 1, 0);
                        }
                        else
                        {
                            nextStepIndex = Math.Min(nearestStep + 1, steps.Length - 1);
                        }

                        newVol = steps[nextStepIndex];
                        assignment.SetVolume(newVol);
                    }
                    else
                    {
                        newVol = getVolFromStage(GetValue(e.MidiEvent));
                        assignment.SetVolume(newVol);
                    }

                    if (assignment.IsDead())
                    {
                        SetRecordLight(true);

                        return(true);
                    }

                    List <Fader> fadersToAffect = GetMatchingFaders();
                    fadersToAffect.ForEach(fader => fader.parent.SetVolumeIndicator(fader.faderNumber, newVol));

                    return(true);
                }

                // Select match
                if (Match(faderNumber, e.MidiEvent, faderDef.selectCode, faderDef.selectOffset, faderDef.selectChannelOverride))
                {
                    if (GetValue(e.MidiEvent) != 127) // Only on button-down
                    {
                        return(true);
                    }

                    Console.WriteLine($@"Attempting to assign current window to fader {faderNumber}");
                    if (assigned)
                    {
                        Unassign();
                        parent.SaveAssignments();
                    }
                    else
                    {
                        var pid          = WindowTools.GetForegroundPID();
                        var mixerSession = parent.audioDevices.FindMixerSession(pid);
                        if (mixerSession != null)
                        {
                            Assign(mixerSession);
                            parent.SaveAssignments();
                        }
                        else
                        {
                            Console.WriteLine($@"MixerSession not found for pid {pid}");
                        }
                    }
                    return(true);
                }

                // Mute match
                if (assigned && Match(faderNumber, e.MidiEvent, faderDef.muteCode, faderDef.muteOffset, faderDef.muteChannelOverride))
                {
                    if (GetValue(e.MidiEvent) != 127) // Only on button-down
                    {
                        return(true);
                    }

                    var muteStatus = assignment.ToggleMute();
                    SetMuteLight(muteStatus);

                    if (assignment.IsDead())
                    {
                        SetRecordLight(true);

                        return(true);
                    }

                    List <Fader> fadersToAffect = GetMatchingFaders();
                    fadersToAffect.ForEach(fader => fader.SetMuteLight(muteStatus));

                    return(true);
                }

                // Record match
                if (
                    assigned &&
                    applicationPath != null &&
                    Match(faderNumber, e.MidiEvent, faderDef.recordCode, faderDef.recordOffset, faderDef.recordChannelOverride)
                    )
                {
                    if (GetValue(e.MidiEvent) != 127) // Only on button-down
                    {
                        return(true);
                    }

                    if (WindowTools.IsProcessByNameRunning(applicationName))
                    {
                        SetRecordLight(false);
                    }
                    else
                    {
                        WindowTools.StartApplication(applicationPath);
                    }

                    return(true);
                }
            }
            return(false);
        }
Beispiel #9
0
        public List <Fader> GetMatchingFaders()
        {
            MixerSession focusMixerSession = null;

            bool haveFocusSlider = parent.faders.Any(fader => (
                                                         fader.assignment != null &&
                                                         fader.assignment.sessionType == SessionType.Focus
                                                         ));

            if (haveFocusSlider)
            {
                int pid = WindowTools.GetForegroundPID();
                focusMixerSession = assignment.devices.FindMixerSession(pid);
            }

            if (assignment == null)
            {
                return(parent.faders.FindAll(fader => fader.assignment == null));
            }

            if (assignment.sessionType == SessionType.Master)
            {
                return(parent.faders.FindAll(fader =>
                {
                    if (fader == this)
                    {
                        return true;
                    }
                    if (fader.assignment == null)
                    {
                        return false;
                    }
                    if (fader.assignment.sessionType != SessionType.Master)
                    {
                        return false;
                    }

                    return fader.assignment.parentDeviceIdentifier == assignment.parentDeviceIdentifier;
                }));
            }

            if (assignment.sessionType == SessionType.Focus)
            {
                return(parent.faders.FindAll(fader =>
                {
                    if (fader == this)
                    {
                        return true;
                    }
                    if (fader.assignment == null)
                    {
                        return false;
                    }
                    if (fader.assignment.sessionType == SessionType.Focus)
                    {
                        return true;
                    }

                    return fader.assignment.HasCrossoverProcesses(focusMixerSession);
                }));
            }

            if (assignment.sessionType == SessionType.Application)
            {
                return(parent.faders.FindAll(fader =>
                {
                    if (fader == this)
                    {
                        return true;
                    }
                    if (fader.assignment == null)
                    {
                        return false;
                    }

                    if (fader.assignment.sessionType == SessionType.Application)
                    {
                        return fader.assignment.HasCrossoverProcesses(assignment);
                    }

                    if (fader.assignment.sessionType == SessionType.Focus)
                    {
                        return assignment.HasCrossoverProcesses(focusMixerSession);
                    }

                    return false;
                }));
            }

            if (assignment.sessionType == SessionType.SystemSounds)
            {
                return(parent.faders.FindAll(fader => (
                                                 fader.assignment != null &&
                                                 fader.assignment.sessionType == SessionType.SystemSounds
                                                 )));
            }

            return(new List <Fader>());
        }
Beispiel #10
0
        public bool HandleEvent(MidiInMessageEventArgs e)
        {
            if (!IsHandling())
            {
                SetHandling(true);

                //if loaded inactive, search again
                if (!assigned && identifier != null && !identifier.Equals(""))
                {
                    assignment = parent.audioDevices.FindMixerSession(identifier);
                    if (assignment != null)
                    {
                        assigned = true;
                    }
                }

                // Fader match
                if (assigned && Match(faderNumber, e.MidiEvent, faderDef.faderCode, faderDef.faderOffset, faderDef.faderChannelOverride))
                {
                    var hardwareValue = GetValue(e.MidiEvent);
                    primaryFaderHardwareValue = hardwareValue;
                    return(SetVolume(hardwareValue));
                }

                // SubFader Match
                if (
                    faderDef.subFaderPresent &&
                    assigned &&
                    Match(faderNumber, e.MidiEvent, faderDef.subFaderCode, faderDef.subFaderOffset, faderDef.subFaderChannelOverride))
                {
                    this.faderPositionMultiplier = GetValue(e.MidiEvent) / faderDef.faderRange;
                    this.steps = calculateSteps();

                    return(SetVolume(primaryFaderHardwareValue));
                }

                // Select match
                if (
                    faderDef.selectPresent &&
                    Match(faderNumber, e.MidiEvent, faderDef.selectCode, faderDef.selectOffset, faderDef.selectChannelOverride)
                    )
                {
                    if (GetValue(e.MidiEvent) != 127) // Only on button-down
                    {
                        return(true);
                    }

                    Console.WriteLine($@"Attempting to assign current window to fader {faderNumber}");
                    if (assigned)
                    {
                        Unassign();
                        parent.SaveAssignments();
                    }
                    else
                    {
                        var pid          = WindowTools.GetForegroundPID();
                        var mixerSession = parent.audioDevices.FindMixerSession(pid);
                        if (mixerSession != null)
                        {
                            Assign(mixerSession);
                            parent.SaveAssignments();
                        }
                        else
                        {
                            Console.WriteLine($@"MixerSession not found for pid {pid}");
                        }
                    }
                    return(true);
                }

                // Mute match
                if (
                    faderDef.mutePresent &&
                    assigned &&
                    Match(faderNumber, e.MidiEvent, faderDef.muteCode, faderDef.muteOffset, faderDef.muteChannelOverride)
                    )
                {
                    if (GetValue(e.MidiEvent) != 127) // Only on button-down
                    {
                        return(true);
                    }

                    var muteStatus = assignment.ToggleMute();
                    SetMuteLight(muteStatus);

                    if (assignment.IsDead())
                    {
                        SetRecordLight(true);

                        return(true);
                    }

                    List <Fader> fadersToAffect = GetMatchingFaders();
                    fadersToAffect.ForEach(fader => fader.SetMuteLight(muteStatus));

                    return(true);
                }

                // Record match
                if (
                    faderDef.recordPresent &&
                    assigned &&
                    applicationPath != null &&
                    Match(faderNumber, e.MidiEvent, faderDef.recordCode, faderDef.recordOffset, faderDef.recordChannelOverride)
                    )
                {
                    if (GetValue(e.MidiEvent) != 127) // Only on button-down
                    {
                        return(true);
                    }

                    if (WindowTools.IsProcessByNameRunning(applicationName))
                    {
                        SetRecordLight(false);
                    }
                    else
                    {
                        WindowTools.StartApplication(applicationPath);
                    }

                    return(true);
                }
            }
            return(false);
        }