Skip to content

Commit 6569d94

Browse files
committed
method chaining update
1 parent f1782b6 commit 6569d94

4 files changed

Lines changed: 71 additions & 48 deletions

File tree

examples/MidiMapperTest_a/MidiMapperTest_a.pde

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sojamo.midimapper.*;
44

55
MidiMapper midi;
6+
AssignedDevice korg;
67

78
float n;
89
Test test;
@@ -21,7 +22,9 @@ void setup() {
2122
test = new Test();
2223

2324
/* connect Midi Mapper to a midi device and assign members of this sketch to midi controls. */
24-
midi.connect("SLIDER/KNOB", midi.assign(16).to("n"), midi.assign(17).to(test,"b"), midi.assign(18).to("c"));
25+
korg = midi.connect("SLIDER/KNOB");
26+
korg.assign(16).to("n").assign(17).to(test,"b").assign(18).to("c");
27+
2528
}
2629

2730

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package sojamo.midimapper;
22

33
import javax.sound.midi.MidiDevice;
4-
import javax.sound.midi.Receiver;
5-
import javax.sound.midi.Transmitter;
64

75
public class AssignedDevice {
86

@@ -12,13 +10,18 @@ public class AssignedDevice {
1210
AssignedDevice( Object theParent , MidiDevice theDevice ) {
1311
parent = theParent;
1412
device = theDevice;
15-
// for ( Receiver receiver : theReceivers ) {
16-
// Transmitter conTrans = device.getTransmitter( );
17-
// conTrans.setReceiver( receiver );
18-
// }
1913
}
20-
14+
2115
public MidiNote assign( int theNote ) {
22-
return new MidiNote( parent , theNote );
16+
return new MidiNote( this , parent , theNote );
17+
}
18+
19+
public MidiDevice get( ) {
20+
return device;
2321
}
22+
23+
public boolean exists( ) {
24+
return !device.equals( null );
25+
}
26+
2427
}

src/main/java/sojamo/midimapper/MidiMapper.java

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,20 @@ public MidiMapper( Object theObject ) {
3030
welcome( );
3131
}
3232

33-
public final boolean test( String theDevice ) {
33+
public final AssignedDevice test( String theDevice ) {
3434
return connect( theDevice , new TestReceiver( theDevice ) );
3535
}
3636

37-
public final boolean connect( int theDeviceId , Receiver ... theReceivers ) {
37+
public final AssignedDevice connect( int theDeviceId , Receiver ... theReceivers ) {
3838
out( "connect to id not yet implemented" );
39-
return false;
39+
return null;
4040
}
41-
41+
42+
public final AssignedDevice connect( int theDeviceId ) {
43+
out( "connect to id not yet implemented" );
44+
return null;
45+
}
46+
4247
public final AssignedDevice connect( String theDevice ) {
4348
try {
4449
MidiDevice device;
@@ -54,6 +59,32 @@ public final AssignedDevice connect( String theDevice ) {
5459
}
5560
}
5661

62+
public final AssignedDevice connect( String theDevice , Receiver ... theReceivers ) {
63+
64+
try {
65+
MidiDevice device;
66+
device = MidiSystem.getMidiDevice( getMidiDeviceInfo( theDevice , false ) );
67+
device.open( );
68+
69+
for ( Receiver receiver : theReceivers ) {
70+
Transmitter conTrans = device.getTransmitter( );
71+
conTrans.setReceiver( receiver );
72+
}
73+
return new AssignedDevice( parent , device );
74+
} catch ( MidiUnavailableException e ) {
75+
// TODO Auto-generated catch block
76+
e.printStackTrace( );
77+
return new AssignedDevice( parent , null );
78+
} catch ( NullPointerException e ) {
79+
log.info( String.format( "No Midi device ( %1s ) is available." , theDevice ) );
80+
return new AssignedDevice( parent , null );
81+
}
82+
}
83+
84+
private final void welcome( ) {
85+
log.info( String.format( "midimap, %1s" , VERSION ) );
86+
}
87+
5788
/* theData1 corresponds to the id of the midi message, theData2 is a midi value between 0-127 */
5889

5990
public MidiMapper send( int theChannel , int theData1 , int theData2 ) {
@@ -81,32 +112,6 @@ public MidiMapper send( Receiver theReceiver , int theCommand , int theChannel ,
81112
return this;
82113
}
83114

84-
public final boolean connect( String theDevice , Receiver ... theReceivers ) {
85-
86-
try {
87-
MidiDevice device;
88-
device = MidiSystem.getMidiDevice( getMidiDeviceInfo( theDevice , false ) );
89-
device.open( );
90-
91-
for ( Receiver receiver : theReceivers ) {
92-
Transmitter conTrans = device.getTransmitter( );
93-
conTrans.setReceiver( receiver );
94-
}
95-
} catch ( MidiUnavailableException e ) {
96-
// TODO Auto-generated catch block
97-
e.printStackTrace( );
98-
return false;
99-
} catch ( NullPointerException e ) {
100-
log.info( String.format( "No Midi device ( %1s ) is available." , theDevice ) );
101-
return false;
102-
}
103-
return true;
104-
}
105-
106-
private final void welcome( ) {
107-
log.info( String.format( "midimap, %1s" , VERSION ) );
108-
}
109-
110115
static public void list( ) {
111116
find( "" );
112117
}
@@ -133,10 +138,6 @@ static public void find( final String thePattern ) {
133138
log.info( msg.toString( ) );
134139
}
135140

136-
public MidiNote assign( int theNote ) {
137-
return new MidiNote( parent , theNote );
138-
}
139-
140141
static public Object invoke( final Object theObject , final String theMember , final Object ... theParams ) {
141142

142143
Class[] cs = new Class[ theParams.length ];
Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
11
package sojamo.midimapper;
22

3+
import javax.sound.midi.MidiUnavailableException;
4+
import javax.sound.midi.Transmitter;
5+
36
public class MidiNote {
47

8+
private final AssignedDevice device;
59
private final int note;
610
private final Object parent;
711

8-
public MidiNote( Object theParent , int theNote ) {
12+
public MidiNote( AssignedDevice theDevice , Object theParent , int theNote ) {
13+
device = theDevice;
914
parent = theParent;
1015
note = theNote;
1116
}
17+
18+
public AssignedDevice to( Object theTarget , String theMember ) {
19+
return to( new MidiReceiver( note , theTarget , theMember ) );
20+
}
1221

13-
public MidiReceiver to( Object theTarget , String theMember ) {
14-
return new MidiReceiver( note , theTarget , theMember );
22+
public AssignedDevice to( String theMember ) {
23+
return to( parent , theMember );
1524
}
1625

17-
public MidiReceiver to( String theMember ) {
18-
return to( parent , theMember );
26+
public AssignedDevice to( MidiReceiver theReceiver ) {
27+
try {
28+
Transmitter conTrans;
29+
conTrans = device.get( ).getTransmitter( );
30+
conTrans.setReceiver( theReceiver );
31+
} catch ( MidiUnavailableException e ) {
32+
e.printStackTrace( );
33+
} catch ( NullPointerException e ) {}
34+
return device;
1935
}
2036
}

0 commit comments

Comments
 (0)