/// <summary> /// Initializes the TempoChangeBuilder with the specified MetaMessage. /// </summary> /// <param name="message"> /// The MetaMessage to use for initializing the TempoChangeBuilder. /// </param> /// <exception cref="ArgumentException"> /// If the specified MetaMessage is not a tempo type. /// </exception> public void Initialize(MetaMessage e) { int t = 0; var b = e.GetBytes(); // If this platform uses little endian byte order. if (BitConverter.IsLittleEndian) { int d = b.Length - 1; // Pack tempo. for (int i = 0; i < e.Length; i++) { t |= b[d] << (Shift * i); d--; } } // Else this platform uses big endian byte order. else { // Pack tempo. for (int i = 0; i < e.Length; i++) { t |= b[i] << (Shift * i); } } tempo = t; }
public KeySignature(MetaMessage msg) { var b = msg.GetBytes(); IsMajor = (((sbyte)b[1]) == 0); Key = FromBytes(b[0], b[1]); }
/// <summary> /// Builds the tempo change MetaMessage. /// </summary> public void Build() { // If the tempo has been changed since the last time the message // was built. if (changed) { byte[] data = new byte[MetaMessage.TempoLength]; // If this platform uses little endian byte order. if (BitConverter.IsLittleEndian) { int d = data.Length - 1; // Unpack tempo. for (int i = 0; i < data.Length; i++) { data[d] = (byte)(tempo >> (Shift * i)); d--; } } // Else this platform uses big endian byte order. else { // Unpack tempo. for (int i = 0; i < data.Length; i++) { data[i] = (byte)(tempo >> (Shift * i)); } } changed = false; result = new MetaMessage(MetaType.Tempo, data); } }
public void Process(MetaMessage message) { #region Require if (message == null) { throw new ArgumentNullException("message"); } #endregion #region Guard if (message.MetaType != MetaType.Tempo) { if (message.MetaType == MetaType.TimeSignature) { var ts = new TimeSignatureBuilder(message); this.ClocksPerMetronomeClick = ts.ClocksPerMetronomeClick; } return; } #endregion TempoChangeBuilder builder = new TempoChangeBuilder(message); // Set the new tempo. Tempo = builder.Tempo; }
public IMidiMessage Clone() { IMidiMessage ret = null; if (this.MessageType == MessageType.Channel) { ret = new ChannelMessage(MessageData); } else if (this.MessageType == MessageType.Meta) { ret = new MetaMessage(this.MetaType, byteData); } else if (this.MessageType == MessageType.SystemCommon) { ret = new SysCommonMessage(MessageData); } else if (this.MessageType == MessageType.SystemExclusive) { ret = new SysExMessage(byteData); } else if (this.MessageType == MessageType.SystemRealtime) { ret = SysRealtimeMessage.FromType(realtimeType); ; } return(ret); }
public override bool Equals(object obj) { bool equal = true; MetaMessage message = (MetaMessage)obj; // If the types do not match. if (MetaType != message.MetaType) { // The messages are not equal equal = false; } // If the message lengths are not equal. if (equal && Length != message.Length) { // The message are not equal. equal = false; } // Check to see if the data is equal. for (int i = 0; i < Length && equal; i++) { // If a data value does not match. if (this.data[i] != message.data[i]) { // The messages are not equal. equal = false; } } return(equal); }
public void Process(MetaMessage message) { #region Require if (message == null) { throw new ArgumentNullException("message"); } #endregion #region Guard if (message.MetaType != MetaType.Tempo) { return; } #endregion TempoChangeBuilder builder = new TempoChangeBuilder(message); // Set the new tempo. Tempo = builder.Tempo; }
private void ReadMetaLyric(string[] ar) { string sy = string.Empty; if (ar.Length != 4) { if (ar.Length < 4) { throw new ArgumentException("Meta Lyric Length"); } for (int i = 3; i < ar.Length; i++) { sy += ar[i]; } } else { sy = ar[3]; } // Format: Track, Time, Lyric_t, Text int ticks = Convert.ToInt32(ar[1]); byte[] newdata = Encoding.Default.GetBytes(sy); MetaMessage mtMsg = new MetaMessage(MetaType.Lyric, newdata); track.Insert(ticks, mtMsg); manageMetaLyrics(newdata, ticks); }
private void Write(int deltaTicks, MetaMessage message) { if (message.MetaType == MetaType.Tempo) { // Delta time. events.AddRange(BitConverter.GetBytes(deltaTicks + offsetTicks)); // Stream ID. events.AddRange(streamID); TempoChangeBuilder builder = new TempoChangeBuilder(message); byte[] t = BitConverter.GetBytes(builder.Tempo); t[t.Length - 1] = MEVT_SHORTMSG | MEVT_TEMPO; // Event code. events.AddRange(t); offsetTicks = 0; } else { offsetTicks += deltaTicks; } }
private void Write(MetaMessage message) { trackData.Add((byte)message.Status); trackData.Add((byte)message.MetaType); WriteVariableLengthValue(message.Length); trackData.AddRange(message.GetBytes()); }
/// <summary> /// Builds the time signature MetaMessage. /// </summary> public void Build() { // If any of the properties have changed since the last time the // message was built. if (changed) { result = new MetaMessage(MetaType.TimeSignature, data); changed = false; } }
/// <summary> /// Initializes the TimeSignatureBuilder with the specified MetaMessage. /// </summary> /// <param name="message"> /// The MetaMessage to use for initializing the TimeSignatureBuilder. /// </param> /// <exception cref="ArgumentException"> /// If the specified MetaMessage is not a time signature type. /// </exception> public void Initialize(MetaMessage message) { #region Require if (message.MetaType != MetaType.TimeSignature) { throw new ArgumentException("Wrong meta event type.", "message"); } #endregion data = message.GetBytes(); }
/// <summary> /// Builds the text MetaMessage. /// </summary> public void Build() { // If the text has changed since the last time this method was // called. if (changed) { // // Build text MetaMessage. // ASCIIEncoding encoding = new ASCIIEncoding(); byte[] data = encoding.GetBytes(text); result = new MetaMessage(Type, data); changed = false; } }
private void ReadMetaText(string[] ar) { if (ar.Length != 4) { throw new ArgumentException("Meta Text Length"); } // Format: Track, Time, Text_t, Text int ticks = Convert.ToInt32(ar[1]); string sy = ar[3]; byte[] newdata = Encoding.Default.GetBytes(sy); MetaMessage mtMsg = new MetaMessage(MetaType.Text, newdata); track.Insert(ticks, mtMsg); manageMetaText(newdata, ticks); }
/// <summary> /// Initializes the MetaMessageTextBuilder with the specified MetaMessage. /// </summary> /// <param name="message"> /// The MetaMessage to use for initializing the MetaMessageTextBuilder. /// </param> /// <exception cref="ArgumentException"> /// If the MetaMessage is not a text based type. /// </exception> public void Initialize(MetaMessage message) { #region Require if (!IsTextType(message.MetaType)) { throw new ArgumentException("Not text based meta message.", "message"); } #endregion ASCIIEncoding encoding = new ASCIIEncoding(); text = encoding.GetString(message.GetBytes()); this.type = message.MetaType; }
public static void clearFingeringInMidiFile(Midi.Sequence file) { Regex signaturePattern = new Regex("^Fingering"); Regex fingerPattern = new Regex("^finger:"); foreach (Midi.Track track in file) { System.Collections.Generic.List <int> toRemove = new System.Collections.Generic.List <int>(); for (int i = track.Count - 1; i >= 0; --i) { Midi.MidiEvent e = track.GetMidiEvent(i); if (e.MidiMessage.MessageType == Midi.MessageType.Meta) { Midi.MetaMessage msg = e.MidiMessage as Midi.MetaMessage; switch (msg.MetaType) { case Midi.MetaType.Text: if (signaturePattern.Match(Encoding.Default.GetString(msg.GetBytes())).Length > 0) { toRemove.Add(i); } break; case Midi.MetaType.Marker: if (fingerPattern.Match(Encoding.Default.GetString(msg.GetBytes())).Length > 0) { toRemove.Add(i); } break; } } } foreach (int i in toRemove) { track.RemoveAt(i); } } }
/// <summary> /// Initializes the TempoChangeBuilder with the specified MetaMessage. /// </summary> /// <param name="message"> /// The MetaMessage to use for initializing the TempoChangeBuilder. /// </param> /// <exception cref="ArgumentException"> /// If the specified MetaMessage is not a tempo type. /// </exception> public void Initialize(MetaMessage e) { #region Require if (e == null) { throw new ArgumentNullException("e"); } else if (e.MetaType != MetaType.Tempo) { throw new ArgumentException("Wrong meta message type.", "e"); } #endregion int t = 0; // If this platform uses little endian byte order. if (BitConverter.IsLittleEndian) { int d = e.Length - 1; // Pack tempo. for (int i = 0; i < e.Length; i++) { t |= e[d] << (Shift * i); d--; } } // Else this platform uses big endian byte order. else { // Pack tempo. for (int i = 0; i < e.Length; i++) { t |= e[i] << (Shift * i); } } tempo = t; }
/// <summary> /// Indicates whether or not the specified MetaType is a text based /// type. /// </summary> /// <param name="type"> /// The MetaType to test. /// </param> /// <returns> /// <b>true</b> if the MetaType is a text based type; /// otherwise, <b>false</b>. /// </returns> private bool IsTextType(MetaType type) { bool result; if (type == MetaType.Copyright || type == MetaType.CuePoint || type == MetaType.DeviceName || type == MetaType.InstrumentName || type == MetaType.Lyric || type == MetaType.Marker || type == MetaType.ProgramName || type == MetaType.Text || type == MetaType.TrackName) { result = true; } else { result = false; } return(result); }
/// <summary> /// Initializes a new instance of the MetaMessageTextBuilder class with the /// specified MetaMessage. /// </summary> /// <param name="message"> /// The MetaMessage to use for initializing the MetaMessageTextBuilder. /// </param> /// <exception cref="ArgumentException"> /// If the MetaMessage is not a text based type. /// </exception> /// <remarks> /// The MetaMessage must be one of the following text based types: /// <list> /// <item> /// Copyright /// </item> /// <item> /// Cuepoint /// </item> /// <item> /// DeviceName /// </item> /// <item> /// InstrumentName /// </item> /// <item> /// Lyric /// </item> /// <item> /// Marker /// </item> /// <item> /// ProgramName /// </item> /// <item> /// Text /// </item> /// <item> /// TrackName /// </item> /// </list> /// If the MetaMessage is not a text based type, an exception will be /// thrown. /// </remarks> public MetaTextBuilder(MetaMessage message) { Initialize(message); }
// // Methods /// <summary> /// Initializes the MetaMessageTextBuilder with the specified MetaMessage. /// </summary> /// <param name="message"> /// The MetaMessage to use for initializing the MetaMessageTextBuilder. /// </param> /// <exception cref="ArgumentException"> /// If the MetaMessage is not a text based type. /// </exception> public void Initialize(MetaMessage message) { text = message.Text; this.type = message.MetaType; }
/// <summary> /// Initializes the KeySignatureBuilder with the specified MetaMessage. /// </summary> /// <param name="message"> /// The key signature MetaMessage to use for initializing the /// KeySignatureBuilder. /// </param> public void Initialize(MetaMessage message) { var ks = new KeySignature(message); key = ks.Key; }
private float TicksPerQuarterSingular(MetaMessage message) { var bytes = message.GetBytes(); return 0; }
private void GetMIDIInfo(string file, bool fillTime) { Midi.Sequence MIDISequence; int tempo = 500000; int maxtick = 0; float msPerTick = (tempo / 48) / 1000.0f; string title = string.Empty; _MIDIChannels = new BindingList <MIDIChannel>(); // load the MIDI file MIDISequence = new Midi.Sequence(); MIDISequence.Load(file); // we don't handle non-PPQN MIDI files if (MIDISequence.SequenceType != Sanford.Multimedia.Midi.SequenceType.Ppqn) { MessageBox.Show("Unsupported MIDI type...sorry!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } foreach (Midi.Track t in MIDISequence) { // get the command enumerator IEnumerator <Midi.MidiEvent> en = t.Iterator().GetEnumerator(); bool channelAdded = false; while (en.MoveNext()) { Midi.MidiEvent e = en.Current; switch (e.MidiMessage.MessageType) { case Sanford.Multimedia.Midi.MessageType.Channel: // track the # of channels Midi.ChannelMessage channel = (Midi.ChannelMessage)e.MidiMessage; if (!channelAdded) { _MIDIChannels.Add(new MIDIChannel(channel.MidiChannel, title)); channelAdded = true; } break; case Sanford.Multimedia.Midi.MessageType.Meta: Midi.MetaMessage meta = (Midi.MetaMessage)e.MidiMessage; switch (meta.MetaType) { // cache away the track name for the grid case Sanford.Multimedia.Midi.MetaType.TrackName: title = Encoding.ASCII.GetString(meta.GetBytes()); break; // get the tempo and convert to a time value we can use case Sanford.Multimedia.Midi.MetaType.Tempo: tempo = meta.GetBytes()[0] << 16 | meta.GetBytes()[1] << 8 | meta.GetBytes()[2]; msPerTick = (tempo / MIDISequence.Division) / 1000.0f; break; } break; } // find the highest time value if (e.AbsoluteTicks > maxtick) { maxtick = e.AbsoluteTicks; } } } // and use that value to fill in the minutes/seconds if (fillTime) { txtMinutes.Text = (((int)(msPerTick * maxtick) / 1000) / 60).ToString(); txtSeconds.Text = (((int)(msPerTick * maxtick) / 1000) % 60 + 1).ToString(); } }
/// <summary> /// Builds the text MetaMessage. /// </summary> public void Build() { // If the text has changed since the last time this method was // called. if(changed) { // // Build text MetaMessage. // ASCIIEncoding encoding = new ASCIIEncoding(); byte[] data = encoding.GetBytes(text); result = new MetaMessage(Type, data); changed = false; } }
/// <summary> /// Builds the key signature MetaMessage. /// </summary> public void Build() { byte[] data = new byte[MetaMessage.KeySigLength]; unchecked { switch (Key) { case Key.CFlatMajor: data[0] = (byte)-7; data[1] = 0; break; case Key.GFlatMajor: data[0] = (byte)-6; data[1] = 0; break; case Key.DFlatMajor: data[0] = (byte)-5; data[1] = 0; break; case Key.AFlatMajor: data[0] = (byte)-4; data[1] = 0; break; case Key.EFlatMajor: data[0] = (byte)-3; data[1] = 0; break; case Key.BFlatMajor: data[0] = (byte)-2; data[1] = 0; break; case Key.FMajor: data[0] = (byte)-1; data[1] = 0; break; case Key.CMajor: data[0] = 0; data[1] = 0; break; case Key.GMajor: data[0] = 1; data[1] = 0; break; case Key.DMajor: data[0] = 2; data[1] = 0; break; case Key.AMajor: data[0] = 3; data[1] = 0; break; case Key.EMajor: data[0] = 4; data[1] = 0; break; case Key.BMajor: data[0] = 5; data[1] = 0; break; case Key.FSharpMajor: data[0] = 6; data[1] = 0; break; case Key.CSharpMajor: data[0] = 7; data[1] = 0; break; case Key.AFlatMinor: data[0] = (byte)-7; data[1] = 1; break; case Key.EFlatMinor: data[0] = (byte)-6; data[1] = 1; break; case Key.BFlatMinor: data[0] = (byte)-5; data[1] = 1; break; case Key.FMinor: data[0] = (byte)-4; data[1] = 1; break; case Key.CMinor: data[0] = (byte)-3; data[1] = 1; break; case Key.GMinor: data[0] = (byte)-2; data[1] = 1; break; case Key.DMinor: data[0] = (byte)-1; data[1] = 1; break; case Key.AMinor: data[0] = 1; data[1] = 0; break; case Key.EMinor: data[0] = 1; data[1] = 1; break; case Key.BMinor: data[0] = 2; data[1] = 1; break; case Key.FSharpMinor: data[0] = 3; data[1] = 1; break; case Key.CSharpMinor: data[0] = 4; data[1] = 1; break; case Key.GSharpMinor: data[0] = 5; data[1] = 1; break; case Key.DSharpMinor: data[0] = 6; data[1] = 1; break; case Key.ASharpMinor: data[0] = 7; data[1] = 1; break; } } result = new MetaMessage(MetaType.KeySignature, data); }
public MetaMessageEventArgs(MetaMessage message) { this.message = message; }
/// <summary> /// Initializes a new instance of the KeySignatureBuilder class with /// the specified key signature MetaMessage. /// </summary> /// <param name="message"> /// The key signature MetaMessage to use for initializing the /// KeySignatureBuilder class. /// </param> public KeySignatureBuilder(MetaMessage message) { Initialize(message); }
/// <summary> /// Initializes the KeySignatureBuilder with the specified MetaMessage. /// </summary> /// <param name="message"> /// The key signature MetaMessage to use for initializing the /// KeySignatureBuilder. /// </param> public void Initialize(MetaMessage message) { #region Require if (message == null) { throw new ArgumentNullException("message"); } else if (message.MetaType != MetaType.KeySignature) { throw new ArgumentException("Wrong meta event type.", "messaege"); } #endregion sbyte b = (sbyte)message[0]; // If the key is major. if (message[1] == 0) { switch (b) { case -7: key = Key.CFlatMajor; break; case -6: key = Key.GFlatMajor; break; case -5: key = Key.DFlatMajor; break; case -4: key = Key.AFlatMajor; break; case -3: key = Key.EFlatMajor; break; case -2: key = Key.BFlatMajor; break; case -1: key = Key.FMajor; break; case 0: key = Key.CMajor; break; case 1: key = Key.GMajor; break; case 2: key = Key.DMajor; break; case 3: key = Key.AMajor; break; case 4: key = Key.EMajor; break; case 5: key = Key.BMajor; break; case 6: key = Key.FSharpMajor; break; case 7: key = Key.CSharpMajor; break; } } // Else the key is minor. else { switch (b) { case -7: key = Key.AFlatMinor; break; case -6: key = Key.EFlatMinor; break; case -5: key = Key.BFlatMinor; break; case -4: key = Key.FMinor; break; case -3: key = Key.CMinor; break; case -2: key = Key.GMinor; break; case -1: key = Key.DMinor; break; case 0: key = Key.AMinor; break; case 1: key = Key.EMinor; break; case 2: key = Key.BMinor; break; case 3: key = Key.FSharpMinor; break; case 4: key = Key.CSharpMinor; break; case 5: key = Key.GSharpMinor; break; case 6: key = Key.DSharpMinor; break; case 7: key = Key.ASharpMinor; break; } } }
/// <summary> /// Initializes the MetaMessageTextBuilder with the specified MetaMessage. /// </summary> /// <param name="message"> /// The MetaMessage to use for initializing the MetaMessageTextBuilder. /// </param> /// <exception cref="ArgumentException"> /// If the MetaMessage is not a text based type. /// </exception> public void Initialize(MetaMessage message) { #region Require if(!IsTextType(message.MetaType)) { throw new ArgumentException("Not text based meta message.", "message"); } #endregion ASCIIEncoding encoding = new ASCIIEncoding(); text = encoding.GetString(message.GetBytes()); this.type = message.MetaType; }
/// <summary> /// Indicates whether or not the specified MetaType is a text based /// type. /// </summary> /// <param name="type"> /// The MetaType to test. /// </param> /// <returns> /// <b>true</b> if the MetaType is a text based type; /// otherwise, <b>false</b>. /// </returns> private bool IsTextType(MetaType type) { bool result; if(type == MetaType.Copyright || type == MetaType.CuePoint || type == MetaType.DeviceName || type == MetaType.InstrumentName || type == MetaType.Lyric || type == MetaType.Marker || type == MetaType.ProgramName || type == MetaType.Text || type == MetaType.TrackName) { result = true; } else { result = false; } return result; }
public static NotationTrack parseMidiTrack(Midi.Track track, int division, ref Regex fingerPattern) { int microsecondsPerBeat = Midi.PpqnClock.DefaultTempo; float time = 0; TrackStatus trackStatus = new TrackStatus(); FingerChordMap fingerMap = new FingerChordMap(); List <Note> notes = new List <Note>(); Regex fingerMetaPattern = new Regex("fingering-marker-pattern:(.*)"); string name = ""; foreach (Midi.MidiEvent e in track.Iterator()) { time += e.DeltaTicks * microsecondsPerBeat / (division * 1000); switch (e.MidiMessage.MessageType) { case Midi.MessageType.Meta: Midi.MetaMessage mm = e.MidiMessage as Midi.MetaMessage; switch (mm.MetaType) { case Midi.MetaType.Tempo: Midi.TempoChangeBuilder builder = new Midi.TempoChangeBuilder(mm); microsecondsPerBeat = builder.Tempo; break; case Midi.MetaType.Text: { string text = Encoding.Default.GetString(mm.GetBytes()); var match = fingerMetaPattern.Match(text); if (match.Success) { fingerPattern = new Regex(match.Groups[1].ToString()); Debug.LogFormat("Finger Pattern found: {0}", fingerPattern.ToString()); } } break; case Midi.MetaType.Marker: if (fingerPattern != null) { string text = Encoding.Default.GetString(mm.GetBytes()); var match = fingerPattern.Match(text); if (match.Success) { //Debug.LogFormat("Finger: {0}", text); try { int pitch = int.Parse(match.Groups[1].ToString()); Finger finger = (Finger)int.Parse(match.Groups[2].ToString()); if (!fingerMap.ContainsKey(e.AbsoluteTicks)) { fingerMap[e.AbsoluteTicks] = new FingerChord(); } fingerMap[e.AbsoluteTicks][pitch] = finger; } catch (System.Exception except) { Debug.LogWarningFormat("fingering marker parse failed: {0}, {1}", text, except.Message); } } //else // Debug.LogWarningFormat("fail marker: {0}", text); } break; case Midi.MetaType.TrackName: name = Encoding.Default.GetString(mm.GetBytes()); break; } break; case Midi.MessageType.Channel: Midi.ChannelMessage cm = e.MidiMessage as Midi.ChannelMessage; if (!trackStatus.ContainsKey(cm.MidiChannel)) { trackStatus[cm.MidiChannel] = new ChannelStatus(); } var commandType = cm.Command; if (commandType == Midi.ChannelCommand.NoteOn && cm.Data2 == 0) { commandType = Midi.ChannelCommand.NoteOff; } switch (commandType) { case Midi.ChannelCommand.NoteOn: { int pitch = cm.Data1; int velocity = cm.Data2; if (pitch >= Piano.PitchMin && pitch <= Piano.PitchMax) { trackStatus[cm.MidiChannel][pitch] = new PitchStatus { tick = e.AbsoluteTicks, startTime = time, velocity = velocity } } ; } break; case Midi.ChannelCommand.NoteOff: { int pitch = cm.Data1; if (!trackStatus[cm.MidiChannel].ContainsKey(pitch)) { Debug.LogWarningFormat("Unexpected noteOff: {0}, {1}", e.AbsoluteTicks, pitch); } else { PitchStatus status = trackStatus[cm.MidiChannel][pitch]; Note note = new Note { tick = status.tick, start = status.startTime, duration = time - status.startTime, pitch = pitch, velocity = status.velocity }; if (fingerMap.ContainsKey(note.tick) && fingerMap[note.tick].ContainsKey(note.pitch)) { note.finger = fingerMap[note.tick][note.pitch]; } notes.Add(note); } } break; } break; } } NotationTrack notation = new NotationTrack(); notation.notes = notes.ToArray(); notation.name = name; return(notation); }
/// <summary> /// Builds the tempo change MetaMessage. /// </summary> public void Build() { // If the tempo has been changed since the last time the message // was built. if(changed) { byte[] data = new byte[MetaMessage.TempoLength]; // If this platform uses little endian byte order. if(BitConverter.IsLittleEndian) { int d = data.Length - 1; // Unpack tempo. for(int i = 0; i < data.Length; i++) { data[d] = (byte)(tempo >> (Shift * i)); d--; } } // Else this platform uses big endian byte order. else { // Unpack tempo. for(int i = 0; i < data.Length; i++) { data[i] = (byte)(tempo >> (Shift * i)); } } changed = false; result = new MetaMessage(MetaType.Tempo, data); } }
private void ProcessMIDI() { Midi.Sequence _MIDISequence; // default MIDI tempos/milliseconds per tick int tempo = 500000; float msPerTick = (tempo / 48) / 1000.0f; _MIDISequence = new Midi.Sequence(); _MIDISequence.Load(_sequence.MusicFile); if (_MIDISequence.SequenceType != Sanford.Multimedia.Midi.SequenceType.Ppqn) { MessageBox.Show("Unsupported MIDI type...sorry!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } foreach (Midi.Track t in _MIDISequence) { IEnumerator <Midi.MidiEvent> en = t.Iterator().GetEnumerator(); while (en.MoveNext()) { Midi.MidiEvent e = en.Current; switch (e.MidiMessage.MessageType) { // starta new channel case Sanford.Multimedia.Midi.MessageType.Channel: Midi.ChannelMessage channel = (Midi.ChannelMessage)e.MidiMessage; // if it's a note on command and it's in our mapping list if (channel.Command == Sanford.Multimedia.Midi.ChannelCommand.NoteOn && _midiMap.ContainsKey(channel.MidiChannel) && (int)((e.AbsoluteTicks * msPerTick) / 50) < _sequence.Channels[_midiMap[channel.MidiChannel]].Data.Length) { // this means the note is on if (channel.Data2 > 0) { _sequence.Channels[_midiMap[channel.MidiChannel]].Data[(int)((e.AbsoluteTicks * msPerTick) / 50)] = true; } else { // the note is off _sequence.Channels[_midiMap[channel.MidiChannel]].Data[(int)((e.AbsoluteTicks * msPerTick) / 50)] = false; if (chkHold.Checked) { // backfill the grid for (int i = (int)((e.AbsoluteTicks * msPerTick) / 50); i > 0 && !_sequence.Channels[_midiMap[channel.MidiChannel]].Data[i]; i--) { _sequence.Channels[_midiMap[channel.MidiChannel]].Data[i] = true; } } } } // true note off...don't see this used much if (channel.Command == Sanford.Multimedia.Midi.ChannelCommand.NoteOff && _midiMap.ContainsKey(channel.MidiChannel)) { _sequence.Channels[_midiMap[channel.MidiChannel]].Data[(int)((e.AbsoluteTicks * msPerTick) / 50)] = false; if (chkHold.Checked) { for (int i = (int)((e.AbsoluteTicks * msPerTick) / 50); i > 0 && !_sequence.Channels[_midiMap[channel.MidiChannel]].Data[i]; i--) { _sequence.Channels[_midiMap[channel.MidiChannel]].Data[i] = true; } } } break; case Sanford.Multimedia.Midi.MessageType.Meta: Midi.MetaMessage meta = (Midi.MetaMessage)e.MidiMessage; switch (meta.MetaType) { // again, get the tempo value case Sanford.Multimedia.Midi.MetaType.Tempo: tempo = meta.GetBytes()[0] << 16 | meta.GetBytes()[1] << 8 | meta.GetBytes()[2]; msPerTick = (tempo / _MIDISequence.Division) / 1000.0f; break; } break; } } } }
/// <summary> /// Builds the key signature MetaMessage. /// </summary> public void Build() { byte[] data = new byte[MetaMessage.KeySigLength]; unchecked { switch(Key) { case Key.CFlatMajor: data[0] = (byte)-7; data[1] = 0; break; case Key.GFlatMajor: data[0] = (byte)-6; data[1] = 0; break; case Key.DFlatMajor: data[0] = (byte)-5; data[1] = 0; break; case Key.AFlatMajor: data[0] = (byte)-4; data[1] = 0; break; case Key.EFlatMajor: data[0] = (byte)-3; data[1] = 0; break; case Key.BFlatMajor: data[0] = (byte)-2; data[1] = 0; break; case Key.FMajor: data[0] = (byte)-1; data[1] = 0; break; case Key.CMajor: data[0] = 0; data[1] = 0; break; case Key.GMajor: data[0] = 1; data[1] = 0; break; case Key.DMajor: data[0] = 2; data[1] = 0; break; case Key.AMajor: data[0] = 3; data[1] = 0; break; case Key.EMajor: data[0] = 4; data[1] = 0; break; case Key.BMajor: data[0] = 5; data[1] = 0; break; case Key.FSharpMajor: data[0] = 6; data[1] = 0; break; case Key.CSharpMajor: data[0] = 7; data[1] = 0; break; case Key.AFlatMinor: data[0] = (byte)-7; data[1] = 1; break; case Key.EFlatMinor: data[0] = (byte)-6; data[1] = 1; break; case Key.BFlatMinor: data[0] = (byte)-5; data[1] = 1; break; case Key.FMinor: data[0] = (byte)-4; data[1] = 1; break; case Key.CMinor: data[0] = (byte)-3; data[1] = 1; break; case Key.GMinor: data[0] = (byte)-2; data[1] = 1; break; case Key.DMinor: data[0] = (byte)-1; data[1] = 1; break; case Key.AMinor: data[0] = 1; data[1] = 0; break; case Key.EMinor: data[0] = 1; data[1] = 1; break; case Key.BMinor: data[0] = 2; data[1] = 1; break; case Key.FSharpMinor: data[0] = 3; data[1] = 1; break; case Key.CSharpMinor: data[0] = 4; data[1] = 1; break; case Key.GSharpMinor: data[0] = 5; data[1] = 1; break; case Key.DSharpMinor: data[0] = 6; data[1] = 1; break; case Key.ASharpMinor: data[0] = 7; data[1] = 1; break; } } result = new MetaMessage(MetaType.KeySignature, data); }
/// <summary> /// Initializes the KeySignatureBuilder with the specified MetaMessage. /// </summary> /// <param name="message"> /// The key signature MetaMessage to use for initializing the /// KeySignatureBuilder. /// </param> public void Initialize(MetaMessage message) { #region Require if(message == null) { throw new ArgumentNullException("message"); } else if(message.MetaType != MetaType.KeySignature) { throw new ArgumentException("Wrong meta event type.", "messaege"); } #endregion sbyte b = (sbyte)message[0]; // If the key is major. if(message[1] == 0) { switch(b) { case -7: key = Key.CFlatMajor; break; case -6: key = Key.GFlatMajor; break; case -5: key = Key.DFlatMajor; break; case -4: key = Key.AFlatMajor; break; case -3: key = Key.EFlatMajor; break; case -2: key = Key.BFlatMajor; break; case -1: key = Key.FMajor; break; case 0: key = Key.CMajor; break; case 1: key = Key.GMajor; break; case 2: key = Key.DMajor; break; case 3: key = Key.AMajor; break; case 4: key = Key.EMajor; break; case 5: key = Key.BMajor; break; case 6: key = Key.FSharpMajor; break; case 7: key = Key.CSharpMajor; break; } } // Else the key is minor. else { switch(b) { case -7: key = Key.AFlatMinor; break; case -6: key = Key.EFlatMinor; break; case -5: key = Key.BFlatMinor; break; case -4: key = Key.FMinor; break; case -3: key = Key.CMinor; break; case -2: key = Key.GMinor; break; case -1: key = Key.DMinor; break; case 0: key = Key.AMinor; break; case 1: key = Key.EMinor; break; case 2: key = Key.BMinor; break; case 3: key = Key.FSharpMinor; break; case 4: key = Key.CSharpMinor; break; case 5: key = Key.GSharpMinor; break; case 6: key = Key.DSharpMinor; break; case 7: key = Key.ASharpMinor; break; } } }
/// <summary> /// Initializes the TempoChangeBuilder with the specified MetaMessage. /// </summary> /// <param name="message"> /// The MetaMessage to use for initializing the TempoChangeBuilder. /// </param> /// <exception cref="ArgumentException"> /// If the specified MetaMessage is not a tempo type. /// </exception> public void Initialize(MetaMessage e) { #region Require if(e == null) { throw new ArgumentNullException("e"); } else if(e.MetaType != MetaType.Tempo) { throw new ArgumentException("Wrong meta message type.", "e"); } #endregion int t = 0; // If this platform uses little endian byte order. if(BitConverter.IsLittleEndian) { int d = e.Length - 1; // Pack tempo. for(int i = 0; i < e.Length; i++) { t |= e[d] << (Shift * i); d--; } } // Else this platform uses big endian byte order. else { // Pack tempo. for(int i = 0; i < e.Length; i++) { t |= e[i] << (Shift * i); } } tempo = t; }
/// <summary> /// Initialize a new instance of the TempoChangeBuilder class with the /// specified MetaMessage. /// </summary> /// <param name="message"> /// The MetaMessage to use for initializing the TempoChangeBuilder class. /// </param> /// <exception cref="ArgumentException"> /// If the specified MetaMessage is not a tempo type. /// </exception> /// <remarks> /// The TempoChangeBuilder uses the specified MetaMessage to initialize /// its property values. /// </remarks> public TempoChangeBuilder(MetaMessage e) { Initialize(e); }
private void Write(MetaMessage message, int trackid, int ticks) { string sy = string.Empty; //trackData.Add((byte)message.Status); //trackData.Add((byte)message.MetaType); //WriteVariableLengthValue(message.Length); //trackData.AddRange(message.GetBytes()); switch (message.MetaType) { case MetaType.Copyright: break; case MetaType.CuePoint: break; case MetaType.DeviceName: break; case MetaType.EndOfTrack: break; case MetaType.InstrumentName: break; case MetaType.KeySignature: break; case MetaType.Lyric: // Track, Time, Lyric_t, Text byte[] b = message.GetBytes(); // Replace byte value 13 (cr) by antislash for (int i = 0; i < b.Length; i++) { if (b[i] == 13) { b[i] = 92; } } sy = System.Text.Encoding.Default.GetString(b); // comma is the separator : remove all comma ? //sy = sy.Replace(',', '-'); stream.WriteLine(string.Format("{0}, {1}, Lyric_t, \"{2}\"", trackid, ticks, sy)); break; case MetaType.Marker: break; case MetaType.ProgramName: break; case MetaType.ProprietaryEvent: break; case MetaType.SequenceNumber: break; case MetaType.SmpteOffset: break; case MetaType.Tempo: break; case MetaType.Text: // Track, Time, Text_t, Text //sy = System.Text.Encoding.Default.GetString(message.GetBytes()).Trim(); sy = System.Text.Encoding.Default.GetString(message.GetBytes()); sy = sy.Replace("\r\n", "\r"); sy = sy.Replace("\r", "\\"); sy = sy.Replace("\n", "\\"); // comma is the separator : remove all comma ? //sy = sy.Replace(',', '-'); stream.WriteLine(string.Format("{0}, {1}, Text_t, \"{2}\"", trackid, ticks, sy)); break; case MetaType.TimeSignature: break; case MetaType.TrackName: break; } }
private void Write(int deltaTicks, MetaMessage message) { if(message.MetaType == MetaType.Tempo) { // Delta time. events.AddRange(BitConverter.GetBytes(deltaTicks + offsetTicks)); // Stream ID. events.AddRange(streamID); TempoChangeBuilder builder = new TempoChangeBuilder(message); byte[] t = BitConverter.GetBytes(builder.Tempo); t[t.Length - 1] = MEVT_SHORTMSG | MEVT_TEMPO; // Event code. events.AddRange(t); offsetTicks = 0; } else { offsetTicks += deltaTicks; } }
public void Process(MetaMessage message) { #region Require if(message == null) { throw new ArgumentNullException("message"); } #endregion #region Guard if(message.MetaType != MetaType.Tempo) { return; } #endregion TempoChangeBuilder builder = new TempoChangeBuilder(message); // Set the new tempo. Tempo = builder.Tempo; }
/// <summary> /// Initializes the TimeSignatureBuilder with the specified MetaMessage. /// </summary> /// <param name="message"> /// The MetaMessage to use for initializing the TimeSignatureBuilder. /// </param> /// <exception cref="ArgumentException"> /// If the specified MetaMessage is not a time signature type. /// </exception> public void Initialize(MetaMessage message) { #region Require if(message.MetaType != MetaType.TimeSignature) { throw new ArgumentException("Wrong meta event type.", "message"); } #endregion data = message.GetBytes(); }
/// <summary> /// Builds the time signature MetaMessage. /// </summary> public void Build() { // If any of the properties have changed since the last time the // message was built. if(changed) { result = new MetaMessage(MetaType.TimeSignature, data); changed = false; } }