1
+ // Hardware: Mega 2560 R2 + Ethernet Shield
2
+
3
+ // These need to be included when using standard Ethernet
4
+ #include < SPI.h>
5
+ #include < WiFi.h>
6
+ #include < WiFiUdp.h>
7
+
8
+ #include " AppleMidi.h"
9
+
10
+ int status = WL_IDLE_STATUS;
11
+ char ssid[] = " yourNetwork" ; // your network SSID (name)
12
+ char pass[] = " secretPassword" ; // your network password (use for WPA, or use as key for WEP)
13
+ int keyIndex = 0 ; // your network key Index number (needed only for WEP)
14
+
15
+
16
+ unsigned long t0 = millis();
17
+
18
+ APPLEMIDI_CREATE_INSTANCE (WiFiUDP, AppleMIDI); // see definition in AppleMidi_Defs.h
19
+
20
+ // -----------------------------------------------------------------------------
21
+ //
22
+ // -----------------------------------------------------------------------------
23
+ void setup ()
24
+ {
25
+ // Serial communications and wait for port to open:
26
+ Serial.begin (115200 );
27
+ while (!Serial) {
28
+ ; // wait for serial port to connect. Needed for Leonardo only
29
+ }
30
+
31
+ Serial.print (" Getting IP address..." );
32
+
33
+
34
+ // check for the presence of the shield:
35
+ if (WiFi.status () == WL_NO_SHIELD) {
36
+ Serial.println (" WiFi shield not present" );
37
+ // don't continue:
38
+ while (true );
39
+ }
40
+
41
+ String fv = WiFi.firmwareVersion ();
42
+ if ( fv != " 1.1.0" )
43
+ Serial.println (" Please upgrade the firmware" );
44
+
45
+ // attempt to connect to Wifi network:
46
+ while ( status != WL_CONNECTED) {
47
+ Serial.print (" Attempting to connect to SSID: " );
48
+ Serial.println (ssid);
49
+ // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
50
+ status = WiFi.begin (ssid);
51
+
52
+ // wait 10 seconds for connection:
53
+ delay (10000 );
54
+ }
55
+
56
+ Serial.println ();
57
+ Serial.print (" IP address is " );
58
+ Serial.println (WiFi.localIP ());
59
+
60
+ Serial.println (" OK, now make sure you an rtpMIDI session that is Enabled" );
61
+ Serial.print (" Add device named Arduino with Host/Port " );
62
+ Serial.print (WiFi.localIP ());
63
+ Serial.println (" :5004" );
64
+ Serial.println (" Then press the Connect button" );
65
+ Serial.println (" Then open a MIDI listener (eg MIDI-OX) and monitor incoming notes" );
66
+
67
+ // Create a session and wait for a remote host to connect to us
68
+ AppleMIDI.begin (" test" );
69
+
70
+ AppleMIDI.OnConnected (OnAppleMidiConnected);
71
+ AppleMIDI.OnDisconnected (OnAppleMidiDisconnected);
72
+
73
+ AppleMIDI.OnReceiveNoteOn (OnAppleMidiNoteOn);
74
+ AppleMIDI.OnReceiveNoteOff (OnAppleMidiNoteOff);
75
+
76
+ Serial.println (" Sending NoteOn/Off of note 45, every second" );
77
+ }
78
+
79
+ // -----------------------------------------------------------------------------
80
+ //
81
+ // -----------------------------------------------------------------------------
82
+ void loop ()
83
+ {
84
+ // Listen to incoming notes
85
+ AppleMIDI.run ();
86
+
87
+ // send a note every second
88
+ // (dont cáll delay(1000) as it will stall the pipeline)
89
+ if ((millis () - t0) > 1000 )
90
+ {
91
+ t0 = millis ();
92
+ // Serial.print(".");
93
+
94
+ int note = 45 ;
95
+ int velocity = 55 ;
96
+ int channel = 1 ;
97
+
98
+ AppleMIDI.noteOn (note, velocity, channel);
99
+ AppleMIDI.noteOff (note, velocity, channel);
100
+ }
101
+ }
102
+
103
+ // ====================================================================================
104
+ // Event handlers for incoming MIDI messages
105
+ // ====================================================================================
106
+
107
+ // -----------------------------------------------------------------------------
108
+ // rtpMIDI session. Device connected
109
+ // -----------------------------------------------------------------------------
110
+ void OnAppleMidiConnected (char * name) {
111
+ // Serial.print("Connected to session ");
112
+ // Serial.println(name);
113
+ }
114
+
115
+ // -----------------------------------------------------------------------------
116
+ // rtpMIDI session. Device disconnected
117
+ // -----------------------------------------------------------------------------
118
+ void OnAppleMidiDisconnected () {
119
+ // Serial.println("Disconnected");
120
+ }
121
+
122
+ // -----------------------------------------------------------------------------
123
+ //
124
+ // -----------------------------------------------------------------------------
125
+ void OnAppleMidiNoteOn (byte channel, byte note, byte velocity) {
126
+ Serial.print (" Incoming NoteOn from channel:" );
127
+ Serial.print (channel);
128
+ Serial.print (" note:" );
129
+ Serial.print (note);
130
+ Serial.print (" velocity:" );
131
+ Serial.print (velocity);
132
+ Serial.println ();
133
+ }
134
+
135
+ // -----------------------------------------------------------------------------
136
+ //
137
+ // -----------------------------------------------------------------------------
138
+ void OnAppleMidiNoteOff (byte channel, byte note, byte velocity) {
139
+ Serial.print (" Incoming NoteOff from channel:" );
140
+ Serial.print (channel);
141
+ Serial.print (" note:" );
142
+ Serial.print (note);
143
+ Serial.print (" velocity:" );
144
+ Serial.print (velocity);
145
+ Serial.println ();
146
+ }
0 commit comments