Ejemplo n.º 1
0
        public void ConvertFromBinary(System.IO.BinaryReader reader)
        {
            this.ChunkSize = UtilBinary.ReadUintValue(reader);

            ChannelEventType lastChannelEventType    = 0;// = ChannelEventType.Unknown;
            byte             lastChannelEventChannel = 0;

            while (true)
            {
                //Debug.WriteLine(reader.BaseStream.Position);

                uint deltaTicks     = UtilBinary.ReadVariableLengthValue(reader);
                byte trackEventType = UtilBinary.ReadByteValue(reader);

                if (trackEventType == (byte)0xFF)
                {
                    //Debug.WriteLine("*****************Meta Event********************");

                    MetaEventType metaEventType = (MetaEventType)UtilBinary.ReadByteValue(reader);

                    //Debug.WriteLine(metaEventType);

                    int lengthOfMetaData = 0;

                    //meta event
                    switch (metaEventType)
                    {
                    case MetaEventType.SequenceNumber:
                        UtilBinary.ReadByteValue(reader);
                        this.SequenceNumber = new SequenceNumberEvent(UtilBinary.ReadByteValue(reader), UtilBinary.ReadByteValue(reader));
                        break;

                    case MetaEventType.TextEvent:
                        lengthOfMetaData = (int)UtilBinary.ReadVariableLengthValue(reader);
                        this.Comments    = new TextEvent(new string(UtilBinary.ReadCharsValue(reader, lengthOfMetaData)));
                        //Debug.WriteLine(this.Comments.Text);
                        break;

                    case MetaEventType.CopyrightNotice:
                        lengthOfMetaData     = (int)UtilBinary.ReadVariableLengthValue(reader);
                        this.CopyrightNotice = new CopyrightNoticeEvent(new string(UtilBinary.ReadCharsValue(reader, lengthOfMetaData)));
                        //Debug.WriteLine(this.CopyrightNotice.Text);
                        break;

                    case MetaEventType.TrackName:
                        lengthOfMetaData = (int)UtilBinary.ReadVariableLengthValue(reader);
                        this.TrackName   = new TrackNameEvent(new string(UtilBinary.ReadCharsValue(reader, lengthOfMetaData)));
                        //Debug.WriteLine(this.TrackName.Text);
                        break;

                    case MetaEventType.InstrumentName:
                        lengthOfMetaData    = (int)UtilBinary.ReadVariableLengthValue(reader);
                        this.InstrumentName = new InstrumentNameEvent(new string(UtilBinary.ReadCharsValue(reader, lengthOfMetaData)));
                        //Debug.WriteLine(this.InstrumentName.Text);
                        break;

                    case MetaEventType.Lyrics:
                        lengthOfMetaData = (int)UtilBinary.ReadVariableLengthValue(reader);
                        this.Lyrics      = new LyricsEvent(new string(UtilBinary.ReadCharsValue(reader, lengthOfMetaData)));
                        //Debug.WriteLine(this.Lyrics.Text);
                        break;

                    case MetaEventType.Marker:
                        lengthOfMetaData = (int)UtilBinary.ReadVariableLengthValue(reader);
                        this.Marker      = new MarkerEvent(new string(UtilBinary.ReadCharsValue(reader, lengthOfMetaData)));
                        //Debug.WriteLine(this.Marker.Text);
                        break;

                    case MetaEventType.CuePoint:
                        lengthOfMetaData = (int)UtilBinary.ReadVariableLengthValue(reader);
                        this.CuePoint    = new CuePointEvent(new string(UtilBinary.ReadCharsValue(reader, lengthOfMetaData)));
                        //Debug.WriteLine(this.CuePoint.Text);
                        break;

                    case MetaEventType.DeviceName:
                        lengthOfMetaData = (int)UtilBinary.ReadVariableLengthValue(reader);
                        this.DeviceName  = new DeviceNameEvent(new string(UtilBinary.ReadCharsValue(reader, lengthOfMetaData)));
                        //Debug.WriteLine(this.DeviceName.Text);
                        break;

                    case MetaEventType.MidiChannel:
                        UtilBinary.ReadByteValue(reader);
                        this.Channel = new MidiChannelEvent(UtilBinary.ReadByteValue(reader));
                        //Debug.WriteLine(this.Channel.Channel);
                        break;

                    case MetaEventType.MidiPort:
                        UtilBinary.ReadByteValue(reader);
                        this.Port = new MidiPortEvent(UtilBinary.ReadByteValue(reader));
                        break;

                    case MetaEventType.EndOfTrack:
                        UtilBinary.ReadByteValue(reader);
                        //this.EndOfTrack = new EndOfTrackEvent();
                        //this.EndOfTrack.DeltaTicks = deltaTicks;
                        //Debug.WriteLine(this.EndOfTrack.ToString());
                        return;

                        break;

                    case MetaEventType.Tempo:
                        UtilBinary.ReadByteValue(reader);
                        uint microSecondsPerQuarterNote = UtilBinary.Read3ByteInteger(reader);
                        this.Tempo = new TempoEvent(microSecondsPerQuarterNote, this.TimeSignature.Denominator);
                        break;

                    case MetaEventType.SMPTEOffSet:
                        UtilBinary.ReadByteValue(reader);
                        this.SMPTEOffSet = new SMPTEOffSetEvent(UtilBinary.ReadByteValue(reader), UtilBinary.ReadByteValue(reader), UtilBinary.ReadByteValue(reader), UtilBinary.ReadByteValue(reader));
                        break;

                    case MetaEventType.TimeSignature:
                        UtilBinary.ReadByteValue(reader);
                        this.TimeSignature                   = new TimeSignatureEvent();
                        this.TimeSignature.Numerator         = UtilBinary.ReadByteValue(reader);
                        this.TimeSignature.Denominator       = (byte)Math.Pow(2, UtilBinary.ReadByteValue(reader));
                        this.TimeSignature.MetronomePulse    = UtilBinary.ReadByteValue(reader);
                        this.TimeSignature.ThirtySecondNotes = UtilBinary.ReadByteValue(reader);
                        //Debug.WriteLine(this.TimeSignature.ToString());
                        //this.Tempo = new TempoEvent(this.Tempo.MicroSecondsPerQuarterNote, this.TimeSignature.Denominator);
                        break;

                    case MetaEventType.KeySignature:
                        UtilBinary.ReadByteValue(reader);
                        this.KeySignature = new KeySignatureEvent(UtilBinary.ReadByteValue(reader), UtilBinary.ReadByteValue(reader));
                        break;

                    case MetaEventType.SequencerSpecific:
                        throw new NotImplementedException();
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    //Debug.WriteLine("---------------------");
                    //Debug.WriteLine(deltaTime);

                    //Debug.WriteLine(UtilBinary.ReadByteValue(reader));
                    //Debug.WriteLine(UtilBinary.ReadByteValue(reader));
                    //Debug.WriteLine(UtilBinary.ReadByteValue(reader));

                    ChannelMidiEvent chan = new ChannelMidiEvent();

                    chan.DeltaTicks = deltaTicks;
                    chan.File       = this.File;

                    byte[] typeAndChannelBytes = UtilBinary.SplitByte(trackEventType);
                    byte   typeOrData          = typeAndChannelBytes[0];

                    if (Enum.IsDefined(typeof(ChannelEventType), typeOrData))
                    {
                        chan.Type            = (ChannelEventType)typeOrData;
                        lastChannelEventType = chan.Type;

                        chan.Channel            = typeAndChannelBytes[1];
                        lastChannelEventChannel = chan.Channel;

                        chan.Parameter1 = UtilBinary.ReadByteValue(reader);
                        chan.Parameter2 = UtilBinary.ReadByteValue(reader);
                    }
                    else
                    {
                        chan.Type       = lastChannelEventType;
                        chan.Channel    = lastChannelEventChannel;
                        chan.Parameter1 = trackEventType;
                        chan.Parameter2 = UtilBinary.ReadByteValue(reader);
                    }

                    if (chan.Type == ChannelEventType.NoteOn && chan.Parameter2 == 0)
                    {
                        chan.Type = ChannelEventType.NoteOff;
                    }

                    //Debug.WriteLine(chan.Type.ToString());
                    //Debug.WriteLine(chan.Channel.ToString());

                    //this.File.Tracks[(int)chan.Channel].ChannelEvents.Add(chan);
                    this.ChannelEvents.Add(chan);

                    //Debug.Write(chan.ToString());
                }
            }
        }