Exemple #1
0
 /// <summary>
 /// Create a MIDI event object from the given MIDI message and timestamp.
 /// <param name="message">the MidiMessage for this event</param>
 /// <param name="tick">the timestamp for this event</param>
 /// </summary>
 public MidiEvent(MidiMessage message, long tick)
 {
     this.message = message;
     this.tick    = tick;
 }
Exemple #2
0
        /// <summary>
        /// Creates C# code from a MIDI sequence.
        /// </summary>
        /// <param name="sequence">The sequence to be generated as C# code and saved.</param>
        /// <param name="outputFilePath">path to text file to store generated code to</param>
        public static void SaveGenerateMidiCode(this Sequence sequence, string outputFilePath)
        {
            // generate code
            int trackno = 1;

            using (var outfile = new StreamWriter(outputFilePath, false)) {
                string   midiTypeName = MidiHelper.GetMidiFormatString(sequence.MidiFileType);
                DateTime now          = DateTime.Now;

                outfile.WriteLine("/*");
                outfile.WriteLine("* Generated by GnuMidi");
                outfile.WriteLine("* Author: [email protected]");
                outfile.WriteLine("* Date: {0}", now.ToString("yyyy-MM-dd"));
                outfile.WriteLine("* Time: {0}", now.ToString("HH:mm"));
                outfile.WriteLine("*/");
                outfile.WriteLine("using System;");
                outfile.WriteLine("using System.IO;");
                outfile.WriteLine("using gnu.sound.midi;");
                outfile.WriteLine("using gnu.sound.midi.file;");
                outfile.WriteLine("using gnu.sound.midi.info;");
                outfile.WriteLine();
                outfile.WriteLine("namespace MidiGeneration");
                outfile.WriteLine("{");
                outfile.WriteLine("	public static class GenerateMidi");
                outfile.WriteLine("	{");
                outfile.WriteLine("		public static void Main(string[] args)");
                outfile.WriteLine("		{");
                outfile.WriteLine("			// Generate Midi");
                outfile.WriteLine("			var sequence = GenerateSequence();");
                outfile.WriteLine("			sequence.DumpMidi(\"generated_dump.txt\");");
                outfile.WriteLine("			new MidiFileWriter().Write(sequence, sequence.MidiFileType, new FileInfo(\"generated.mid\"));");
                outfile.WriteLine("		}");
                outfile.WriteLine();

                outfile.WriteLine("		public static Sequence GenerateSequence() {");
                outfile.WriteLine();
                outfile.WriteLine("			// Generate midi file");
                outfile.WriteLine("			var sequence = new Sequence({0}, {1}, 0, (int) MidiHelper.MidiFormat.{2});", sequence.DivisionType, sequence.Resolution, midiTypeName);

                foreach (var track in sequence.Tracks)
                {
                    outfile.WriteLine();
                    outfile.WriteLine("			var track{0} = sequence.CreateTrack();", trackno);
                    foreach (var ev in track.Events)
                    {
                        long tick          = ev.Tick;
                        int  beat          = (int)tick / sequence.Resolution;
                        int  tickRemainder = (int)tick % sequence.Resolution;

                        MidiMessage msg = ev.Message;

                        // check if this is a meta message
                        var mm = msg as MetaMessage;
                        if (mm != null)
                        {
                            string metaCodeString = mm.CreateMetaEventGeneratedCode(tick, sequence.Resolution);
                            outfile.WriteLine("			track{0}.Add({1});", trackno, metaCodeString);
                        }

                        // check if this is a short message
                        var sm = msg as ShortMessage;
                        if (sm != null)
                        {
                            string shortCodeString = sm.CreateShortEventGeneratedCode(true, tick);
                            outfile.WriteLine("			track{0}.Add({1}", trackno, shortCodeString);
                        }

                        // check if this is a sysex message
                        var ss = msg as SysexMessage;
                        if (ss != null)
                        {
                            var    data        = ss.GetMessage();
                            string sysexString = MidiHelper.ByteArrayToString(data, ",");
                            outfile.WriteLine("			track{0}.Add(SysexEvent.CreateSysexEvent(\"{1}\", {2}));", trackno, sysexString, tick);
                        }
                    }
                    trackno++;
                }

                outfile.WriteLine();
                outfile.WriteLine("			return sequence;");
                outfile.WriteLine("		}");

                outfile.WriteLine("	}");
                outfile.WriteLine("}");
            }
        }