Skip to content

Commit ec84c17

Browse files
committed
First commit
0 parents  commit ec84c17

11 files changed

Lines changed: 396 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package tk.tsg100.launchpadAPI;
2+
3+
public enum Channels {
4+
5+
C1(0), C2(1), C3(2), C4(3), C5(4), C6(5), C7(6), C8(7), //
6+
C9(8), C10(9), C11(10), C12(11), C13(12), C14(13), C15(14), C16(15);
7+
8+
private final int systemChannel;
9+
10+
Channels(int channel) {
11+
this.systemChannel = channel;
12+
}
13+
14+
public int channelForSystem() {
15+
return this.systemChannel;
16+
}
17+
18+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package tk.tsg100.launchpadAPI;
2+
3+
public enum Color {
4+
5+
R0G0(0x00), R1G0(0x01), R2G0(0x02), R3G0(0x03),
6+
R0G1(0x10), R1G1(0x11), R2G1(0x12), R3G1(0x13),
7+
R0G2(0x20), R1G2(0x21), R2G2(0x22), R3G2(0x23),
8+
R0G3(0x30), R1G3(0x31), R2G3(0x32), R3G3(0x33);
9+
10+
/**
11+
* Alias for {@link #R0G0}
12+
*/
13+
public static final Color BLANK = R0G0;
14+
/**
15+
* Alias for {@link #R0G3}
16+
*/
17+
public static final Color GREEN = R0G3;
18+
/**
19+
* Alias for {@link #R3G0}
20+
*/
21+
public static final Color RED = R3G0;
22+
/**
23+
* Alias for {@link #R3G3}
24+
*/
25+
public static final Color AMBER = R3G3;
26+
27+
private int code;
28+
29+
private Color(int code) {
30+
this.code = code;
31+
}
32+
33+
/**
34+
* Provide the code to be sent to the launchpad device.
35+
*
36+
* @return the code to be sent to the launchpad device
37+
*/
38+
public int getCode() {
39+
return code;
40+
}
41+
42+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package tk.tsg100.launchpadAPI;
2+
3+
import javax.sound.midi.*;
4+
5+
public class Launchpad implements Receiver {
6+
7+
private final Channels channel;
8+
private final Transmitter transmitter;
9+
private final Receiver receiver;
10+
private final LaunchpadReceiver launchpadReceiver;
11+
12+
public Launchpad() throws MidiUnavailableException {
13+
this(Channels.C1);
14+
}
15+
public Launchpad(Channels channel) throws MidiUnavailableException {
16+
this(channel, null);
17+
}
18+
19+
public Launchpad(Channels channel, LaunchpadReceiver launchpadReceiver) throws MidiUnavailableException {
20+
this.channel = channel;
21+
transmitter = MidiSystem.getTransmitter();
22+
receiver = MidiSystem.getReceiver();
23+
this.launchpadReceiver = launchpadReceiver;
24+
25+
transmitter.setReceiver(this);
26+
27+
}
28+
29+
public Launchpad(LaunchpadReceiver launchpadReceiver) throws MidiUnavailableException {
30+
this(Channels.C1, launchpadReceiver);
31+
}
32+
33+
@Override //pls dont use PLSSS :C
34+
public void send(MidiMessage message, long timeStamp) {
35+
if (this.launchpadReceiver != null && message instanceof ShortMessage
36+
&& (((ShortMessage) message).getChannel() == this.channel.channelForSystem())
37+
&& (((ShortMessage) message).getCommand() == ShortMessage.NOTE_ON)
38+
&& (((ShortMessage) message).getData2() > 0)) {
39+
this.launchpadReceiver.receive(Pad.findMidi(((ShortMessage) message).getData1()));
40+
}
41+
}
42+
43+
44+
public void set(final Pad pad, final Color color) throws InvalidMidiDataException, MidiUnavailableException {
45+
SendMidi.send(0x90, pad.getCode(), color.getCode());
46+
}
47+
48+
@Override
49+
public void close() {
50+
this.receiver.close();
51+
this.transmitter.close();
52+
53+
}
54+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package tk.tsg100.launchpadAPI;
2+
3+
public interface LaunchpadReceiver {
4+
public void receive(Pad pad);
5+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package tk.tsg100.launchpadAPI;
2+
3+
import javax.sound.midi.ShortMessage;
4+
import java.util.Arrays;
5+
6+
public enum Pad {
7+
8+
CON1(6, 8, ShortMessage.CONTROL_CHANGE), CON2(6, 9, ShortMessage.CONTROL_CHANGE), //
9+
CON3(6, 10, ShortMessage.CONTROL_CHANGE), CON4(6, 11, ShortMessage.CONTROL_CHANGE), //
10+
CON5(6, 12, ShortMessage.CONTROL_CHANGE), CON6(6, 13, ShortMessage.CONTROL_CHANGE), //
11+
CON7(6, 14, ShortMessage.CONTROL_CHANGE), CON8(6, 15, ShortMessage.CONTROL_CHANGE), //
12+
A1(0, 0), A2(0, 1), A3(0, 2), A4(0, 3), A5(0, 4), A6(0, 5), A7(0, 6), A8(0, 7), A(0, 8), //
13+
B1(1, 0), B2(1, 1), B3(1, 2), B4(1, 3), B5(1, 4), B6(1, 5), B7(1, 6), B8(1, 7), B(1, 8), //
14+
C1(2, 0), C2(2, 1), C3(2, 2), C4(2, 3), C5(2, 4), C6(2, 5), C7(2, 6), C8(2, 7), C(2, 8), //
15+
D1(3, 0), D2(3, 1), D3(3, 2), D4(3, 3), D5(3, 4), D6(3, 5), D7(3, 6), D8(3, 7), D(3, 8), //
16+
E1(4, 0), E2(4, 1), E3(4, 2), E4(4, 3), E5(4, 4), E6(4, 5), E7(4, 6), E8(4, 7), E(4, 8), //
17+
F1(5, 0), F2(5, 1), F3(5, 2), F4(5, 3), F5(5, 4), F6(5, 5), F7(5, 6), F8(5, 7), F(5, 8), //
18+
G1(6, 0), G2(6, 1), G3(6, 2), G4(6, 3), G5(6, 4), G6(6, 5), G7(6, 6), G8(6, 7), G(6, 8), //
19+
H1(7, 0), H2(7, 1), H3(7, 2), H4(7, 3), H5(7, 4), H6(7, 5), H7(7, 6), H8(7, 7), H(7, 8);
20+
21+
private final int x;
22+
private final int y;
23+
private final int command;
24+
25+
private Pad(int x, int y) {
26+
this(x, y, ShortMessage.NOTE_ON);
27+
}
28+
29+
private Pad(int x, int y, int command) {
30+
this.x = x;
31+
this.y = y;
32+
this.command = command;
33+
}
34+
35+
public int getX() {
36+
return x;
37+
}
38+
39+
public int getY() {
40+
return y;
41+
}
42+
43+
public int getCode() {
44+
return x * 16 + y;
45+
}
46+
47+
public int getCommand() {
48+
return command;
49+
}
50+
51+
public static Pad find(int x, int y) {
52+
return Arrays.stream(values())
53+
.filter(p -> p.command == ShortMessage.NOTE_ON && p.x == x && p.y == y).findFirst()
54+
.orElse(null);
55+
}
56+
57+
public static Pad findMidi(int midiByte) {
58+
return find((midiByte & 0xf0) >> 4, midiByte & 0x0f);
59+
}
60+
61+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package tk.tsg100.launchpadAPI;
2+
3+
import javax.sound.midi.*;
4+
5+
public class SendMidi {
6+
7+
public static void send(int command, int data1, int data2){
8+
MidiDevice device;
9+
MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
10+
11+
for(int i = 0; i < devices.length; i++){
12+
if(devices[i].getDescription().equalsIgnoreCase("External MIDI Port")){ //select device
13+
try {
14+
device = MidiSystem.getMidiDevice(devices[i]);
15+
16+
device.open(); //open device
17+
18+
Receiver receiver = device.getReceiver(); //prepare sender
19+
MidiMessage message;
20+
21+
//0x90 = note on (set color on pad X)
22+
message = new ShortMessage(0x90, 0, data1, data2); //prepare message
23+
long timestamp = -1;
24+
receiver.send(message, timestamp); //send message
25+
26+
receiver.close();
27+
device.close(); //close everything
28+
29+
break; //exit loop
30+
} catch (MidiUnavailableException | InvalidMidiDataException e) {
31+
e.printStackTrace();
32+
}
33+
34+
35+
}
36+
}
37+
}
38+
39+
//Only use if launchpad isn't listed as External MIDI Port
40+
public static void sendPort(int command, int data1, int data2, String deviceDescription){
41+
MidiDevice device;
42+
MidiDevice.Info[] devices = MidiSystem.getMidiDeviceInfo();
43+
44+
for(int i = 0; i < devices.length; i++){
45+
if(devices[i].getDescription().equalsIgnoreCase(deviceDescription)){ //select device
46+
try {
47+
device = MidiSystem.getMidiDevice(devices[i]);
48+
49+
device.open(); //open device
50+
51+
Receiver receiver = device.getReceiver(); //prepare sender
52+
MidiMessage message;
53+
54+
//0x90 = note on (set color on pad X)
55+
message = new ShortMessage(0x90, 0, data1, data2); //prepare message
56+
long timestamp = -1;
57+
receiver.send(message, timestamp); //send message
58+
59+
receiver.close();
60+
device.close(); //close everything
61+
62+
break; //exit loop
63+
} catch (MidiUnavailableException | InvalidMidiDataException e) {
64+
e.printStackTrace();
65+
}
66+
67+
68+
}
69+
}
70+
}
71+
}
72+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package tk.tsg100.launchpadAPI.utils;
2+
3+
import tk.tsg100.launchpadAPI.SendMidi;
4+
5+
6+
public class Commands {
7+
8+
public static void reset(){
9+
SendMidi.send(0xB0, 0x0, 0x0);
10+
}
11+
12+
public static void allYellow(int value){ //full yellow 0=low 1=mid 2=max
13+
14+
switch (value){
15+
case(0):
16+
SendMidi.send(0xB0, 0x0, 0x7D);
17+
break;
18+
case(1):
19+
SendMidi.send(0xB0, 0x0, 0x7E);
20+
break;
21+
default:
22+
SendMidi.send(0xB0, 0x0, 0x7F);
23+
break;
24+
}
25+
26+
}
27+
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package tk.tsg100.launchpadAPI.utils;
2+
3+
import tk.tsg100.launchpadAPI.SendMidi;
4+
5+
public class Flash {
6+
7+
public static void startFlash(){ SendMidi.send(0xB0, 0x0, 0x28); }
8+
//leave all lights on after stopping to flash
9+
public static void stopOn(){
10+
SendMidi.send(0xB0, 0x0 ,0x20);
11+
}
12+
//turn all lights of after stopping to flash
13+
public static void stopOff(){
14+
SendMidi.send(0xB0, 0x0, 0x21);
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package tk.tsg100.launchpadAPI.utils;
2+
3+
import tk.tsg100.launchpadAPI.Color;
4+
import tk.tsg100.launchpadAPI.SendMidi;
5+
6+
public class Full {
7+
8+
public static void fullOn(Color color){
9+
10+
for(int i = 0; i<120; i++){
11+
SendMidi.send(0x90, i, color.getCode());
12+
}
13+
for(int i = 0; i<7; i++){
14+
TopRow.change(i, color);
15+
}
16+
17+
}
18+
19+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package tk.tsg100.launchpadAPI.utils;
2+
3+
import tk.tsg100.launchpadAPI.Color;
4+
5+
import java.util.Random;
6+
7+
public class RandomColor {
8+
9+
10+
public static Color getRandomColor(){
11+
Color color;
12+
13+
Random random = new Random();
14+
int i = random.nextInt(3);
15+
16+
switch(i){
17+
case(0):
18+
color = Color.RED;
19+
break;
20+
case(1):
21+
color = Color.AMBER;
22+
break;
23+
case(2):
24+
color = Color.GREEN;
25+
break;
26+
default:
27+
color = Color.BLANK;
28+
break;
29+
}
30+
31+
32+
33+
34+
return color;
35+
}
36+
37+
}

0 commit comments

Comments
 (0)