Exemple #1
0
        static void Main(string[] args)
        {
            Console.Write("Enter midi file name: ");
            string file = Console.ReadLine();

            MidiFile midiFile = new MidiFile(file);

            Console.WriteLine("Converting...");
            TimeSpan startTime = TimeSpan.FromTicks(DateTime.Now.Ticks);
            MusicTwo music     = MusicTwo.FromMidiFile(midiFile);

            Console.Write("Saving... ");
            music.SaveAsCFilesBest(file);
            Console.WriteLine("Done in {0}!", TimeSpan.FromTicks(DateTime.Now.Ticks) - startTime);

            ConsoleKeyInfo enteredKey;

            do
            {
                Console.Write("Play track?(Y/N) ");
                enteredKey = Console.ReadKey();
                Console.WriteLine();
            } while (enteredKey.KeyChar != 'Y' & enteredKey.KeyChar != 'N' & enteredKey.KeyChar != 'y' & enteredKey.KeyChar != 'n');
            if (enteredKey.KeyChar == 'Y' || enteredKey.KeyChar == 'y')
            {
                music.Play();
            }
            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }
Exemple #2
0
        /// <summary>
        /// Creates MusicTwo instance using midi file
        /// </summary>
        /// <param name="midiFile">Midi file to use</param>
        /// <param name="firstNoteMode">Use first note mode. If some notes appears at same time, use the first one, else the last</param>
        /// <returns>Created MusicTwo instance</returns>
        public static MusicTwo FromMidiFile(MidiFile midiFile, bool firstNoteMode)
        {
            double       tempo  = 0;
            List <Track> tracks = new List <Track>();

            for (int track_ = 1; track_ <= midiFile.Tracks; track_++)
            {
                Track track__ = new Track();
                foreach (var e in midiFile.Events)
                {
                    foreach (var ee in e)
                    {
                        if (ee is NoteEvent)
                        {
                            NoteEvent eee = (NoteEvent)ee;
                            if (eee is NoteOnEvent)
                            {
                                var  eeee = (NoteOnEvent)eee;
                                Note nt   = new Note();
                                nt.time   = (int)(eeee.AbsoluteTime / (double)midiFile.DeltaTicksPerQuarterNote * tempo);
                                nt.length = 0;
                                if (eeee.OffEvent != null)
                                {
                                    nt.length = (int)(eeee.NoteLength / (double)midiFile.DeltaTicksPerQuarterNote * tempo);
                                }
                                nt.number     = eeee.NoteNumber;
                                nt.instrument = track__.instrument;
                                if (eeee.Channel == track_)
                                {
                                    track__.notes.Add(nt);
                                }
                            }
                        }
                        if (ee is MetaEvent)
                        {
                            MetaEvent eee = (MetaEvent)ee;
                            if (eee.MetaEventType == MetaEventType.SetTempo)
                            {
                                TempoEvent eeee = (TempoEvent)eee;
                                tempo = eeee.MicrosecondsPerQuarterNote / 1000;
                            }
                        }

                        if (ee is PatchChangeEvent)
                        {
                            PatchChangeEvent eee = (PatchChangeEvent)ee;
                            if (eee.Channel == track_)
                            {
                                track__.instrument = eee.Patch;
                            }
                        }
                    }
                }
                tracks.Add(track__);
            }
            for (int i = 0; i < tracks.Count; i++)
            {
                for (int ii = 0; ii < tracks[i].notes.Count; ii++)
                {
                    if (tracks[i].notes[ii].length == 0)
                    {
                        tracks[i].notes.RemoveAt(ii);
                    }
                }
                if (tracks[i].notes.Count == 0)
                {
                    tracks.RemoveAt(i);
                    i--;
                }
            }



            MusicTwo music = new MusicTwo();

            for (int tracko = 0; tracko < tracks.Count; tracko++)
            {
                Track    track = tracks[tracko];
                TrackTwo nts   = new TrackTwo();
                NoteTwo  ntt   = new NoteTwo();
                ntt.Number = 0;
                ntt.Length = 0;
                nts.Notes.Add(ntt);
                Note lastNote = (new Note());
                Note en       = new Note();
                lastNote.time   = 0;
                lastNote.number = 0;
                lastNote.length = 0;
                en.time         = 0;
                en.number       = 0;
                en.length       = 0;
                for (int i = 0; i < track.notes.Last().time + track.notes.Last().length; i++)
                {
                    Note nowNote = en;
                    for (int ii = 0; ii < track.notes.Count; ii++)
                    {
                        if (i >= track.notes[ii].time && i <= track.notes[ii].time + track.notes[ii].length)
                        {
                            nowNote = track.notes[ii];
                            if (firstNoteMode)
                            {
                                goto c1;
                            }
                        }
                    }
c1:
                    if (nts.Notes.Last().Number == nowNote.number)
                    {
                        nts.Notes.Last().Length++;
                        continue;
                    }
                    else
                    {
                        NoteTwo nttt = new NoteTwo();
                        nttt.Number = nowNote.number;
                        nttt.Length = 1;
                        nttt.IsBass = isInstrumentBass(nowNote.instrument);
                        nts.Notes.Add(nttt);
                    }
                }
                music.tracks.Add(nts);
            }
            return(music);
        }
Exemple #3
0
        /// <summary>
        /// Creates MusicTwo instance using midi file
        /// </summary>
        /// <param name="midiFile">Midi file to use</param>
        /// <param name="firstNoteMode">Use first note mode. If some notes appears at same time, use the first one, else the last</param>
        /// <returns>Created MusicTwo instance</returns>
        public static MusicTwo FromMidiFile(MidiFile midiFile, bool firstNoteMode)
        {
            double tempo = 0;
            List<Track> tracks = new List<Track>();

            for (int track_ = 1; track_ <= midiFile.Tracks; track_++)
            {
                Track track__ = new Track();
                foreach (var e in midiFile.Events)
                {
                    foreach (var ee in e)
                    {
                        if (ee is NoteEvent)
                        {
                            NoteEvent eee = (NoteEvent)ee;
                            if (eee is NoteOnEvent)
                            {
                                var eeee = (NoteOnEvent)eee;
                                Note nt = new Note();
                                nt.time = (int)(eeee.AbsoluteTime / (double)midiFile.DeltaTicksPerQuarterNote * tempo);
                                nt.length = 0;
                                if (eeee.OffEvent != null)
                                    nt.length = (int)(eeee.NoteLength / (double)midiFile.DeltaTicksPerQuarterNote * tempo);
                                nt.number = eeee.NoteNumber;
                                nt.instrument = track__.instrument;
                                if (eeee.Channel == track_)
                                    track__.notes.Add(nt);
                            }
                        }
                        if (ee is MetaEvent)
                        {
                            MetaEvent eee = (MetaEvent)ee;
                            if (eee.MetaEventType == MetaEventType.SetTempo)
                            {
                                TempoEvent eeee = (TempoEvent)eee;
                                tempo = eeee.MicrosecondsPerQuarterNote / 1000;
                            }
                        }

                        if (ee is PatchChangeEvent)
                        {
                            PatchChangeEvent eee = (PatchChangeEvent)ee;
                            if (eee.Channel == track_)
                                track__.instrument = eee.Patch;
                        }
                    }
                }
                tracks.Add(track__);
            }
            for (int i = 0; i < tracks.Count; i++)
            {
                for (int ii = 0; ii < tracks[i].notes.Count; ii++)
                {
                    if (tracks[i].notes[ii].length == 0)
                        tracks[i].notes.RemoveAt(ii);
                }
                if (tracks[i].notes.Count == 0)
                {
                    tracks.RemoveAt(i);
                    i--;
                }
            }

            MusicTwo music = new MusicTwo();
            for (int tracko = 0; tracko < tracks.Count; tracko++)
            {
                Track track = tracks[tracko];
                TrackTwo nts = new TrackTwo();
                NoteTwo ntt = new NoteTwo();
                ntt.Number = 0;
                ntt.Length = 0;
                nts.Notes.Add(ntt);
                Note lastNote = (new Note());
                Note en = new Note();
                lastNote.time = 0;
                lastNote.number = 0;
                lastNote.length = 0;
                en.time = 0;
                en.number = 0;
                en.length = 0;
                for (int i = 0; i < track.notes.Last().time + track.notes.Last().length; i++)
                {
                    Note nowNote = en;
                    for (int ii = 0; ii < track.notes.Count; ii++)
                    {
                        if (i >= track.notes[ii].time && i <= track.notes[ii].time + track.notes[ii].length)
                        {
                            nowNote = track.notes[ii];
                            if (firstNoteMode)
                                goto c1;
                        }
                    }
                    c1:
                    if (nts.Notes.Last().Number == nowNote.number)
                    {
                        nts.Notes.Last().Length++;
                        continue;
                    }
                    else
                    {
                        NoteTwo nttt = new NoteTwo();
                        nttt.Number = nowNote.number;
                        nttt.Length = 1;
                        nttt.IsBass = isInstrumentBass(nowNote.instrument);
                        nts.Notes.Add(nttt);
                    }
                }
                music.tracks.Add(nts);
            }
            return music;
        }