Esempio n. 1
0
        private static void AddInfo(Config config, DLS dls)
        {
            var info = new ListChunk("INFO");

            dls.Add(info);
            info.Add(new InfoSubChunk("INAM", config.Name));
            //info.Add(new InfoSubChunk("ICOP", config.Creator));
            info.Add(new InfoSubChunk("IENG", "Kermalis"));
            info.Add(new InfoSubChunk("ISFT", Util.Utils.ProgramName));
        }
Esempio n. 2
0
        public static void Save(Config config, string path)
        {
            var dls = new DLS();

            AddInfo(config, dls);
            Dictionary <int, (WaveSampleChunk, int)> sampleDict = AddSamples(config, dls);

            AddInstruments(config, dls, sampleDict);
            dls.Save(path);
        }
Esempio n. 3
0
        private static Dictionary <int, (WaveSampleChunk, int)> AddSamples(Config config, DLS dls)
        {
            ListChunk waves      = dls.WavePool;
            var       sampleDict = new Dictionary <int, (WaveSampleChunk, int)>((int)config.SampleTableSize);

            for (int i = 0; i < config.SampleTableSize; i++)
            {
                int ofs = config.Reader.ReadInt32(config.SampleTableOffset + (i * 4));
                if (ofs == 0)
                {
                    continue; // Skip null samples
                }

                ofs += config.SampleTableOffset;
                SampleHeader sh = config.Reader.ReadObject <SampleHeader>(ofs);

                // Create format chunk
                var fmt = new FormatChunk(WaveFormat.PCM);
                fmt.WaveInfo.Channels        = 1;
                fmt.WaveInfo.SamplesPerSec   = (uint)(sh.SampleRate >> 10);
                fmt.WaveInfo.AvgBytesPerSec  = fmt.WaveInfo.SamplesPerSec;
                fmt.WaveInfo.BlockAlign      = 1;
                fmt.FormatInfo.BitsPerSample = 8;
                // Create wave sample chunk and add loop if there is one
                var wsmp = new WaveSampleChunk
                {
                    UnityNote = 60,
                    Options   = WaveSampleOptions.NoTruncation | WaveSampleOptions.NoCompression
                };
                if (sh.DoesLoop == 0x40000000)
                {
                    wsmp.Loop = new WaveSampleLoop
                    {
                        LoopStart  = (uint)sh.LoopOffset,
                        LoopLength = (uint)(sh.Length - sh.LoopOffset),
                        LoopType   = LoopType.Forward
                    };
                }
                // Get PCM sample
                byte[] pcm = new byte[sh.Length];
                Array.Copy(config.ROM, ofs + 0x10, pcm, 0, sh.Length);

                // Add
                int dlsIndex = waves.Count;
                waves.Add(new ListChunk("wave")
                {
                    fmt,
                    wsmp,
                    new DataChunk(pcm),
                    new ListChunk("INFO")
                    {
                        new InfoSubChunk("INAM", $"Sample {i}")
                    }
                });
                sampleDict.Add(i, (wsmp, dlsIndex));
            }
            return(sampleDict);
        }
Esempio n. 4
0
 private static void AddInstruments(Config config, DLS dls, Dictionary <int, (WaveSampleChunk, int)> sampleDict)
Esempio n. 5
0
        static void Main()
        {
            InputEncoding  = Encoding.Unicode;
            OutputEncoding = Encoding.Unicode;

            Node[] nodes = { new Node("Київ"),         new Node("Житомир"),   new Node("Новоград-Волинський"),
                             new Node("Рівно"),        new Node("Луцьк"),     new Node("Бердичів"),           new Node("Вінниця"),
                             new Node("Хмельницький"), new Node("Тернопіль"), new Node("Шепетовка"),          new Node("Біла Церква"),
                             new Node("Умань"),        new Node("Черкаси"),   new Node("Кременчуг"),          new Node("Полтава"),
                             new Node("Харків"),       new Node("Прилуки"),   new Node("Суми"),               new Node("Миргород") };

            nodes[0].AddChildren(nodes[1]).AddChildren(nodes[10]).AddChildren(nodes[16]);

            nodes[1].AddChildren(nodes[9]).AddChildren(nodes[5]).AddChildren(nodes[2]);

            nodes[2].AddChildren(nodes[3]);

            nodes[3].AddChildren(nodes[4]);

            nodes[5].AddChildren(nodes[6]);

            nodes[6].AddChildren(nodes[7]);

            nodes[7].AddChildren(nodes[8]);

            nodes[10].AddChildren(nodes[11]).AddChildren(nodes[12]).AddChildren(nodes[14]);

            nodes[12].AddChildren(nodes[13]);

            nodes[14].AddChildren(nodes[15]);

            nodes[16].AddChildren(nodes[17]).AddChildren(nodes[18]);

            ConsoleKeyInfo keySwitcher;
            bool           repeat;

            do
            {
                WriteLine("Натисніть одну з наступних кнопок:\n" +
                          "1 - DFS\n" +
                          "2 - DLS\n" +
                          "3 - BFS\n\n" +
                          "На екран буде виведено шлях роботи алгоритму по проходженню по графу (DFS / DLS / BFS)\n\n" +
                          "4 - вивести усі маршрути з Києва\n" +
                          "Esc - вихід з програми");
                keySwitcher = ReadKey();

                if (keySwitcher.Key != ConsoleKey.Escape)
                {
                    repeat = true;
                }
                else
                {
                    repeat = false;
                }
                Clear();
                switch (keySwitcher.Key)
                {
                case ConsoleKey.D1:
                    var search_DFS = new DFS();
                    var path_DFS   = search_DFS.DFSList(nodes[8], nodes[15]);
                    PrintPath_DFS(path_DFS);
                    path_DFS = search_DFS.DFSList(nodes[8], nodes[15]);
                    PrintPath_DFS(path_DFS);
                    ReadKey();
                    break;

                case ConsoleKey.D2:
                    var search_DLS = new DLS();
                    var path_DLS   = search_DLS.DLSList(nodes[9], nodes[17], 4);
                    PrintPath_DLS(path_DLS);
                    path_DLS = search_DLS.DLSList(nodes[9], nodes[17], 4);
                    PrintPath_DLS(path_DLS);
                    ReadKey();
                    break;

                case ConsoleKey.D3:
                    var search_BFS = new BFS();
                    var path_BFS   = search_BFS.BFSList(nodes[0], nodes[15]);
                    PrintPath_BFS(path_BFS);
                    path_BFS = search_BFS.BFSList(nodes[0], nodes[15]);
                    PrintPath_BFS(path_BFS);
                    ReadKey();
                    break;

                case ConsoleKey.D4:
                    var search = new DFS();
                    var path   = search.DFSList(nodes[0], nodes[8]);
                    PrintPath_DFS(path);
                    path = search.DFSList(nodes[0], nodes[18]);
                    PrintPath_DFS(path);
                    path = search.DFSList(nodes[0], nodes[17]);
                    PrintPath_DFS(path);
                    path = search.DFSList(nodes[0], nodes[4]);
                    PrintPath_DFS(path);
                    path = search.DFSList(nodes[0], nodes[13]);
                    PrintPath_DFS(path);
                    path = search.DFSList(nodes[0], nodes[15]);
                    PrintPath_DFS(path);
                    path = search.DFSList(nodes[0], nodes[11]);
                    PrintPath_DFS(path);
                    ReadKey();
                    break;

                case ConsoleKey.Escape:
                    break;
                }
                Clear();
            }while (repeat);
        }
Esempio n. 6
0
        private SF2 SoundFundry(AKAO sequencer, AKAO[] sampleCollections)
        {
            DLS dls = new DLS();

            dls.SetName(FileName + ".dls");
            SF2 sf2 = new SF2();

            sf2.InfoChunk.Bank      = "Vagrant Story SoundFont for " + FileName;
            sf2.InfoChunk.Products  = "Vagrant Story";
            sf2.InfoChunk.Tools     = "https://github.com/korobetski/Vagrant-Story-Unity-Parser";
            sf2.InfoChunk.Designer  = "Korobetski Sigfrid";
            sf2.InfoChunk.Date      = DateTime.Now.ToString();
            sf2.InfoChunk.Copyright = "Musics & Samples belong to Hitoshi Sakimoto @ Squaresoft";
            sf2.InfoChunk.Comment   = string.Concat("This SoundFont was generated by reading raw AKAO format from the original game in SOUND folder.\r",
                                                    "\nNever forget that musics and samples belongs to SquareEnix, don't use them as your own. Sample collections : ",
                                                    sampleCollections[0].FileName, ", ",
                                                    sampleCollections[1].FileName);

            List <AKAOSample> Samples = new List <AKAOSample>();

            // MUSIC024.DAT has no instruments Oo, maybe unfinished work, or development test
            // we use composer program change to load articulations from WAVE000.DAT the only file where start id = 0
            if (sequencer.instruments == null)
            {
                foreach (uint id in sequencer.composer.progIDs)
                {
                    AKAOInstrument instrument = new AKAOInstrument(id, AKAOInstrument.InstrumentType.INSTR_MELODIC);
                    instrument.name    = "No instrument " + id;
                    instrument.regions = new AKAORegion[1];
                    AKAORegion defaultRegion = new AKAORegion();
                    defaultRegion.articulationId = (byte)id;
                    instrument.regions[0]        = defaultRegion;
                    sequencer.instruments        = new List <AKAOInstrument>();
                    sequencer.instruments.Add(instrument);
                }
            }

            if (sequencer.instruments != null)
            {
                uint i = 0;
                foreach (AKAOInstrument instrument in sequencer.instruments)
                {
                    if (composer.progIDs.Contains(instrument.program) || composer.A1Calls.Contains(instrument.program) || instrument.IsDrum())
                    {
                        uint midiBank = 0x00000000;
                        if (instrument.IsDrum())
                        {
                            midiBank = DLS.F_INSTRUMENT_DRUMS;
                            sf2.AddPreset(instrument.name, 0, 128);
                        }
                        else
                        {
                            sf2.AddPreset(instrument.name, (ushort)instrument.program, 0);
                        }

                        sf2.AddPresetBag();
                        sf2.AddPresetGenerator(SF2Generator.ReverbEffectsSend, new SF2GeneratorAmount {
                            UAmount = (ushort)1000
                        });
                        sf2.AddPresetGenerator(SF2Generator.Instrument, new SF2GeneratorAmount {
                            UAmount = (ushort)i
                        });
                        sf2.AddInstrument(instrument.name);
                        i++;

                        if (instrument.regions.Length > 0)
                        {
                            Lins DSLInstrument = new Lins(midiBank, instrument.program, instrument.name);

                            foreach (AKAORegion region in instrument.regions)
                            {
                                AKAOArticulation articulation = null;
                                AKAO             coll         = sampleCollections[2];

                                if (region.articulationId >= 0 && region.articulationId < 32)
                                {
                                    // trick for MUSIC024.DAT
                                    coll         = sampleCollections[1];
                                    articulation = coll.articulations[region.articulationId];
                                }
                                else if (region.articulationId >= 32 && region.articulationId < 64)
                                {
                                    coll         = sampleCollections[0];
                                    articulation = coll.articulations[region.articulationId - coll.startingArticulationId];
                                }
                                else if (region.articulationId >= 64 && region.articulationId < 128)
                                {
                                    coll = sampleCollections[1];
                                    if (region.articulationId - coll.startingArticulationId < coll.articulations.Length /* && !instrument.a1*/)
                                    {
                                        articulation = coll.articulations[region.articulationId - coll.startingArticulationId];
                                    }
                                    else
                                    {
                                        // we check in additional collection
                                        //Debug.LogWarning(region.articulationId);
                                        coll         = sampleCollections[2];
                                        articulation = coll.articulations[region.articulationId - coll.startingArticulationId];
                                    }
                                }
                                if (UseDebug)
                                {
                                    Debug.Log(string.Concat("Instrument ", i, "  ", instrument.name, "  |  Region articulation  " + region.articulationId + "  found in ", coll.FileName));
                                }



                                if (articulation != null)
                                {
                                    articulation.BuildADSR();
                                    region.articulation = articulation;
                                    AKAOSample sample = coll.samples[articulation.sampleNum];

                                    if (instrument.IsDrum())
                                    {
                                        region.unityKey = (uint)articulation.unityKey + region.lowRange - region.relativeKey; // maybe
                                    }
                                    else
                                    {
                                        region.unityKey = articulation.unityKey;
                                    }

                                    short ft = articulation.fineTune;
                                    if (ft < 0)
                                    {
                                        ft += short.MaxValue;
                                    }

                                    double freq_multiplier = ((ft * 32) + 0x100000) / (double)0x100000;
                                    double cents           = (short)(1200 * Math.Log(freq_multiplier, 2));
                                    if (articulation.fineTune < 0)
                                    {
                                        cents -= 1200;
                                    }

                                    region.fineTune  = (short)cents;
                                    sample.loopStart = (uint)(articulation.loopPt * 1.75);
                                    sample.unityKey  = (byte)region.unityKey;

                                    if (!Samples.Contains(sample))
                                    {
                                        Samples.Add(sample);
                                    }

                                    int sampleIDX = Samples.IndexOf(sample);


                                    // Making DLS
                                    Lrgn   reg = new Lrgn(region.lowRange, region.hiRange, 0x00, 0x7F);
                                    CKwsmp smp = new CKwsmp((ushort)region.unityKey, region.fineTune, region.attenuation, 1);
                                    if (articulation.loopPt != 0)
                                    {
                                        smp.AddLoop(new Loop(1, (uint)(articulation.loopPt * 1.75f), (uint)(sample.size * 1.75f - articulation.loopPt * 1.75f)));
                                    }
                                    reg.SetSample(smp);
                                    CKart2 iart = new CKart2();
                                    iart.AddPan(0x40);
                                    iart.AddADSR(articulation.A, articulation.D, articulation.S, articulation.R, articulation.AT, articulation.RT);
                                    reg.AddArticulation(iart);
                                    reg.SetWaveLinkInfo(0, 0, 1, region.sampleNum);
                                    DSLInstrument.AddRegion(reg);

                                    // http://linuxmao.org/SoundFont+specification+SF2
                                    sf2.AddInstrumentBag();
                                    sf2.AddInstrumentGenerator(SF2Generator.KeyRange, new SF2GeneratorAmount {
                                        LowByte = region.lowRange, HighByte = region.hiRange
                                    });
                                    //sf2.AddInstrumentGenerator(SF2Generator.VelRange, new SF2GeneratorAmount { LowByte = region.lowVel, HighByte = region.hiVel }); // not sure
                                    sf2.AddInstrumentGenerator(SF2Generator.VelRange, new SF2GeneratorAmount {
                                        LowByte = 0, HighByte = 127
                                    });

                                    /* C'est l'atténuation, en centibels, pour laquelle une note est atténuée en dessous de la valeur maximum prévue.
                                     * Si = 0, il n'y a aucune atténuation, la note sera jouée au maximum prévu.
                                     * Ex : 60 indique que la note sera jouée à 6 dB en-dessous du maximum prévu pour la note.
                                     * Max value = 1440 */
                                    sf2.AddInstrumentGenerator(SF2Generator.InitialAttenuation, new SF2GeneratorAmount {
                                        UAmount = (ushort)(region.attenuation / 10)
                                    });
                                    //sf2.AddInstrumentGenerator(SF2Generator.ReverbEffectsSend, new SF2GeneratorAmount { Amount = 1000 });
                                    sf2.AddInstrumentGenerator(SF2Generator.Pan, new SF2GeneratorAmount {
                                        Amount = region.pan
                                    });
                                    sf2.AddInstrumentGenerator(SF2Generator.SampleModes, new SF2GeneratorAmount {
                                        UAmount = (articulation.loopPt != 0) ? (ushort)1 : (ushort)0
                                    });
                                    sf2.AddInstrumentGenerator(SF2Generator.OverridingRootKey, new SF2GeneratorAmount {
                                        UAmount = (ushort)region.unityKey
                                    });

                                    sf2.AddInstrumentGenerator(SF2Generator.DelayVolEnv, new SF2GeneratorAmount {
                                        Amount = (short)short.MinValue
                                    });

                                    /* En timecents absolu, c'est la durée, depuis la fin du délai de l'enveloppe de volume jusqu'au point où la valeur de l'enveloppe de volume atteint son apogée.
                                     * Une valeur de 0 indique 1 seconde. Une valeur négative indique un temps inférieur à une seconde, une valeur positive un temps supérieur à une seconde.
                                     * Le nombre le plus négatif (-32768) indique conventionnellement une attaque instantanée.
                                     * Ex : un temps d'attaque de 10 ms serait 1200log2 (.01) = -7973.
                                     * En musique, le logarithme binaire intervient dans la formule permettant de déterminer la valeur en cents d’un intervalle.
                                     * Un cent, ou centième de demi-ton au tempérament égal, vaut 1200 fois le logarithme binaire du rapport de fréquence des sons concernés.
                                     * 546 * 60 ~= short.MaxValue */
                                    sf2.AddInstrumentGenerator(SF2Generator.AttackVolEnv, new SF2GeneratorAmount {
                                        Amount = (short)(1200 * Math.Log(articulation.A, 2))
                                    });
                                    sf2.AddInstrumentGenerator(SF2Generator.HoldVolEnv, new SF2GeneratorAmount {
                                        Amount = (short)0
                                    });

                                    /* C'est le temps, en timecents absolus, pour une variation de 100% de la valeur de l'enveloppe du volume pendant la phase de décroissance.
                                     * Pour l'enveloppe de volume, la décroissance tend linéairement vers le niveau de maintien, ce qui provoque un changement de dB constant pour chaque unité de temps.
                                     * Si le niveau de maintien = -100dB, le temps de décroissance de l'enveloppe de volume = temps de la phase de décroissance.
                                     * Une valeur de 0 indique 1 seconde de temps de décroissance pour un niveau zéro. Une valeur négative indique un temps inférieur à une seconde,
                                     * une valeur positive un temps supérieur à une seconde.
                                     * Ex : un temps de décroissance de 10 msec serait 1200log2 (.01) = -7973.*/
                                    sf2.AddInstrumentGenerator(SF2Generator.DecayVolEnv, new SF2GeneratorAmount {
                                        Amount = (short)(1200 * Math.Log(articulation.D, 2))
                                    });

                                    /* C'est le taux de la diminution, exprimé en centibels, pour laquelle l'enveloppe de volume décroît au cours de la phase de décroissance.
                                     * Pour l'enveloppe de volume, le niveau d'atténuation du sustain est mieux exprimé en centibels. Une valeur de 0 indique que le niveau est maximum.
                                     * Une valeur positive indique une décroissance au niveau correspondant. Les valeurs inférieures à zéro doivent être interprétés comme zéro;
                                     * conventionnellement 1000 indique une atténuation complète.
                                     * Ex : un niveau de soutien qui correspond à une valeur absolue de 12 dB en dessous du pic serait 120.*/
                                    sf2.AddInstrumentGenerator(SF2Generator.SustainVolEnv, new SF2GeneratorAmount {
                                        Amount = (short)(articulation.S)
                                    });

                                    /* C'est la durée, en timecents absolu, pour une variation de 100% de la valeur de l'enveloppe du volume pendant la phase de libération (release).
                                     * Pour l'enveloppe de volume, la phase de libération tend linéairement vers zéro depuis la niveau en cours,
                                     * ce qui provoque un changement en dB constant pour chaque unité de temps.
                                     * Si le niveau actuel est maximum, la durée du release de l'enveloppe de volume sera le temps de libération jusqu'à ce que 100 dB d'atténuation soit atteint.
                                     * Une valeur de 0 indique 1 seconde de temps de décroissance pour finir complètement. Une valeur négative indique un temps inférieur à une seconde,
                                     * une valeur positive un temps de plus d'une seconde.
                                     * Ex : un temps de libération de 10 msec serait 1200log2 (.01) = -7973. */
                                    sf2.AddInstrumentGenerator(SF2Generator.ReleaseVolEnv, new SF2GeneratorAmount {
                                        Amount = (short)(1200 * Math.Log(articulation.R, 2))
                                    });

                                    /* Décalage de la hauteur, en cents, qui sera appliqué à la note.
                                     * Il est additionnel à coarseTune. Une valeur positive indique que le son est reproduit à une hauteur plus élevée, une valeur négative indique une hauteur inférieure.
                                     * Ex : une valeur finetune = -5 provoquera un son joué cinq cents plus bas. */
                                    sf2.AddInstrumentGenerator(SF2Generator.FineTune, new SF2GeneratorAmount {
                                        Amount = (short)(region.fineTune)
                                    });
                                    sf2.AddInstrumentGenerator(SF2Generator.SampleID, new SF2GeneratorAmount {
                                        UAmount = (ushort)sampleIDX
                                    });
                                }
                            }

                            dls.AddInstrument(DSLInstrument);
                        }
                    }
                }
            }

            if (Samples.Count > 0)
            {
                foreach (AKAOSample AKAOsmp in Samples)
                {
                    WAV nw = AKAOsmp.ConvertToWAV();
                    nw.SetName(AKAOsmp.name);
                    nw.Riff = false;
                    dls.AddWave(nw);

                    short[] pcm = AKAOsmp.WAVDatas.ToArray();
                    sf2.AddSample(pcm, AKAOsmp.name, (AKAOsmp.loopStart > 0), AKAOsmp.loopStart, 44100, AKAOsmp.unityKey, 0);
                }
            }

            if (bDLS)
            {
                ToolBox.DirExNorCreate("Assets/Resources/Sounds/DLS/");
                dls.WriteFile("Assets/Resources/Sounds/DLS/" + FileName + ".dls");
            }

            if (bSF2)
            {
                ToolBox.DirExNorCreate("Assets/Resources/Sounds/SF2/");
                sf2.Save("Assets/Resources/Sounds/SF2/" + FileName + ".sf2");
            }


            return(sf2);
        }