/// <summary> /// Reads a meta-event from a stream /// </summary> /// <param name="br">A binary reader based on the stream of MIDI data</param> /// <returns>A new MetaEvent object</returns> public static MetaEvent ReadMetaEvent(BinaryReader br) { MetaEventType metaEvent = (MetaEventType) br.ReadByte(); int length = ReadVarInt(br); MetaEvent me = new MetaEvent(); switch(metaEvent) { case MetaEventType.TrackSequenceNumber: // Sets the track's sequence number. me = new TrackSequenceNumberEvent(br,length); break; case MetaEventType.TextEvent: // Text event case MetaEventType.Copyright: // Copyright case MetaEventType.SequenceTrackName: // Sequence / Track Name case MetaEventType.TrackInstrumentName: // Track instrument name case MetaEventType.Lyric: // lyric case MetaEventType.Marker: // marker case MetaEventType.CuePoint: // cue point case MetaEventType.ProgramName: case MetaEventType.DeviceName: me = new TextEvent(br,length); break; case MetaEventType.EndTrack: // This event must come at the end of each track if(length != 0) { throw new FormatException("End track length"); } break; case MetaEventType.SetTempo: // Set tempo me = new TempoEvent(br,length); break; case MetaEventType.TimeSignature: // Time signature me = new TimeSignatureEvent(br,length); break; case MetaEventType.KeySignature: // Key signature me = new KeySignatureEvent(br, length); break; case MetaEventType.SequencerSpecific: // Sequencer specific information me = new SequencerSpecificEvent(br, length); break; case MetaEventType.SmpteOffset: me = new SmpteOffsetEvent(br, length); break; default: //System.Windows.Forms.MessageBox.Show(String.Format("Unsupported MetaEvent {0} length {1} pos {2}",metaEvent,length,br.BaseStream.Position)); me.data = br.ReadBytes(length); if (me.data.Length != length) { throw new FormatException("Failed to read metaevent's data fully"); } break; } me.metaEvent = metaEvent; me.metaDataLength = length; return me; }
private string ToMBT(long eventTime, int ticksPerQuarterNote, TimeSignatureEvent timeSignature) { int beatsPerBar = timeSignature == null ? 4 : timeSignature.Numerator; int ticksPerBar = timeSignature == null ? ticksPerQuarterNote * 4 : (timeSignature.Numerator * ticksPerQuarterNote * 4) / (1 << timeSignature.Denominator); int ticksPerBeat = ticksPerBar / beatsPerBar; long bar = 1 + (eventTime / ticksPerBar); long beat = 1 + ((eventTime % ticksPerBar) / ticksPerBeat); long tick = eventTime % ticksPerBeat; return String.Format("{0}:{1}:{2}", bar, beat, tick); }
/// <summary> /// Reads a meta-event from a stream /// </summary> /// <param name="br">A binary reader based on the stream of MIDI data</param> /// <returns>A new MetaEvent object</returns> public static MetaEvent ReadMetaEvent(BinaryReader br) { MetaEventType metaEvent = (MetaEventType)br.ReadByte(); int length = ReadVarInt(br); MetaEvent me = new MetaEvent(); switch (metaEvent) { case MetaEventType.TextEvent: // Text event case MetaEventType.Copyright: // Copyright case MetaEventType.SequenceTrackName: // Sequence / Track Name case MetaEventType.TrackInstrumentName: // Track instrument name case MetaEventType.Lyric: // lyric case MetaEventType.Marker: // marker case MetaEventType.CuePoint: // cue point case MetaEventType.ProgramName: case MetaEventType.DeviceName: me = new TextEvent(br, length); break; case MetaEventType.EndTrack: // This event must come at the end of each track if (length != 0) { throw new FormatException("End track length"); } break; case MetaEventType.SetTempo: // Set tempo me = new TempoEvent(br, length); break; case MetaEventType.TimeSignature: // Time signature me = new TimeSignatureEvent(br, length); break; default: //System.Windows.Forms.MessageBox.Show(String.Format("Unsupported MetaEvent {0} length {1} pos {2}",metaEvent,length,br.BaseStream.Position)); me.data = br.ReadBytes(length); if (me.data.Length != length) { throw new FormatException("Failed to read metaevent's data fully"); } break; } me.metaEvent = metaEvent; me.metaDataLength = length; return(me); }
private string ToMBT(long eventTime, int ticksPerQuarterNote, NAudio.Midi.TimeSignatureEvent timeSignature) { int beatsPerBar = timeSignature == null ? 4 : timeSignature.Numerator; int ticksPerBar = timeSignature == null ? ticksPerQuarterNote * 4 : (timeSignature.Numerator * ticksPerQuarterNote * 4) / (1 << timeSignature.Denominator); int ticksPerBeat = ticksPerBar / beatsPerBar; long bar = (eventTime / ticksPerBar); long beat = ((eventTime % ticksPerBar) / ticksPerBeat); long tick = eventTime % ticksPerBeat; float milsPerTick = milliSecondsPerQuartNote / ticksPerBeat; String initialMidiString = currentMidiEvent.ToString(); int currentChannel = getChannelNumber(initialMidiString); int noteLength = getMidiNoteLength(initialMidiString); if (noteLength != 0 && currentChannel > -1) { //START TIME AND DURATION TIME ARE CALCULATED HERE!!!! Note newNote = new Note(); //Double theStartTime = (bar * milliSecondsPerQuartNote * 4) + (milliSecondsPerQuartNote * (beat)); Double theStartTime = eventTime * milsPerTick; newNote.startTime = theStartTime; //Start time Measured in milliseconds String midiInfo = currentMidiEvent.ToString(); String typeOfNote = getTypeOfNote(midiInfo, currentChannel); newNote.notePitch = typeOfNote; newNote.noteVelocity = getVelocity(midiInfo); newNote.notePitchNumber = GDMethods.getPitchNumber(newNote.notePitch); //Debug.Log("Converted " + typeOfNote + " to -> " + newNote.notePitchNumber); Double timeOfNote = noteLength; //newNote.durationTime = ((((double)(timeOfNote))/(double)ticksPerBeat)* milliSecondsPerQuartNote);//Duration slso meseaured in milliseconds //TODO - check this newNote.durationTime = noteLength * milsPerTick; Channels[currentChannel].Notes.Add(newNote); if (!Channels[currentChannel].TypesOfNotes.Contains(typeOfNote)) { Channels[currentChannel].TypesOfNotes.Add(typeOfNote); } } string finalReturn = String.Format("{0}:{1}:{2}", bar + 1, beat + 1, tick); Console.WriteLine(finalReturn); return(finalReturn); //Print out this string to see full output }
public static MetaEvent ReadMetaEvent(BinaryReader br) { MetaEventType metaEventType = (MetaEventType)br.ReadByte(); int num = MidiEvent.ReadVarInt(br); MetaEvent metaEvent = new MetaEvent(); MetaEventType metaEventType2 = metaEventType; if (metaEventType2 <= MetaEventType.SetTempo) { switch (metaEventType2) { case MetaEventType.TrackSequenceNumber: metaEvent = new TrackSequenceNumberEvent(br, num); goto IL_E9; case MetaEventType.TextEvent: case MetaEventType.Copyright: case MetaEventType.SequenceTrackName: case MetaEventType.TrackInstrumentName: case MetaEventType.Lyric: case MetaEventType.Marker: case MetaEventType.CuePoint: case MetaEventType.ProgramName: case MetaEventType.DeviceName: metaEvent = new TextEvent(br, num); goto IL_E9; default: if (metaEventType2 != MetaEventType.EndTrack) { if (metaEventType2 == MetaEventType.SetTempo) { metaEvent = new TempoEvent(br, num); goto IL_E9; } } else { if (num != 0) { throw new FormatException("End track length"); } goto IL_E9; } break; } } else { if (metaEventType2 == MetaEventType.SmpteOffset) { metaEvent = new SmpteOffsetEvent(br, num); goto IL_E9; } switch (metaEventType2) { case MetaEventType.TimeSignature: metaEvent = new TimeSignatureEvent(br, num); goto IL_E9; case MetaEventType.KeySignature: metaEvent = new KeySignatureEvent(br, num); goto IL_E9; default: if (metaEventType2 == MetaEventType.SequencerSpecific) { metaEvent = new SequencerSpecificEvent(br, num); goto IL_E9; } break; } } metaEvent.data = br.ReadBytes(num); if (metaEvent.data.Length != num) { throw new FormatException("Failed to read metaevent's data fully"); } IL_E9: metaEvent.metaEvent = metaEventType; metaEvent.metaDataLength = num; return(metaEvent); }