Ejemplo n.º 1
0
        static int Main(string[] args)
        {
            Log.writeLine += WriteLine;
#if DEBUG
            //Log.writeLine += WriteLineF;
            Log.level = LogLevel.INFO;
#else
            Log.level = LogLevel.INFO;
#endif
            int fnIndex = AnalyzeOption(args);
            int mIndex  = -1;

            if (args != null)
            {
                for (int i = fnIndex; i < args.Length; i++)
                {
                    if ((Path.GetExtension(args[i]).ToLower().IndexOf(Common.Common.objExtension) < 0) &&
                        (Path.GetExtension(args[i]).ToUpper().IndexOf(".XML") < 0)
                        )
                    {
                        continue;
                    }
                    mIndex = i;
                    break;
                }
            }

            if (mIndex < 0)
            {
                Log.WriteLine(LogLevel.INFO, string.Format("引数({0}ファイル)1個欲しいよぉ...", Common.Common.objExtension));
                return(-1);
            }

            srcFile = args[mIndex];

            if (!File.Exists(args[mIndex]))
            {
                Log.WriteLine(LogLevel.ERROR, string.Format("ファイル[{0}]が見つかりません", args[mIndex]));
                return(-1);
            }

            rsc = CheckDevice();

            try
            {
                SineWaveProvider16 waveProvider;
                int latency = 1000;

                switch (device)
                {
                case 0:
                    waveProvider = new SineWaveProvider16();
                    waveProvider.SetWaveFormat((int)SamplingRate, 2);
                    callBack    = EmuCallback;
                    audioOutput = new DirectSoundOut(latency);
                    audioOutput.Init(waveProvider);
                    break;

                case 1:
                case 2:
                    trdMain              = new Thread(new ThreadStart(RealCallback));
                    trdMain.Priority     = ThreadPriority.Highest;
                    trdMain.IsBackground = true;
                    trdMain.Name         = "trdVgmReal";
                    sw     = Stopwatch.StartNew();
                    swFreq = Stopwatch.Frequency;
                    break;
                }

                MDSound.ymf278b      ymf278b = new MDSound.ymf278b();
                MDSound.MDSound.Chip chip    = new MDSound.MDSound.Chip
                {
                    type         = MDSound.MDSound.enmInstrumentType.YMF278B,
                    ID           = 0,
                    Instrument   = ymf278b,
                    Update       = ymf278b.Update,
                    Start        = ymf278b.Start,
                    Stop         = ymf278b.Stop,
                    Reset        = ymf278b.Reset,
                    SamplingRate = SamplingRate,
                    Clock        = opl4MasterClock,
                    Volume       = 0,
                    Option       = new object[] { GetApplicationFolder() }
                };

                mds = new MDSound.MDSound(SamplingRate, samplingBuffer, new MDSound.MDSound.Chip[] { chip });
                //ppz8em = new PPZ8em(SamplingRate);
                //ppsdrv = new PPSDRV(SamplingRate);



                Common.Environment env = new Common.Environment();
                env.AddEnv("moondriver");
                env.AddEnv("moondriveropt");
                envMoonDriver    = env.GetEnvVal("moondriver");
                envMoonDriverOpt = env.GetEnvVal("moondriveropt");

                List <string> opt = (envMoonDriverOpt == null) ? (new List <string>()) : envMoonDriverOpt.ToList();
                for (int i = fnIndex; i < args.Length; i++)
                {
                    opt.Add(args[i]);
                }
                mIndex += (envMoonDriverOpt == null ? 0 : envMoonDriverOpt.Length) - fnIndex;

#if NETCOREAPP
                System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
#endif
                drv = new Driver.Driver();
                Driver.MoonDriverDotNETOption dop = new Driver.MoonDriverDotNETOption();
                //dop.isAUTO = isAUTO;
                //dop.isNRM = isNRM;
                //dop.isSPB = isSPB;
                //dop.isVA = isVA;
                //dop.usePPS = usePPS;
                //dop.usePPZ = usePPZ;
                //dop.isLoadADPCM = false;
                //dop.loadADPCMOnly = false;
                //dop.ppz8em = ppz8em;
                //dop.ppsdrv = ppsdrv;
                //dop.envPmd = envPmd;
                //dop.srcFile = srcFile;
                //dop.jumpIndex = -1;// 112;// -1;
                List <string> pop = new List <string>();
                //bool pmdvolFound = false;
                for (int i = 0; i < opt.Count; i++)
                {
                    if (i == mIndex)
                    {
                        continue;
                    }
                    string op = opt[i].ToUpper().Trim();
                    pop.Add(op);
                    //if (op.IndexOf("-D") >= 0 || op.IndexOf("/D") >= 0)
                    //    pmdvolFound = true;
                }

                Log.WriteLine(LogLevel.INFO, "");

                ((Driver.Driver)drv).Init(
                    srcFile
                    , OPL4Write
                    , SamplingRate
                    , dop
                    , pop.ToArray()
                    , appendFileReaderCallback
                    );


                ////AUTO指定の場合に構成が変わるので、構成情報を受け取ってから音量設定を行う
                //isNRM = dop.isNRM;
                //isSPB = dop.isSPB;
                //isVA = dop.isVA;
                //usePPS = dop.usePPS;
                //usePPZ = dop.usePPZ;
                //string[] pmdOptionVol = SetVolume();
                ////ユーザーがコマンドラインでDオプションを指定していない場合はpmdVolを適用させる
                //if (!pmdvolFound && pmdOptionVol != null && pmdOptionVol.Length > 0)
                //{
                //    ((Driver.Driver)drv).resetOption(pmdOptionVol);//
                //}


                List <Tuple <string, string> > tags = drv.GetTags();
                if (tags != null)
                {
                    foreach (Tuple <string, string> tag in tags)
                    {
                        if (tag.Item1 == "")
                        {
                            continue;
                        }
                        WriteLine2(LogLevel.INFO, string.Format("{0,-16} : {1}", tag.Item1, tag.Item2), 16 + 3);
                    }
                }

                Log.WriteLine(LogLevel.INFO, "");

                drv.StartRendering((int)SamplingRate
                                   , new Tuple <string, int>[] { new Tuple <string, int>("YMF278B", (int)opl4MasterClock) });

                drv.MusicSTART(0);

                switch (device)
                {
                case 0:
                    audioOutput.Play();
                    break;

                case 1:
                case 2:
                    trdMain.Start();
                    break;
                }

                Log.WriteLine(LogLevel.INFO, "演奏を終了する場合は何かキーを押してください(実chip時は特に。)");

                while (true)
                {
                    System.Threading.Thread.Sleep(1);
                    if (Console.KeyAvailable)
                    {
                        break;
                    }
                    //ステータスが0(終了)又は0未満(エラー)の場合はループを抜けて終了
                    if (drv.GetStatus() <= 0)
                    {
                        if (drv.GetStatus() == 0)
                        {
                            System.Threading.Thread.Sleep((int)(latency * 2.0));//実際の音声が発音しきるまでlatency*2の分だけ待つ
                        }
                        break;
                    }

                    if (loop != 0 && drv.GetNowLoopCounter() > loop)
                    {
                        System.Threading.Thread.Sleep((int)(latency * 2.0));//実際の音声が発音しきるまでlatency*2の分だけ待つ
                        break;
                    }
                }

                drv.MusicSTOP();
                drv.StopRendering();
                ((Driver.Driver)drv).dispStatus();
            }
            catch (MoonDriverException pe)
            {
                Log.WriteLine(LogLevel.ERROR, pe.Message);
            }
            catch (Exception ex)
            {
                Log.WriteLine(LogLevel.FATAL, "演奏失敗");
                Log.WriteLine(LogLevel.FATAL, string.Format("message:{0}", ex.Message));
                Log.WriteLine(LogLevel.FATAL, string.Format("stackTrace:{0}", ex.StackTrace));
            }
            finally
            {
                if (((Driver.Driver)drv).renderingException != null)
                {
                    Log.WriteLine(LogLevel.FATAL, "演奏失敗");
                    Log.WriteLine(LogLevel.FATAL, string.Format("message:{0}", ((Driver.Driver)drv).renderingException.Message));
                    Log.WriteLine(LogLevel.FATAL, string.Format("stackTrace:{0}", ((Driver.Driver)drv).renderingException.StackTrace));
                }

                if (audioOutput != null)
                {
                    audioOutput.Stop();
                    while (audioOutput.PlaybackState == PlaybackState.Playing)
                    {
                        Thread.Sleep(1);
                    }
                    audioOutput.Dispose();
                    audioOutput = null;
                }
                if (trdMain != null)
                {
                    trdClosed = true;
                    while (!trdStopped)
                    {
                        Thread.Sleep(1);
                    }
                }
                if (nc86ctl != null)
                {
                    nc86ctl.deinitialize();
                    nc86ctl = null;
                }
                if (nScci != null)
                {
                    nScci.Dispose();
                    nScci = null;
                }
            }

            return(0);
        }
Ejemplo n.º 2
0
        private static RSoundChip CheckDevice()
        {
            SChipType ct     = null;
            int       iCount = 0;

            switch (device)
            {
            case 1:    //GIMIC存在チェック
                nc86ctl = new Nc86ctl.Nc86ctl();
                try
                {
                    nc86ctl.initialize();
                    iCount = nc86ctl.getNumberOfChip();
                }
                catch
                {
                    iCount = 0;
                }
                if (iCount == 0)
                {
                    try { nc86ctl.deinitialize(); } catch { }
                    nc86ctl = null;
                    Log.WriteLine(LogLevel.ERROR, "Not found G.I.M.I.C");
                    device = 0;
                    break;
                }
                for (int i = 0; i < iCount; i++)
                {
                    NIRealChip rc  = nc86ctl.getChipInterface(i);
                    NIGimic2   gm  = rc.QueryInterface();
                    ChipType   cct = gm.getModuleType();
                    int        o   = -1;
                    if (cct == ChipType.CHIP_OPL3)
                    {
                        ct = new SChipType();
                        ct.SoundLocation = -1;
                        ct.BusID         = i;
                        string seri = gm.getModuleInfo().Serial;
                        if (!int.TryParse(seri, out o))
                        {
                            o  = -1;
                            ct = null;
                            continue;
                        }
                        ct.SoundChip     = o;
                        ct.ChipName      = gm.getModuleInfo().Devname;
                        ct.InterfaceName = gm.getMBInfo().Devname;
                        //isGimicOPNA = (ct.ChipName == "GMC-OPNA");
                        break;
                    }
                }
                RC86ctlSoundChip rsc = null;
                if (ct == null)
                {
                    nc86ctl.deinitialize();
                    nc86ctl = null;
                    Log.WriteLine(LogLevel.ERROR, "Not found G.I.M.I.C(OPNA module)");
                    device = 0;
                }
                else
                {
                    rsc        = new RC86ctlSoundChip(-1, ct.BusID, ct.SoundChip);
                    rsc.c86ctl = nc86ctl;
                    rsc.init();

                    rsc.SetMasterClock(opl4MasterClock);    //SoundBoardII
                    //rsc.setSSGVolume(63);//PC-8801
                }
                return(rsc);

            case 2:    //SCCI存在チェック
                nScci  = new NScci.NScci();
                iCount = nScci.NSoundInterfaceManager_.getInterfaceCount();
                if (iCount == 0)
                {
                    nScci.Dispose();
                    nScci = null;
                    Log.WriteLine(LogLevel.ERROR, "Not found SCCI.");
                    device = 0;
                    break;
                }
                for (int i = 0; i < iCount; i++)
                {
                    NSoundInterface      iIntfc = nScci.NSoundInterfaceManager_.getInterface(i);
                    NSCCI_INTERFACE_INFO iInfo  = nScci.NSoundInterfaceManager_.getInterfaceInfo(i);
                    int sCount = iIntfc.getSoundChipCount();
                    for (int s = 0; s < sCount; s++)
                    {
                        NSoundChip sc = iIntfc.getSoundChip(s);
                        int        t  = sc.getSoundChipType();
                        if (t == 1)
                        {
                            ct = new SChipType();
                            ct.SoundLocation = 0;
                            ct.BusID         = i;
                            ct.SoundChip     = s;
                            ct.ChipName      = sc.getSoundChipInfo().cSoundChipName;
                            ct.InterfaceName = iInfo.cInterfaceName;
                            goto scciExit;
                        }
                    }
                }
                scciExit :;
                RScciSoundChip rssc = null;
                if (ct == null)
                {
                    nScci.Dispose();
                    nScci = null;
                    Log.WriteLine(LogLevel.ERROR, "Not found SCCI(OPNA module).");
                    device = 0;
                }
                else
                {
                    rssc      = new RScciSoundChip(0, ct.BusID, ct.SoundChip);
                    rssc.scci = nScci;
                    rssc.init();
                }
                return(rssc);
            }

            return(null);
        }