예제 #1
0
        public static void processTicks()
        {
            if (!userDeactivated)
            {
                if (Configuration.option_autopause)
                {
                    if (ticksInactive == -1) //Logical part starts to run here
                    {
                        checkStates();
                        decide();
                    }
                    else if (ticksInactive == 0)
                    {
                        //Queues are not full, so this fills the queues with their average values
                        QueueControl.fillQueuesWithAverage(ref otherSoundQueue, ref spotifySoundQueue, ref Configuration.SPOTIFY_QUEUE_SIZE, ref currentQueueSize);
                        ticksInactive--;
                    }
                    else if (ticksInactive > 0)
                    {
                        ticksInactive--;
                    }
                }


                //2 ticks after pause/play, because transition is not instant which causes noise
                if (tickDelay == 0)
                {
                    SessionOperation.processCurrentPeaks(ref otherSoundQueue, ref spotifySoundQueue);
                }
                else
                {
                    tickDelay--;
                }
            }
        }
예제 #2
0
        public static void pauseSpotify(ref Queue <float> otherSoundQueue, ref Queue <float> spotifySoundQueue, ref int spotifyQueueSize, ref int currentQueueSize, ref int ticksInactive, ref int tickDelay)
        {
            SpotifyController.pausePlay();

            QueueControl.fillQueuesAfterPause(ref otherSoundQueue, ref spotifySoundQueue, ref spotifyQueueSize, ref currentQueueSize);

            tickDelay     = 2;
            ticksInactive = Configuration.TICKS_BEFORE_START;
        }
예제 #3
0
        //Brings program to the initial state
        public static void setInitials()
        {
            lastPress     = 1;
            programStatus = 1;
            currentState  = 0;
            tickDelay     = 0;

            userDeactivated = false;
            transition      = false;

            spotifyWarningShown = false;
            unknownErrorShown   = false;

            currentQueueSize = Configuration.SHORT_QUEUE_SIZE;
            ticksInactive    = Configuration.TICKS_BEFORE_START;

            QueueControl.fillQueuesInitial(ref otherSoundQueue, ref spotifySoundQueue, ref Configuration.SPOTIFY_QUEUE_SIZE, ref currentQueueSize);
        }
예제 #4
0
        public static void processCurrentPeaks(ref Queue <float> otherSoundQueue, ref Queue <float> spotifySoundQueue)
        {
            IMMDevice               speakers;
            object                  audioSessionInterface;
            Guid                    IID_IAudioSessionManager2;
            IMMDeviceEnumerator     deviceEnumerator;
            IAudioSessionEnumerator sessionEnumerator;
            IAudioSessionManager2   mgr;

            deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
            deviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia, out speakers);
            IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
            speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out audioSessionInterface);
            mgr = (IAudioSessionManager2)audioSessionInterface;

            myctr++;

            mgr.GetSessionEnumerator(out sessionEnumerator);

            int sessionCount;

            sessionEnumerator.GetCount(out sessionCount);



            var hWnd = FindWindow("SpotifyMainWindow", null);

            if (hWnd == IntPtr.Zero && !PauseControl.spotifyWarningShown)
            {
                //Program.processIcon.showNotification(5000, Constants.appName, "Spotify is not running", ToolTipIcon.None);
                PauseControl.spotifyWarningShown = true;
            }
            else if (!(hWnd == IntPtr.Zero))
            {
                uint pID;
                GetWindowThreadProcessId(hWnd, out pID);
                spotify_pid = pID;
                if (pID == 0 && !PauseControl.unknownErrorShown)
                {
                    //Program.processIcon.showNotification(5000, Constants.appName, "Something bad happened", ToolTipIcon.None);
                    PauseControl.unknownErrorShown = true;
                }


                StringBuilder sb = new StringBuilder(Configuration.MAX_WINDOWNAME_SIZE);
                SendMessageGetText(hWnd, Configuration.WM_GETTEXT, new UIntPtr(Configuration.MAX_WINDOWNAME_SIZE), sb);
                PauseControl.spotifyWindowName = sb.ToString(); //think about a better way

                float maxPeak = 0;


                for (int i = 0; i < sessionCount; i++)
                {
                    IAudioSessionControl   audioSessionControl;
                    IAudioSessionControl2  audioSessionControl2;
                    IAudioMeterInformation audioMeterInformation;
                    ISimpleAudioVolume     simpleAudioVolume;


                    sessionEnumerator.GetSession(i, out audioSessionControl);

                    audioMeterInformation = audioSessionControl as IAudioMeterInformation;

                    audioSessionControl2 = audioSessionControl as IAudioSessionControl2;

                    simpleAudioVolume = audioSessionControl as ISimpleAudioVolume;

                    float currentPeak;
                    audioMeterInformation.GetPeakValue(out currentPeak);

                    uint sessionProcessId = 0;
                    audioSessionControl2.GetProcessId(out sessionProcessId);



                    /*
                     * finally figured out how to solve a very frustrating problem. Basically spotify has multiple
                     * sessions open for reasons I don't know, which makes volumemixer(and me indirectly) go crazy,
                     * eg. it shows spotify volume at lowest but spotify still plays sound, but if you manually click
                     * to lowest level, spotify gets muted. Apparently, the visual indicator in volume mixer gets
                     * its value from *one* of spotify sessions, but if you control the indicator yourself, you
                     * set all spotify sessions to the same value.
                     */
                    if (sessionProcessId == pID)                                      //Spotify Session
                    {
                        QueueControl.dequeue(ref spotifySoundQueue);                  //Remove the oldest peak from queue
                        QueueControl.enqueue(ref spotifySoundQueue, ref currentPeak); //Add current sound level to queue


                        simpleAudioVolume.GetMasterVolume(out sessionVolume);

                        if (sessionVolume != 0f && AdControl.sound_muted)
                        {
                            //this is not handled properly now
                            //AdControl.handleUserUnmute();
                        }

                        //Ads part
                        if (AdControl.ad_alarm && !AdControl.sound_muted)
                        {
                            Guid guid = Guid.Empty;
                            sessionVolume = 0f;
                            simpleAudioVolume.SetMasterVolume(0f, ref guid);
                        }
                        else if (!AdControl.ad_alarm && AdControl.sound_muted)
                        {
                            Guid guid = Guid.Empty;
                            if (sessionVolume == 0f && Configuration.option_remember)
                            {
                                sessionVolume = 1f;
                            }
                            if (!Configuration.option_remember)
                            {
                                sessionVolume = Configuration.spotify_volume / 100;
                            }
                            simpleAudioVolume.SetMasterVolume(sessionVolume, ref guid);
                        }
                    }
                    else if (sessionProcessId != 0)
                    {
                        if (maxPeak < currentPeak)
                        {
                            maxPeak = currentPeak;
                        }
                    }

                    Marshal.ReleaseComObject(audioSessionControl);
                    Marshal.ReleaseComObject(audioSessionControl2);
                    Marshal.ReleaseComObject(audioMeterInformation);
                }


                if (AdControl.ad_alarm && !AdControl.sound_muted)
                {
                    Guid guid = Guid.Empty;
                    sessionVolume         = 0f;
                    AdControl.sound_muted = true;
                }
                else if (!AdControl.ad_alarm && AdControl.sound_muted)
                {
                    Guid guid = Guid.Empty;
                    if (sessionVolume == 0f && Configuration.option_remember)
                    {
                        sessionVolume = 1f;
                    }
                    if (!Configuration.option_remember)
                    {
                        sessionVolume = Configuration.spotify_volume / 100;
                    }
                    AdControl.sound_muted = false;
                }



                if (maxPeak > Configuration.NORMALIZE_VALUE)
                {
                    maxPeak = Configuration.NORMALIZE_VALUE;
                }                                                       //Normalization
                QueueControl.dequeue(ref otherSoundQueue);              //Remove the oldest peak from queue
                QueueControl.enqueue(ref otherSoundQueue, ref maxPeak); //Add current sound level to queue
            }

            Marshal.ReleaseComObject(sessionEnumerator);
            Marshal.ReleaseComObject(mgr);
            Marshal.ReleaseComObject(speakers);
            Marshal.ReleaseComObject(deviceEnumerator);
        }