private static void set_measures(Sequence sequence, D_Staff staff) { // This function requires staff.num_of_beats to be set! // TimeSignature messages are in channel 0 List <Tuple <MidiEvent, MetaMessage> > meta_messages = getMetaMessages(sequence[0]); foreach (Tuple <MidiEvent, MetaMessage> metaPair in meta_messages) { MidiEvent midiEvent = metaPair.Item1; MetaMessage metaMessage = metaPair.Item2; byte[] meta_bytes = metaMessage.GetBytes(); // Time signature if (metaMessage.MetaType == MetaType.TimeSignature) { int top_number = meta_bytes[0]; int bottom_number = (int)(Math.Pow(2, meta_bytes[1])); int start_beat = midiEvent.AbsoluteTicks / sequence.Division; staff.addMeasure(top_number, bottom_number, start_beat); } } staff.setMeasureEndTimes(); }
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; Tempo = Tempo; }
private async Task <string> GetWsdlFromMetaBodyWriter <T>(SoapSerializer serializer) { var service = new ServiceDescription(typeof(T)); var baseUrl = "http://tempuri.org/"; var xmlNamespaceManager = Namespaces.CreateDefaultXmlNamespaceManager(); var bodyWriter = serializer == SoapSerializer.DataContractSerializer ? new MetaWCFBodyWriter(service, baseUrl, null) as BodyWriter : new MetaBodyWriter(service, baseUrl, null, xmlNamespaceManager) as BodyWriter; var encoder = new SoapMessageEncoder(MessageVersion.Soap12WSAddressingAugust2004, System.Text.Encoding.UTF8, XmlDictionaryReaderQuotas.Max, false, true); var responseMessage = Message.CreateMessage(encoder.MessageVersion, null, bodyWriter); responseMessage = new MetaMessage(responseMessage, service, null, xmlNamespaceManager); using (var memoryStream = new MemoryStream()) { await encoder.WriteMessageAsync(responseMessage, memoryStream); memoryStream.Position = 0; using (var streamReader = new StreamReader(memoryStream)) { var result = streamReader.ReadToEnd(); return(result); } } }
private void KeySigParsed(MetaMessage meta) { byte scale = (byte)(meta.GetBytes()[1] == 0 ? ScaleType.Major : ScaleType.Minor); OnKeySignatureParsed(KeyProviderFactory.GetKeyProvider().ConvertAccidentalCountToKeyRootPositionInOctave( meta.GetBytes()[0], scale), scale); }
private void SetTimeSignature(MetaMessage metaMessage, StringBuilder lilypondContent) { byte[] timeSignatureBytes = metaMessage.GetBytes(); _beatNote = timeSignatureBytes[0]; _beatsPerBar = (int)(1 / Math.Pow(timeSignatureBytes[1], -2)); lilypondContent.AppendLine($"\\time {_beatNote}/{_beatsPerBar}"); }
public void TestTimeSignature() { int numerator = 4; int denominator = 4; byte ticksPerBeat = 24; byte thirtySecondNotesPerBeat = 8; TimeSignatureMetaMessage message = new TimeSignatureMetaMessage( numerator, denominator, ticksPerBeat, thirtySecondNotesPerBeat, 0 ); int subdivision = (int)(Math.Log(denominator) / Math.Log(2)); MetaMessage bytes = message.MetaMessage; MetaMessage expected = new MetaMessage { MetaType = 0x58, Data = new byte[] { (byte)numerator, (byte)subdivision, ticksPerBeat, thirtySecondNotesPerBeat }, TimeDelta = 0 }; Assert.Equal(expected, bytes); }
private static void WriteMetaMessage(string receiver, string sender, MetaMessage message) { Console.Write("META MESSAGE: "); switch (message.State) { case MetaState.Added: Console.WriteLine($"Added {message.ChatMember}"); break; case MetaState.Removed: Console.WriteLine($"Removed {message.ChatMember}"); break; case MetaState.Online: Console.WriteLine($"{message.ChatMember} is online"); break; case MetaState.Offline: Console.WriteLine($"{message.ChatMember} is offline"); break; default: throw new ArgumentOutOfRangeException(); } }
public void TestKeySignature() { // D major sbyte sharpsFlats = 2; byte majorMinor = 0; KeySignature keySignatureExpected = new KeySignature(Tonic.D, Scale.MAJOR); KeySignatureMetaMessage message1 = new KeySignatureMetaMessage( keySignatureExpected, 0); KeySignatureMetaMessage message2 = new KeySignatureMetaMessage( sharpsFlats, majorMinor, 0); Assert.True(message2.KeySignature.Equals(keySignatureExpected)); MetaMessage bytes1 = message1.MetaMessage; MetaMessage bytes2 = message2.MetaMessage; MetaMessage expected = new MetaMessage { MetaType = 0x59, Data = new byte[] { (byte)sharpsFlats, majorMinor }, TimeDelta = 0 }; Assert.Equal(expected, bytes1); Assert.Equal(expected, bytes2); }
private void ProcessTempo(MetaMessage message) { var b = message.GetBytes(); var tempoBytes = new[] { b[2], b[1], b[0], (byte)0x00 }; this._tempo = 60000000.0 / BitConverter.ToInt32(tempoBytes, 0); }
/// <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 static string GetMetaString(MetaMessage metaMessage) { byte[] bytes = metaMessage.GetBytes(); switch (metaMessage.MetaType) { case MetaType.Tempo: // Bitshifting is nodig om het tempo in BPM te be int tempo = (bytes[0] & 0xff) << 16 | (bytes[1] & 0xff) << 8 | (bytes[2] & 0xff); int bpm = 60000000 / tempo; return(metaMessage.MetaType + ": " + bpm); //case MetaType.SmpteOffset: // break; case MetaType.TimeSignature: //kwart = 1 / 0.25 = 4 return(metaMessage.MetaType + ": (" + bytes[0] + " / " + 1 / Math.Pow(bytes[1], -2) + ") "); //case MetaType.KeySignature: // break; //case MetaType.ProprietaryEvent: // break; case MetaType.TrackName: return(metaMessage.MetaType + ": " + Encoding.Default.GetString(metaMessage.GetBytes())); default: return(metaMessage.MetaType + ": " + Encoding.Default.GetString(metaMessage.GetBytes())); } }
private void ParseMetaMessage(MetaMessage message, MidiEvent @event) { switch (message.MetaType) { case MetaType.Lyric: OnLyricParsed(Encoding.ASCII.GetString(message.GetBytes())); HandledMidiEvent?.Invoke(this, new MidiEventArgs(@event)); break; case MetaType.Marker: OnMarkerParsed(Encoding.ASCII.GetString(message.GetBytes())); HandledMidiEvent?.Invoke(this, new MidiEventArgs(@event)); break; case MetaType.Tempo: WhenTempoChanged(message); HandledMidiEvent?.Invoke(this, new MidiEventArgs(@event)); break; case MetaType.TimeSignature: OnTimeSignatureParsed(message.GetBytes()[0], message.GetBytes()[1]); HandledMidiEvent?.Invoke(this, new MidiEventArgs(@event)); break; case MetaType.KeySignature: KeySigParsed(message); HandledMidiEvent?.Invoke(this, new MidiEventArgs(@event)); break; default: UnhandledMidiEvent?.Invoke(this, new MidiEventArgs(@event)); break; } }
private void ProcessTempoMessage(MidiEvent evt, MetaMessage msg) { var msgBytes = msg.GetBytes(); int microSecondsPB = (msgBytes[0] << 16 | msgBytes[1] << 8 | msgBytes[2]); int BPM = 60_000_000 / microSecondsPB; composition.Tokens.Add(new TempoBuilder().WithBPM(BPM).Build()); }
private void SetTempo(MetaMessage metaMessage, StringBuilder lilypondContent) { byte[] tempoBytes = metaMessage.GetBytes(); int tempo = (tempoBytes[0] & 0xff) << 16 | (tempoBytes[1] & 0xff) << 8 | (tempoBytes[2] & 0xff); _bpm = 60000000 / tempo; lilypondContent.AppendLine($"\\tempo 4={_bpm}"); }
/// <summary> /// Test if the message data can be edited in the track editor /// </summary> /// <param name="mess">the message to test</param> /// <returns><code>true</code> if the message data can be edited</returns> public static bool IsEditable(this MetaMessage mess) { int type = mess.GetMetaMessageType(); return((type >= 1 && type <= 9) || // MetaEvent between TextEvent (0x01) and DeviceName (0x09), see MidiHelper.MetaEventType type == (int)MidiHelper.MetaEventType.Tempo || type == (int)MidiHelper.MetaEventType.KeySignature); }
private void WhenTempoChanged(MetaMessage meta) { int newTempoMSPQ = (meta.GetBytes()[2] & 0xFF) | ((meta.GetBytes()[1] & 0xFF) << 8) | ((meta.GetBytes()[0] & 0xFF) << 16); this.tempoBPM = newTempoMSPQ = 60000000 / newTempoMSPQ; OnTempoChanged(tempoBPM); }
private async void OnSendNormalMessagePressed(object sender, EventArgs e) { string recipientUuid = RecipientUuid.Text; string messageText = Message.Text; MetaMessage textMessage = MessageUtils.ComposeNormalTextMessage(recipientUuid, messageText); await _connection.SendMessageAsync(textMessage); }
//private void HandleMarker() //{ // MetaMessage metaMessage = _midiEvent.MidiMessage as MetaMessage; // byte[] markerBytes = metaMessage.GetBytes(); // string markerText = System.Text.Encoding.UTF8.GetString(markerBytes); // Marker marker = (Marker)Enum.Parse(typeof(Marker), markerText); // this._midiStrategy.HandleMarker(_midiEvent.AbsoluteTicks, marker); //} private void HandleMarker() { MetaMessage metaMessage = _midiEvent.MidiMessage as MetaMessage; byte[] markersBytes = metaMessage.GetBytes(); List <Tuple <Marker, int> > markers = Deserialize(markersBytes); this._midiStrategy.HandleMarker(_midiEvent.AbsoluteTicks, markers); }
public void handleMessage(MetaMessage metaMessage, MidiInterpreter midi, Score score) { byte[] timeSignatureBytes = metaMessage.GetBytes(); var timeSignature = new TimeSignature(timeSignatureBytes[0], (Length)(int)Math.Pow(2, timeSignatureBytes[1])); midi.beatsPerMessage = timeSignature.beatsPerMeasure; midi.timeSignatureLengthNote = timeSignature.lengthOfOneBeat; score.staffsInScore.Last().bars.Last().notes.Add(timeSignature); }
private void Write(MetaMessage message) { trackData.Add((byte)message.Status); trackData.Add((byte)message.MetaType); WriteVariableLengthValue(message.Length); trackData.AddRange(message.GetBytes()); }
public void handleMessage(MetaMessage metaMessage, MidiInterpreter midi, Score score) { byte[] tempoBytes = metaMessage.GetBytes(); int tempo = (tempoBytes[0] & 0xff) << 16 | (tempoBytes[1] & 0xff) << 8 | (tempoBytes[2] & 0xff); var bpm = 60000000 / tempo; var metronome = new Metronome(Length.Quarter, bpm); score.staffsInScore.Last().bars.Last().notes.Add(metronome); }
/// <summary> /// Create a Midi Meta event /// </summary> /// <param name="type">the type of the event as an int</param> /// <param name="data">a String that represents the data for the event.</param> /// <see cref="SetMetaData"/> for details. /// <param name="tick">the position of the event in the sequence</param> /// <param name="ticksPerBeat">the tick resolution of the sequence</param> /// <returns>the created Midi Meta event</returns> public static MidiEvent CreateMetaEvent(int type, string data, long tick, int ticksPerBeat) { var mm = new MetaMessage(); mm.SetMessage(type, null, 0); SetMetaData(mm, data, ticksPerBeat); var ev = new MidiEvent(mm, tick); return(ev); }
private string getTimeSignatureString(MetaMessage metaMessage, ref StringBuilder lilyString) { byte[] timeSignatureBytes = metaMessage.GetBytes(); _beatNote = timeSignatureBytes[0]; _beatsPerBar = (int)(1 / Math.Pow(timeSignatureBytes[1], -2)); string returnString = $"\\time {_beatNote}/{_beatsPerBar}"; lilyString.AppendLine(returnString); return(returnString); }
/// <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; } }
public void ProcessToken(TimeSignature timeSignature) { byte[] messageData = new byte[4]; messageData[0] = Convert.ToByte(timeSignature.Count); messageData[1] = Convert.ToByte(Math.Log(timeSignature.Denominator.Value, 2)); var metaMessage = new MetaMessage(MetaType.TimeSignature, messageData); metaTrack.Insert(currentAbsoluteTicks, metaMessage); currentTimeSignature = timeSignature; }
public override string GetFormattedLength() { Dictionary <int, int> tempoEvents = new Dictionary <int, int>(); // Ordered by: AbsoluteTicks, Tempo int id = 0; foreach (var t in sequence.Tracks) { foreach (var something in t.Iterator()) { if (something.MidiMessage.MessageType == MessageType.Meta) { MetaMessage meta = (MetaMessage)something.MidiMessage; if (meta.MetaType == MetaType.Tempo) { // I think, something.AbsoluteTicks is useful // As for getting the actual Tempo out of it... var bytes = meta.GetBytes(); // Apparently it's... backwards? byte[] tempoBytes = new byte[4]; tempoBytes[2] = bytes[0]; tempoBytes[1] = bytes[1]; tempoBytes[0] = bytes[2]; int tempo = BitConverter.ToInt32(tempoBytes, 0); // This seems legit tempoEvents[something.AbsoluteTicks] = tempo; } } } } TimeSpan time = TimeSpan.Zero; int lastTempo = sequence.FirstTempo; int lastTime = 0; foreach (var kvp in tempoEvents) { time += TimeSpan.FromSeconds((((double)(kvp.Key - lastTime) / sequence.Division) * lastTempo / 1000000d)); lastTempo = kvp.Value; lastTime = kvp.Key; } lastLength = sequence.GetLength(); // And then get the rest to the end if (time != TimeSpan.Zero) { time += TimeSpan.FromSeconds((((double)(lastLength - lastTime) / sequence.Division) * lastTempo / 1000000d)); } //time = TimeSpan.FromSeconds((int)((sequence.GetLength() / sequence.Division) * this.sequence.FirstTempo / 1000000f)); if (time == TimeSpan.Zero) { time = TimeSpan.FromSeconds((((double)lastLength / sequence.Division) * this.sequence.FirstTempo / 1000000d)); // Old method } lastDuration = time; return(time.ToString(@"mm\:ss")); }
private async void OnSendInitialMessagePressed(object sender, EventArgs e) { string recipientUuid = RecipientUuid.Text; string messageText = Message.Text; ChatPackage chatPackage = await _connection.GetChatPackage(recipientUuid); MetaMessage initialMessage = MessageUtils.ComposeInitialMessage(recipientUuid, chatPackage, messageText); await _connection.SendMessageAsync(initialMessage); }
public void Convert(MidiEvent midiEvent, ref Models.Track track) { MetaMessage metaMessage = midiEvent.MidiMessage as MetaMessage; MetaType metaType = metaMessage.MetaType; if (converters.ContainsKey(metaType)) { var converter = converters[metaType]; converter.Convert(midiEvent, ref track); } }
public void HandleMetaData(MidiEvent midiEvent) { _midiEvent = midiEvent; MetaMessage metaMessage = midiEvent.MidiMessage as MetaMessage; if (MetaTypesDictionary.TryGetValue(metaMessage.MetaType, out var action)) { action(); } }
private string getTempoString(MetaMessage metaMessage, ref StringBuilder lilyString) { byte[] tempoBytes = metaMessage.GetBytes(); int tempo = (tempoBytes[0] & 0xff) << 16 | (tempoBytes[1] & 0xff) << 8 | (tempoBytes[2] & 0xff); _bpm = 60000000 / tempo; string returnString = $"\\tempo 4={_bpm}"; lilyString.AppendLine(returnString); return(returnString); }