Esempio n. 1
0
        public bool Save(Project project, string filename)
        {
            try
            {
                var serializer = new ProjectSaveBuffer(project);
                project.SerializeState(serializer);
                var buffer = serializer.GetBuffer();

                using (var stream = File.Create(filename))
                {
                    stream.Write(BitConverter.GetBytes(MagicNumber), 0, 4);
                    stream.Write(BitConverter.GetBytes(Project.Version), 0, 4);

                    buffer = Compression.CompressBytes(buffer, CompressionLevel.Optimal);
                    stream.Write(buffer, 0, buffer.Length);
                    stream.Flush();
                    stream.Close();

                    project.Filename = filename;

                    return(true);
                }
            }
            catch
            {
                return(false);
            }
        }
Esempio n. 2
0
        public static void SaveNotes(Project project, Note[] notes)
        {
            if (notes == null)
            {
                SetClipboardData(null);
                return;
            }

            var instruments = new HashSet <Instrument>();

            foreach (var note in notes)
            {
                if (note.Instrument != null)
                {
                    instruments.Add(note.Instrument);
                }
            }

            var serializer = new ProjectSaveBuffer(null);

            SaveInstrumentList(serializer, instruments);

            int numNotes = notes.Length;

            serializer.Serialize(ref numNotes);
            foreach (var note in notes)
            {
                note.SerializeState(serializer);
            }

            var buffer        = Compression.CompressBytes(serializer.GetBuffer(), CompressionLevel.Fastest);
            var clipboardData = new List <byte>();

            clipboardData.AddRange(BitConverter.GetBytes(MagicNumberClipboardNotes));
            clipboardData.AddRange(buffer);

            SetClipboardData(clipboardData.ToArray());
        }
Esempio n. 3
0
        public static void SavePatterns(Project project, Pattern[,] patterns, Song.PatternCustomSetting[] customSettings = null)
        {
            if (patterns == null)
            {
                SetClipboardDataInternal(null);
                return;
            }

            var uniqueInstruments  = new HashSet <Instrument>();
            var uniqueArpeggios    = new HashSet <Arpeggio>();
            var uniquePatterns     = new HashSet <Pattern>();
            var uniqueDPCMMappings = new Dictionary <int, DPCMSampleMapping>();
            var numPatterns        = patterns.GetLength(0);
            var numChannels        = patterns.GetLength(1);

            Debug.Assert(customSettings == null || customSettings.Length == numPatterns);

            for (int i = 0; i < numPatterns; i++)
            {
                for (int j = 0; j < numChannels; j++)
                {
                    var pattern = patterns[i, j];
                    if (pattern != null)
                    {
                        uniquePatterns.Add(pattern);
                        if (pattern.ChannelType == Channel.Dpcm)
                        {
                            foreach (var n in pattern.Notes.Values)
                            {
                                var mapping = project.GetDPCMMapping(n.Value);
                                if (mapping != null && mapping.Sample != null)
                                {
                                    uniqueDPCMMappings[n.Value] = mapping;
                                }
                            }
                        }
                        else
                        {
                            foreach (var n in pattern.Notes.Values)
                            {
                                if (n.Instrument != null)
                                {
                                    uniqueInstruments.Add(n.Instrument);
                                }
                                if (n.Arpeggio != null)
                                {
                                    uniqueArpeggios.Add(n.Arpeggio);
                                }
                            }
                        }
                    }
                }
            }

            if (uniquePatterns.Count == 0)
            {
                SetClipboardDataInternal(null);
                return;
            }

            var serializer = new ProjectSaveBuffer(null);

            SaveSampleList(serializer, uniqueDPCMMappings);
            SaveArpeggioList(serializer, uniqueArpeggios);
            SaveInstrumentList(serializer, uniqueInstruments);
            SavePatternList(serializer, uniquePatterns);

            serializer.Serialize(ref numPatterns);
            serializer.Serialize(ref numChannels);

            for (int i = 0; i < numPatterns; i++)
            {
                for (int j = 0; j < numChannels; j++)
                {
                    var pattern = patterns[i, j];
                    var patId   = pattern == null ? -1 : pattern.Id;
                    serializer.Serialize(ref patId);
                }
            }

            var hasCustomSettings = customSettings != null;
            var tempoMode         = project.TempoMode;

            serializer.Serialize(ref hasCustomSettings);
            serializer.Serialize(ref tempoMode);

            if (hasCustomSettings)
            {
                for (int i = 0; i < numPatterns; i++)
                {
                    serializer.Serialize(ref customSettings[i].useCustomSettings);
                    serializer.Serialize(ref customSettings[i].patternLength);
                    serializer.Serialize(ref customSettings[i].noteLength);
                    serializer.Serialize(ref customSettings[i].beatLength);
                }
            }

            var buffer        = Compression.CompressBytes(serializer.GetBuffer(), CompressionLevel.Fastest);
            var clipboardData = new List <byte>();

            clipboardData.AddRange(BitConverter.GetBytes(MagicNumberClipboardPatterns));
            clipboardData.AddRange(buffer);

            SetClipboardDataInternal(clipboardData.ToArray());
        }
Esempio n. 4
0
        public static void SaveNotes(Project project, Note[] notes, bool dpcm)
        {
            if (notes == null)
            {
                SetClipboardDataInternal(null);
                return;
            }

            var serializer  = new ProjectSaveBuffer(null);
            var instruments = new HashSet <Instrument>();
            var arpeggios   = new HashSet <Arpeggio>();
            var samples     = new Dictionary <int, DPCMSampleMapping>();

            if (dpcm)
            {
                foreach (var note in notes)
                {
                    if (note != null)
                    {
                        var mapping = project.GetDPCMMapping(note.Value);
                        if (mapping != null && mapping.Sample != null)
                        {
                            samples[note.Value] = mapping;
                        }
                    }
                }
            }
            else
            {
                foreach (var note in notes)
                {
                    if (note != null)
                    {
                        if (note.Instrument != null)
                        {
                            instruments.Add(note.Instrument);
                        }
                        if (note.Arpeggio != null)
                        {
                            arpeggios.Add(note.Arpeggio);
                        }
                    }
                }
            }

            SaveSampleList(serializer, samples);
            SaveArpeggioList(serializer, arpeggios);
            SaveInstrumentList(serializer, instruments);

            var numNotes = notes.Length;

            serializer.Serialize(ref numNotes);
            foreach (var note in notes)
            {
                if (note == null)
                {
                    Note.EmptyNote.SerializeState(serializer);
                }
                else
                {
                    note.SerializeState(serializer);
                }
            }

            var buffer        = Compression.CompressBytes(serializer.GetBuffer(), CompressionLevel.Fastest);
            var clipboardData = new List <byte>();

            clipboardData.AddRange(BitConverter.GetBytes(MagicNumberClipboardNotes));
            clipboardData.AddRange(buffer);

            SetClipboardDataInternal(clipboardData.ToArray());
        }
Esempio n. 5
0
        public static void SavePatterns(Project project, Pattern[,] patterns)
        {
            if (patterns == null)
            {
                SetClipboardData(null);
                return;
            }

            var uniqueInstruments = new HashSet <Instrument>();
            var uniquePatterns    = new HashSet <Pattern>();
            var numPatterns       = patterns.GetLength(0);
            var numChannels       = patterns.GetLength(1);

            for (int i = 0; i < numPatterns; i++)
            {
                for (int j = 0; j < numChannels; j++)
                {
                    var pattern = patterns[i, j];
                    if (pattern != null)
                    {
                        uniquePatterns.Add(pattern);
                        for (int k = 0; k < pattern.Song.PatternLength; k++)
                        {
                            var inst = pattern.Notes[k].Instrument;
                            if (inst != null)
                            {
                                uniqueInstruments.Add(inst);
                            }
                        }
                    }
                }
            }

            if (uniquePatterns.Count == 0)
            {
                SetClipboardData(null);
                return;
            }

            var serializer = new ProjectSaveBuffer(null);

            SaveInstrumentList(serializer, uniqueInstruments);
            SavePatternList(serializer, uniquePatterns);

            serializer.Serialize(ref numPatterns);
            serializer.Serialize(ref numChannels);

            for (int i = 0; i < numPatterns; i++)
            {
                for (int j = 0; j < numChannels; j++)
                {
                    var pattern = patterns[i, j];
                    var patId   = pattern == null ? -1 : pattern.Id;
                    serializer.Serialize(ref patId);
                }
            }

            var buffer        = Compression.CompressBytes(serializer.GetBuffer(), CompressionLevel.Fastest);
            var clipboardData = new List <byte>();

            clipboardData.AddRange(BitConverter.GetBytes(MagicNumberClipboardPatterns));
            clipboardData.AddRange(buffer);

            SetClipboardData(clipboardData.ToArray());
        }