Esempio n. 1
0
        void RefreshBuffer()
        {
            CalculationFunctions calc = new CalculationFunctions();
            int ReadPosition;
            // variable to count byte difference in compressed and non compressed data of audio file
            int reduction = 0;

            while (m_lPlayed < m_lLength)
            {            //1
                if (SoundBuffer.Status.BufferLost)
                {
                    SoundBuffer.Restore();
                }

                reduction = 0;
                Thread.Sleep(50);
                ReadPosition = SoundBuffer.PlayPosition;
                if (ReadPosition < ((m_SizeBuffer) - m_UpdateVMArrayLength))
                {
                    Array.Copy(SoundBuffer.Read(ReadPosition, typeof(byte), LockFlag.None, m_UpdateVMArrayLength), arUpdateVM, m_UpdateVMArrayLength);
                    ob_UpdateVuMeter.NotifyUpdateVuMeter(this, ob_UpdateVuMeter);
                }
                // check if play cursor is in second half , then refresh first half else second
                if ((m_BufferCheck % 2) == 1 && SoundBuffer.PlayPosition > m_RefreshLength)
                {                //2
                    // Checks if file is played or byteBuffer is played
                    if (m_PlayFile == true)
                    {                    //3
                        // check for normal or fast play
                        if (m_FastPlay == false)
                        {                        //4
                            SoundBuffer.Write(0, fs, m_RefreshLength, 0);
                        }                        //-4
                        else
                        {                        //4
                            for (int i = 0; i < m_RefreshLength; i = i + (m_Step * m_FrameSize))
                            {
                                SoundBuffer.Write(i, fs, m_Step * m_FrameSize, 0);
                                i         = i - (2 * m_FrameSize);
                                reduction = reduction + (2 * m_FrameSize);
                            }            //-4
                        }                //-3
                    }                    //-2
                    else
                    {                    //2
                        for (int i = 0; i < m_RefreshLength; i++)
                        {
                            RefreshArray[i] = ByteBuffer [m_lArrayPosition + i];
                        }

                        SoundBuffer.Write(0, RefreshArray, 0);
                        m_lArrayPosition = m_lArrayPosition + m_RefreshLength;
                    }                    //-2
                    m_lPlayed = m_lPlayed + m_RefreshLength + reduction;

                    m_BufferCheck++;
                }                //-1
                else if ((m_BufferCheck % 2 == 0) && SoundBuffer.PlayPosition < m_RefreshLength)
                {                //1
                    if (m_PlayFile == true)
                    {            //2
                        if (m_FastPlay == false)
                        {        //3
                            SoundBuffer.Write(m_RefreshLength, fs, m_RefreshLength, 0);
                        }        //-3
                        else
                        {        //3
                            for (int i = 0; i < m_RefreshLength; i = i + (m_Step * m_FrameSize))
                            {    //4
                                SoundBuffer.Write(i + m_RefreshLength, fs, m_Step * m_FrameSize, 0);
                                i         = i - (2 * m_FrameSize);
                                reduction = reduction + (2 * m_FrameSize);
                            }                                                                           //-4

                            // end of FastPlay check
                        }                //-3
                    }                    //-2
                    else
                    {                    //2
                        for (int i = 0; i < m_RefreshLength; i++)
                        {
                            RefreshArray[i] = ByteBuffer [m_lArrayPosition + i];
                        }

                        SoundBuffer.Write(m_RefreshLength, RefreshArray, 0);
                        m_lArrayPosition = m_lArrayPosition + m_RefreshLength;
                        // end of file check
                    }                    //-2

                    m_lPlayed = m_lPlayed + m_RefreshLength + reduction;

                    m_BufferCheck++;

                    // end of even/ odd part of buffer;
                }                //-1


                // end of while
            }
            // calculate time to stop according to remaining data
            int    time;
            long   lRemaining = (m_lPlayed - m_lLength);
            double dTemp;

            dTemp = ((m_RefreshLength + m_RefreshLength - lRemaining) * 1000) / m_RefreshLength;
            time  = Convert.ToInt32(dTemp * 0.48);

            //if (m_FastPlay == true)
            //time = (time-250) * (1- (2/m_Step));

            Thread.Sleep(time);

            // Stopping process begins
            SoundBuffer.Stop();
            if (m_PlayFile == true)
            {
                fs.Close();
                ob_EndOfAudioAsset.NotifyEndOfAudioAsset(this, ob_EndOfAudioAsset);
            }
            else
            {
                m_lArrayPosition = 0;
                ByteBuffer       = null;
                ob_EndOfAudioBuffer.NotifyEndOfAudioBuffer(this, ob_EndOfAudioBuffer);
            }
            //Stop () ;
            // changes the state and trigger events
            //ob_StateChanged = new StateChanged (m_State) ;

            //StateChanged ob_StateChanged = new StateChanged (m_State) ;
            StateChanged ob_StateChanged = StateChanged.From(m_State);

            m_State = AudioPlayerState.stopped;

            TriggerStateChangedEvent(ob_StateChanged);
            StateChangedEvent(this, ob_StateChanged);
            //ob_EndOfFile.TriggerEndOfFileEvent ( m_Asset.Path);

            // RefreshBuffer ends
        }