Exemple #1
0
 public void HandleRead(object sender, byte[] data)
 {
     if (_device != null)
     {
         var message = "";
         ReadWithDevice(data, ref message);
         PushData?.Invoke(this, message);
     }
     else
     {
         PushData?.Invoke(this, data);
     }
 }
Exemple #2
0
        private void TimerCallback(object o)
        {
            if (!Monitor.TryEnter(this))
            {
                Console.WriteLine("timer tacts colision");
            }
            else
            {
                Monitor.Exit(this);
            }

            lock (this)
            {
                if (!NowPlay)
                {
                    return;
                }

                if (_frames == null || _frames.Count == 0)
                {
                    return;
                }

                // Collect all frames for a next second
                TimeSpan begin = _component.SongProgress;

                // This is the end
                if (begin >= _frames.Last().Key)
                {
                    _endFrameTimer.Change(int.MaxValue, int.MaxValue);
                    _endFrameTimer.Dispose();
                    _endFrameTimer      = null;
                    _frames             = null;
                    _nextPartBeginIndex = -1;

                    // SongEnded?.Invoke(this, EventArgs.Empty);
                    // will be called or already was called by decoratorHandler

                    return;
                }

                if (begin < TimeSpan.FromSeconds(1))
                {
                    begin = TimeSpan.Zero;
                }



                TimeSpan end = begin.Add(TimeSpan.FromSeconds(1));
                if (end > _frames.Last().Key)
                {
                    end = _frames.Last().Key;
                }

                List <Mp3Frame> currentInterval = new List <Mp3Frame>(50); // 50 avarage value of frames per second based on one song

                if (_nextPartBeginIndex != -1 &&
                    _nextPartBeginIndex < _frames.Count &&
                    TimeIsMatch(_frames[_nextPartBeginIndex].Key, begin, 50))
                {
                    // Song was ended. Timer mast be renuwed
                    if (_nextPartBeginIndex >= _frames.Count)
                    {
                        _endFrameTimer.Change(int.MaxValue, int.MaxValue);
                        _endFrameTimer.Dispose();
                        _endFrameTimer      = null;
                        _frames             = null;
                        _nextPartBeginIndex = -1;

                        // SongEnded?.Invoke(this, EventArgs.Empty);
                        // will be called or already was called by decoratorHandler

                        return;
                    }

                    int lastMatchIndex = _frames.FindIndex(_nextPartBeginIndex, t => t.Key >= end);
                    lastMatchIndex = lastMatchIndex == -1 ? _frames.Count - 1 : lastMatchIndex;


                    for (int i = _nextPartBeginIndex; i <= lastMatchIndex; i++)
                    {
                        currentInterval.Add(_frames[i].Value);
                    }

                    _nextPartBeginIndex = lastMatchIndex + 1;
                }
                else
                {
                    int firstMatch = _frames.FindIndex(t => t.Key >= begin);
                    int lastMatch  = _frames.FindIndex(firstMatch, t => t.Key >= end);

                    // <= for correct sending last frame ( not good idea, should refactor )
                    for (int i = firstMatch; i <= lastMatch; i++)
                    {
                        currentInterval.Add(_frames[i].Value);
                    }

                    _nextPartBeginIndex = lastMatch + 1;
                }

                int    packetLen = currentInterval.Sum(t => t.RawData.Length);
                byte[] data      = new byte[packetLen];

                for (int i = 0, dest = 0; i < currentInterval.Count; i++)
                {
                    Array.Copy(currentInterval[i].RawData, 0, data, dest, currentInterval[i].RawData.Length);
                    dest += currentInterval[i].RawData.Length;
                }
                //Console.Write("Send {0} bytes ",data.Length);
                PushData?.Invoke(this, data);
                //Console.WriteLine("OK");
            }
        }