Example #1
0
        protected override void ReadList(IntPtr ptr, IntPtr ptrTerm, string listType)
        {
            switch (listType)
            {
            case "rgn ":
                var rgn = new RGN(ptr, ptrTerm);
                List.Add(rgn.Header, rgn);
                break;

            default:
                throw new Exception(string.Format("Unknown ListType [{0}]", listType));
            }
        }
Example #2
0
        public static void SaveFromPack(string filePath, Pack pack)
        {
            var saveFile = new File();

            // WAVE
            saveFile.WavePool      = new WVPL();
            saveFile.WavePool.List = new Dictionary <int, WAVE>();
            foreach (var wav in pack.Wave.ToArray())
            {
                var wavh = new WAVE();
                wavh.Format             = new CK_FMT();
                wavh.Format.Tag         = 1;
                wavh.Format.Bits        = 16;
                wavh.Format.Channels    = 1;
                wavh.Format.SampleRate  = wav.Header.SampleRate;
                wavh.Format.BlockAlign  = (ushort)(wavh.Format.Bits * wavh.Format.Channels / 8);
                wavh.Format.BytesPerSec = wavh.Format.SampleRate * wavh.Format.BlockAlign;

                wavh.Sampler           = new CK_WSMP();
                wavh.Sampler.Gain      = wav.Header.Gain;
                wavh.Sampler.FineTune  = (short)(Math.Log(wav.Header.Pitch, 2.0) * 1200);
                wavh.Sampler.UnityNote = wav.Header.UnityNote;
                if (0 != wav.Header.LoopEnable)
                {
                    wavh.Sampler.LoopCount = 1;
                    var loop = new WaveLoop();
                    loop.Type   = 1;
                    loop.Start  = wav.Header.LoopBegin;
                    loop.Length = wav.Header.LoopLength;
                    wavh.Loops  = new Dictionary <int, WaveLoop>();
                    wavh.Loops.Add(0, loop);
                }

                wavh.Info      = new Info();
                wavh.Info.Name = wav.Info.Name;

                wavh.Data = new byte[wav.Data.Length * 2];
                var pData = Marshal.AllocHGlobal(wavh.Data.Length);
                Marshal.Copy(wav.Data, 0, pData, wav.Data.Length);
                Marshal.Copy(pData, wavh.Data, 0, wavh.Data.Length);
                Marshal.FreeHGlobal(pData);

                saveFile.WavePool.List.Add(saveFile.WavePool.List.Count, wavh);
            }

            // Inst
            saveFile.Instruments      = new LINS();
            saveFile.Instruments.List = new SortedDictionary <MidiLocale, INS>(new LINS.Sort());
            foreach (var srcPre in pack.Preset.Values)
            {
                if (1 != srcPre.Layer.Count)
                {
                    continue;
                }

                var srcIns = pack.Inst[srcPre.Layer[0].InstIndex];

                var ins = new INS();
                ins.Header                = new CK_INSH();
                ins.Header.Locale         = new MidiLocale();
                ins.Header.Locale.BankFlg = (byte)(srcPre.Header.IsDrum ? 0x80 : 0x00);
                ins.Header.Locale.BankMSB = srcPre.Header.BankMSB;
                ins.Header.Locale.BankLSB = srcPre.Header.BankLSB;
                ins.Header.Locale.ProgNum = srcPre.Header.ProgNum;
                ins.Header.Regions        = (uint)srcIns.Region.Count;

                ins.Info          = new Info();
                ins.Info.Name     = srcPre.Info.Name;
                ins.Info.Category = srcPre.Info.Category;

                ins.Articulations          = new LART();
                ins.Articulations.ART      = new ART();
                ins.Articulations.ART.List = new Dictionary <int, Connection>();
                foreach (var srcArt in srcIns.Art.ToArray())
                {
                    switch (srcArt.Type)
                    {
                    case ART_TYPE.EG_AMP_ATTACK:
                        var ampA = new Connection();
                        ampA.Source      = SRC_TYPE.NONE;
                        ampA.Control     = SRC_TYPE.NONE;
                        ampA.Destination = DST_TYPE.EG1_ATTACK_TIME;
                        ampA.Value       = srcArt.Value;
                        ins.Articulations.ART.List.Add(ins.Articulations.ART.List.Count, ampA);
                        break;

                    case ART_TYPE.EG_AMP_HOLD:
                        var ampH = new Connection();
                        ampH.Source      = SRC_TYPE.NONE;
                        ampH.Control     = SRC_TYPE.NONE;
                        ampH.Destination = DST_TYPE.EG1_HOLD_TIME;
                        ampH.Value       = srcArt.Value;
                        ins.Articulations.ART.List.Add(ins.Articulations.ART.List.Count, ampH);
                        break;

                    case ART_TYPE.EG_AMP_DECAY:
                        var ampD = new Connection();
                        ampD.Source      = SRC_TYPE.NONE;
                        ampD.Control     = SRC_TYPE.NONE;
                        ampD.Destination = DST_TYPE.EG1_DECAY_TIME;
                        ampD.Value       = srcArt.Value;
                        ins.Articulations.ART.List.Add(ins.Articulations.ART.List.Count, ampD);
                        break;

                    case ART_TYPE.EG_AMP_SUSTAIN:
                        var ampS = new Connection();
                        ampS.Source      = SRC_TYPE.NONE;
                        ampS.Control     = SRC_TYPE.NONE;
                        ampS.Destination = DST_TYPE.EG1_SUSTAIN_LEVEL;
                        ampS.Value       = srcArt.Value;
                        ins.Articulations.ART.List.Add(ins.Articulations.ART.List.Count, ampS);
                        break;

                    case ART_TYPE.EG_AMP_RELEASE:
                        var ampR = new Connection();
                        ampR.Source      = SRC_TYPE.NONE;
                        ampR.Control     = SRC_TYPE.NONE;
                        ampR.Destination = DST_TYPE.EG1_RELEASE_TIME;
                        ampR.Value       = srcArt.Value;
                        ins.Articulations.ART.List.Add(ins.Articulations.ART.List.Count, ampR);
                        break;

                    case ART_TYPE.EG_CUTOFF_ATTACK:
                        var fcA = new Connection();
                        fcA.Source      = SRC_TYPE.NONE;
                        fcA.Control     = SRC_TYPE.NONE;
                        fcA.Destination = DST_TYPE.EG1_ATTACK_TIME;
                        fcA.Value       = srcArt.Value;
                        ins.Articulations.ART.List.Add(ins.Articulations.ART.List.Count, fcA);
                        break;

                    case ART_TYPE.EG_CUTOFF_HOLD:
                        var fcH = new Connection();
                        fcH.Source      = SRC_TYPE.NONE;
                        fcH.Control     = SRC_TYPE.NONE;
                        fcH.Destination = DST_TYPE.EG1_HOLD_TIME;
                        fcH.Value       = srcArt.Value;
                        ins.Articulations.ART.List.Add(ins.Articulations.ART.List.Count, fcH);
                        break;

                    case ART_TYPE.EG_CUTOFF_DECAY:
                        var fcD = new Connection();
                        fcD.Source      = SRC_TYPE.NONE;
                        fcD.Control     = SRC_TYPE.NONE;
                        fcD.Destination = DST_TYPE.EG1_DECAY_TIME;
                        fcD.Value       = srcArt.Value;
                        ins.Articulations.ART.List.Add(ins.Articulations.ART.List.Count, fcD);
                        break;

                    case ART_TYPE.EG_CUTOFF_SUSTAIN:
                        var fcS = new Connection();
                        fcS.Source      = SRC_TYPE.NONE;
                        fcS.Control     = SRC_TYPE.NONE;
                        fcS.Destination = DST_TYPE.EG1_SUSTAIN_LEVEL;
                        fcS.Value       = srcArt.Value;
                        ins.Articulations.ART.List.Add(ins.Articulations.ART.List.Count, fcS);
                        break;

                    case ART_TYPE.EG_CUTOFF_RELEASE:
                        var fcR = new Connection();
                        fcR.Source      = SRC_TYPE.NONE;
                        fcR.Control     = SRC_TYPE.NONE;
                        fcR.Destination = DST_TYPE.EG1_RELEASE_TIME;
                        fcR.Value       = srcArt.Value;
                        ins.Articulations.ART.List.Add(ins.Articulations.ART.List.Count, fcR);
                        break;
                    }
                }

                ins.Regions      = new LRGN();
                ins.Regions.List = new SortedDictionary <CK_RGNH, RGN>(new LRGN.Sort());
                foreach (var srcRgn in srcIns.Region.Array)
                {
                    var rgn = new RGN();
                    rgn.Header.Key.Hi = srcRgn.Header.KeyHi;
                    rgn.Header.Key.Lo = srcRgn.Header.KeyLo;
                    rgn.Header.Vel.Hi = srcRgn.Header.VelHi;
                    rgn.Header.Vel.Lo = srcRgn.Header.VelLo;

                    rgn.Articulations          = new LART();
                    rgn.Articulations.ART      = new ART();
                    rgn.Articulations.ART.List = new Dictionary <int, Connection>();
                    foreach (var srcArt in srcRgn.Art.ToArray())
                    {
                        switch (srcArt.Type)
                        {
                        case ART_TYPE.WAVE_INDEX:
                            rgn.WaveLink.Channel    = 1;
                            rgn.WaveLink.TableIndex = (uint)srcArt.Value;
                            break;

                        case ART_TYPE.GAIN:
                            rgn.Sampler.Gain = srcArt.Value;
                            break;

                        case ART_TYPE.UNITY_KEY:
                            rgn.Sampler.UnityNote = (ushort)srcArt.Value;
                            break;

                        case ART_TYPE.FINE_TUNE:
                            rgn.Sampler.FineTune = (short)(Math.Log(srcArt.Value, 2.0) * 1200);
                            break;

                        case ART_TYPE.EG_AMP_ATTACK:
                            var ampA = new Connection();
                            ampA.Source      = SRC_TYPE.NONE;
                            ampA.Control     = SRC_TYPE.NONE;
                            ampA.Destination = DST_TYPE.EG1_ATTACK_TIME;
                            ampA.Value       = srcArt.Value;
                            rgn.Articulations.ART.List.Add(rgn.Articulations.ART.List.Count, ampA);
                            break;

                        case ART_TYPE.EG_AMP_HOLD:
                            var ampH = new Connection();
                            ampH.Source      = SRC_TYPE.NONE;
                            ampH.Control     = SRC_TYPE.NONE;
                            ampH.Destination = DST_TYPE.EG1_HOLD_TIME;
                            ampH.Value       = srcArt.Value;
                            rgn.Articulations.ART.List.Add(rgn.Articulations.ART.List.Count, ampH);
                            break;

                        case ART_TYPE.EG_AMP_DECAY:
                            var ampD = new Connection();
                            ampD.Source      = SRC_TYPE.NONE;
                            ampD.Control     = SRC_TYPE.NONE;
                            ampD.Destination = DST_TYPE.EG1_DECAY_TIME;
                            ampD.Value       = srcArt.Value;
                            rgn.Articulations.ART.List.Add(rgn.Articulations.ART.List.Count, ampD);
                            break;

                        case ART_TYPE.EG_AMP_SUSTAIN:
                            var ampS = new Connection();
                            ampS.Source      = SRC_TYPE.NONE;
                            ampS.Control     = SRC_TYPE.NONE;
                            ampS.Destination = DST_TYPE.EG1_SUSTAIN_LEVEL;
                            ampS.Value       = srcArt.Value;
                            rgn.Articulations.ART.List.Add(rgn.Articulations.ART.List.Count, ampS);
                            break;

                        case ART_TYPE.EG_AMP_RELEASE:
                            var ampR = new Connection();
                            ampR.Source      = SRC_TYPE.NONE;
                            ampR.Control     = SRC_TYPE.NONE;
                            ampR.Destination = DST_TYPE.EG1_RELEASE_TIME;
                            ampR.Value       = srcArt.Value;
                            rgn.Articulations.ART.List.Add(rgn.Articulations.ART.List.Count, ampR);
                            break;

                        case ART_TYPE.EG_CUTOFF_ATTACK:
                            var fcA = new Connection();
                            fcA.Source      = SRC_TYPE.NONE;
                            fcA.Control     = SRC_TYPE.NONE;
                            fcA.Destination = DST_TYPE.EG1_ATTACK_TIME;
                            fcA.Value       = srcArt.Value;
                            rgn.Articulations.ART.List.Add(rgn.Articulations.ART.List.Count, fcA);
                            break;

                        case ART_TYPE.EG_CUTOFF_HOLD:
                            var fcH = new Connection();
                            fcH.Source      = SRC_TYPE.NONE;
                            fcH.Control     = SRC_TYPE.NONE;
                            fcH.Destination = DST_TYPE.EG1_HOLD_TIME;
                            fcH.Value       = srcArt.Value;
                            rgn.Articulations.ART.List.Add(rgn.Articulations.ART.List.Count, fcH);
                            break;

                        case ART_TYPE.EG_CUTOFF_DECAY:
                            var fcD = new Connection();
                            fcD.Source      = SRC_TYPE.NONE;
                            fcD.Control     = SRC_TYPE.NONE;
                            fcD.Destination = DST_TYPE.EG1_DECAY_TIME;
                            fcD.Value       = srcArt.Value;
                            rgn.Articulations.ART.List.Add(rgn.Articulations.ART.List.Count, fcD);
                            break;

                        case ART_TYPE.EG_CUTOFF_SUSTAIN:
                            var fcS = new Connection();
                            fcS.Source      = SRC_TYPE.NONE;
                            fcS.Control     = SRC_TYPE.NONE;
                            fcS.Destination = DST_TYPE.EG1_SUSTAIN_LEVEL;
                            fcS.Value       = srcArt.Value;
                            rgn.Articulations.ART.List.Add(rgn.Articulations.ART.List.Count, fcS);
                            break;

                        case ART_TYPE.EG_CUTOFF_RELEASE:
                            var fcR = new Connection();
                            fcR.Source      = SRC_TYPE.NONE;
                            fcR.Control     = SRC_TYPE.NONE;
                            fcR.Destination = DST_TYPE.EG1_RELEASE_TIME;
                            fcR.Value       = srcArt.Value;
                            rgn.Articulations.ART.List.Add(rgn.Articulations.ART.List.Count, fcR);
                            break;
                        }
                    }

                    ins.Regions.List.Add(rgn.Header, rgn);
                }

                saveFile.Instruments.List.Add(ins.Header.Locale, ins);
            }

            saveFile.Save(filePath);
        }