Example #1
0
        static void Main(string[] args)
        {
            Conversion conv = new Conversion();

            string projectfilename = "Default.tcproject";

            if (args.Count() > 0)
            {
                projectfilename = args[0];
            }

            projectfilename = Path.GetFullPath(projectfilename);

            string jsonString;
            var    jsonserializeroptions = new JsonSerializerOptions
            {
                WriteIndented       = true,
                AllowTrailingCommas = true,
                ReadCommentHandling = JsonCommentHandling.Skip,
            };

            if (!File.Exists(projectfilename))
            {
                conv.Project = new TCProject();
                jsonString   = JsonSerializer.Serialize(conv.Project, jsonserializeroptions);
                File.WriteAllText(projectfilename, jsonString);
                Console.WriteLine("Default project file written. It will be good for you. " + projectfilename);
                Environment.Exit(0);
            }
            Directory.SetCurrentDirectory(Path.GetDirectoryName(projectfilename));

            jsonString   = File.ReadAllText(projectfilename);
            conv.Project = JsonSerializer.Deserialize <TCProject>(jsonString, jsonserializeroptions);

            conv.XM = new XMRead(conv.Project.TrackFile);
            conv.XM.Open();

            conv.ITF = conv.XM.ToITF();


            Console.WindowWidth  = 200;
            Console.WindowHeight = 60;

            conv.XM.Header.ToConsole();


            int instrnr = 1;

            foreach (Instrument instrument in conv.Project.YM2151Instruments)
            {
                YM2151Instrument instr = new YM2151Instrument();
                instr.ReadFromDMP(instrument.DMPFile);
                conv.DefinedInstruments[instrnr] = instr;
                instrnr++;
                Console.WriteLine("Instrument " + instrnr);
                instr.ToConsole();
            }

            Console.WriteLine("Patterns:");
            for (int i = 0; i < conv.XM.Patterns.Length; i++)
            {
                XMPattern pattern = conv.XM.Patterns[i];
                Console.WriteLine(i + ": " + pattern.LenghtOfPatternHeader + " " + pattern.PatternPackType + " " + pattern.NumberOfRows + " " + pattern.SizeOfPatternData);
            }

            Console.WriteLine("Instruments:");
            for (int i = 0; i < conv.XM.Instruments.Length; i++)
            {
                XMInstrument instrument = conv.XM.Instruments[i];
                Console.WriteLine(i + ": " + instrument.NumberOfSamples + " " + instrument.InstrumentName + " " + instrument.Samples[0].NameOfSample + " " + instrument.nextinstrumentofset);
            }

            List <XMNote> allnotes = new List <XMNote>();

            foreach (var item in conv.XM.Header.PatternOrderTable)
            {
                XMPattern pattern = conv.XM.Patterns[item];
                if (pattern.PatArr != null)
                {
                    foreach (var arre in pattern.PatArr)
                    {
                        if (arre == null)
                        {
                            Console.WriteLine("NULL");
                        }
                        allnotes.Add(arre);
                    }
                }
            }


            Console.WriteLine("Used effects:");
            var effects = allnotes.Select(x => x.Effect).Distinct().OrderBy(x => x).ToList();

            foreach (var item in effects)
            {
                Console.WriteLine(item.ToString("X2"));
            }


            ////foreach (var item in conv.XM.Header.PatternOrderTable)
            ////{
            ////    conv.XM.Patterns[item].PatternToConsole();
            ////    break;
            ////}


            ////for (int i = 0; i < 8; i++)
            //int i = 0;
            //{
            //    var e = instr.ToControlBytes(i);
            //    foreach (var item in e)
            //    {
            //        bytes.Add(item.Key);
            //        bytes.Add(item.Value);
            //    }
            //}


            //XMNote note = new XMNote() { octave = 3, note = 0 };
            //note.NoteIntoBytes(bytes, 0, 0b01100000);
            //note.note = 4;
            //note.NoteIntoBytes(bytes, 0, 0b00011000);

            conv.CurrentBPM        = conv.ITF.BeatPerMinute;
            conv.CurrentTickPerRow = conv.ITF.TickPerRow;
            if (conv.Project.OverrideRowPerBeat.HasValue)
            {
                conv.CurrentRowPerBeat = conv.Project.OverrideRowPerBeat.Value;
            }

            //Start pause
            conv.OBs.AddRange(new byte[] { 0, 60 });


            int patterncounter = 0;

            bool[] channelson = { true, true, true, true, true, true, true, true };
            foreach (var item in conv.ITF.PlayOrder)
            {
                int?FromRow = null;
                int?ToRow   = null;

                if (conv.Project.ConvertFrom != null && conv.Project.ConvertFrom.Track.HasValue)
                {
                    if (conv.Project.ConvertFrom.Track.Value == patterncounter)
                    {
                        FromRow = conv.Project.ConvertFrom.Row;
                    }
                    if (conv.Project.ConvertFrom.Track.Value > patterncounter)
                    {
                        patterncounter++;
                        continue;
                    }
                }

                if (conv.Project.ConvertTo != null && conv.Project.ConvertTo.Track.HasValue)
                {
                    if (conv.Project.ConvertTo.Track.Value == patterncounter)
                    {
                        ToRow = conv.Project.ConvertTo.Row;
                    }
                    if (conv.Project.ConvertTo.Track.Value < patterncounter)
                    {
                        patterncounter++;
                        continue;
                    }
                }

                conv.OBs.AddRange(item.PatternToBytes(conv, channelson, FromRow, ToRow));
                //conv.XM.Patterns[item].PatternToConsole();
                patterncounter++;
            }

            if (conv.Project.OutputDirectory != null)
            {
                Directory.SetCurrentDirectory(conv.Project.OutputDirectory);
            }

            //Trailing pause
            conv.OBs.AddRange(new byte[] { 0, 60 });

            CX16BasicWriter.ToFile(conv.OBs);
            DumbBinWriter.ToFile(conv.OBs);
            DMFWriter.ToDMF(conv);
            VGMWriter.ToFile(conv.OBs);
        }
Example #2
0
        public static void NoteIntoBytes(this ITFNote cn, List <byte> bytes, int channel, Conversion conv, byte operatormask = 0b01111000)
        {
            if (channel > 7)
            {
                return;
            }
            byte register, value = 0;

            //Instrument setup for channel
            if (cn.Instrument != null)
            {
                if (conv.CYMS.LastInstrumentPerChannel[channel] != cn.Instrument)
                {
                    ResetChannel(bytes, channel, conv.CYMS);
                    YM2151Instrument ci = conv.DefinedInstruments[cn.Instrument.Value];
                    var cbs             = ci.ToControlBytes(channel);
                    foreach (var item in cbs)
                    {
                        addtobytes(bytes, item.Key, item.Value, conv.CYMS);
                    }
                    conv.CYMS.LastInstrumentPerChannel[channel] = (byte)cn.Instrument.Value;
                }
            }

            //Volume control
            if (!cn.NoteOff && cn.Note != 0 && cn.Volume == 0)
            {
                cn.Volume = 64;
            }
            if (cn.Effect == 0xC)
            {
                cn.Volume = cn.EffectParam;
            }
            if (cn.Volume != 0)
            {
                if (conv.CYMS.LastInstrumentPerChannel[channel] != 0)
                {
                    YM2151Instrument ci = conv.DefinedInstruments[conv.CYMS.LastInstrumentPerChannel[channel]];
                    //Csak a hallható operátor hangerejét kéne módosítani. Elvileg.
                    //De most még nem.

                    Dictionary <byte, byte> tempd = new Dictionary <byte, byte>();
                    ci.SetVolumeToBytes(tempd, channel, (byte)cn.Volume.Value);
                    foreach (var item in tempd)
                    {
                        addtobytes(bytes, item.Key, item.Value, conv.CYMS);
                    }
                }
            }

            if (cn.NoteOff || cn.Effect == 0x14)
            {
                //$08       -​S​S​S​S​C​C​C​    Key On(Play Sound)​ C = Channel S = Slot(C2 M2 C1 M1)​
                //Minden operátor kuss.
                register = (byte)(0x08);
                value    = 0b00000000;
                value   += (byte)(channel & 0b00000111);
                addtobytes(bytes, register, value, conv.CYMS);
            }
            else
            {
                if (cn.Note.HasValue)
                {
                    if (conv.Project.YM2151Instruments[(byte)cn.Instrument - 1].Detune.HasValue)
                    {
                        int newnote = cn.Note.Value + conv.Project.YM2151Instruments[(byte)cn.Instrument - 1].Detune.Value;
                        if (newnote <= 0)
                        {
                            throw new Exception("Detune error. Out of range downwards.");
                        }
                        cn.Note = (byte)(newnote);
                        if (cn.NoteOff)
                        {
                            throw new Exception("Detune error. Out of range upwards.");
                        }
                    }

                    //$28 -$2F -​O​O​O​N​N​N​N​     Chn0 - 7    KeyCode​  O = Octive, N = Note​
                    register = (byte)(0x28 + channel);
                    value    = 0;
                    value   += (byte)((cn.ym2151octave() & 0b00000111) << 4);
                    value   += (byte)(cn.ym2151note() & 0b00001111);
                    addtobytes(bytes, register, value, conv.CYMS);

                    //$08       -​S​S​S​S​C​C​C​    Key On(Play Sound)​ C = Channel S = Slot(C2 M2 C1 M1)​
                    //Most minden operátor szóljon.
                    register = (byte)(0x08);
                    value    = operatormask;
                    value   += (byte)(channel & 0b00000111);
                    addtobytes(bytes, register, value, conv.CYMS);
                }
            }
        }