private int ProcessAndOutputSong(int songIdx)
        {
            var song = project.Songs[songIdx];

            int minSize     = 65536;
            int bestChannel = 0;
            int bestFactor  = 1;

            // Take the channel with the most speed effect as the speed channel.
            // This was a really dumb optimziation in FT2...
            var speedEffectCount = new int[song.Channels.Length];

            for (int c = 0; c < song.Channels.Length; c++)
            {
                foreach (var pattern in song.Channels[c].Patterns)
                {
                    foreach (var note in pattern.Notes.Values)
                    {
                        if (note.HasSpeed)
                        {
                            speedEffectCount[c]++;
                        }
                    }
                }
            }

            int speedChannel    = 0;
            int maxSpeedEffects = 0;

            for (int c = 0; c < song.Channels.Length; c++)
            {
                if (speedEffectCount[c] > maxSpeedEffects)
                {
                    maxSpeedEffects = speedEffectCount[c];
                    speedChannel    = c;
                }
            }

            for (int factor = 1; factor <= song.PatternLength; factor++)
            {
                if ((song.PatternLength % factor) == 0 &&
                    (song.PatternLength / factor) >= MinPatternLength)
                {
                    var splitSong = project.DuplicateSong(song);
                    if (splitSong.Split(factor))
                    {
                        int size = OutputSong(splitSong, songIdx, speedChannel, factor, true);

                        if (size < minSize)
                        {
                            minSize     = size;
                            bestChannel = speedChannel;
                            bestFactor  = factor;
                        }
                    }
                    project.DeleteSong(splitSong);
                }
            }

            var bestSplitSong = project.DuplicateSong(song);

            bestSplitSong.Split(bestFactor);

            var songSize = OutputSong(bestSplitSong, songIdx, bestChannel, bestFactor, false);

            project.DeleteSong(song);

            return(songSize);
        }