public Instrument CreateFromFile(Project project, string filename)
        {
            var bytes = System.IO.File.ReadAllBytes(filename);

            if (!bytes.Skip(0).Take(3).SequenceEqual(Encoding.ASCII.GetBytes("FTI")))
            {
                Log.LogMessage(LogSeverity.Error, "Incompatible file.");
                return(null);
            }
            if (!bytes.Skip(3).Take(3).SequenceEqual(Encoding.ASCII.GetBytes("2.4")))
            {
                Log.LogMessage(LogSeverity.Error, "Incompatible FTI version.");
                return(null);
            }

            var instType = InstrumentTypeLookup[bytes[6]];

            // Needs to match the current expansion audio. Our enum happens to match (-1) for now.
            if (instType != ExpansionType.None && instType != project.ExpansionAudio)
            {
                Log.LogMessage(LogSeverity.Error, "Audio expansion does not match the current project expansion.");
                return(null);
            }

            var offset  = 7;
            var nameLen = BitConverter.ToInt32(bytes, offset); offset += 4;
            var name    = Encoding.ASCII.GetString(bytes, offset, nameLen); offset += nameLen;

            if (project.GetInstrument(name) != null)
            {
                Log.LogMessage(LogSeverity.Error, $"An instrument named '{name}' already exists.");
                return(null);
            }

            var instrument = project.CreateInstrument(instType, name);

            if (instType == ExpansionType.Fds)
            {
                var wavEnv = instrument.Envelopes[EnvelopeType.FdsWaveform];
                for (int i = 0; i < wavEnv.Length; i++)
                {
                    wavEnv.Values[i] = (sbyte)bytes[offset++];
                }

                var modEnv = instrument.Envelopes[EnvelopeType.FdsModulation];
                for (int i = 0; i < modEnv.Length; i++)
                {
                    modEnv.Values[i] = (sbyte)bytes[offset++];
                }

                instrument.FdsWavePreset = WavePresetType.Custom;
                instrument.FdsModPreset  = WavePresetType.Custom;

                modEnv.ConvertFdsModulationToAbsolute();

                // Skip mod speed/depth/delay.
                offset += sizeof(int) * 3;

                ReadEnvelope(bytes, ref offset, instrument, EnvelopeType.Volume);
                ReadEnvelope(bytes, ref offset, instrument, EnvelopeType.Arpeggio);
                ReadEnvelope(bytes, ref offset, instrument, EnvelopeType.Pitch);
            }
            else if (instType == ExpansionType.None ||
                     instType == ExpansionType.N163)
            {
                var seqCount = bytes[offset++];

                if (seqCount != SEQ_COUNT)
                {
                    Log.LogMessage(LogSeverity.Error, $"Unexpected number of envelopes ({seqCount}).");
                    return(null);
                }

                // Envelopes
                for (int i = 0; i < SEQ_COUNT; i++)
                {
                    if (bytes[offset++] == 1)
                    {
                        ReadEnvelope(bytes, ref offset, instrument, EnvelopeTypeLookup[i]);
                    }
                }
            }
            else if (instType == ExpansionType.Vrc7)
            {
                instrument.Vrc7Patch = (byte)BitConverter.ToInt32(bytes, offset); offset += 4;

                if (instrument.Vrc7Patch == 0)
                {
                    for (int i = 0; i < 8; ++i)
                    {
                        instrument.Vrc7PatchRegs[i] = bytes[offset++];
                    }
                }
            }

            if (instType == ExpansionType.N163)
            {
                int waveSize  = BitConverter.ToInt32(bytes, offset); offset += 4;
                int wavePos   = BitConverter.ToInt32(bytes, offset); offset += 4;
                int waveCount = BitConverter.ToInt32(bytes, offset); offset += 4;

                instrument.N163WavePreset = WavePresetType.Custom;
                instrument.N163WaveSize   = (byte)waveSize;
                instrument.N163WavePos    = (byte)wavePos;

                var wavEnv = instrument.Envelopes[EnvelopeType.N163Waveform];

                // Only read the first wave for now.
                for (int j = 0; j < waveSize; j++)
                {
                    wavEnv.Values[j] = (sbyte)bytes[offset++];
                }

                if (waveCount > 1)
                {
                    Log.LogMessage(LogSeverity.Warning, $"Multiple N163 waveforms detected, only loading the first one.");
                }
            }

            // Samples
            if (instType == ExpansionType.None)
            {
                // Skip over the sample mappings for now, we will load them after the actual sample data.
                var assignedCount = BitConverter.ToInt32(bytes, offset); offset += 4;
                var mappingOffset = offset;
                offset += assignedCount * 4;

                var sampleCount = BitConverter.ToInt32(bytes, offset); offset += 4;
                var sampleMap   = new DPCMSample[sampleCount];

                for (int i = 0; i < sampleCount; i++)
                {
                    var sampleIdx     = BitConverter.ToInt32(bytes, offset); offset += 4;
                    var sampleNameLen = BitConverter.ToInt32(bytes, offset); offset += 4;
                    var sampleName    = Encoding.ASCII.GetString(bytes, offset, sampleNameLen); offset += sampleNameLen;
                    var sampleSize    = BitConverter.ToInt32(bytes, offset); offset += 4;

                    var sampleData = new byte[sampleSize];
                    Array.Copy(bytes, offset, sampleData, 0, sampleSize); offset += sampleSize;

                    sampleMap[sampleIdx] = project.CreateDPCMSampleFromDmcData(sampleName, sampleData);
                }

                for (int i = 0; i < assignedCount; i++)
                {
                    byte idx    = bytes[mappingOffset++];
                    byte sample = bytes[mappingOffset++];
                    byte pitch  = bytes[mappingOffset++];
                    byte delta  = bytes[mappingOffset++];

                    if (project.NoteSupportsDPCM(idx + 1))
                    {
                        project.MapDPCMSample(idx + 1, sampleMap[sample - 1], pitch & 0x0f, (pitch & 0x80) != 0);
                    }
                }
            }

            project.SortInstruments();

            return(instrument);
        }
Exemple #2
0
        public Project Load(string filename)
        {
#if !DEBUG
            try
#endif
            {
                var lines      = File.ReadAllLines(filename);
                var parameters = new Dictionary <string, string>();
                var project    = (Project)null;
                var instrument = (Instrument)null;
                var arpeggio   = (Arpeggio)null;
                var song       = (Song)null;
                var channel    = (Channel)null;
                var pattern    = (Pattern)null;

                SetInvariantCulture();

                foreach (var line in lines)
                {
                    var cmd = SplitLine(line.Trim(), ref parameters);

                    switch (cmd)
                    {
                    case "Project":
                    {
                        project = new Project();
                        parameters.TryGetValue("Version", out var version);
                        if (parameters.TryGetValue("Name", out var name))
                        {
                            project.Name = name;
                        }
                        if (parameters.TryGetValue("Author", out var author))
                        {
                            project.Author = author;
                        }
                        if (parameters.TryGetValue("Copyright", out var copyright))
                        {
                            project.Copyright = copyright;
                        }
                        if (parameters.TryGetValue("TempoMode", out var tempoMode))
                        {
                            project.TempoMode = TempoType.GetValueForName(tempoMode);
                        }
                        if (parameters.TryGetValue("PAL", out var pal))
                        {
                            project.PalMode = bool.Parse(pal);
                        }
                        if (parameters.TryGetValue("Expansions", out var expansions))
                        {
                            var expansionMask    = 0;
                            var expansionStrings = expansions.Split(',');

                            foreach (var s in expansionStrings)
                            {
                                var exp = ExpansionType.GetValueForShortName(s.Trim());
                                expansionMask |= ExpansionType.GetMaskFromValue(exp);
                            }

                            var numN163Channels = 1;
                            if ((expansionMask & ExpansionType.N163Mask) != 0 && parameters.TryGetValue("NumN163Channels", out var numN163ChannelsStr))
                            {
                                numN163Channels = int.Parse(numN163ChannelsStr);
                            }

                            project.SetExpansionAudioMask(expansionMask, numN163Channels);
                        }

                        if (!version.StartsWith("3.2"))
                        {
                            Log.LogMessage(LogSeverity.Error, "File was created with an incompatible version of FamiStudio. The text format is only compatible with the current version.");
                            return(null);
                        }
                        break;
                    }

                    case "DPCMSample":
                    {
                        var str  = parameters["Data"];
                        var data = new byte[str.Length / 2];
                        for (int i = 0; i < data.Length; i++)
                        {
                            data[i] = Convert.ToByte(str.Substring(i * 2, 2), 16);
                        }
                        var sample = project.CreateDPCMSampleFromDmcData(parameters["Name"], data);
                        break;
                    }

                    case "DPCMMapping":
                    {
                        var pitch = 15;
                        var loop  = false;
                        if (parameters.TryGetValue("Pitch", out var pitchStr))
                        {
                            pitch = int.Parse(pitchStr);
                        }
                        if (parameters.TryGetValue("Loop", out var loopStr))
                        {
                            loop = bool.Parse(loopStr);
                        }
                        project.MapDPCMSample(Note.FromFriendlyName(parameters["Note"]), project.GetSample(parameters["Sample"]), pitch, loop);
                        break;
                    }

                    case "Instrument":
                    {
                        var instrumentExp = ExpansionType.None;
                        if (parameters.TryGetValue("Expansion", out var instrumentExpStr))
                        {
                            instrumentExp = ExpansionType.GetValueForShortName(instrumentExpStr);
                        }
                        instrument = project.CreateInstrument(instrumentExp, parameters["Name"]);

                        if (instrument.IsFdsInstrument)
                        {
                            if (parameters.TryGetValue("FdsWavePreset", out var wavPresetStr))
                            {
                                instrument.FdsWavePreset = (byte)WavePresetType.GetValueForName(wavPresetStr);
                            }
                            if (parameters.TryGetValue("FdsModPreset", out var modPresetStr))
                            {
                                instrument.FdsWavePreset = (byte)WavePresetType.GetValueForName(modPresetStr);
                            }
                            if (parameters.TryGetValue("FdsMasterVolume", out var masterVolumeStr))
                            {
                                instrument.FdsMasterVolume = byte.Parse(masterVolumeStr);
                            }
                            if (parameters.TryGetValue("FdsModSpeed", out var fdsModSpeedStr))
                            {
                                instrument.FdsModSpeed = ushort.Parse(fdsModSpeedStr);
                            }
                            if (parameters.TryGetValue("FdsModDepth", out var fdsModDepthStr))
                            {
                                instrument.FdsModDepth = byte.Parse(fdsModDepthStr);
                            }
                            if (parameters.TryGetValue("FdsModDelay", out var fdsModDelayStr))
                            {
                                instrument.FdsModDelay = byte.Parse(fdsModDelayStr);
                            }
                        }
                        else if (instrument.IsN163Instrument)
                        {
                            if (parameters.TryGetValue("N163WavePreset", out var wavPresetStr))
                            {
                                instrument.N163WavePreset = (byte)WavePresetType.GetValueForName(wavPresetStr);
                            }
                            if (parameters.TryGetValue("N163WaveSize", out var n163WavSizeStr))
                            {
                                instrument.N163WaveSize = byte.Parse(n163WavSizeStr);
                            }
                            if (parameters.TryGetValue("N163WavePos", out var n163WavPosStr))
                            {
                                instrument.N163WavePos = byte.Parse(n163WavPosStr);
                            }
                        }
                        else if (instrument.IsVrc6Instrument)
                        {
                            if (parameters.TryGetValue("Vrc6SawMasterVolume", out var vrc6SawVolumeStr))
                            {
                                instrument.Vrc6SawMasterVolume = (byte)Vrc6SawMasterVolumeType.GetValueForName(vrc6SawVolumeStr);
                            }
                        }
                        else if (instrument.IsVrc7Instrument)
                        {
                            if (parameters.TryGetValue("Vrc7Patch", out var vrc7PatchStr))
                            {
                                instrument.Vrc7Patch = byte.Parse(vrc7PatchStr);
                            }

                            if (instrument.Vrc7Patch == Vrc7InstrumentPatch.Custom)
                            {
                                for (int i = 0; i < 8; i++)
                                {
                                    if (parameters.TryGetValue($"Vrc7Reg{i}", out var regStr))
                                    {
                                        instrument.Vrc7PatchRegs[i] = byte.Parse(regStr);
                                    }
                                }
                            }
                        }

                        break;
                    }

                    case "Arpeggio":
                    {
                        arpeggio = project.CreateArpeggio(parameters["Name"]);
                        arpeggio.Envelope.Length = int.Parse(parameters["Length"]);

                        if (parameters.TryGetValue("Loop", out var loopStr))
                        {
                            arpeggio.Envelope.Loop = int.Parse(loopStr);
                        }

                        var values = parameters["Values"].Split(',');
                        for (int j = 0; j < values.Length; j++)
                        {
                            arpeggio.Envelope.Values[j] = sbyte.Parse(values[j]);
                        }

                        break;
                    }

                    case "Envelope":
                    {
                        var env = instrument.Envelopes[EnvelopeType.GetValueForShortName(parameters["Type"])];
                        if (env != null)
                        {
                            if (env.CanResize)
                            {
                                env.Length = int.Parse(parameters["Length"]);
                            }

                            if (parameters.TryGetValue("Loop", out var loopStr))
                            {
                                env.Loop = int.Parse(loopStr);
                            }
                            if (parameters.TryGetValue("Release", out var releaseStr))
                            {
                                env.Release = int.Parse(releaseStr);
                            }
                            if (parameters.TryGetValue("Relative", out var relativeStr))
                            {
                                env.Relative = bool.Parse(relativeStr);
                            }

                            var values = parameters["Values"].Split(',');
                            for (int j = 0; j < values.Length; j++)
                            {
                                env.Values[j] = sbyte.Parse(values[j]);
                            }
                        }
                        break;
                    }

                    case "Song":
                    {
                        song = project.CreateSong(parameters["Name"]);
                        song.SetLength(int.Parse(parameters["Length"]));
                        song.SetBeatLength(int.Parse(parameters["BeatLength"]));
                        song.SetLoopPoint(int.Parse(parameters["LoopPoint"]));

                        if (song.UsesFamiTrackerTempo)
                        {
                            song.SetDefaultPatternLength(int.Parse(parameters["PatternLength"]));
                            song.FamitrackerTempo = int.Parse(parameters["FamiTrackerTempo"]);
                            song.FamitrackerSpeed = int.Parse(parameters["FamiTrackerSpeed"]);
                        }
                        else
                        {
                            var noteLength = int.Parse(parameters["NoteLength"]);

                            var groove            = parameters["Groove"].Split('-').Select(Int32.Parse).ToArray();
                            var groovePaddingMode = GroovePaddingType.GetValueForName(parameters["GroovePaddingMode"]);

                            if (!FamiStudioTempoUtils.ValidateGroove(groove) || Utils.Min(groove) != noteLength)
                            {
                                Log.LogMessage(LogSeverity.Error, "Invalid tempo settings.");
                                return(null);
                            }

                            song.ChangeFamiStudioTempoGroove(groove, false);
                            song.SetBeatLength(song.BeatLength * noteLength);
                            song.SetDefaultPatternLength(int.Parse(parameters["PatternLength"]) * noteLength);
                            song.SetGroovePaddingMode(groovePaddingMode);
                        }
                        break;
                    }

                    case "PatternCustomSettings":
                    {
                        if (project.UsesFamiTrackerTempo)
                        {
                            var beatLength = song.BeatLength;
                            if (parameters.TryGetValue("BeatLength", out var beatLengthStr))
                            {
                                beatLength = int.Parse(beatLengthStr);
                            }

                            song.SetPatternCustomSettings(int.Parse(parameters["Time"]), int.Parse(parameters["Length"]), beatLength);
                        }
                        else
                        {
                            var patternLength = int.Parse(parameters["Length"]);
                            var noteLength    = int.Parse(parameters["NoteLength"]);
                            var beatLength    = int.Parse(parameters["BeatLength"]);

                            var groove            = parameters["Groove"].Split('-').Select(Int32.Parse).ToArray();
                            var groovePaddingMode = GroovePaddingType.GetValueForName(parameters["GroovePaddingMode"]);

                            if (!FamiStudioTempoUtils.ValidateGroove(groove) || Utils.Min(groove) != noteLength)
                            {
                                Log.LogMessage(LogSeverity.Error, "Invalid tempo settings.");
                                return(null);
                            }

                            song.SetPatternCustomSettings(int.Parse(parameters["Time"]), patternLength * noteLength, beatLength * noteLength, groove, groovePaddingMode);
                        }
                        break;
                    }

                    case "Channel":
                    {
                        var channelType = ChannelType.GetValueForShortName(parameters["Type"]);
                        channel = song.GetChannelByType(channelType);
                        break;
                    }

                    case "Pattern":
                    {
                        pattern = channel.CreatePattern(parameters["Name"]);
                        break;
                    }

                    case "Note":
                    {
                        var time = int.Parse(parameters["Time"]);
                        var note = pattern.GetOrCreateNoteAt(time);

                        if (parameters.TryGetValue("Value", out var valueStr))
                        {
                            note.Value = (byte)Note.FromFriendlyName(valueStr);
                        }
                        if (note.IsMusical && parameters.TryGetValue("Duration", out var durationStr))
                        {
                            note.Duration = int.Parse(durationStr);
                        }
                        else if (note.IsStop)
                        {
                            note.Duration = 1;
                        }
                        if (note.IsMusical && parameters.TryGetValue("Release", out var releaseStr))
                        {
                            note.Release = int.Parse(releaseStr);
                        }
                        if (note.IsMusical && parameters.TryGetValue("Instrument", out var instStr) && channel.SupportsInstrument(project.GetInstrument(instStr)))
                        {
                            note.Instrument = project.GetInstrument(instStr);
                        }
                        if (note.IsMusical && parameters.TryGetValue("Arpeggio", out var arpStr) && channel.SupportsArpeggios)
                        {
                            note.Arpeggio = project.GetArpeggio(arpStr);
                        }
                        if (note.IsMusical && parameters.TryGetValue("SlideTarget", out var slideStr) && channel.SupportsSlideNotes)
                        {
                            note.SlideNoteTarget = (byte)Note.FromFriendlyName(slideStr);
                        }
                        if (note.IsMusical && parameters.TryGetValue("Attack", out var attackStr))
                        {
                            note.HasAttack = bool.Parse(attackStr);
                        }

                        if (parameters.TryGetValue("Volume", out var volumeStr) && channel.SupportsEffect(Note.EffectVolume))
                        {
                            note.Volume = byte.Parse(volumeStr);
                        }
                        if (parameters.TryGetValue("VolumeSlideTarget", out var volumeSlideStr) && channel.SupportsEffect(Note.EffectVolumeSlide))
                        {
                            note.VolumeSlideTarget = byte.Parse(volumeSlideStr);
                        }
                        if (parameters.TryGetValue("VibratoSpeed", out var vibSpeedStr) && channel.SupportsEffect(Note.EffectVibratoSpeed))
                        {
                            note.VibratoSpeed = byte.Parse(vibSpeedStr);
                        }
                        if (parameters.TryGetValue("VibratoDepth", out var vibDepthStr) && channel.SupportsEffect(Note.EffectVibratoDepth))
                        {
                            note.VibratoDepth = byte.Parse(vibDepthStr);
                        }
                        if (parameters.TryGetValue("Speed", out var speedStr) && channel.SupportsEffect(Note.EffectSpeed))
                        {
                            note.Speed = byte.Parse(speedStr);
                        }
                        if (parameters.TryGetValue("FinePitch", out var finePitchStr) && channel.SupportsEffect(Note.EffectFinePitch))
                        {
                            note.FinePitch = sbyte.Parse(finePitchStr);
                        }
                        if (parameters.TryGetValue("FdsModSpeed", out var modSpeedStr) && channel.SupportsEffect(Note.EffectFdsModSpeed))
                        {
                            note.FdsModSpeed = ushort.Parse(modSpeedStr);
                        }
                        if (parameters.TryGetValue("FdsModDepth", out var modDepthStr) && channel.SupportsEffect(Note.EffectFdsModDepth))
                        {
                            note.FdsModDepth = byte.Parse(modDepthStr);
                        }
                        if (parameters.TryGetValue("DutyCycle", out var dutyCycleStr) && channel.SupportsEffect(Note.EffectDutyCycle))
                        {
                            note.DutyCycle = byte.Parse(dutyCycleStr);
                        }
                        if (parameters.TryGetValue("NoteDelay", out var noteDelayStr) && channel.SupportsEffect(Note.EffectNoteDelay))
                        {
                            note.NoteDelay = byte.Parse(noteDelayStr);
                        }
                        if (parameters.TryGetValue("CutDelay", out var cutDelayStr) && channel.SupportsEffect(Note.EffectCutDelay))
                        {
                            note.CutDelay = byte.Parse(cutDelayStr);
                        }

                        break;
                    }

                    case "PatternInstance":
                    {
                        var time = int.Parse(parameters["Time"]);
                        channel.PatternInstances[time] = channel.GetPattern(parameters["Pattern"]);
                        break;
                    }
                    }
                }

                project.SortEverything(false);
                ResetCulture();

                return(project);
            }
#if !DEBUG
            catch (Exception e)
            {
                Log.LogMessage(LogSeverity.Error, "Please contact the developer on GitHub!");
                Log.LogMessage(LogSeverity.Error, e.Message);
                Log.LogMessage(LogSeverity.Error, e.StackTrace);
                ResetCulture();
                return(null);
            }
#endif
        }
        public static Instrument CreateFromFile(Project project, string filename)
        {
            var bytes = System.IO.File.ReadAllBytes(filename);

            if (!bytes.Skip(0).Take(3).SequenceEqual(Encoding.ASCII.GetBytes("FTI")))
            {
                return(null);
            }
            if (!bytes.Skip(3).Take(3).SequenceEqual(Encoding.ASCII.GetBytes("2.4")))
            {
                return(null);
            }

            var instType = (InstrumentType)bytes[6];

            // Needs to match the current expansion audio. Our enum happens to match (-1) for now.
            if (instType != InstrumentType.INST_2A03 && (int)(instType - 1) != project.ExpansionAudio)
            {
                return(null);
            }

            var offset  = 7;
            var nameLen = BitConverter.ToInt32(bytes, offset); offset += 4;
            var name    = Encoding.ASCII.GetString(bytes, offset, nameLen); offset += nameLen;

            if (bytes[offset++] != (int)SequenceType.SEQ_COUNT)
            {
                return(null);
            }

            if (project.GetInstrument(name) != null)
            {
                return(null);
            }

            var instrument = project.CreateInstrument((int)instType - 1, name);

            // Envelopes
            for (int i = 0; i < (int)SequenceType.SEQ_COUNT; i++)
            {
                if (bytes[offset++] == 1)
                {
                    var itemCount    = BitConverter.ToInt32(bytes, offset); offset += 4;
                    var loopPoint    = BitConverter.ToInt32(bytes, offset); offset += 4;
                    var releasePoint = BitConverter.ToInt32(bytes, offset); offset += 4;
                    var setting      = BitConverter.ToInt32(bytes, offset); offset += 4;
                    var seq          = new sbyte[itemCount];

                    for (int j = 0; j < itemCount; j++)
                    {
                        seq[j] = (sbyte)bytes[offset++];
                    }

                    if (releasePoint >= 0 && i != (int)SequenceType.SEQ_VOLUME)
                    {
                        releasePoint = -1;
                    }

                    // FamiTracker allows envelope with release with no loop. We dont allow that.
                    if (i == (int)SequenceType.SEQ_VOLUME && releasePoint != -1)
                    {
                        if (loopPoint == -1)
                        {
                            loopPoint = releasePoint;
                        }
                        if (releasePoint != -1)
                        {
                            releasePoint++;
                        }
                    }

                    Envelope env = null;
                    switch ((SequenceType)i)
                    {
                    case SequenceType.SEQ_VOLUME:    env = instrument.Envelopes[Envelope.Volume]; break;

                    case SequenceType.SEQ_PITCH:     env = instrument.Envelopes[Envelope.Pitch]; env.Relative = true; break;

                    case SequenceType.SEQ_ARPEGGIO:  env = instrument.Envelopes[Envelope.Arpeggio]; break;

                    case SequenceType.SEQ_DUTYCYCLE: instrument.DutyCycle = seq[0]; break;
                    }

                    if (env != null)
                    {
                        env.Length  = itemCount;
                        env.Loop    = loopPoint;
                        env.Release = releasePoint;
                        Array.Copy(seq, 0, env.Values, 0, itemCount);
                    }
                }
            }

            // Samples
            if (instType == InstrumentType.INST_2A03)
            {
                // Skip over the sample mappings for now, we will load them after the actual sample data.
                var assignedCount = BitConverter.ToInt32(bytes, offset); offset += 4;
                var mappingOffset = offset;
                offset += assignedCount * 4;

                var sampleCount = BitConverter.ToInt32(bytes, offset); offset += 4;
                var sampleMap   = new DPCMSample[sampleCount];

                for (int i = 0; i < sampleCount; i++)
                {
                    var sampleIdx     = BitConverter.ToInt32(bytes, offset); offset += 4;
                    var sampleNameLen = BitConverter.ToInt32(bytes, offset); offset += 4;
                    var sampleName    = Encoding.ASCII.GetString(bytes, offset, sampleNameLen); offset += sampleNameLen;
                    var sampleSize    = BitConverter.ToInt32(bytes, offset); offset += 4;
                    var sampleData    = new byte[sampleSize];

                    Array.Copy(bytes, offset, sampleData, 0, sampleSize); offset += sampleSize;

                    sampleMap[sampleIdx] = project.CreateDPCMSample(sampleName, sampleData);
                }

                for (int i = 0; i < assignedCount; i++)
                {
                    byte idx    = bytes[mappingOffset++];
                    byte sample = bytes[mappingOffset++];
                    byte pitch  = bytes[mappingOffset++];
                    byte delta  = bytes[mappingOffset++];

                    if (project.NoteSupportsDPCM(idx + 1))
                    {
                        project.MapDPCMSample(idx + 1, sampleMap[sample - 1], pitch);
                    }
                }
            }

            return(instrument);
        }
        public static Project Load(string filename)
        {
            var project = new Project();

            var envelopes = new Dictionary <int, Envelope>[Envelope.Max] {
                new Dictionary <int, Envelope>(), new Dictionary <int, Envelope>(), new Dictionary <int, Envelope>()
            };
            var duties      = new Dictionary <int, int>();
            var instruments = new Dictionary <int, Instrument>();
            var dpcms       = new Dictionary <int, DPCMSample>();
            var columns     = new int[5] {
                1, 1, 1, 1, 1
            };
            var noteLookup = new Dictionary <string, int>
            {
                ["A-"] = 9,
                ["A#"] = 10,
                ["B-"] = 11,
                ["C-"] = 0,
                ["C#"] = 1,
                ["D-"] = 2,
                ["D#"] = 3,
                ["E-"] = 4,
                ["F-"] = 5,
                ["F#"] = 6,
                ["G-"] = 7,
                ["G#"] = 8
            };

            DPCMSample currentDpcm  = null;
            int        dpcmWriteIdx = 0;
            Song       song         = null;
            string     patternName  = "";
            string     projectName;

            var lines = File.ReadAllLines(filename);

            for (int i = 0; i < lines.Length; i++)
            {
                var line = lines[i].Trim();

                if (line.StartsWith("TITLE"))
                {
                    projectName = line.Substring(5).Trim(' ', '"');
                }
                else if (line.StartsWith("MACRO"))
                {
                    var halves = line.Substring(5).Split(':');
                    var param  = halves[0].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var curve  = halves[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    var type = int.Parse(param[0]);
                    var idx  = int.Parse(param[1]);
                    var loop = int.Parse(param[2]);

                    if (type < 3)
                    {
                        var env = new Envelope();
                        env.Length = curve.Length;
                        env.Loop   = loop;
                        for (int j = 0; j < curve.Length; j++)
                        {
                            env.Values[j] = sbyte.Parse(curve[j]);
                        }
                        if (type == 2)
                        {
                            env.ConvertToAbsolute();
                        }
                        envelopes[type][idx] = env;
                    }
                    else if (type == 4)
                    {
                        duties[idx] = int.Parse(curve[0]);
                    }
                }
                else if (line.StartsWith("DPCMDEF"))
                {
                    var param = SplitStringKeepQuotes(line.Substring(7));
                    var name  = param[2];
                    var j     = 2;

                    while (!project.IsDPCMSampleNameUnique(name))
                    {
                        name = param[2] + "-" + j++;
                    }
                    currentDpcm = project.CreateDPCMSample(name, new byte[int.Parse(param[1])]);
                    dpcms[int.Parse(param[0])] = currentDpcm;
                    dpcmWriteIdx = 0;
                }
                else if (line.StartsWith("DPCM"))
                {
                    var param = line.Substring(6).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var s in param)
                    {
                        currentDpcm.Data[dpcmWriteIdx++] = Convert.ToByte(s, 16);
                    }
                }
                else if (line.StartsWith("KEYDPCM"))
                {
                    var param = line.Substring(7).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (param[0] == "0")
                    {
                        int octave   = int.Parse(param[1]);
                        int semitone = int.Parse(param[2]);
                        int note     = octave * 12 + semitone - 11;

                        if (note >= Note.NoteMin && note <= Note.NoteMax)
                        {
                            int dpcm  = int.Parse(param[3]);
                            int pitch = int.Parse(param[4]);
                            int loop  = int.Parse(param[5]);

                            project.MapDPCMSample(note, dpcms[dpcm], pitch, loop != 0);
                        }
                    }
                }
                else if (line.StartsWith("INST2A03"))
                {
                    var param = SplitStringKeepQuotes(line.Substring(8));

                    int idx  = int.Parse(param[0]);
                    int vol  = int.Parse(param[1]);
                    int arp  = int.Parse(param[2]);
                    int pit  = int.Parse(param[3]);
                    int dut  = int.Parse(param[5]);
                    var name = param[6];
                    var j    = 2;

                    if (!project.IsInstrumentNameUnique(name))
                    {
                        name = param[6] + "-" + j++;
                    }

                    var instrument = project.CreateInstrument(name);

                    if (vol >= 0)
                    {
                        instrument.Envelopes[0] = envelopes[0][vol].Clone();
                    }
                    if (arp >= 0)
                    {
                        instrument.Envelopes[1] = envelopes[1][arp].Clone();
                    }
                    if (pit >= 0)
                    {
                        instrument.Envelopes[2] = envelopes[2][pit].Clone();
                    }
                    if (dut >= 0)
                    {
                        instrument.DutyCycle = duties[dut];
                    }

                    instruments[idx] = instrument;
                }
                else if (line.StartsWith("TRACK"))
                {
                    var param = SplitStringKeepQuotes(line.Substring(5));

                    song               = project.CreateSong(param[3]);
                    song.Length        = 0;
                    song.PatternLength = int.Parse(param[0]);
                    song.Speed         = int.Parse(param[1]);
                    song.Tempo         = int.Parse(param[2]);
                }
                else if (line.StartsWith("COLUMNS"))
                {
                    var param = line.Substring(7).Split(new[] { ' ', ':' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int j = 0; j < 5; j++)
                    {
                        columns[j] = int.Parse(param[j]);
                    }
                }
                else if (line.StartsWith("ORDER"))
                {
                    var orderIdx = Convert.ToInt32(line.Substring(6, 2), 16);
                    var values   = line.Substring(5).Split(':')[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var order    = new int[5];
                    for (int j = 0; j < 5; j++)
                    {
                        int patternIdx = Convert.ToInt32(values[j], 16);
                        var name       = values[j];
                        var pattern    = song.Channels[j].GetPattern(name);

                        if (pattern == null)
                        {
                            pattern = song.Channels[j].CreatePattern(name);
                        }

                        song.Channels[j].PatternInstances[orderIdx] = pattern;
                    }

                    song.Length++;
                }
                else if (line.StartsWith("PATTERN"))
                {
                    patternName = line.Substring(8);
                }
                else if (line.StartsWith("ROW"))
                {
                    var channels = line.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    var rowIdx   = Convert.ToInt32(channels[0].Substring(4, 2), 16);

                    for (int j = 1; j <= 5; j++)
                    {
                        var noteData = channels[j].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        var pattern  = song.Channels[j - 1].GetPattern(patternName);

                        if (pattern == null)
                        {
                            continue;
                        }

                        if (noteData[0] == "---")
                        {
                            pattern.Notes[rowIdx].Value = 0;
                        }
                        else if (noteData[0] != "...")
                        {
                            int famitoneNote;

                            if (j == 4)
                            {
                                famitoneNote = (Convert.ToInt32(noteData[0].Substring(0, 1), 16) + 31) + 1;
                            }
                            else
                            {
                                int semitone = noteLookup[noteData[0].Substring(0, 2)];
                                int octave   = noteData[0][2] - '0';
                                famitoneNote = octave * 12 + semitone - 11;
                            }

                            if (famitoneNote > 0 && famitoneNote < 64)
                            {
                                pattern.Notes[rowIdx].Value      = (byte)famitoneNote;
                                pattern.Notes[rowIdx].Instrument = j == 5 ? null : instruments[Convert.ToInt32(noteData[1], 16)];
                            }
                            else
                            {
                                // Note outside of range.
                            }
                        }

                        // Read FX.
                        for (int k = 0; k < columns[j - 1]; k++)
                        {
                            string fx = noteData[3 + k];

                            switch (fx[0])
                            {
                            case 'B':     // Jump
                                pattern.Notes[rowIdx].Effect = Note.EffectJump;
                                break;

                            case 'D':     // Skip
                                pattern.Notes[rowIdx].Effect = Note.EffectSkip;
                                break;

                            case 'F':     // Tempo
                                pattern.Notes[rowIdx].Effect = Note.EffectSpeed;
                                break;

                            default:
                                continue;
                            }

                            pattern.Notes[rowIdx].EffectParam = Convert.ToByte(fx.Substring(1), 16);
                        }
                    }
                }
            }

            foreach (var s in project.Songs)
            {
                s.RemoveEmptyPatterns();

                foreach (var c in s.Channels)
                {
                    c.ColorizePatterns();
                }
            }

            return(project);
        }
Exemple #5
0
        public Project Load(string filename)
        {
            try
            {
                var lines      = File.ReadAllLines(filename);
                var parameters = new Dictionary <string, string>();
                var project    = (Project)null;
                var instrument = (Instrument)null;
                var song       = (Song)null;
                var channel    = (Channel)null;
                var pattern    = (Pattern)null;

                foreach (var line in lines)
                {
                    var cmd = SplitLine(line.Trim(), ref parameters);

                    switch (cmd)
                    {
                    case "Project":
                    {
                        project = new Project();
                        if (parameters.TryGetValue("Name", out var name))
                        {
                            project.Name = name;
                        }
                        if (parameters.TryGetValue("Author", out var author))
                        {
                            project.Author = author;
                        }
                        if (parameters.TryGetValue("Copyright", out var copyright))
                        {
                            project.Copyright = copyright;
                        }
                        if (parameters.TryGetValue("Expansion", out var expansion))
                        {
                            project.SetExpansionAudio(Array.IndexOf(Project.ExpansionShortNames, expansion));
                        }
                        if (parameters.TryGetValue("TempoMode", out var tempoMode))
                        {
                            project.TempoMode = Array.IndexOf(Project.TempoModeNames, tempoMode);
                        }
                        if (parameters.TryGetValue("PAL", out var pal))
                        {
                            project.PalMode = bool.Parse(pal);
                        }
                        break;
                    }

                    case "DPCMSample":
                    {
                        var str  = parameters["Data"];
                        var data = new byte[str.Length / 2];
                        for (int i = 0; i < data.Length; i++)
                        {
                            data[i] = Convert.ToByte(str.Substring(i * 2, 2), 16);
                        }
                        project.CreateDPCMSample(parameters["Name"], data);
                        break;
                    }

                    case "DPCMMapping":
                    {
                        var pitch = 15;
                        var loop  = false;
                        if (parameters.TryGetValue("Pitch", out var pitchStr))
                        {
                            pitch = int.Parse(pitchStr);
                        }
                        if (parameters.TryGetValue("Loop", out var loopStr))
                        {
                            loop = bool.Parse(loopStr);
                        }
                        project.MapDPCMSample(Note.FromFriendlyName(parameters["Note"]), project.GetSample(parameters["Sample"]), pitch, loop);
                        break;
                    }

                    case "Instrument":
                    {
                        instrument = project.CreateInstrument(parameters.TryGetValue("Expansion", out _) ? project.ExpansionAudio : Project.ExpansionNone, parameters["Name"]);

                        if (instrument.ExpansionType == Project.ExpansionFds)
                        {
                            if (parameters.TryGetValue("FdsWavePreset", out var wavPresetStr))
                            {
                                instrument.FdsWavePreset = (byte)Array.IndexOf(Envelope.PresetNames, wavPresetStr);
                            }
                            if (parameters.TryGetValue("FdsModPreset", out var modPresetStr))
                            {
                                instrument.FdsWavePreset = (byte)Array.IndexOf(Envelope.PresetNames, modPresetStr);
                            }
                            if (parameters.TryGetValue("FdsMasterVolume", out var masterVolumeStr))
                            {
                                instrument.FdsMasterVolume = byte.Parse(masterVolumeStr);
                            }
                            if (parameters.TryGetValue("FdsModSpeed", out var fdsModSpeedStr))
                            {
                                instrument.FdsModSpeed = ushort.Parse(fdsModSpeedStr);
                            }
                            if (parameters.TryGetValue("FdsModDepth", out var fdsModDepthStr))
                            {
                                instrument.FdsModDepth = byte.Parse(fdsModDepthStr);
                            }
                            if (parameters.TryGetValue("FdsModDelay", out var fdsModDelayStr))
                            {
                                instrument.FdsModDelay = byte.Parse(fdsModDelayStr);
                            }
                        }
                        else if (instrument.ExpansionType == Project.ExpansionN163)
                        {
                            if (parameters.TryGetValue("N163WavePreset", out var wavPresetStr))
                            {
                                instrument.N163WavePreset = (byte)Array.IndexOf(Envelope.PresetNames, wavPresetStr);
                            }
                            if (parameters.TryGetValue("N163WaveSize", out var n163WavSizeStr))
                            {
                                instrument.N163WaveSize = byte.Parse(n163WavSizeStr);
                            }
                            if (parameters.TryGetValue("N163WavePos", out var n163WavPosStr))
                            {
                                instrument.N163WavePos = byte.Parse(n163WavPosStr);
                            }
                        }
                        else if (instrument.ExpansionType == Project.ExpansionVrc7)
                        {
                            if (parameters.TryGetValue("Vrc7Patch", out var vrc7PatchStr))
                            {
                                instrument.Vrc7Patch = byte.Parse(vrc7PatchStr);
                            }

                            if (instrument.Vrc7Patch == 0)
                            {
                                for (int i = 0; i < 8; i++)
                                {
                                    if (parameters.TryGetValue($"Vrc7Reg{i}", out var regStr))
                                    {
                                        instrument.Vrc7PatchRegs[i] = byte.Parse(regStr);
                                    }
                                }
                            }
                        }

                        break;
                    }

                    case "Envelope":
                    {
                        var env = instrument.Envelopes[Array.IndexOf(Envelope.EnvelopeShortNames, parameters["Type"])];
                        if (env != null)
                        {
                            if (env.CanResize)
                            {
                                env.Length = int.Parse(parameters["Length"]);
                            }

                            if (parameters.TryGetValue("Loop", out var loopStr))
                            {
                                env.Loop = int.Parse(loopStr);
                            }
                            if (parameters.TryGetValue("Release", out var releaseStr))
                            {
                                env.Release = int.Parse(releaseStr);
                            }
                            if (parameters.TryGetValue("Relative", out var relativeStr))
                            {
                                env.Relative = bool.Parse(relativeStr);
                            }

                            var values = parameters["Values"].Split(',');
                            for (int j = 0; j < values.Length; j++)
                            {
                                env.Values[j] = sbyte.Parse(values[j]);
                            }
                        }
                        break;
                    }

                    case "Song":
                    {
                        song = project.CreateSong(parameters["Name"]);
                        song.SetLength(int.Parse(parameters["Length"]));
                        song.SetBarLength(int.Parse(parameters["BarLength"]));
                        song.SetLoopPoint(int.Parse(parameters["LoopPoint"]));

                        if (song.UsesFamiTrackerTempo)
                        {
                            song.SetDefaultPatternLength(int.Parse(parameters["PatternLength"]));
                            song.FamitrackerTempo = int.Parse(parameters["FamiTrackerTempo"]);
                            song.FamitrackerSpeed = int.Parse(parameters["FamiTrackerSpeed"]);
                        }
                        else
                        {
                            var noteLength = int.Parse(parameters["NoteLength"]);
                            song.ResizeNotes(noteLength, false);
                            song.SetBarLength(int.Parse(parameters["BarLength"]) * noteLength);
                            song.SetDefaultPatternLength(int.Parse(parameters["PatternLength"]) * noteLength);
                        }
                        break;
                    }

                    case "PatternCustomSettings":
                    {
                        if (project.UsesFamiTrackerTempo)
                        {
                            song.SetPatternCustomSettings(int.Parse(parameters["Time"]), int.Parse(parameters["Length"]));
                        }
                        else
                        {
                            var patternLength = int.Parse(parameters["Length"]);
                            var noteLength    = int.Parse(parameters["NoteLength"]);
                            var barLength     = int.Parse(parameters["BarLength"]);

                            song.SetPatternCustomSettings(int.Parse(parameters["Time"]), patternLength * noteLength, noteLength, barLength * noteLength);
                        }
                        break;
                    }

                    case "Channel":
                    {
                        var channelType = Array.IndexOf(Channel.ChannelExportNames, parameters["Type"]);
                        channel = song.GetChannelByType(channelType);
                        break;
                    }

                    case "Pattern":
                    {
                        pattern = channel.CreatePattern(parameters["Name"]);
                        break;
                    }

                    case "Note":
                    {
                        var time = int.Parse(parameters["Time"]);
                        var note = pattern.GetOrCreateNoteAt(time);

                        if (parameters.TryGetValue("Value", out var valueStr))
                        {
                            note.Value = (byte)Note.FromFriendlyName(valueStr);
                        }
                        if (parameters.TryGetValue("Instrument", out var instStr))
                        {
                            note.Instrument = project.GetInstrument(instStr);
                        }
                        if (parameters.TryGetValue("Attack", out var attackStr))
                        {
                            note.HasAttack = bool.Parse(attackStr);
                        }
                        if (parameters.TryGetValue("Volume", out var volumeStr))
                        {
                            note.Volume = byte.Parse(volumeStr);
                        }
                        if (parameters.TryGetValue("VibratoSpeed", out var vibSpeedStr))
                        {
                            note.VibratoSpeed = byte.Parse(vibSpeedStr);
                        }
                        if (parameters.TryGetValue("VibratoDepth", out var vibDepthStr))
                        {
                            note.VibratoDepth = byte.Parse(vibDepthStr);
                        }
                        if (parameters.TryGetValue("FinePitch", out var finePitchStr))
                        {
                            note.FinePitch = sbyte.Parse(finePitchStr);
                        }
                        if (parameters.TryGetValue("SlideTarget", out var slideStr))
                        {
                            note.SlideNoteTarget = (byte)Note.FromFriendlyName(valueStr);
                        }
                        if (parameters.TryGetValue("FdsModSpeed", out var modSpeedStr))
                        {
                            note.FdsModSpeed = ushort.Parse(modSpeedStr);
                        }
                        if (parameters.TryGetValue("FdsModDepth", out var modDepthStr))
                        {
                            note.FdsModDepth = byte.Parse(modDepthStr);
                        }

                        break;
                    }

                    case "PatternInstance":
                    {
                        var time = int.Parse(parameters["Time"]);
                        channel.PatternInstances[time] = channel.GetPattern(parameters["Pattern"]);
                        break;
                    }
                    }
                }

                return(project);
            }
            catch
            {
                return(null);
            }
        }
Exemple #6
0
        public static Project Load(string filename)
        {
            var project = new Project();

            var envelopes   = new Dictionary <int, Envelope> [Project.ExpansionCount, Envelope.Max];
            var duties      = new Dictionary <int, int> [Project.ExpansionCount];
            var instruments = new Dictionary <int, Instrument>();
            var dpcms       = new Dictionary <int, DPCMSample>();
            var columns     = new int[5] {
                1, 1, 1, 1, 1
            };
            var patternFxData = new Dictionary <Pattern, RowFxData[, ]>();
            var noteLookup    = new Dictionary <string, int>
            {
                ["A-"] = 9,
                ["A#"] = 10,
                ["B-"] = 11,
                ["C-"] = 0,
                ["C#"] = 1,
                ["D-"] = 2,
                ["D#"] = 3,
                ["E-"] = 4,
                ["F-"] = 5,
                ["F#"] = 6,
                ["G-"] = 7,
                ["G#"] = 8
            };

            for (int i = 0; i < envelopes.GetLength(0); i++)
            {
                for (int j = 0; j < envelopes.GetLength(1); j++)
                {
                    envelopes[i, j] = new Dictionary <int, Envelope>();
                }
            }

            for (int i = 0; i < duties.Length; i++)
            {
                duties[i] = new Dictionary <int, int>();
            }

            DPCMSample currentDpcm  = null;
            int        dpcmWriteIdx = 0;
            Song       song         = null;
            string     patternName  = "";

            var lines = File.ReadAllLines(filename);

            for (int i = 0; i < lines.Length; i++)
            {
                var line = lines[i].Trim();

                if (line.StartsWith("TITLE"))
                {
                    project.Name = line.Substring(5).Trim(' ', '"');
                }
                else if (line.StartsWith("AUTHOR"))
                {
                    project.Author = line.Substring(6).Trim(' ', '"');
                }
                else if (line.StartsWith("COPYRIGHT"))
                {
                    project.Copyright = line.Substring(9).Trim(' ', '"');
                }
                else if (line.StartsWith("EXPANSION"))
                {
                    var exp = int.Parse(line.Substring(9));
                    if (exp == 1)
                    {
                        project.SetExpansionAudio(Project.ExpansionVrc6);
                    }
                }
                else if (line.StartsWith("MACRO"))
                {
                    var expansion = line.Substring(5, 4) == "VRC6" ? Project.ExpansionVrc6 : Project.ExpansionNone;
                    var halves    = line.Substring(line.IndexOf(' ')).Split(':');
                    var param     = halves[0].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var curve     = halves[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    var type = int.Parse(param[0]);
                    var idx  = int.Parse(param[1]);
                    var loop = int.Parse(param[2]);
                    var rel  = int.Parse(param[3]);

                    if (type < 3)
                    {
                        var env = new Envelope();
                        env.Length = curve.Length;

                        // FamiTracker allows envelope with release with no loop. We dont allow that.
                        if (type == Envelope.Volume && loop == -1 && rel != -1)
                        {
                            loop = rel;
                        }

                        env.Loop    = loop;
                        env.Release = type == Envelope.Volume && rel != -1 ? rel + 1 : -1;

                        for (int j = 0; j < curve.Length; j++)
                        {
                            env.Values[j] = sbyte.Parse(curve[j]);
                        }
                        if (type == 2)
                        {
                            env.Relative = true;
                        }
                        envelopes[expansion, type][idx] = env;
                    }
                    else if (type == 4)
                    {
                        duties[expansion][idx] = int.Parse(curve[0]);
                    }
                }
                else if (line.StartsWith("DPCMDEF"))
                {
                    var param = SplitStringKeepQuotes(line.Substring(7));
                    var name  = param[2];
                    var j     = 2;

                    while (!project.IsDPCMSampleNameUnique(name))
                    {
                        name = param[2] + "-" + j++;
                    }
                    currentDpcm = project.CreateDPCMSample(name, new byte[int.Parse(param[1])]);
                    dpcms[int.Parse(param[0])] = currentDpcm;
                    dpcmWriteIdx = 0;
                }
                else if (line.StartsWith("DPCM"))
                {
                    var param = line.Substring(6).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var s in param)
                    {
                        currentDpcm.Data[dpcmWriteIdx++] = Convert.ToByte(s, 16);
                    }
                }
                else if (line.StartsWith("KEYDPCM"))
                {
                    var param = line.Substring(7).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (param[0] == "0")
                    {
                        int octave   = int.Parse(param[1]);
                        int semitone = int.Parse(param[2]);
                        int note     = octave * 12 + semitone + 1;

                        if (project.NoteSupportsDPCM(note))
                        {
                            int dpcm  = int.Parse(param[3]);
                            int pitch = int.Parse(param[4]);
                            int loop  = int.Parse(param[5]);

                            project.MapDPCMSample(note, dpcms[dpcm], pitch, loop != 0);
                        }
                    }
                }
                else if (line.StartsWith("INST2A03") || line.StartsWith("INSTVRC6"))
                {
                    var expansion = line.Substring(4, 4) == "VRC6" ? Project.ExpansionVrc6 : Project.ExpansionNone;
                    var param     = SplitStringKeepQuotes(line.Substring(line.IndexOf(' ')));

                    int idx  = int.Parse(param[0]);
                    int vol  = int.Parse(param[1]);
                    int arp  = int.Parse(param[2]);
                    int pit  = int.Parse(param[3]);
                    int dut  = int.Parse(param[5]);
                    var name = param[6];
                    var j    = 2;

                    if (!project.IsInstrumentNameUnique(name))
                    {
                        name = param[6] + "-" + j++;
                    }

                    var expansionType = line.StartsWith("INSTVRC6") ? Project.ExpansionVrc6 : Project.ExpansionNone;
                    var instrument    = project.CreateInstrument(expansionType, name);

                    if (vol >= 0)
                    {
                        instrument.Envelopes[0] = envelopes[expansion, 0][vol].Clone();
                    }
                    if (arp >= 0)
                    {
                        instrument.Envelopes[1] = envelopes[expansion, 1][arp].Clone();
                    }
                    if (pit >= 0)
                    {
                        instrument.Envelopes[2] = envelopes[expansion, 2][pit].Clone();
                    }
                    if (dut >= 0)
                    {
                        instrument.DutyCycle = duties[expansionType][dut];
                    }

                    instruments[idx] = instrument;
                }
                else if (line.StartsWith("TRACK"))
                {
                    var param = SplitStringKeepQuotes(line.Substring(5));

                    song = project.CreateSong(param[3]);
                    song.SetLength(0);
                    song.SetPatternLength(int.Parse(param[0]));
                    song.Speed = int.Parse(param[1]);
                    song.Tempo = int.Parse(param[2]);
                    columns    = new int[song.Channels.Length];
                }
                else if (line.StartsWith("COLUMNS"))
                {
                    var param = line.Substring(7).Split(new[] { ' ', ':' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int j = 0; j < song.Channels.Length; j++)
                    {
                        columns[j] = int.Parse(param[j]);
                    }
                }
                else if (line.StartsWith("ORDER"))
                {
                    var orderIdx = Convert.ToInt32(line.Substring(6, 2), 16);
                    var values   = line.Substring(5).Split(':')[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var order    = new int[song.Channels.Length];
                    for (int j = 0; j < song.Channels.Length; j++)
                    {
                        int patternIdx = Convert.ToInt32(values[j], 16);
                        var name       = values[j];
                        var pattern    = song.Channels[j].GetPattern(name);

                        if (pattern == null)
                        {
                            pattern = song.Channels[j].CreatePattern(name);
                        }

                        song.Channels[j].PatternInstances[orderIdx] = pattern;
                    }

                    song.SetLength(song.Length + 1);
                }
                else if (line.StartsWith("PATTERN"))
                {
                    patternName = line.Substring(8);
                }
                else if (line.StartsWith("ROW"))
                {
                    var channels = line.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    var rowIdx   = Convert.ToInt32(channels[0].Substring(4, 2), 16);

                    for (int j = 1; j <= song.Channels.Length; j++)
                    {
                        var noteData = channels[j].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        var pattern  = song.Channels[j - 1].GetPattern(patternName);

                        if (pattern == null)
                        {
                            continue;
                        }

                        //var fxData = new RowFxData[song.PatternLength];
                        if (!patternFxData.ContainsKey(pattern))
                        {
                            patternFxData[pattern] = new RowFxData[song.PatternLength, 4];
                        }

                        // Note
                        if (noteData[0] == "---")
                        {
                            pattern.Notes[rowIdx].Value = Note.NoteStop;
                        }
                        else if (noteData[0] == "===")
                        {
                            pattern.Notes[rowIdx].Value = Note.NoteRelease;
                        }
                        else if (noteData[0] != "...")
                        {
                            int famitoneNote;

                            if (j == 4)
                            {
                                famitoneNote = (Convert.ToInt32(noteData[0].Substring(0, 1), 16) + 31) + 1;
                            }
                            else
                            {
                                int semitone = noteLookup[noteData[0].Substring(0, 2)];
                                int octave   = noteData[0][2] - '0';
                                famitoneNote = octave * 12 + semitone + 1;
                            }

                            if (famitoneNote >= Note.MusicalNoteMin && famitoneNote <= Note.MusicalNoteMax)
                            {
                                pattern.Notes[rowIdx].Value      = (byte)famitoneNote;
                                pattern.Notes[rowIdx].Instrument = j == 5 ? null : instruments[Convert.ToInt32(noteData[1], 16)];
                            }
                            else
                            {
                                // Note outside of range.
                            }
                        }

                        // Volume
                        if (noteData[2] != ".")
                        {
                            pattern.Notes[rowIdx].Volume = Convert.ToByte(noteData[2], 16);
                        }

                        // Read FX.
                        for (int k = 0; k < columns[j - 1]; k++)
                        {
                            var fx = noteData[3 + k];

                            if (fx == "...")
                            {
                                continue;
                            }

                            var param = Convert.ToByte(fx.Substring(1), 16);

                            var fxData = patternFxData[pattern];
                            fxData[rowIdx, k].fx    = fx[0];
                            fxData[rowIdx, k].param = param;

                            switch (fx[0])
                            {
                            case 'B':     // Jump
                                pattern.Notes[rowIdx].Jump = param;
                                break;

                            case 'D':     // Skip
                                pattern.Notes[rowIdx].Skip = param;
                                break;

                            case 'F':              // Tempo
                                if (param <= 0x1f) // We only support speed change for now.
                                {
                                    pattern.Notes[rowIdx].Speed = param;
                                }
                                break;

                            case '4':     // Vibrato
                                pattern.Notes[rowIdx].VibratoDepth = (byte)(param & 0x0f);
                                pattern.Notes[rowIdx].VibratoSpeed = (byte)VibratoSpeedImportLookup[param >> 4];

                                if (pattern.Notes[rowIdx].VibratoDepth == 0 ||
                                    pattern.Notes[rowIdx].VibratoSpeed == 0)
                                {
                                    pattern.Notes[rowIdx].Vibrato = 0;
                                }
                                break;
                            }
                        }
                    }
                }
            }

            foreach (var s in project.Songs)
            {
                CreateSlideNotes(s, patternFxData);

                s.RemoveEmptyPatterns();
                s.SetSensibleBarLength();

                foreach (var c in s.Channels)
                {
                    c.ColorizePatterns();
                }
            }

            project.UpdateAllLastValidNotesAndVolume();

            return(project);
        }
Exemple #7
0
        public Project Load(string filename)
        {
            var envelopes   = new Dictionary <int, Envelope> [Project.ExpansionCount, Envelope.Count];
            var instruments = new Dictionary <int, Instrument>();
            var dpcms       = new Dictionary <int, DPCMSample>();
            var columns     = new int[5] {
                1, 1, 1, 1, 1
            };

            for (int i = 0; i < envelopes.GetLength(0); i++)
            {
                for (int j = 0; j < envelopes.GetLength(1); j++)
                {
                    envelopes[i, j] = new Dictionary <int, Envelope>();
                }
            }

            project           = new Project();
            project.TempoMode = Project.TempoFamiTracker;

            DPCMSample currentDpcm  = null;
            int        dpcmWriteIdx = 0;
            Song       song         = null;
            string     patternName  = "";

            var lines = File.ReadAllLines(filename);

            for (int i = 0; i < lines.Length; i++)
            {
                var line = lines[i].Trim();

                if (line.StartsWith("TITLE"))
                {
                    project.Name = line.Substring(5).Trim(' ', '"');
                }
                else if (line.StartsWith("AUTHOR"))
                {
                    project.Author = line.Substring(6).Trim(' ', '"');
                }
                else if (line.StartsWith("COPYRIGHT"))
                {
                    project.Copyright = line.Substring(9).Trim(' ', '"');
                }
                else if (line.StartsWith("EXPANSION"))
                {
                    var exp          = int.Parse(line.Substring(9));
                    var convertedExp = ConvertExpansionAudio(exp);

                    if (convertedExp < 0)
                    {
                        return(null);
                    }

                    project.SetExpansionAudio(convertedExp);
                }
                else if (line.StartsWith("N163CHANNELS"))
                {
                    var numExpChannels = int.Parse(line.Substring(12).Trim(' ', '"'));
                    project.SetExpansionAudio(Project.ExpansionN163, numExpChannels);
                }
                else if (line.StartsWith("MACRO"))
                {
                    var expansion = line.StartsWith("MACROVRC6") ? Project.ExpansionVrc6  :
                                    line.StartsWith("MACRON163") ? Project.ExpansionN163 : Project.ExpansionNone;

                    var halves = line.Substring(line.IndexOf(' ')).Split(':');
                    var param  = halves[0].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var curve  = halves[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    var type = int.Parse(param[0]);
                    var idx  = int.Parse(param[1]);
                    var loop = int.Parse(param[2]);
                    var rel  = int.Parse(param[3]);

                    var famistudioType = EnvelopeTypeLookup[type];

                    if (famistudioType < Envelope.Count)
                    {
                        var env = new Envelope(famistudioType);
                        env.Length = curve.Length;

                        // FamiTracker allows envelope with release with no loop. We dont allow that.
                        if (env.CanRelease && loop == -1 && rel != -1)
                        {
                            loop = rel;
                        }

                        env.Loop     = loop;
                        env.Release  = env.CanRelease && rel != -1 ? rel + 1 : -1;
                        env.Relative = famistudioType == Envelope.Pitch;

                        for (int j = 0; j < curve.Length; j++)
                        {
                            env.Values[j] = sbyte.Parse(curve[j]);
                        }

                        envelopes[expansion, famistudioType][idx] = env;
                    }
                }
                else if (line.StartsWith("DPCMDEF"))
                {
                    var param = SplitStringKeepQuotes(line.Substring(7));
                    currentDpcm = CreateUniquelyNamedSample(param[2], new byte[int.Parse(param[1])]);
                    dpcms[int.Parse(param[0])] = currentDpcm;
                    dpcmWriteIdx = 0;
                }
                else if (line.StartsWith("DPCM"))
                {
                    if (currentDpcm != null) // Can happen if more than 16KB of samples
                    {
                        var param = line.Substring(6).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var s in param)
                        {
                            currentDpcm.Data[dpcmWriteIdx++] = Convert.ToByte(s, 16);
                        }
                    }
                }
                else if (line.StartsWith("KEYDPCM"))
                {
                    var param = line.Substring(7).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (param[0] == "0")
                    {
                        int octave   = int.Parse(param[1]);
                        int semitone = int.Parse(param[2]);
                        int note     = octave * 12 + semitone + 1;

                        if (project.NoteSupportsDPCM(note))
                        {
                            int dpcm  = int.Parse(param[3]);
                            int pitch = int.Parse(param[4]);
                            int loop  = int.Parse(param[5]);

                            project.MapDPCMSample(note, dpcms[dpcm], pitch, loop != 0);
                        }
                    }
                }
                else if (line.StartsWith("INST2A03") || line.StartsWith("INSTVRC6") || line.StartsWith("INSTN163"))
                {
                    var expansion = line.StartsWith("INSTVRC6") ? Project.ExpansionVrc6  :
                                    line.StartsWith("INSTN163") ? Project.ExpansionN163 : Project.ExpansionNone;

                    var param = SplitStringKeepQuotes(line.Substring(line.IndexOf(' ')));

                    int idx = int.Parse(param[0]);
                    int vol = int.Parse(param[1]);
                    int arp = int.Parse(param[2]);
                    int pit = int.Parse(param[3]);
                    int dut = int.Parse(param[5]);

                    var instrument = CreateUniquelyNamedInstrument(expansion, param[param.Length - 1]);

                    if (expansion == Project.ExpansionN163)
                    {
                        instrument.N163WavePreset = Envelope.WavePresetCustom;
                        instrument.N163WaveSize   = byte.Parse(param[6]);
                        instrument.N163WavePos    = byte.Parse(param[7]);
                    }

                    if (vol >= 0 && instrument.IsEnvelopeActive(Envelope.Volume))
                    {
                        instrument.Envelopes[Envelope.Volume] = envelopes[expansion, Envelope.Volume][vol].ShallowClone();
                    }
                    if (arp >= 0 && instrument.IsEnvelopeActive(Envelope.Arpeggio))
                    {
                        instrument.Envelopes[Envelope.Arpeggio] = envelopes[expansion, Envelope.Arpeggio][arp].ShallowClone();
                    }
                    if (pit >= 0 && instrument.IsEnvelopeActive(Envelope.Pitch))
                    {
                        instrument.Envelopes[Envelope.Pitch] = envelopes[expansion, Envelope.Pitch][pit].ShallowClone();
                    }
                    if (dut >= 0 && instrument.IsEnvelopeActive(Envelope.DutyCycle))
                    {
                        instrument.Envelopes[Envelope.DutyCycle] = envelopes[expansion, Envelope.DutyCycle][dut].ShallowClone();
                    }

                    instruments[idx] = instrument;
                }
                else if (line.StartsWith("INSTVRC7"))
                {
                    var param = SplitStringKeepQuotes(line.Substring(line.IndexOf(' ')));

                    int idx        = int.Parse(param[0]);
                    var instrument = CreateUniquelyNamedInstrument(Project.ExpansionVrc7, param[param.Length - 1]);

                    instrument.Vrc7Patch = byte.Parse(param[1]);
                    if (instrument.Vrc7Patch == 0)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            instrument.Vrc7PatchRegs[j] = Convert.ToByte(param[2 + j], 16);
                        }
                    }

                    instruments[idx] = instrument;
                }
                else if (line.StartsWith("INSTFDS"))
                {
                    var param = SplitStringKeepQuotes(line.Substring(line.IndexOf(' ')));

                    int idx       = int.Parse(param[0]);
                    int modEnable = int.Parse(param[1]);
                    int modSpeed  = int.Parse(param[2]);
                    int modDepth  = int.Parse(param[3]);
                    int modDelay  = int.Parse(param[4]);

                    if (modEnable == 0)
                    {
                        modSpeed = 0;
                        modDepth = 0;
                        modDelay = 0;
                    }

                    instruments[idx]               = CreateUniquelyNamedInstrument(Project.ExpansionFds, param[5]);
                    instruments[idx].FdsModSpeed   = (ushort)modSpeed;
                    instruments[idx].FdsModDepth   = (byte)modDepth;
                    instruments[idx].FdsModDelay   = (byte)modDelay;
                    instruments[idx].FdsWavePreset = Envelope.WavePresetCustom;
                    instruments[idx].FdsModPreset  = Envelope.WavePresetCustom;
                }
                else if (line.StartsWith("FDSMACRO"))
                {
                    var halves = line.Substring(line.IndexOf(' ')).Split(':');
                    var param  = halves[0].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var curve  = halves[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    var inst = int.Parse(param[0]);
                    var type = int.Parse(param[1]);
                    var loop = int.Parse(param[2]);
                    var rel  = int.Parse(param[3]);

                    var famistudioType = EnvelopeTypeLookup[type];

                    var env = instruments[inst].Envelopes[famistudioType];

                    env.Length = curve.Length;

                    // FamiTracker allows envelope with release with no loop. We dont allow that.
                    if (env.CanRelease && loop == -1 && rel != -1)
                    {
                        loop = rel;
                    }

                    env.Loop     = loop;
                    env.Release  = env.CanRelease && rel != -1 ? rel + 1 : -1;
                    env.Relative = famistudioType == Envelope.Pitch;

                    for (int j = 0; j < curve.Length; j++)
                    {
                        env.Values[j] = sbyte.Parse(curve[j]);
                    }
                }
                else if (line.StartsWith("FDSMOD") || line.StartsWith("FDSWAVE"))
                {
                    var mod    = line.StartsWith("FDSMOD");
                    var halves = line.Substring(line.IndexOf(' ')).Split(':');
                    var param  = halves[0].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var curve  = halves[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    var inst = int.Parse(param[0]);
                    var env  = instruments[inst].Envelopes[mod ? Envelope.FdsModulation : Envelope.FdsWaveform];
                    for (int j = 0; j < curve.Length; j++)
                    {
                        env.Values[j] = sbyte.Parse(curve[j]);
                    }
                    if (mod)
                    {
                        env.ConvertFdsModulationToAbsolute();
                    }
                }
                else if (line.StartsWith("N163WAVE"))
                {
                    var param = line.Substring(8).Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                    var instIdx = int.Parse(param[0]);
                    var waveIdx = int.Parse(param[1]);

                    // TODO: We could create different instruments for each wave.
                    if (waveIdx == 0)
                    {
                        var env = instruments[instIdx].Envelopes[Envelope.N163Waveform];

                        for (int j = 3; j < param.Length; j++)
                        {
                            env.Values[j - 3] = sbyte.Parse(param[j]);
                        }
                    }
                }
                else if (line.StartsWith("TRACK"))
                {
                    var param = SplitStringKeepQuotes(line.Substring(5));

                    song = project.CreateSong(param[3]);
                    song.SetLength(0);
                    song.SetDefaultPatternLength(int.Parse(param[0]));
                    song.FamitrackerSpeed = int.Parse(param[1]);
                    song.FamitrackerTempo = int.Parse(param[2]);
                    columns = new int[song.Channels.Length];
                }
                else if (line.StartsWith("COLUMNS"))
                {
                    var param = line.Substring(7).Split(new[] { ' ', ':' }, StringSplitOptions.RemoveEmptyEntries);
                    for (int j = 0; j < song.Channels.Length; j++)
                    {
                        columns[j] = int.Parse(param[j]);
                    }
                }
                else if (line.StartsWith("ORDER"))
                {
                    var orderIdx = Convert.ToInt32(line.Substring(6, 2), 16);
                    var values   = line.Substring(5).Split(':')[1].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var order    = new int[song.Channels.Length];
                    for (int j = 0; j < song.Channels.Length; j++)
                    {
                        int patternIdx = Convert.ToInt32(values[j], 16);
                        var name       = values[j];
                        var pattern    = song.Channels[j].GetPattern(name);

                        if (pattern == null)
                        {
                            pattern = song.Channels[j].CreatePattern(name);
                        }

                        song.Channels[j].PatternInstances[orderIdx] = pattern;
                    }

                    song.SetLength(song.Length + 1);
                }
                else if (line.StartsWith("PATTERN"))
                {
                    patternName = line.Substring(8);
                }
                else if (line.StartsWith("ROW"))
                {
                    var channels = line.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    var n        = Convert.ToInt32(channels[0].Substring(4, 2), 16);

                    for (int j = 1; j <= song.Channels.Length; j++)
                    {
                        var noteData = channels[j].Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        var pattern  = song.Channels[j - 1].GetPattern(patternName);

                        if (pattern == null)
                        {
                            continue;
                        }

                        //var fxData = new RowFxData[song.PatternLength];
                        if (!patternFxData.ContainsKey(pattern))
                        {
                            patternFxData[pattern] = new RowFxData[song.PatternLength, 4];
                        }

                        // Note
                        if (noteData[0] == "---")
                        {
                            pattern.GetOrCreateNoteAt(n).Value = Note.NoteStop;
                        }
                        else if (noteData[0] == "===")
                        {
                            pattern.GetOrCreateNoteAt(n).Value = Note.NoteRelease;
                        }
                        else if (noteData[0] != "...")
                        {
                            int famitoneNote;

                            if (j == 4)
                            {
                                famitoneNote = (Convert.ToInt32(noteData[0].Substring(0, 1), 16) + 31) + 1;
                            }
                            else
                            {
                                int semitone = TextToNoteLookup[noteData[0].Substring(0, 2)];
                                int octave   = noteData[0][2] - '0';
                                famitoneNote = octave * 12 + semitone + 1;
                            }

                            if (famitoneNote >= Note.MusicalNoteMin && famitoneNote <= Note.MusicalNoteMax)
                            {
                                var note = pattern.GetOrCreateNoteAt(n);
                                note.Value      = (byte)famitoneNote;
                                note.Instrument = j == 5 ? null : instruments[Convert.ToInt32(noteData[1], 16)];
                            }
                            else
                            {
                                // Note outside of range.
                            }
                        }

                        // Volume
                        if (noteData[2] != ".")
                        {
                            pattern.GetOrCreateNoteAt(n).Volume = Convert.ToByte(noteData[2], 16);
                        }

                        // Read FX.
                        for (int k = 0; k < columns[j - 1]; k++)
                        {
                            var fxStr = noteData[3 + k];

                            if (fxStr == "...")
                            {
                                continue;
                            }

                            var fx = new RowFxData();

                            if (project.ExpansionAudio == Project.ExpansionFds && FdsTextToEffectLookup.TryGetValue(fxStr[0], out var fdsFx))
                            {
                                fx.fx = (byte)fdsFx;
                            }
                            else
                            {
                                fx.fx = TextToEffectLookup[fxStr[0]];
                            }

                            fx.param = Convert.ToByte(fxStr.Substring(1), 16);
                            patternFxData[pattern][n, k] = fx;

                            ApplySimpleEffects(fx, pattern, n, patternLengths);
                        }
                    }
                }
            }

            FinishImport();

            return(project);
        }
Exemple #8
0
        public Project Load(string filename)
        {
#if !DEBUG
            try
#endif
            {
                var lines                   = File.ReadAllLines(filename);
                var parameters              = new Dictionary <string, string>();
                var project                 = (Project)null;
                var instrument              = (Instrument)null;
                var arpeggio                = (Arpeggio)null;
                var song                    = (Song)null;
                var channel                 = (Channel)null;
                var pattern                 = (Pattern)null;
                var version                 = "0.0.0";
                var isVersion230OrNewer     = false;
                var isVersion240OrNewer     = false;
                var beatLengthAttributeName = "BeatLength";

                foreach (var line in lines)
                {
                    var cmd = SplitLine(line.Trim(), ref parameters);

                    switch (cmd)
                    {
                    case "Project":
                    {
                        project = new Project();
                        parameters.TryGetValue("Version", out version);
                        if (parameters.TryGetValue("Name", out var name))
                        {
                            project.Name = name;
                        }
                        if (parameters.TryGetValue("Author", out var author))
                        {
                            project.Author = author;
                        }
                        if (parameters.TryGetValue("Copyright", out var copyright))
                        {
                            project.Copyright = copyright;
                        }
                        if (parameters.TryGetValue("Expansion", out var expansion))
                        {
                            project.SetExpansionAudio(ExpansionType.GetValueForShortName(expansion));
                        }
                        if (parameters.TryGetValue("TempoMode", out var tempoMode))
                        {
                            project.TempoMode = TempoType.GetValueForName(tempoMode);
                        }
                        if (parameters.TryGetValue("PAL", out var pal))
                        {
                            project.PalMode = bool.Parse(pal);
                        }
                        isVersion230OrNewer = string.CompareOrdinal(version, "2.3.0") >= 0;
                        isVersion240OrNewer = string.CompareOrdinal(version, "2.4.0") >= 0;
                        if (!isVersion230OrNewer)
                        {
                            beatLengthAttributeName = "BarLength";
                        }
                        break;
                    }

                    case "DPCMSample":
                    {
                        var str  = parameters["Data"];
                        var data = new byte[str.Length / 2];
                        for (int i = 0; i < data.Length; i++)
                        {
                            data[i] = Convert.ToByte(str.Substring(i * 2, 2), 16);
                        }
                        var sample = project.CreateDPCMSampleFromDmcData(parameters["Name"], data);
                        // Any DPCM processing options are no longer supported in 2.4.0 and newer. We just import/export the processed DMC data.
                        if (!isVersion240OrNewer && parameters.TryGetValue("ReverseBits", out var reverseBitStr))
                        {
                            sample.ReverseBits = bool.Parse(reverseBitStr);
                            sample.Process();
                        }
                        break;
                    }

                    case "DPCMMapping":
                    {
                        var pitch = 15;
                        var loop  = false;
                        if (parameters.TryGetValue("Pitch", out var pitchStr))
                        {
                            pitch = int.Parse(pitchStr);
                        }
                        if (parameters.TryGetValue("Loop", out var loopStr))
                        {
                            loop = bool.Parse(loopStr);
                        }
                        project.MapDPCMSample(Note.FromFriendlyName(parameters["Note"]), project.GetSample(parameters["Sample"]), pitch, loop);
                        break;
                    }

                    case "Instrument":
                    {
                        instrument = project.CreateInstrument(parameters.TryGetValue("Expansion", out _) ? project.ExpansionAudio : ExpansionType.None, parameters["Name"]);

                        if (instrument.ExpansionType == ExpansionType.Fds)
                        {
                            if (parameters.TryGetValue("FdsWavePreset", out var wavPresetStr))
                            {
                                instrument.FdsWavePreset = (byte)WavePresetType.GetValueForName(wavPresetStr);
                            }
                            if (parameters.TryGetValue("FdsModPreset", out var modPresetStr))
                            {
                                instrument.FdsWavePreset = (byte)WavePresetType.GetValueForName(modPresetStr);
                            }
                            if (parameters.TryGetValue("FdsMasterVolume", out var masterVolumeStr))
                            {
                                instrument.FdsMasterVolume = byte.Parse(masterVolumeStr);
                            }
                            if (parameters.TryGetValue("FdsModSpeed", out var fdsModSpeedStr))
                            {
                                instrument.FdsModSpeed = ushort.Parse(fdsModSpeedStr);
                            }
                            if (parameters.TryGetValue("FdsModDepth", out var fdsModDepthStr))
                            {
                                instrument.FdsModDepth = byte.Parse(fdsModDepthStr);
                            }
                            if (parameters.TryGetValue("FdsModDelay", out var fdsModDelayStr))
                            {
                                instrument.FdsModDelay = byte.Parse(fdsModDelayStr);
                            }
                        }
                        else if (instrument.ExpansionType == ExpansionType.N163)
                        {
                            if (parameters.TryGetValue("N163WavePreset", out var wavPresetStr))
                            {
                                instrument.N163WavePreset = (byte)WavePresetType.GetValueForName(wavPresetStr);
                            }
                            if (parameters.TryGetValue("N163WaveSize", out var n163WavSizeStr))
                            {
                                instrument.N163WaveSize = byte.Parse(n163WavSizeStr);
                            }
                            if (parameters.TryGetValue("N163WavePos", out var n163WavPosStr))
                            {
                                instrument.N163WavePos = byte.Parse(n163WavPosStr);
                            }
                        }
                        else if (instrument.ExpansionType == ExpansionType.Vrc7)
                        {
                            if (parameters.TryGetValue("Vrc7Patch", out var vrc7PatchStr))
                            {
                                instrument.Vrc7Patch = byte.Parse(vrc7PatchStr);
                            }

                            if (instrument.Vrc7Patch == Vrc7InstrumentPatch.Custom)
                            {
                                for (int i = 0; i < 8; i++)
                                {
                                    if (parameters.TryGetValue($"Vrc7Reg{i}", out var regStr))
                                    {
                                        instrument.Vrc7PatchRegs[i] = byte.Parse(regStr);
                                    }
                                }
                            }
                        }

                        break;
                    }

                    case "Arpeggio":
                    {
                        arpeggio = project.CreateArpeggio(parameters["Name"]);
                        arpeggio.Envelope.Length = int.Parse(parameters["Length"]);

                        if (parameters.TryGetValue("Loop", out var loopStr))
                        {
                            arpeggio.Envelope.Loop = int.Parse(loopStr);
                        }

                        var values = parameters["Values"].Split(',');
                        for (int j = 0; j < values.Length; j++)
                        {
                            arpeggio.Envelope.Values[j] = sbyte.Parse(values[j]);
                        }

                        break;
                    }

                    case "Envelope":
                    {
                        var env = instrument.Envelopes[EnvelopeType.GetValueForShortName(parameters["Type"])];
                        if (env != null)
                        {
                            if (env.CanResize)
                            {
                                env.Length = int.Parse(parameters["Length"]);
                            }

                            if (parameters.TryGetValue("Loop", out var loopStr))
                            {
                                env.Loop = int.Parse(loopStr);
                            }
                            if (parameters.TryGetValue("Release", out var releaseStr))
                            {
                                env.Release = int.Parse(releaseStr);
                            }
                            if (parameters.TryGetValue("Relative", out var relativeStr))
                            {
                                env.Relative = bool.Parse(relativeStr);
                            }

                            var values = parameters["Values"].Split(',');
                            for (int j = 0; j < values.Length; j++)
                            {
                                env.Values[j] = sbyte.Parse(values[j]);
                            }
                        }
                        break;
                    }

                    case "Song":
                    {
                        song = project.CreateSong(parameters["Name"]);
                        song.SetLength(int.Parse(parameters["Length"]));
                        song.SetBeatLength(int.Parse(parameters[beatLengthAttributeName]));
                        song.SetLoopPoint(int.Parse(parameters["LoopPoint"]));

                        if (song.UsesFamiTrackerTempo)
                        {
                            song.SetDefaultPatternLength(int.Parse(parameters["PatternLength"]));
                            song.FamitrackerTempo = int.Parse(parameters["FamiTrackerTempo"]);
                            song.FamitrackerSpeed = int.Parse(parameters["FamiTrackerSpeed"]);
                        }
                        else
                        {
                            var noteLength = int.Parse(parameters["NoteLength"]);
                            song.ResizeNotes(noteLength, false);
                            song.SetBeatLength(song.BeatLength * noteLength);
                            song.SetDefaultPatternLength(int.Parse(parameters["PatternLength"]) * noteLength);
                        }
                        break;
                    }

                    case "PatternCustomSettings":
                    {
                        if (project.UsesFamiTrackerTempo)
                        {
                            var beatLength = song.BeatLength;
                            if (parameters.TryGetValue(beatLengthAttributeName, out var beatLengthStr))
                            {
                                beatLength = int.Parse(beatLengthStr);
                            }

                            song.SetPatternCustomSettings(int.Parse(parameters["Time"]), int.Parse(parameters["Length"]), beatLength);
                        }
                        else
                        {
                            var patternLength = int.Parse(parameters["Length"]);
                            var noteLength    = int.Parse(parameters["NoteLength"]);
                            var beatLength    = int.Parse(parameters[beatLengthAttributeName]);

                            song.SetPatternCustomSettings(int.Parse(parameters["Time"]), patternLength * noteLength, beatLength * noteLength, noteLength);
                        }
                        break;
                    }

                    case "Channel":
                    {
                        var channelType = ChannelType.GetValueForShortName(parameters["Type"]);
                        channel = song.GetChannelByType(channelType);
                        break;
                    }

                    case "Pattern":
                    {
                        pattern = channel.CreatePattern(parameters["Name"]);
                        break;
                    }

                    case "Note":
                    {
                        var time = int.Parse(parameters["Time"]);
                        var note = pattern.GetOrCreateNoteAt(time);

                        if (parameters.TryGetValue("Value", out var valueStr))
                        {
                            note.Value = (byte)Note.FromFriendlyName(valueStr);
                        }
                        if (parameters.TryGetValue("Instrument", out var instStr) && channel.SupportsInstrument(project.GetInstrument(instStr)))
                        {
                            note.Instrument = project.GetInstrument(instStr);
                        }
                        if (parameters.TryGetValue("Arpeggio", out var arpStr) && channel.SupportsArpeggios)
                        {
                            note.Arpeggio = project.GetArpeggio(arpStr);
                        }
                        if (parameters.TryGetValue("SlideTarget", out var slideStr) && channel.SupportsSlideNotes)
                        {
                            note.SlideNoteTarget = (byte)Note.FromFriendlyName(slideStr);
                        }
                        if (parameters.TryGetValue("Attack", out var attackStr))
                        {
                            note.HasAttack = bool.Parse(attackStr);
                        }
                        if (parameters.TryGetValue("Volume", out var volumeStr) && channel.SupportsEffect(Note.EffectVolume))
                        {
                            note.Volume = byte.Parse(volumeStr);
                        }
                        if (parameters.TryGetValue("VibratoSpeed", out var vibSpeedStr) && channel.SupportsEffect(Note.EffectVibratoSpeed))
                        {
                            note.VibratoSpeed = byte.Parse(vibSpeedStr);
                        }
                        if (parameters.TryGetValue("VibratoDepth", out var vibDepthStr) && channel.SupportsEffect(Note.EffectVibratoDepth))
                        {
                            note.VibratoDepth = byte.Parse(vibDepthStr);
                        }
                        if (parameters.TryGetValue("Speed", out var speedStr) && channel.SupportsEffect(Note.EffectSpeed))
                        {
                            note.Speed = byte.Parse(speedStr);
                        }
                        if (parameters.TryGetValue("FinePitch", out var finePitchStr) && channel.SupportsEffect(Note.EffectFinePitch))
                        {
                            note.FinePitch = sbyte.Parse(finePitchStr);
                        }
                        if (parameters.TryGetValue("FdsModSpeed", out var modSpeedStr) && channel.SupportsEffect(Note.EffectFdsModSpeed))
                        {
                            note.FdsModSpeed = ushort.Parse(modSpeedStr);
                        }
                        if (parameters.TryGetValue("FdsModDepth", out var modDepthStr) && channel.SupportsEffect(Note.EffectFdsModDepth))
                        {
                            note.FdsModDepth = byte.Parse(modDepthStr);
                        }
                        if (parameters.TryGetValue("DutyCycle", out var dutyCycleStr) && channel.SupportsEffect(Note.EffectDutyCycle))
                        {
                            note.DutyCycle = byte.Parse(dutyCycleStr);
                        }
                        if (parameters.TryGetValue("NoteDelay", out var noteDelayStr) && channel.SupportsEffect(Note.EffectNoteDelay))
                        {
                            note.NoteDelay = byte.Parse(noteDelayStr);
                        }
                        if (parameters.TryGetValue("CutDelay", out var cutDelayStr) && channel.SupportsEffect(Note.EffectCutDelay))
                        {
                            note.CutDelay = byte.Parse(cutDelayStr);
                        }

                        break;
                    }

                    case "PatternInstance":
                    {
                        var time = int.Parse(parameters["Time"]);
                        channel.PatternInstances[time] = channel.GetPattern(parameters["Pattern"]);
                        break;
                    }
                    }
                }

                project.SortEverything();

                return(project);
            }
#if !DEBUG
            catch (Exception e)
            {
                Log.LogMessage(LogSeverity.Error, "Please contact the developer on GitHub!");
                Log.LogMessage(LogSeverity.Error, e.Message);
                Log.LogMessage(LogSeverity.Error, e.StackTrace);
                return(null);
            }
#endif
        }