Skip to content

Commit

Permalink
method chaining update
Browse files Browse the repository at this point in the history
  • Loading branch information
sojamo committed Feb 17, 2015
1 parent f1782b6 commit 6569d94
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 48 deletions.
5 changes: 4 additions & 1 deletion examples/MidiMapperTest_a/MidiMapperTest_a.pde
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sojamo.midimapper.*;

MidiMapper midi;
AssignedDevice korg;

float n;
Test test;
Expand All @@ -21,7 +22,9 @@ void setup() {
test = new Test();

/* connect Midi Mapper to a midi device and assign members of this sketch to midi controls. */
midi.connect("SLIDER/KNOB", midi.assign(16).to("n"), midi.assign(17).to(test,"b"), midi.assign(18).to("c"));
korg = midi.connect("SLIDER/KNOB");
korg.assign(16).to("n").assign(17).to(test,"b").assign(18).to("c");

}


Expand Down
19 changes: 11 additions & 8 deletions src/main/java/sojamo/midimapper/AssignedDevice.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package sojamo.midimapper;

import javax.sound.midi.MidiDevice;
import javax.sound.midi.Receiver;
import javax.sound.midi.Transmitter;

public class AssignedDevice {

Expand All @@ -12,13 +10,18 @@ public class AssignedDevice {
AssignedDevice( Object theParent , MidiDevice theDevice ) {
parent = theParent;
device = theDevice;
// for ( Receiver receiver : theReceivers ) {
// Transmitter conTrans = device.getTransmitter( );
// conTrans.setReceiver( receiver );
// }
}

public MidiNote assign( int theNote ) {
return new MidiNote( parent , theNote );
return new MidiNote( this , parent , theNote );
}

public MidiDevice get( ) {
return device;
}

public boolean exists( ) {
return !device.equals( null );
}

}
69 changes: 35 additions & 34 deletions src/main/java/sojamo/midimapper/MidiMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@ public MidiMapper( Object theObject ) {
welcome( );
}

public final boolean test( String theDevice ) {
public final AssignedDevice test( String theDevice ) {
return connect( theDevice , new TestReceiver( theDevice ) );
}

public final boolean connect( int theDeviceId , Receiver ... theReceivers ) {
public final AssignedDevice connect( int theDeviceId , Receiver ... theReceivers ) {
out( "connect to id not yet implemented" );
return false;
return null;
}


public final AssignedDevice connect( int theDeviceId ) {
out( "connect to id not yet implemented" );
return null;
}

public final AssignedDevice connect( String theDevice ) {
try {
MidiDevice device;
Expand All @@ -54,6 +59,32 @@ public final AssignedDevice connect( String theDevice ) {
}
}

public final AssignedDevice connect( String theDevice , Receiver ... theReceivers ) {

try {
MidiDevice device;
device = MidiSystem.getMidiDevice( getMidiDeviceInfo( theDevice , false ) );
device.open( );

for ( Receiver receiver : theReceivers ) {
Transmitter conTrans = device.getTransmitter( );
conTrans.setReceiver( receiver );
}
return new AssignedDevice( parent , device );
} catch ( MidiUnavailableException e ) {
// TODO Auto-generated catch block
e.printStackTrace( );
return new AssignedDevice( parent , null );
} catch ( NullPointerException e ) {
log.info( String.format( "No Midi device ( %1s ) is available." , theDevice ) );
return new AssignedDevice( parent , null );
}
}

private final void welcome( ) {
log.info( String.format( "midimap, %1s" , VERSION ) );
}

/* theData1 corresponds to the id of the midi message, theData2 is a midi value between 0-127 */

public MidiMapper send( int theChannel , int theData1 , int theData2 ) {
Expand Down Expand Up @@ -81,32 +112,6 @@ public MidiMapper send( Receiver theReceiver , int theCommand , int theChannel ,
return this;
}

public final boolean connect( String theDevice , Receiver ... theReceivers ) {

try {
MidiDevice device;
device = MidiSystem.getMidiDevice( getMidiDeviceInfo( theDevice , false ) );
device.open( );

for ( Receiver receiver : theReceivers ) {
Transmitter conTrans = device.getTransmitter( );
conTrans.setReceiver( receiver );
}
} catch ( MidiUnavailableException e ) {
// TODO Auto-generated catch block
e.printStackTrace( );
return false;
} catch ( NullPointerException e ) {
log.info( String.format( "No Midi device ( %1s ) is available." , theDevice ) );
return false;
}
return true;
}

private final void welcome( ) {
log.info( String.format( "midimap, %1s" , VERSION ) );
}

static public void list( ) {
find( "" );
}
Expand All @@ -133,10 +138,6 @@ static public void find( final String thePattern ) {
log.info( msg.toString( ) );
}

public MidiNote assign( int theNote ) {
return new MidiNote( parent , theNote );
}

static public Object invoke( final Object theObject , final String theMember , final Object ... theParams ) {

Class[] cs = new Class[ theParams.length ];
Expand Down
26 changes: 21 additions & 5 deletions src/main/java/sojamo/midimapper/MidiNote.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
package sojamo.midimapper;

import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Transmitter;

public class MidiNote {

private final AssignedDevice device;
private final int note;
private final Object parent;

public MidiNote( Object theParent , int theNote ) {
public MidiNote( AssignedDevice theDevice , Object theParent , int theNote ) {
device = theDevice;
parent = theParent;
note = theNote;
}

public AssignedDevice to( Object theTarget , String theMember ) {
return to( new MidiReceiver( note , theTarget , theMember ) );
}

public MidiReceiver to( Object theTarget , String theMember ) {
return new MidiReceiver( note , theTarget , theMember );
public AssignedDevice to( String theMember ) {
return to( parent , theMember );
}

public MidiReceiver to( String theMember ) {
return to( parent , theMember );
public AssignedDevice to( MidiReceiver theReceiver ) {
try {
Transmitter conTrans;
conTrans = device.get( ).getTransmitter( );
conTrans.setReceiver( theReceiver );
} catch ( MidiUnavailableException e ) {
e.printStackTrace( );
} catch ( NullPointerException e ) {}
return device;
}
}

0 comments on commit 6569d94

Please sign in to comment.