Exemple #1
0
 public MidiTime(MidiTime copyMe)
 {
     this.File          = copyMe.File;
     this.TimeSignature = copyMe.TimeSignature;
     this.Measures      = copyMe.Measures;
     this.Beats         = copyMe.Beats;
     this.Ticks         = copyMe.Ticks;
     this.Region        = copyMe.Region;
 }
Exemple #2
0
        public uint GetAsTotalTicks(uint pulsesPerQuarterNote, TimeSignatureEvent timeSig)
        {
            uint ticks = 0;

            uint convertedPulsesPerQuarterNote = (uint)(pulsesPerQuarterNote / (timeSig.Denominator / 4));

            ticks += ((this.Measures - 1) * convertedPulsesPerQuarterNote * timeSig.Numerator);
            ticks += ((this.Beats - 1) * convertedPulsesPerQuarterNote);
            ticks += this.Ticks;

            return(ticks);
        }
Exemple #3
0
        /// <summary>
        /// the position of the region boundaries are calculated on the fly by using the current time
        /// signature and pulses per QN
        /// </summary>
        /// <param name="position"></param>
        /// <param name="pulsesPerQuarterNote"></param>
        /// <param name="timeSig"></param>
        /// <returns></returns>
        public Region GetRegion(MidiTime position, uint pulsesPerQuarterNote, TimeSignatureEvent timeSig)
        {
            uint posTicks = position.GetAsTotalTicks(pulsesPerQuarterNote, timeSig);

            foreach (Region r in this.Regions)
            {
                if (posTicks >= r.Start.GetAsTotalTicks(pulsesPerQuarterNote, timeSig) &&
                    posTicks < r.End.GetAsTotalTicks(pulsesPerQuarterNote, timeSig))
                {
                    return(r);
                }
            }

            return(null);
        }
Exemple #4
0
        public MidiTime(MidiFile midiFile, uint totalTicks, TimeSignatureEvent timeSig)
        {
            this.File          = midiFile;
            this.TimeSignature = timeSig;

            uint convertedPulsesPerQuarterNote = (uint)(this.File.Header.PulsesPerQuarterNote / (this.TimeSignature.Denominator / 4));

            this.Measures = ((totalTicks / convertedPulsesPerQuarterNote) / this.TimeSignature.Numerator); //number of beats in a measure
            this.Beats    = (totalTicks / convertedPulsesPerQuarterNote) % this.TimeSignature.Numerator;   //number of notes in a beat
            this.Ticks    = (totalTicks % convertedPulsesPerQuarterNote);

            this.Measures++;
            this.Beats++;

            this.Region = this.File.GetRegion(this, this.File.Header.PulsesPerQuarterNote, this.TimeSignature);
        }
Exemple #5
0
        public MidiTime(MidiFile midiFile, String measuresBeatsTicksString, TimeSignatureEvent timeSig)
        {
            this.File          = midiFile;
            this.TimeSignature = timeSig;

            char[]   splitChars = { '.' };
            String[] parts      = measuresBeatsTicksString.Split(splitChars);
            this.Measures = uint.Parse(parts[0]);

            if (this.Measures < 1)
            {
                throw new ApplicationException("measures must be 1 or above");
            }

            this.Beats = uint.Parse(parts[1]);
            this.Ticks = uint.Parse(parts[2]);

            //this.Region = this.File.GetRegion(this, this.File.Header.PulsesPerQuarterNote, this.TimeSignature);
        }
Exemple #6
0
        public MidiTime(MidiFile midiFile, uint measures, uint beats, uint ticks, TimeSignatureEvent timeSig)
        {
            this.File          = midiFile;
            this.TimeSignature = timeSig;

            this.Measures = measures;

            if (this.Measures < 1)
            {
                throw new ApplicationException("measures must be 1 or above");
            }

            this.Beats = beats;
            this.Ticks = ticks;

            //uint convertedPulsesPerQuarterNote = (uint)(this.File.Header.PulsesPerQuarterNote / (this.TimeSignature.Denominator / 4));

            this.Region = this.File.GetRegion(this, this.File.Header.PulsesPerQuarterNote, this.TimeSignature);
        }
Exemple #7
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());
                }
            }
        }