Esempio n. 1
0
        private void Main()
        {
            try
            {
                Thread.CurrentThread.Name = "DataSender";
                while (true)
                {
                    Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;
                    while (!GetStart())
                    {
                        if (unmount)
                        {
                            return;
                        }
                        Thread.Sleep(0);
                    }
                    Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;

                    WaitSync?.Invoke();

                    lock (lockObj) isRunning = true;

                    double step = 1 / (double)Frq;
                    SeqCounter             = Def_SeqCounter;
                    processOverFlowCounter = 0;
                    ISpeedCount spdc;
                    switch (musicInterruptTimer)
                    {
                    case MusicInterruptTimer.StopWatch:
                    default:
                        spdc = new TimeCountByStopWatch();
                        break;

                    case MusicInterruptTimer.DateTime:
                        spdc = new TimeCountByDateTime();
                        break;

                    case MusicInterruptTimer.QueryPerformanceCounter:
                        spdc = new TimeCountByPerformanceCounter();
                        break;
                    }
                    spdc.Start();

                    double o = spdc.ElapsedMilliSec();// sw.ElapsedTicks / swFreq;

                    while (true)
                    {
                        if (!GetStart())
                        {
                            break;
                        }
                        if (unmount)
                        {
                            return;
                        }
                        Thread.Sleep(0);

                        if (parent.isVirtualOnlySend)
                        {
                            Thread.Sleep(100);
                            continue;
                        }

                        double el1 = spdc.ElapsedMilliSec();// sw.ElapsedTicks / swFreq;
                        if (el1 - o < step)
                        {
                            continue;
                        }
                        if (el1 - o >= step * Frq / 1.0)//閾値1000ms
                        {
                            processOverFlowCounter++;
                            o = el1 - step;
                            //do
                            //{
                            //    o += step;
                            //} while (el1 - o >= step);
                        }
                        //else
                        //{
                        //o += step;
                        //}

                        skipframe = Math.Max(Math.Min((int)((el1 - o) / step), 500), 1);

                        for (int skipf = 0; skipf < skipframe; skipf++)
                        {
                            o += step;

                            double lapPtr = spdc.ElapsedMilliSec();

                            {
                                //待ち合わせ割り込み
                                if (parent.GetInterrupt())
                                {
                                    //Thread.Sleep(0);
                                    continue;
                                }

                                SeqSpeed += SeqSpeedDelta;
                                if (SeqSpeedDelta == 0 && Audio.StepCounter > 0)
                                {
                                    SeqSpeed++;
                                    Audio.StepCounter--;
                                }

                                while (SeqSpeed >= 1.0)
                                {
                                    SeqCounter++;
                                    SeqSpeed -= 1.0;
                                }

                                if (SeqCounter < 0)
                                {
                                    continue;
                                }

                                //イベント実施
                                OnDataSeqFrq(SeqCounter);

                                //if (parent.Mode == SendMode.Both)
                                //{
                                //throw new ArgumentOutOfRangeException("演奏時は両モード同時指定できません");
                                //}
                                if ((parent.Mode & SendMode.MML) == SendMode.MML)
                                {
                                    if (ringBuffer.GetDataSize() == 0)
                                    {
                                        if (!parent.IsRunningAtDataMaker())
                                        {
                                            parent.ResetMode(SendMode.MML);
                                            if ((parent.Mode & SendMode.RealTime) != SendMode.RealTime)
                                            {
                                                break;
                                            }
                                        }
                                        continue;
                                    }
                                }
                                if ((parent.Mode & SendMode.RealTime) == SendMode.RealTime)
                                {
                                    if (reqSendStopData)
                                    {
                                        reqSendStopData = false;
                                        if (SendStopData() == -1)
                                        {
                                            return;
                                        }
                                    }
                                }
                                if (parent.Mode == SendMode.none)
                                {
                                    //モード指定がnoneの場合はループを抜ける
                                    break;
                                }
                            }

                            process1_Lap = spdc.ElapsedMilliSec() - lapPtr;
                            lapPtr       = spdc.ElapsedMilliSec();

                            //dataが貯まってます!
                            SendData();

                            process2_Lap = spdc.ElapsedMilliSec() - lapPtr;

                            if (reqResetSync)
                            {
                                break;
                            }
                        }

                        if (parent.Mode == SendMode.none)
                        {
                            //モード指定がnoneの場合はループを抜ける
                            break;
                        }

                        if (reqResetSync)
                        {
                            reqResetSync = false;
                            o            = spdc.ElapsedMilliSec();
                        }
                    }

                    if (SendStopData() == -1)
                    {
                        return;
                    }

                    lock (lockObj)
                    {
                        isRunning = false;
                        Counter   = 0;
                        Start     = false;
                    }
                    parent.RequestStopAtEmuChipSender();
                    parent.RequestStopAtRealChipSender();
                }
            }
            catch (Exception ex)
            {
                mml2vgmIDE.log.ForcedWrite(ex);
                lock (lockObj)
                {
                    isRunning = false;
                    Start     = false;
                }
            }
            finally
            {
                procExit = true;
                Thread.CurrentThread.Priority = ThreadPriority.Normal;
            }
        }
Esempio n. 2
0
        private void Main()
        {
            try
            {
                while (true)
                {
                    Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;
                    while (!GetStart())
                    {
                        if (unmount)
                        {
                            return;
                        }
                        Thread.Sleep(100);
                    }
                    Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;

                    WaitSync?.Invoke();

                    lock (lockObj) isRunning = true;

                    double step = 1 / (double)Frq;
                    SeqCounter             = Def_SeqCounter;
                    processOverFlowCounter = 0;
                    ISpeedCount spdc;
                    switch (musicInterruptTimer)
                    {
                    case MusicInterruptTimer.StopWatch:
                    default:
                        spdc = new TimeCountByStopWatch();
                        break;

                    case MusicInterruptTimer.DateTime:
                        spdc = new TimeCountByDateTime();
                        break;

                    case MusicInterruptTimer.QueryPerformanceCounter:
                        spdc = new TimeCountByPerformanceCounter();
                        break;
                    }
                    spdc.Start();

                    double o = spdc.ElapsedMilliSec();// sw.ElapsedTicks / swFreq;

                    while (true)
                    {
                        if (!GetStart())
                        {
                            break;
                        }
                        if (unmount)
                        {
                            return;
                        }
                        Thread.Sleep(0);

                        double el1 = spdc.ElapsedMilliSec();// sw.ElapsedTicks / swFreq;
                        if (el1 - o < step)
                        {
                            continue;
                        }
                        if (el1 - o >= step * Frq / 1.0)//閾値1000ms
                        {
                            processOverFlowCounter++;
                            o = el1 - step;
                            //do
                            //{
                            //    o += step;
                            //} while (el1 - o >= step);
                        }
                        //else
                        //{
                        //o += step;
                        //}

                        skipframe = Math.Max(Math.Min((int)((el1 - o) / step), 500), 1);

                        for (int skipf = 0; skipf < skipframe; skipf++)
                        {
                            o += step;

                            double lapPtr = spdc.ElapsedMilliSec();

                            {
                                //待ち合わせ割り込み
                                if (parent.GetInterrupt())
                                {
                                    //Thread.Sleep(0);
                                    continue;
                                }

                                SeqSpeed += SeqSpeedDelta;
                                while (SeqSpeed >= 1.0)
                                {
                                    SeqCounter++;
                                    SeqSpeed -= 1.0;
                                }

                                if (SeqCounter < 0)
                                {
                                    continue;
                                }

                                //イベント実施
                                OnDataSeqFrq(SeqCounter);

                                //if (parent.Mode == SendMode.Both)
                                //{
                                //throw new ArgumentOutOfRangeException("演奏時は両モード同時指定できません");
                                //}
                                if ((parent.Mode & SendMode.MML) == SendMode.MML)
                                {
                                    if (ringBuffer.GetDataSize() == 0)
                                    {
                                        if (!parent.IsRunningAtDataMaker())
                                        {
                                            parent.ResetMode(SendMode.MML);
                                            if ((parent.Mode & SendMode.RealTime) != SendMode.RealTime)
                                            {
                                                break;
                                            }
                                        }
                                        continue;
                                    }
                                }
                                if ((parent.Mode & SendMode.RealTime) == SendMode.RealTime)
                                {
                                    if (reqSendStopData)
                                    {
                                        reqSendStopData = false;
                                        if (SendStopData() == -1)
                                        {
                                            return;
                                        }
                                    }
                                }
                                if (parent.Mode == SendMode.none)
                                {
                                    //モード指定がnoneの場合はループを抜ける
                                    break;
                                }
                            }

                            process1_Lap = spdc.ElapsedMilliSec() - lapPtr;
                            lapPtr       = spdc.ElapsedMilliSec();

                            //dataが貯まってます!
                            while (SeqCounter >= ringBuffer.LookUpCounter())
                            {
                                if (unmount)
                                {
                                    return;
                                }
                                if (!ringBuffer.Deq(ref od, ref Counter, ref Chip, ref Type, ref Address, ref Data, ref ExData))
                                {
                                    break;
                                }

                                //パラメーターセット
                                SetMMLParameter?.Invoke(ref od, ref Counter, ref Chip, ref Type, ref Address, ref Data, ref ExData);

                                //データ加工
                                ProcessingData?.Invoke(ref od, ref Counter, ref Chip, ref Type, ref Address, ref Data, ref ExData);


                                if (od != null && od.type == enmMMLType.Tempo)
                                {
                                    parent.CurrentTempo      = (int)od.args[0];
                                    parent.CurrentClockCount = (int)od.args[1];
                                    continue;
                                }

                                if (od != null && od.type == enmMMLType.Length)
                                {
                                    if (od.linePos.chip == parent.CurrentChip &&
                                        od.linePos.ch == parent.CurrentCh - 1)
                                    {
                                        parent.CurrentNoteLength = (int)od.args[0];
                                    }
                                    continue;
                                }

                                //振り分けてEnqueue
                                if (Chip.Model == EnmVRModel.VirtualModel)
                                {
                                    while (!EmuEnq(od, Counter, Chip, Type, Address, Data, ExData))
                                    {
                                        if (!Start)
                                        {
                                            break;
                                        }
                                        if (unmount)
                                        {
                                            return;
                                        }
                                        Thread.Sleep(0);
                                    }
                                }
                                else if (Chip.Model == EnmVRModel.RealModel)
                                {
                                    while (!RealEnq(od, Counter, Chip, Type, Address, Data, ExData))
                                    {
                                        if (!Start)
                                        {
                                            break;
                                        }
                                        if (unmount)
                                        {
                                            return;
                                        }
                                        Thread.Sleep(0);
                                    }
                                }
                                else
                                {
                                    //演奏制御処理
                                    switch (Type)
                                    {
                                    case EnmDataType.Normal:
                                        break;

                                    case EnmDataType.FadeOut:
                                        parent.SetFadeOut();
                                        break;

                                    case EnmDataType.Loop:
                                        parent.CountUpLoopCounter();
                                        break;
                                    }
                                }
                            }

                            process2_Lap = spdc.ElapsedMilliSec() - lapPtr;
                        }

                        if (parent.Mode == SendMode.none)
                        {
                            //モード指定がnoneの場合はループを抜ける
                            break;
                        }
                    }

                    if (SendStopData() == -1)
                    {
                        return;
                    }

                    lock (lockObj)
                    {
                        isRunning = false;
                        Counter   = 0;
                        Start     = false;
                    }
                    parent.RequestStopAtEmuChipSender();
                    parent.RequestStopAtRealChipSender();
                }
            }
            catch (Exception ex)
            {
                mml2vgmIDE.log.ForcedWrite(ex);
                lock (lockObj)
                {
                    isRunning = false;
                    Start     = false;
                }
            }
            finally
            {
                procExit = true;
                Thread.CurrentThread.Priority = ThreadPriority.Normal;
            }
        }