예제 #1
0
파일: AudioPlayer.cs 프로젝트: daisy/obi
 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);
     }
 }
예제 #2
0
        void InitPlay(long lStartPosition, long lEndPosition)
        {
            CalculationFunctions calc = new CalculationFunctions();

            // Check if anything is playing or input length is out of bound
            if (m_State.Equals(AudioPlayerState.stopped))
            {
                // 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;
                // creates file stream from file
                fs = new FileStream(m_Asset.Path, FileMode.Open,
                                    FileAccess.Read);

                // lEndPosition = 0 means that file is played to end
                if (lEndPosition != 0)
                {
                    m_lLength = (lEndPosition) - lStartPosition;
                }
                else
                {
                    m_lLength = (m_Asset.SizeInBytes - 44 - lStartPosition);
                }

                // suet the file pointer position ahead header of file
                fs.Position = lStartPosition + 44;

                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;
                m_RefreshLength = (m_Asset.SampleRate / 2) * m_Asset.FrameSize;
                // calculate the size of VuMeter Update array length
                m_UpdateVMArrayLength = m_SizeBuffer / 50;
                m_UpdateVMArrayLength = Convert.ToInt32(calc.AdaptToFrame(Convert.ToInt32(m_UpdateVMArrayLength), m_FrameSize));
                arUpdateVM            = new byte [m_UpdateVMArrayLength];

                // 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);

                // check for fast play
                int reduction = 0;
                if (m_FastPlay == false)
                {
                    SoundBuffer.Write(0, fs, 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, fs, 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
                m_lPlayed = m_SizeBuffer + reduction;

                m_PlayFile = true;

                // Change the state and trigger event
                StateChanged ob_StateChanged = new StateChanged(m_State);
                m_State = AudioPlayerState.playing;

                TriggerStateChangedEvent(ob_StateChanged);
                StateChangedEvent(this, StateChanged.FromStopped);

                // 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 playing check
            }
            // end of function
        }