Esempio n. 1
0
        SmfTrack ReadTrack()
        {
            var tr = new SmfTrack();

            if (
                ReadByte() != 'M' ||
                ReadByte() != 'T' ||
                ReadByte() != 'r' ||
                ReadByte() != 'k')
            {
                throw ParseError("MTrk is expected");
            }
            int trackSize = ReadInt32();

            current_track_size = 0;
            int total = 0;

            while (current_track_size < trackSize)
            {
                int delta = ReadVariableLength();
                tr.Messages.Add(ReadMessage(delta));
                total += delta;
            }
            if (current_track_size != trackSize)
            {
                throw ParseError("Size information mismatch");
            }
            return(tr);
        }
Esempio n. 2
0
        public void WriteTrack(SmfTrack track)
        {
            stream.Write(Encoding.UTF8.GetBytes("MTrk"), 0, 4);
            WriteInt(GetTrackDataSize(track));

            byte running_status = 0;

            foreach (SmfMessage e in track.Messages)
            {
                Write7BitVariableInteger(e.DeltaTime);
                switch (e.Event.EventType)
                {
                case SmfEvent.Meta:
                    meta_event_writer(false, e, stream);
                    break;

                case SmfEvent.SysEx1:
                case SmfEvent.SysEx2:
                    stream.WriteByte(e.Event.EventType);
                    Write7BitVariableInteger(e.Event.Data.Length);
                    stream.Write(e.Event.Data, 0, e.Event.Data.Length);
                    break;

                default:
                    if (DisableRunningStatus || e.Event.StatusByte != running_status)
                    {
                        stream.WriteByte(e.Event.StatusByte);
                    }
                    int len = SmfEvent.FixedDataSize(e.Event.EventType);
                    stream.WriteByte(e.Event.Msb);
                    if (len > 1)
                    {
                        stream.WriteByte(e.Event.Lsb);
                    }
                    if (len > 2)
                    {
                        throw new Exception("Unexpected data size: " + len);
                    }
                    break;
                }
                running_status = e.Event.StatusByte;
            }
        }
Esempio n. 3
0
 SmfTrack GenerateTrack(MmlResolvedTrack source)
 {
     var rtrk = new SmfTrack ();
     int cur = 0;
     foreach (var ev in source.Events) {
         SmfEvent evt;
         if (ev.Arguments.Count == 3)
             evt = new SmfEvent (ev.Arguments [0], ev.Arguments [1], ev.Arguments [2], null);
         else if (ev.Arguments [0] == 0xFF)
             evt = new SmfEvent (ev.Arguments [0], ev.Arguments [1], 0, ev.Arguments.Skip (2).ToArray ());
         else
             evt = new SmfEvent (ev.Arguments [0], 0, 0, ev.Arguments.Skip (1).ToArray ());
         var msg = new SmfMessage (ev.Tick - cur, evt);
         rtrk.Messages.Add (msg);
         cur = ev.Tick;
     }
     rtrk.Messages.Add (new SmfMessage (0, new SmfEvent (0xFF, 0x2F, 0, new byte [0])));
     return rtrk;
 }
Esempio n. 4
0
        int GetTrackDataSize(SmfTrack track)
        {
            int  size           = 0;
            byte running_status = 0;

            foreach (SmfMessage e in track.Messages)
            {
                // delta time
                size += GetVariantLength(e.DeltaTime);

                // arguments
                switch (e.Event.EventType)
                {
                case SmfEvent.Meta:
                    size += meta_event_writer(true, e, null);
                    break;

                case SmfEvent.SysEx1:
                case SmfEvent.SysEx2:
                    size++;
                    size += GetVariantLength(e.Event.Data.Length);
                    size += e.Event.Data.Length;
                    break;

                default:
                    // message type & channel
                    if (DisableRunningStatus || running_status != e.Event.StatusByte)
                    {
                        size++;
                    }
                    size += SmfEvent.FixedDataSize(e.Event.EventType);
                    break;
                }

                running_status = e.Event.StatusByte;
            }
            return(size);
        }
Esempio n. 5
0
        int GetTrackDataSize(SmfTrack track)
        {
            int size = 0;
            byte running_status = 0;
            foreach (SmfMessage e in track.Messages) {
                // delta time
                size += GetVariantLength (e.DeltaTime);

                // arguments
                switch (e.Event.EventType) {
                case SmfEvent.Meta:
                    size += meta_event_writer (true, e, null);
                    break;
                case SmfEvent.SysEx1:
                case SmfEvent.SysEx2:
                    size++;
                    size += GetVariantLength (e.Event.Data.Length);
                    size += e.Event.Data.Length;
                    break;
                default:
                    // message type & channel
                    if (DisableRunningStatus || running_status != e.Event.StatusByte)
                        size++;
                    size += SmfEvent.FixedDataSize (e.Event.EventType);
                    break;
                }

                running_status = e.Event.StatusByte;
            }
            return size;
        }
Esempio n. 6
0
        public void WriteTrack(SmfTrack track)
        {
            stream.Write (Encoding.UTF8.GetBytes ("MTrk"), 0, 4);
            WriteInt (GetTrackDataSize (track));

            byte running_status = 0;

            foreach (SmfMessage e in track.Messages) {
                Write7BitVariableInteger (e.DeltaTime);
                switch (e.Event.EventType) {
                case SmfEvent.Meta:
                    meta_event_writer (false, e, stream);
                    break;
                case SmfEvent.SysEx1:
                case SmfEvent.SysEx2:
                    stream.WriteByte (e.Event.EventType);
                    Write7BitVariableInteger (e.Event.Data.Length);
                    stream.Write (e.Event.Data, 0, e.Event.Data.Length);
                    break;
                default:
                    if (DisableRunningStatus || e.Event.StatusByte != running_status)
                        stream.WriteByte (e.Event.StatusByte);
                    int len = SmfEvent.FixedDataSize (e.Event.EventType);
                    stream.WriteByte (e.Event.Msb);
                    if (len > 1)
                        stream.WriteByte (e.Event.Lsb);
                    if (len > 2)
                        throw new Exception ("Unexpected data size: " + len);
                    break;
                }
                running_status = e.Event.StatusByte;
            }
        }
Esempio n. 7
0
 public SplitTrack(int trackID)
 {
     TrackID = trackID;
     Track = new SmfTrack ();
 }
Esempio n. 8
0
 SmfTrack ReadTrack()
 {
     var tr = new SmfTrack ();
     if (
         ReadByte ()  != 'M'
         || ReadByte ()  != 'T'
         || ReadByte ()  != 'r'
         || ReadByte ()  != 'k')
         throw ParseError ("MTrk is expected");
     int trackSize = ReadInt32 ();
     current_track_size = 0;
     int total = 0;
     while (current_track_size < trackSize) {
         int delta = ReadVariableLength ();
         tr.Messages.Add (ReadMessage (delta));
         total += delta;
     }
     if (current_track_size != trackSize)
         throw ParseError ("Size information mismatch");
     return tr;
 }
Esempio n. 9
0
 public void AddTrack(SmfTrack track)
 {
     this.tracks.Add (track);
 }
Esempio n. 10
0
        public static void Main(string [] args)
        {
            int inId = -1, outId = -1, bufsize = -1;
            string filename = null;
            TimeSpan interval = TimeSpan.Zero;
            byte [] ex = null;
            foreach (var arg in args) {
                if (arg.StartsWith ("--in:")) {
                    if (!int.TryParse (arg.Substring (5), out inId))
                        inId = -1;
                }
                if (arg.StartsWith ("--out:")) {
                    if (!int.TryParse (arg.Substring (6), out outId))
                        outId = -1;
                }
                if (arg.StartsWith ("--buffer:")) {
                    if (!int.TryParse (arg.Substring (9), out bufsize))
                        bufsize = -1;
                }
                if (arg.StartsWith ("--file:"))
                    filename = arg.Substring (7);
                if (arg.StartsWith ("--interval:")) {
                    int intervalint;
                    if (int.TryParse (arg.Substring (11), out intervalint))
                        interval = TimeSpan.FromMilliseconds (intervalint);
                }
                if (arg.StartsWith ("--sysex:")) {
                    string [] l = arg.Substring (8).Split (',');
                    ex = new byte [l.Length];
                    byte v;
                    for (int i = 0; i < ex.Length; i++) {
                        if (byte.TryParse (l [i], NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo, out v))
                            ex [i] = v;
                        else {
                            ex = null;
                            break;
                        }
                    }
                    if (ex == null) {
                        Console.WriteLine ("invalid sysex: " + arg);
                        return;
                    }
                }
            }
            var a = new List<MidiDeviceInfo> (MidiDeviceManager.AllDevices);
            if (inId < 0) {
                foreach (var dev in a)
                    if (dev.IsInput)
                        Console.WriteLine ("ID {0}: {1}", dev.ID, dev.Name);
                Console.WriteLine ("Type number to select MIDI In Device to use (type anything else to quit)");
                if (!int.TryParse (Console.ReadLine (), out inId))
                    return;
            }
            if (outId < 0) {
                foreach (var dev in a)
                    if (dev.IsOutput)
                        Console.WriteLine ("ID {0}: {1}", dev.ID, dev.Name);
                Console.WriteLine ("Type number to select MIDI Out Device to use (type anything else to quit)");
                if (!int.TryParse (Console.ReadLine (), out outId))
                    return;
            }

            var dump = new BulkDump ();
            if (interval != TimeSpan.Zero)
                dump.Interval = interval;
            if (bufsize > 0)
                dump.BufferSize = bufsize;
            if (ex != null)
                dump.SetSysEx (ex);
            dump.Start (inId, outId);
            Console.WriteLine ("Type [CR] to stop receiving");
            Console.ReadLine ();
            dump.Stop ();

            if (String.IsNullOrEmpty (filename)) {
                Console.Write ("Type filename to save if you want: ");
                filename = Console.ReadLine ();
            }
            if (filename.Length > 0) {
                var music = new SmfMusic ();
                var track = new SmfTrack ();
                foreach (var e in dump.Results) {
                    if (e.SysEx != null)
                        track.Messages.Add (new SmfMessage (e.Timestamp, new SmfEvent (0xF0, 0, 0, e.SysEx)));
                    else
                        track.Messages.Add (new SmfMessage (e.Timestamp, new SmfEvent (e.Message.Value)));
                }
                music.Tracks.Add (track);
                using (var f = File.Create (filename))
                    new SmfWriter (f).WriteMusic (music);
            }
        }
Esempio n. 11
0
        public static void Main(string [] args)
        {
            int      inId = -1, outId = -1, bufsize = -1;
            string   filename = null;
            TimeSpan interval = TimeSpan.Zero;

            byte [] ex = null;
            foreach (var arg in args)
            {
                if (arg.StartsWith("--in:"))
                {
                    if (!int.TryParse(arg.Substring(5), out inId))
                    {
                        inId = -1;
                    }
                }
                if (arg.StartsWith("--out:"))
                {
                    if (!int.TryParse(arg.Substring(6), out outId))
                    {
                        outId = -1;
                    }
                }
                if (arg.StartsWith("--buffer:"))
                {
                    if (!int.TryParse(arg.Substring(9), out bufsize))
                    {
                        bufsize = -1;
                    }
                }
                if (arg.StartsWith("--file:"))
                {
                    filename = arg.Substring(7);
                }
                if (arg.StartsWith("--interval:"))
                {
                    int intervalint;
                    if (int.TryParse(arg.Substring(11), out intervalint))
                    {
                        interval = TimeSpan.FromMilliseconds(intervalint);
                    }
                }
                if (arg.StartsWith("--sysex:"))
                {
                    string [] l = arg.Substring(8).Split(',');
                    ex = new byte [l.Length];
                    byte v;
                    for (int i = 0; i < ex.Length; i++)
                    {
                        if (byte.TryParse(l [i], NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo, out v))
                        {
                            ex [i] = v;
                        }
                        else
                        {
                            ex = null;
                            break;
                        }
                    }
                    if (ex == null)
                    {
                        Console.WriteLine("invalid sysex: " + arg);
                        return;
                    }
                }
            }
            var a = new List <MidiDeviceInfo> (MidiDeviceManager.AllDevices);

            if (inId < 0)
            {
                foreach (var dev in a)
                {
                    if (dev.IsInput)
                    {
                        Console.WriteLine("ID {0}: {1}", dev.ID, dev.Name);
                    }
                }
                Console.WriteLine("Type number to select MIDI In Device to use (type anything else to quit)");
                if (!int.TryParse(Console.ReadLine(), out inId))
                {
                    return;
                }
            }
            if (outId < 0)
            {
                foreach (var dev in a)
                {
                    if (dev.IsOutput)
                    {
                        Console.WriteLine("ID {0}: {1}", dev.ID, dev.Name);
                    }
                }
                Console.WriteLine("Type number to select MIDI Out Device to use (type anything else to quit)");
                if (!int.TryParse(Console.ReadLine(), out outId))
                {
                    return;
                }
            }

            var dump = new BulkDump();

            if (interval != TimeSpan.Zero)
            {
                dump.Interval = interval;
            }
            if (bufsize > 0)
            {
                dump.BufferSize = bufsize;
            }
            if (ex != null)
            {
                dump.SetSysEx(ex);
            }
            dump.Start(inId, outId);
            Console.WriteLine("Type [CR] to stop receiving");
            Console.ReadLine();
            dump.Stop();

            if (String.IsNullOrEmpty(filename))
            {
                Console.Write("Type filename to save if you want: ");
                filename = Console.ReadLine();
            }
            if (filename.Length > 0)
            {
                var music = new SmfMusic();
                var track = new SmfTrack();
                foreach (var e in dump.Results)
                {
                    if (e.SysEx != null)
                    {
                        track.Messages.Add(new SmfMessage(e.Timestamp, new SmfEvent(0xF0, 0, 0, e.SysEx)));
                    }
                    else
                    {
                        track.Messages.Add(new SmfMessage(e.Timestamp, new SmfEvent(e.Message.Value)));
                    }
                }
                music.Tracks.Add(track);
                using (var f = File.Create(filename))
                    new SmfWriter(f).WriteMusic(music);
            }
        }
Esempio n. 12
0
 public SplitTrack(int trackID)
 {
     TrackID = trackID;
     Track   = new SmfTrack();
 }
Esempio n. 13
0
 public void AddTrack(SmfTrack track)
 {
     this.tracks.Add(track);
 }