protected bool FinishImport()
        {
            foreach (var s in project.Samples)
            {
                s.Process();
            }

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

                    for (int p = 0; p < s.Length; p++)
                    {
                        var pattern = c.PatternInstances[p];
                        if (pattern != null && patternLengths.TryGetValue(pattern, out var instLength))
                        {
                            if (instLength < s.GetPatternLength(p))
                            {
                                s.SetPatternCustomSettings(p, instLength, s.BeatLength);
                            }
                        }
                    }
                }

                s.DeleteNotesPastMaxInstanceLength();
                s.UpdatePatternStartNotes();

                // FamiTracker always assumes 4 rows per beat for BPM calculation, but let's assume
                // the artists properly set first row highlight to that.
                if (barLength == -1)
                {
                    s.SetSensibleBeatLength();
                }
                else
                {
                    s.SetBeatLength(barLength);
                }

                ApplyHaltEffect(s, patternFxData);
                CreateArpeggios(s, patternFxData);
                CreateSlideNotes(s, patternFxData);

                s.DeleteEmptyPatterns();
            }

            project.UpdateAllLastValidNotesAndVolume();
            project.SortEverything();
            project.Validate();

            PrintAdditionalWarnings();

            return(true);
        }
        protected bool FinishImport()
        {
            foreach (var s in project.Songs)
            {
                foreach (var c in s.Channels)
                {
                    c.ColorizePatterns();

                    for (int p = 0; p < s.Length; p++)
                    {
                        var pattern = c.PatternInstances[p];
                        if (pattern != null && patternLengths.TryGetValue(pattern, out var instLength))
                        {
                            if (instLength < s.GetPatternLength(p))
                            {
                                s.SetPatternCustomSettings(p, instLength);
                            }
                        }
                    }
                }

                s.DeleteNotesPastMaxInstanceLength();
                s.UpdatePatternStartNotes();

                if (barLength == -1)
                {
                    s.SetSensibleBarLength();
                }
                else
                {
                    s.SetBarLength(barLength);
                }

                ApplyHaltEffect(s, patternFxData);
                CreateArpeggios(s, patternFxData);
                CreateSlideNotes(s, patternFxData);

                s.DeleteEmptyPatterns();
            }

            project.UpdateAllLastValidNotesAndVolume();
            project.Validate();

            return(true);
        }
Example #3
0
        public Project Load(string filename, int songIndex, int duration, int patternLength, int startFrame, bool removeIntroSilence, bool reverseDpcm, bool preserveDpcmPad)
        {
            nsf = NsfOpen(filename);

            if (nsf == null)
            {
                return(null);
            }

            var trackCount = NsfGetTrackCount(nsf);

            if (songIndex < 0 || songIndex > trackCount)
            {
                return(null);
            }

            preserveDpcmPadding = preserveDpcmPad;

            var palSource = (NsfIsPal(nsf) & 1) == 1;
            var numFrames = duration * (palSource ? 50 : 60);

            project = new Project();

            project.Name      = Marshal.PtrToStringAnsi(NsfGetTitle(nsf));
            project.Author    = Marshal.PtrToStringAnsi(NsfGetArtist(nsf));
            project.Copyright = Marshal.PtrToStringAnsi(NsfGetCopyright(nsf));
            project.PalMode   = palSource;

            switch (NsfGetExpansion(nsf))
            {
            case EXTSOUND_VRC6: project.SetExpansionAudio(ExpansionType.Vrc6); break;

            case EXTSOUND_VRC7: project.SetExpansionAudio(ExpansionType.Vrc7); break;

            case EXTSOUND_FDS:  project.SetExpansionAudio(ExpansionType.Fds);  break;

            case EXTSOUND_MMC5: project.SetExpansionAudio(ExpansionType.Mmc5); break;

            case EXTSOUND_N163: project.SetExpansionAudio(ExpansionType.N163, GetNumNamcoChannels(filename, songIndex, numFrames)); break;

            case EXTSOUND_S5B:  project.SetExpansionAudio(ExpansionType.S5B);  break;

            case 0: break;

            default:
                NsfClose(nsf);     // Unsupported expansion combination.
                return(null);
            }

            var songName = Marshal.PtrToStringAnsi(NsfGetTrackName(nsf, songIndex));

            song          = project.CreateSong(string.IsNullOrEmpty(songName) ? $"Song {songIndex + 1}" : songName);
            channelStates = new ChannelState[song.Channels.Length];

            NsfSetTrack(nsf, songIndex);

            song.ResizeNotes(1, false);
            song.SetDefaultPatternLength(patternLength);

            for (int i = 0; i < song.Channels.Length; i++)
            {
                channelStates[i] = new ChannelState();
            }

            var foundFirstNote = !removeIntroSilence;

            var p = 0;
            var n = 0;
            var f = startFrame;

            for (int i = 0; i < numFrames; i++)
            {
                p = f / song.PatternLength;
                n = f % song.PatternLength;

                if (p >= Song.MaxLength - 1)
                {
                    break;
                }

                var playCalled = 0;
                do
                {
                    playCalled = NsfRunFrame(nsf);
                }while (playCalled == 0);

                for (int c = 0; c < song.Channels.Length; c++)
                {
                    foundFirstNote |= UpdateChannel(p, n, song.Channels[c], channelStates[c]);
                }

                if (foundFirstNote)
                {
                    f++;
                }
                else
                {
                    // Reset everything until we find our first note.
                    project.DeleteAllInstrument();
                    project.DeleteAllSamples();
                    for (int c = 0; c < song.Channels.Length; c++)
                    {
                        channelStates[c] = new ChannelState();
                    }
                }
            }

            song.SetLength(p + 1);

            NsfClose(nsf);

            var factors = Utils.GetFactors(song.PatternLength, Song.MaxNoteLength);

            if (factors.Length > 0)
            {
                var noteLen = factors[0];

                // Look for a factor that generates a note length < 10 and gives a pattern length that is a multiple of 16.
                foreach (var factor in factors)
                {
                    if (factor <= 10)
                    {
                        noteLen = factor;
                        if (((song.PatternLength / noteLen) % 16) == 0)
                        {
                            break;
                        }
                    }
                }

                song.ResizeNotes(noteLen, false);
            }
            else
            {
                song.ResizeNotes(1, false);
            }

            song.SetSensibleBeatLength();
            song.DeleteEmptyPatterns();
            song.UpdatePatternStartNotes();
            project.DeleteUnusedInstruments();
            project.UpdateAllLastValidNotesAndVolume();

            foreach (var sample in project.Samples)
            {
                sample.ReverseBits = reverseDpcm;
            }

            return(project);
        }
Example #4
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);
        }
Example #5
0
        public Project Load(string filename, int songIndex, int duration, int patternLength, int startFrame, bool removeIntroSilence)
        {
            nsf = NsfOpen(filename);

            if (nsf == null)
            {
                return(null);
            }

            var numFrames = duration * (NsfIsPal(nsf) != 0 ? 50 : 60);

            project = new Project();

            project.Name      = Marshal.PtrToStringAnsi(NsfGetTitle(nsf));
            project.Author    = Marshal.PtrToStringAnsi(NsfGetArtist(nsf));
            project.Copyright = Marshal.PtrToStringAnsi(NsfGetCopyright(nsf));

            switch (NsfGetExpansion(nsf))
            {
            case EXTSOUND_VRC6: project.SetExpansionAudio(Project.ExpansionVrc6); break;

            case EXTSOUND_VRC7: project.SetExpansionAudio(Project.ExpansionVrc7); break;

            case EXTSOUND_FDS:  project.SetExpansionAudio(Project.ExpansionFds);  break;

            case EXTSOUND_MMC5: project.SetExpansionAudio(Project.ExpansionMmc5); break;

            case EXTSOUND_N163: project.SetExpansionAudio(Project.ExpansionN163, GetNumNamcoChannels(filename, songIndex, numFrames)); break;

            case EXTSOUND_S5B:  project.SetExpansionAudio(Project.ExpansionS5B);  break;

            case 0: break;

            default:
                NsfClose(nsf);     // Unsupported expansion combination.
                return(null);
            }

            var songName = Marshal.PtrToStringAnsi(NsfGetTrackName(nsf, songIndex));

            song          = project.CreateSong(string.IsNullOrEmpty(songName) ? $"Song {songIndex + 1}" : songName);
            channelStates = new ChannelState[song.Channels.Length];

            NsfSetTrack(nsf, songIndex);

            song.ResizeNotes(1, false);
            song.SetDefaultPatternLength(patternLength);

            for (int i = 0; i < song.Channels.Length; i++)
            {
                channelStates[i] = new ChannelState();
            }

            var foundFirstNote = !removeIntroSilence;

            var p = 0;
            var n = 0;
            var f = startFrame;

            for (int i = 0; i < numFrames; i++)
            {
                p = f / song.PatternLength;
                n = f % song.PatternLength;

                if (p >= Song.MaxLength - 1)
                {
                    break;
                }

                var playCalled = 0;
                do
                {
                    playCalled = NsfRunFrame(nsf);
                }while (playCalled == 0);

                for (int c = 0; c < song.Channels.Length; c++)
                {
                    foundFirstNote |= UpdateChannel(p, n, song.Channels[c], channelStates[c]);
                }

                if (foundFirstNote)
                {
                    f++;
                }
                else
                {
                    // Reset everything until we find our first note.
                    project.DeleteAllInstrument();
                    project.DeleteAllSamples();
                    for (int c = 0; c < song.Channels.Length; c++)
                    {
                        channelStates[c] = new ChannelState();
                    }
                }
            }

            song.SetLength(p + 1);

            NsfClose(nsf);

            var factors = Utils.GetFactors(song.PatternLength, Song.MaxNoteLength);

            if (factors.Length > 0 && factors[0] <= Song.MaxNoteLength)
            {
                song.ResizeNotes(factors[0], false);
            }
            else
            {
                song.ResizeNotes(1, false);
            }

            song.SetSensibleBarLength();
            song.DeleteEmptyPatterns();
            song.UpdatePatternStartNotes();
            project.DeleteUnusedInstruments();
            project.UpdateAllLastValidNotesAndVolume();

            return(project);
        }