public MIDIPlayer(Context context, MIDIStyle style) { this.context = context; Style = style; MIDISession.GetInstance().Init(context); MIDISession.GetInstance().Start(); Bundle rinfo = new Bundle(); rinfo.PutString(MIDIConstants.RINFO_ADDR, "192.168.1.118"); //rinfo.PutString(MIDIConstants.RINFO_ADDR, "192.168.2.229"); rinfo.PutInt(MIDIConstants.RINFO_PORT, 5008); rinfo.PutBoolean(MIDIConstants.RINFO_RECON, true); MIDISession.GetInstance().Connect(rinfo); currentSongPosition = 0; СurrentTempo = 120; // 120BPM msOnPulse = 60000000 / СurrentTempo; // 500 000 pulsesPerQuarterNote = style.MidiSection.MidiHeaderInfo.Ticks; thread = new System.Threading.Thread(new ThreadStart(Run)); thread.Start(); isPlaying = false; currentPressedNotes = new List <byte>(); Subscribe(); }
public OutDataBuffer GeneratePayload() { OutDataBuffer buffer = new OutDataBuffer(); int firstByte = 0; firstByte |= this.version << 6; firstByte |= this.padding ? 0x20 : 0; firstByte |= this.hasExtension ? 0x10 : 0; // csrcs = 0... so just skip this // firstByte |= (this.csrcs.length > 15 ? 15 : this.csrcs.length); int secondByte = this.payloadType | (this.marker ? 0x80 : 0); buffer.Write8(firstByte); buffer.Write8(secondByte); buffer.Write16(SequenceNumber); long t = MIDISession.GetInstance().GetNow(); // Log.e("RTPMessage","t:"+t+" t8:"+(t >>> 8)+" t16:"+(t >>>16)+" tint:"+(int)t); // timestamp = (int)t >>> 8; timestamp = (int)t; buffer.Write32(timestamp << 0); buffer.Write32(ssrc); return(buffer); }
private void SendInvitationRejected(Bundle rinfo) { MIDIControl message = new MIDIControl(); message.CreateInvitationRejected(this.initiator_token, MIDISession.GetInstance().ssrc, MIDISession.GetInstance().bonjourName); MIDISession.GetInstance().SendUDPMessage(message, rinfo); }
protected void PlayCurrentMarkerPositions() { MIDISession session = MIDISession.GetInstance(); foreach (MIDITrack track in Tracks) { // for (int i = track.CurrentEventIndex; i < track.MidiEvents.Count; i++) for (int i = CurrentMarker.StartIndex; i < CurrentMarker.StopIndex; i++) { if (track.MidiEvents[i].absTime == currentSongPosition) { if (track.MidiEvents[i].MidiMessage[0] == 0xFF) { ProcessMetaEvent(track.MidiEvents[i].MidiMessage); } else { session.SendMessage(Style.ProcessMessage(track, i)); } } else if (track.MidiEvents[i].absTime > currentSongPosition) { track.CurrentEventIndex = i; break; } } } }
protected void AllNotesOff() { MIDISession session = MIDISession.GetInstance(); byte[] b = new byte[3] { 0, 123, 0 }; for (byte i = 0xB0; i < 0xC0; i++) { b[0] = i; session.SendMessage(b); } }
private void SendInvitationAccepted(Bundle rinfo) { MIDIControl message = new MIDIControl(); message.CreateInvitationAccepted(initiator_token, MIDISession.GetInstance().ssrc, MIDISession.GetInstance().bonjourName); MIDISession.GetInstance().SendUDPMessage(message, rinfo); if (!syncStarted && rinfo2 != null) { syncStarted = true; MessagingCenter.Send <SyncronizeStartedEvent>(new SyncronizeStartedEvent(this.rinfo1), "SyncronizeStartedEvent"); } }
public void SendInvitation(Bundle rinfo) { if (DEBUG) { Log.Debug("MIDIStream", "sendInvitation " + rinfo.GetString(MIDIConstants.RINFO_ADDR) + ":" + rinfo.GetInt(MIDIConstants.RINFO_PORT)); } MIDIControl invite = new MIDIControl(); invite.CreateInvitation(initiator_token, MIDISession.GetInstance().ssrc, MIDISession.GetInstance().bonjourName); // invite.dumppacket(); MIDISession.GetInstance().SendUDPMessage(invite, rinfo); MessagingCenter.Send <MIDIConnectionSentRequestEvent>(new MIDIConnectionSentRequestEvent(rinfo), "MIDIConnectionSentRequestEvent"); }
private void HandleInvitation(MIDIControl control, Bundle rinfo) { if (rinfo1 == null) { rinfo1 = (Bundle)rinfo.Clone(); this.initiator_token = control.initiator_token; this.name = control.name; this.ssrc = control.ssrc; rinfo1.PutString(midi.MIDIConstants.RINFO_NAME, control.name); MessagingCenter.Send <MIDIConnectionRequestReceivedEvent>(new MIDIConnectionRequestReceivedEvent(rinfo1), "MIDIConnectionRequestReceivedEvent"); } else if (rinfo.GetInt(midi.MIDIConstants.RINFO_PORT) == rinfo1.GetInt(midi.MIDIConstants.RINFO_PORT)) { if (MIDISession.GetInstance().IsHostConnectionAllowed(rinfo)) { this.SendInvitationAccepted(rinfo); } else { this.SendInvitationRejected(rinfo); } return; } else if (rinfo2 == null) { rinfo2 = (Bundle)rinfo.Clone(); this.isConnected = true; } else { return; } // this needs work... if (MIDISession.GetInstance().IsHostConnectionAllowed(rinfo)) { this.SendInvitationAccepted(rinfo); } else { this.SendInvitationRejected(rinfo); } }
public void SendEnd(Bundle rinfo) { Log.Debug(TAG, "send end " + rinfo.ToString()); MIDIControl message = new MIDIControl(); message.CreateEnd(this.initiator_token, MIDISession.GetInstance().ssrc, MIDISession.GetInstance().bonjourName); MIDISession.GetInstance().SendUDPMessage(message, rinfo); if (isConnected) { MessagingCenter.Send <StreamDisconnectEvent>(new StreamDisconnectEvent(ssrc, (Bundle)this.rinfo1.Clone()), "StreamDisconnectEvent"); } else { MessagingCenter.Send <StreamDisconnectEvent>(new StreamDisconnectEvent(ssrc, initiator_token, (Bundle)this.rinfo1.Clone()), "StreamDisconnectEvent"); } isConnected = false; }
public void SendSynchronization(MIDIControl inboundSyncMessage) { long now = MIDISession.GetInstance().GetNow(); MIDIControl outboundSyncMessage; int count = (inboundSyncMessage != null) ? inboundSyncMessage.count : -1; if (count == 3) { count = -1; } outboundSyncMessage = new MIDIControl(); outboundSyncMessage.CreateSyncronization( MIDISession.GetInstance().ssrc, count + 1, (count != -1) ? inboundSyncMessage.timestamp1 : 0, (count != -1) ? inboundSyncMessage.timestamp2 : 0, (count != -1) ? inboundSyncMessage.timestamp3 : 0); switch (count) { case -1: // send count:0 outboundSyncMessage.timestamp1 = now; outboundSyncMessage.timestamp2 = (timeDifference != 0L) ? now - timeDifference : 0L; outboundSyncMessage.timestamp3 = (timeDifference != 0L) ? now + timeDifference : 0L; break; case 0: // received count:0, respond count:1 outboundSyncMessage.timestamp2 = now; outboundSyncMessage.timestamp3 = now - timeDifference; break; case 1: // received count:1, respond count:2 // outboundSyncMessage.sync_timestamp3 = now; // latency = inboundSyncMessage.sync_timestamp3 - inboundSyncMessage.sync_timestamp1; // timeDifference = Math.round(inboundSyncMessage.sync_timestamp3-inboundSyncMessage.sync_timestamp2) - latency; timeDifference = (inboundSyncMessage.timestamp3 - inboundSyncMessage.timestamp1) / 2; timeDifference = inboundSyncMessage.timestamp3 + timeDifference - now; outboundSyncMessage.timestamp3 = now; break; case 2: // received count:2, sending nothing /* compute media delay */ // diff = ( command->data.sync.timestamp3 - command->data.sync.timestamp1 ) / 2; /* approximate time difference between peer and self */ // diff = command->data.sync.timestamp3 + diff - timestamp; timeDifference = (inboundSyncMessage.timestamp3 - inboundSyncMessage.timestamp1) / 2; timeDifference = inboundSyncMessage.timestamp3 + timeDifference - now; break; default: break; } if (outboundSyncMessage.count < 3) { MIDISession.GetInstance().SendUDPMessage(outboundSyncMessage, rinfo2); if (!primarySyncComplete && ++syncCount > 3) { primarySyncComplete = true; MessagingCenter.Send <SyncronizeStoppedEvent>(new SyncronizeStoppedEvent(this.rinfo1), "SyncronizeStoppedEvent"); MessagingCenter.Send <ConnectionEstablishedEvent>(new ConnectionEstablishedEvent(rinfo1), "ConnectionEstablishedEvent"); } } }
public void SendMessage(MIDIMessage m) { this.lastSentSequenceNr = (this.lastSentSequenceNr + 1) % 0x10000; m.SequenceNumber = this.lastSentSequenceNr; MIDISession.GetInstance().SendUDPMessage(m, rinfo2); }