/// <summary>
        /// Creates a track chunk with the specified timed events.
        /// </summary>
        /// <param name="events">Collection of timed events to create a track chunk.</param>
        /// <returns><see cref="TrackChunk"/> containing the specified timed events.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="events"/> is null.</exception>
        public static TrackChunk ToTrackChunk(this IEnumerable <TimedEvent> events)
        {
            ThrowIfArgument.IsNull(nameof(events), events);

            var trackChunk = new TrackChunk();

            trackChunk.AddTimedEvents(events);

            return(trackChunk);
        }
예제 #2
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Path.GetExtension(filePath) == ".mid")
            {
                var chunks      = song.GetTrackChunks().ToArray();
                var tempoMap    = song.GetTempoMap();
                var fixedMidi   = new MidiFile();
                var headerChunk = new TrackChunk();

                int i       = 0;
                var t0Notes = chunks[0].GetNotes().ToArray();
                if (t0Notes.Length == 0)    // check if there are notes in Track0
                {
                    i = 1;
                    var timedEvents = chunks[0].GetTimedEvents().ToArray();
                    var title       = timedEvents.Where(t => t.Event.EventType == MidiEventType.SequenceTrackName);
                    if (title.Count() == 1)
                    {
                        headerChunk.AddTimedEvents(title);
                    }
                }

                fixedMidi.Chunks.Add(headerChunk);
                fixedMidi.ReplaceTempoMap(tempoMap);

                for (int ch = 0; i < chunks.Count(); i++, ch++)
                {
                    var newChunk    = new TrackChunk();
                    var timedEvents = chunks[i].GetTimedEvents();

                    using (var timedEventsManager = newChunk.ManageTimedEvents())
                    {
                        foreach (var timedEvent in timedEvents)
                        {
                            var eventType = timedEvent.Event.EventType;
                            if (eventType == MidiEventType.SequenceTrackName)
                            {
                                timedEventsManager.Events.Add(timedEvent);
                            }
                            else if (eventType == MidiEventType.ProgramChange)
                            {
                                var pcEvent = timedEvent.Event as ProgramChangeEvent;
                                pcEvent.Channel = (FourBitNumber)ch;
                                timedEventsManager.Events.AddEvent(pcEvent, timedEvent.Time);
                            }
                        }
                    }

                    using (var notesManager = newChunk.ManageNotes())
                    {
                        var notes = chunks[i].GetNotes().ToArray();
                        foreach (var n in notes)
                        {
                            var newNote = n;
                            newNote.Channel = (FourBitNumber)ch;   // change channel of the notes in track
                            notesManager.Notes.Add(newNote);
                        }
                    }

                    fixedMidi.Chunks.Add(newChunk);
                }

                string outputFilename = Path.GetFileNameWithoutExtension(filePath) + "_fix.mid";
                WriteMidiToFile(fixedMidi, outputFilename);
                textBox1.AppendText("Wrote to file: " + outputFilename + Environment.NewLine);
            }
            else if (Path.GetExtension(filePath) == ".mml")
            {
                string outputFilename = Path.GetFileNameWithoutExtension(filePath) + "_fix.mml";
                File.WriteAllText(outputFilename, sb.ToString());
                textBox1.AppendText("Wrote to file: " + outputFilename + Environment.NewLine);
            }
        }