34
34
import java .util .regex .Matcher ;
35
35
import java .util .regex .Pattern ;
36
36
37
+ import javax .sound .midi .MidiMessage ;
38
+
37
39
import org .python .core .CompileMode ;
38
40
import org .python .core .CompilerFlags ;
39
41
import org .python .core .Py ;
@@ -122,6 +124,13 @@ private enum Mode {
122
124
private boolean detectedSmooth , detectedNoSmooth , detectedFullScreen ;
123
125
private PyObject processedStaticSketch ;
124
126
127
+ private static int argCount (final PyObject func ) {
128
+ if (!(func instanceof PyFunction )) {
129
+ return -1 ;
130
+ }
131
+ return ((PyBaseCode ) ((PyFunction ) func ).__code__ ).co_argcount ;
132
+ }
133
+
125
134
/**
126
135
* The Processing event handling functions can take 0 or 1 argument. This class represents such a
127
136
* function.
@@ -137,7 +146,7 @@ private abstract class EventFunction<T> {
137
146
138
147
public EventFunction (final String funcName ) {
139
148
func = (PyFunction ) interp .get (funcName );
140
- argCount = func == null ? - 1 : (( PyBaseCode ) ( func ). __code__ ). co_argcount ;
149
+ argCount = argCount ( func );
141
150
}
142
151
143
152
public void invoke () {
@@ -183,6 +192,18 @@ public void invoke(final T event) {
183
192
// Implement the oscP5 library's callback.
184
193
private PyObject oscEventMeth ;
185
194
195
+ // Implement themidibus callbacks.
196
+ private PyObject noteOn3Meth ,
197
+ noteOff3Meth ,
198
+ controllerChange3Meth ,
199
+ rawMidi1Meth ,
200
+ midiMessage1Meth ,
201
+ noteOn5Meth ,
202
+ noteOff5Meth ,
203
+ controllerChange5Meth ,
204
+ rawMidi3Meth ,
205
+ midiMessage3Meth ;
206
+
186
207
private SketchPositionListener sketchPositionListener ;
187
208
188
209
private void processSketch (final String scriptSource ) throws PythonSketchError {
@@ -566,6 +587,69 @@ public PyObject __call__(final PyObject[] args, final String[] kws) {
566
587
567
588
// oscP5 library callback.
568
589
oscEventMeth = interp .get ("oscEvent" );
590
+
591
+ // themidibus callbacks
592
+ PyObject meth ;
593
+ if ((meth = interp .get ("noteOn" )) != null ) {
594
+ switch (argCount (meth )) {
595
+ default :
596
+ throw new RuntimeException (
597
+ "only noteOn(channel, pitch, velocity) or "
598
+ + "noteOn(channel, pitch, velocity, timestamp, bus_name) "
599
+ + "are supported by Python Mode" );
600
+ case 3 :
601
+ noteOn3Meth = meth ;
602
+ break ;
603
+ case 5 :
604
+ noteOn5Meth = meth ;
605
+ }
606
+ }
607
+ if ((meth = interp .get ("noteOff" )) != null ) {
608
+ switch (argCount (meth )) {
609
+ case 1 :
610
+ throw new RuntimeException (
611
+ "only noteOff(channel, pitch, velocity) or "
612
+ + "noteOff(channel, pitch, velocity, timestamp, bus_name) "
613
+ + "are supported by Python Mode" );
614
+ case 3 :
615
+ noteOff3Meth = meth ;
616
+ break ;
617
+ case 5 :
618
+ noteOff5Meth = meth ;
619
+ }
620
+ }
621
+ if ((meth = interp .get ("controllerChange" )) != null ) {
622
+ switch (argCount (meth )) {
623
+ case 1 :
624
+ throw new RuntimeException (
625
+ "only controllerChange(channel, pitch, velocity) or "
626
+ + "controllerChange(channel, pitch, velocity, timestamp, bus_name) "
627
+ + "are supported by Python Mode" );
628
+ case 3 :
629
+ controllerChange3Meth = meth ;
630
+ break ;
631
+ case 5 :
632
+ controllerChange5Meth = meth ;
633
+ }
634
+ }
635
+ if ((meth = interp .get ("rawMidi" )) != null ) {
636
+ switch (argCount (meth )) {
637
+ case 1 :
638
+ rawMidi1Meth = meth ;
639
+ break ;
640
+ case 3 :
641
+ rawMidi3Meth = meth ;
642
+ }
643
+ }
644
+ if ((meth = interp .get ("midiMessage" )) != null ) {
645
+ switch (argCount (meth )) {
646
+ case 1 :
647
+ midiMessage1Meth = meth ;
648
+ break ;
649
+ case 3 :
650
+ midiMessage3Meth = meth ;
651
+ }
652
+ }
569
653
}
570
654
571
655
/*
@@ -1390,6 +1474,87 @@ public void oscEvent(final Object oscMessage) {
1390
1474
}
1391
1475
}
1392
1476
1477
+ // themidibus callbacks
1478
+ public void noteOn (final int channel , final int pitch , final int velocity ) {
1479
+ if (noteOn3Meth != null ) {
1480
+ noteOn3Meth .__call__ (pyint (channel ), pyint (pitch ), pyint (velocity ));
1481
+ }
1482
+ }
1483
+
1484
+ public void noteOn (
1485
+ final int channel ,
1486
+ final int pitch ,
1487
+ final int velocity ,
1488
+ final long time ,
1489
+ final String busName ) {
1490
+ if (noteOn5Meth != null ) {
1491
+ noteOn5Meth .__call__ (
1492
+ new PyObject [] {
1493
+ pyint (channel ), pyint (pitch ), pyint (velocity ), Py .newLong (time ), Py .newString (busName )
1494
+ });
1495
+ }
1496
+ }
1497
+
1498
+ public void noteOff (final int channel , final int pitch , final int velocity ) {
1499
+ if (noteOff3Meth != null ) {
1500
+ noteOff3Meth .__call__ (pyint (channel ), pyint (pitch ), pyint (velocity ));
1501
+ }
1502
+ }
1503
+
1504
+ public void noteOff (
1505
+ final int channel ,
1506
+ final int pitch ,
1507
+ final int velocity ,
1508
+ final long time ,
1509
+ final String busName ) {
1510
+ if (noteOff5Meth != null ) {
1511
+ noteOff5Meth .__call__ (
1512
+ new PyObject [] {
1513
+ pyint (channel ), pyint (pitch ), pyint (velocity ), Py .newLong (time ), Py .newString (busName )
1514
+ });
1515
+ }
1516
+ }
1517
+
1518
+ public void controllerChange (final int channel , final int number , final int value ) {
1519
+ if (controllerChange3Meth != null ) {
1520
+ controllerChange3Meth .__call__ (pyint (channel ), pyint (number ), pyint (value ));
1521
+ }
1522
+ }
1523
+
1524
+ public void controllerChange (
1525
+ final int channel , final int number , final int value , final long time , final String busName ) {
1526
+ if (controllerChange5Meth != null ) {
1527
+ controllerChange5Meth .__call__ (
1528
+ new PyObject [] {
1529
+ pyint (channel ), pyint (number ), pyint (value ), Py .newLong (time ), Py .newString (busName )
1530
+ });
1531
+ }
1532
+ }
1533
+
1534
+ public void rawMidi (final byte [] data ) {
1535
+ if (rawMidi1Meth != null ) {
1536
+ rawMidi1Meth .__call__ (Py .java2py (data ));
1537
+ }
1538
+ }
1539
+
1540
+ public void rawMidi (final byte [] data , final long time , final String busName ) {
1541
+ if (rawMidi3Meth != null ) {
1542
+ rawMidi3Meth .__call__ (Py .java2py (data ), Py .newLong (time ), Py .newString (busName ));
1543
+ }
1544
+ }
1545
+
1546
+ public void midiMessage (final MidiMessage msg ) {
1547
+ if (midiMessage1Meth != null ) {
1548
+ midiMessage1Meth .__call__ (Py .java2py (msg ));
1549
+ }
1550
+ }
1551
+
1552
+ public void midiMessage (final MidiMessage msg , final long time , final String busName ) {
1553
+ if (midiMessage3Meth != null ) {
1554
+ midiMessage3Meth .__call__ (Py .java2py (msg ), Py .newLong (time ), Py .newString (busName ));
1555
+ }
1556
+ }
1557
+
1393
1558
public void setSketchPositionListener (final SketchPositionListener sketchPositionListener ) {
1394
1559
this .sketchPositionListener = sketchPositionListener ;
1395
1560
}
0 commit comments