Example #1
0
        /// <summary>
        /// Return the string representation of this object
        /// </summary>
        /// <returns>the string representation of this object</returns>
        public override string ToString()
        {
            // GetMetaStrings: { type name, length, value string }
            object[] meta        = this.GetMetaStrings();
            string   metaStrings = string.Format("{0,-22} '{2}' ({1} bytes)", meta[0], meta[1], meta[2]);

            int    messageType = GetMetaMessageType();
            string typeName    = MidiHelper.GetMetaString(messageType);

            byte[] messageData = GetMetaMessageData();
            string hex         = MidiHelper.ByteArrayToString(messageData, ",");

            //return string.Format("{0} [{1}:{2}: {3}]", metaStrings, messageType, typeName, hex);
            return(string.Format("{0}", metaStrings));
        }
Example #2
0
        /// <summary>
        /// Return the string representation of this object
        /// </summary>
        /// <returns>the string representation of this object</returns>
        public override string ToString()
        {
            // Event, Note, Value, Patch, Text, Channel
            object[] meta        = this.GetShortStrings(false);
            string   metaStrings = string.Format("{0,-22} {1,-8} {2,-8} {3,-22} {4,-8} {5,-8}", meta[0], meta[1], meta[2], meta[3], meta[4], meta[5]);

            int    command     = GetCommand();
            string commandName = MidiHelper.GetEventTypeString((MidiHelper.MidiEventType)command);

            int channel = GetChannel();

            byte[] messageData = GetMessage();
            string hex         = MidiHelper.ByteArrayToString(messageData, ",");

            //return string.Format("{0} [{1}:{2}: {3}]", metaStrings, command, commandName, hex);
            return(string.Format("{0}", metaStrings));
        }
Example #3
0
        /// <summary>
        /// Return the string representation of this object
        /// </summary>
        /// <returns>the string representation of this object</returns>
        public override string ToString()
        {
            string hex = MidiHelper.ByteArrayToString(data, ",");

            return(string.Format("Sysex: [{0}]", hex));
        }
Example #4
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("}");
            }
        }