Esempio n. 1
0
 void TriggerStateChangedEvent(events.AudioPlayerEvents.StateChanged e)
 {
     if (m_EventsEnabled)
     {
         StateChanged(this, e);
     }
 }
Esempio n. 2
0
 public void Resume()
 {
     if (m_State.Equals(AudioPlayerState.Paused))
     {
         events.AudioPlayerEvents.StateChanged e = new events.AudioPlayerEvents.StateChanged(m_State);
         m_State = AudioPlayerState.Playing;
         TriggerStateChangedEvent(e);
         SoundBuffer.Play(0, BufferPlayFlags.Looping);
     }
 }
Esempio n. 3
0
 public void Pause()
 {
     if (m_State.Equals(AudioPlayerState.Playing))
     {
         SoundBuffer.Stop();
         // Change the state and trigger event
         events.AudioPlayerEvents.StateChanged e = new events.AudioPlayerEvents.StateChanged(m_State);
         m_State = AudioPlayerState.Paused;
         TriggerStateChangedEvent(e);
     }
 }
Esempio n. 4
0
 public void Stop()
 {
     if (!m_State.Equals(AudioPlayerState.Stopped))
     {
         SoundBuffer.Stop();
         RefreshThread.Abort();
         if (ob_VuMeter != null)
         {
             ob_VuMeter.Reset();
         }
         if (m_PlayFile)
         {
             m_br.Close();
         }
     }
     events.AudioPlayerEvents.StateChanged e = new events.AudioPlayerEvents.StateChanged(m_State);
     m_State = AudioPlayerState.Stopped;
     TriggerStateChangedEvent(e);
 }
Esempio n. 5
0
 public void CatchStateChangedEvent(object sender, VirtualAudioBackend.events.AudioPlayerEvents.StateChanged state)
 {
     MessageBox.Show(state.OldState.ToString());
 }
Esempio n. 6
0
        void RefreshBuffer()
        {
            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);
                    //if ( m_EventsEnabled == true)
                    //ob_UpdateVuMeter.NotifyUpdateVuMeter ( this, ob_UpdateVuMeter ) ;
                    UpdateVuMeter(this, new events.AudioPlayerEvents.UpdateVuMeter());                      // JQ
                }
                // 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
                        LoadStream(false);
                        // check for normal or fast play
                        if (m_FastPlay == false)
                        {                        //4
                            SoundBuffer.Write(0, m_MemoryStream, m_RefreshLength, 0);
                        }                        //-4
                        else
                        {                        //4
                            for (int i = 0; i < m_RefreshLength; i = i + (m_Step * m_FrameSize))
                            {
                                SoundBuffer.Write(i, m_MemoryStream, m_Step * m_FrameSize, 0);
                                i         = i - (2 * m_FrameSize);
                                reduction = reduction + (2 * m_FrameSize);
                            }            //-4
                        }                //-3
                    }                    //-2

                    m_lPlayed = m_lPlayed + m_RefreshLength + reduction + m_CompAddition;

                    m_BufferCheck++;
                }                //-1
                else if ((m_BufferCheck % 2 == 0) && SoundBuffer.PlayPosition < m_RefreshLength)
                {                //1
                    if (m_PlayFile == true)
                    {            //2
                        LoadStream(false);
                        if (m_FastPlay == false)
                        {                        //3
                            SoundBuffer.Write(m_RefreshLength, m_MemoryStream, 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, m_MemoryStream, m_Step * m_FrameSize, 0);
                                i         = i - (2 * m_FrameSize);
                                reduction = reduction + (2 * m_FrameSize);
                            }                                                                           //-4

                            // end of FastPlay check
                        }                //-3
                    }                    //-2


                    m_lPlayed = m_lPlayed + m_RefreshLength + reduction + m_CompAddition;

                    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 (ob_VuMeter != null)
            {
                ob_VuMeter.Reset();
            }
            if (m_PlayFile == true)
            {
                m_br.Close();
                //ob_EndOfAudioAsset.NotifyEndOfAudioAsset ( this , ob_EndOfAudioAsset) ;
            }


            //Stop () ;
            // changes the state and trigger events
            //ob_StateChanged = new StateChanged (m_State) ;

            EndOfAudioAsset(this, new events.AudioPlayerEvents.EndOfAudioAsset());              // JQ


            events.AudioPlayerEvents.StateChanged e = new events.AudioPlayerEvents.StateChanged(m_State);
            m_State = AudioPlayerState.Stopped;
            TriggerStateChangedEvent(e);

            // RefreshBuffer ends
        }
Esempio n. 7
0
        void InitPlay(long lStartPosition, long lEndPosition)
        {
            // Adjust the start and end position according to frame size
            lStartPosition = Calc.AdaptToFrame(lStartPosition, m_Asset.FrameSize);
            lEndPosition   = Calc.AdaptToFrame(lEndPosition, m_Asset.FrameSize);
            m_SamplingRate = m_Asset.SampleRate;

            // lEndPosition = 0 means that file is played to end
            if (lEndPosition != 0)
            {
                m_lLength = (lEndPosition) - lStartPosition;
            }
            else
            {
                // folowing one line is modified on 2 Aug 2006
                //m_lLength = (m_Asset .SizeInBytes  - lStartPosition ) ;
                m_lLength = (m_Asset.SizeInBytes);
            }

            WaveFormat newFormat = new WaveFormat();

            BufferDesc = new BufferDescription();

            // retrieve format from file
            m_FrameSize = m_Asset.FrameSize;
            m_Channels  = m_Asset.Channels;
            newFormat.AverageBytesPerSecond = m_Asset.SampleRate * m_Asset.FrameSize;
            newFormat.BitsPerSample         = Convert.ToInt16(m_Asset.BitDepth);
            newFormat.BlockAlign            = Convert.ToInt16(m_Asset.FrameSize);
            newFormat.Channels = Convert.ToInt16(m_Asset.Channels);

            newFormat.FormatTag = WaveFormatTag.Pcm;

            newFormat.SamplesPerSecond = m_Asset.SampleRate;

            // loads  format to buffer description
            BufferDesc.Format = newFormat;

            // calculate size of buffer so as to contain 1 second of audio
            m_SizeBuffer = m_Asset.SampleRate * m_Asset.FrameSize;
            if (m_SizeBuffer > m_lLength)
            {
                m_SizeBuffer = Convert.ToInt32(m_lLength);
            }

            m_RefreshLength = (m_Asset.SampleRate / 2) * m_Asset.FrameSize;
            // calculate the size of VuMeter Update array length
            m_UpdateVMArrayLength = m_SizeBuffer / 20;
            m_UpdateVMArrayLength = Convert.ToInt32(Calc.AdaptToFrame(Convert.ToInt32(m_UpdateVMArrayLength), m_FrameSize));
            arUpdateVM            = new byte [m_UpdateVMArrayLength];
            // reset the VuMeter (if set)
            if (ob_VuMeter != null)
            {
                ob_VuMeter.Reset();
            }

            // sets the calculated size of buffer
            BufferDesc.BufferBytes = m_SizeBuffer;

            // Global focus is set to true so that the sound can be played in background also
            BufferDesc.GlobalFocus = true;

            // initialising secondary buffer
            SoundBuffer = new SecondaryBuffer(BufferDesc, SndDevice);

            // Compensate played length due to the skip of frames during compression
            if (m_Step != 1)
            {
                m_CompAddition = (m_RefreshLength * 2) / m_Step;
                m_CompAddition = Convert.ToInt32(Calc.AdaptToFrame(m_CompAddition, m_FrameSize));
            }

            // Load from file to memory
            LoadStream(true);

            // check for fast play
            int reduction = 0;

            if (m_FastPlay == false)
            {
                SoundBuffer.Write(0, m_MemoryStream, m_SizeBuffer, 0);
            }
            else
            {
                // for fast play buffer is filled in parts with new part overlapping previous part
                for (int i = 0; i < m_SizeBuffer; i = i + (m_Step * m_FrameSize))
                {
                    SoundBuffer.Write(i, m_MemoryStream, m_Step * m_FrameSize, 0);
                    i = i - (2 * m_FrameSize);
                    // compute the difference in bytes skipped
                    reduction = reduction + (2 * m_FrameSize);
                }
            }
            // Adds the length (count) of file played into a variable
            // Folowing one line was modified on 2 Aug 2006 i.e lStartPosition is added
            m_lPlayed = m_SizeBuffer + reduction + (2 * m_CompAddition) + lStartPosition;


            m_PlayFile = true;

            // trigger  events (modified JQ)
            events.AudioPlayerEvents.StateChanged e = new events.AudioPlayerEvents.StateChanged(m_State);
            m_State = AudioPlayerState.Playing;
            StateChanged(this, e);
            // starts playing
            SoundBuffer.Play(0, BufferPlayFlags.Looping);
            m_BufferCheck = 1;

            //initialise and start thread for refreshing buffer
            RefreshThread = new Thread(new ThreadStart(RefreshBuffer));
            RefreshThread.Start();

            // end of function
        }