Guest User

Untitled

a guest
Jun 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. package de.snke.dev;
  2.  
  3. import javax.sound.midi.*;;
  4.  
  5.  
  6.  
  7. public class Main extends Object implements Receiver{
  8.  
  9. static MidiClass myMidi;
  10.  
  11. public static void main(String[] args) throws Exception {
  12.  
  13. MidiDevice.Info[] info =
  14. MidiSystem.getMidiDeviceInfo();
  15.  
  16. for (int i=0; i < info.length; i++) {
  17. System.out.println(i + ") " + info[i]);
  18. System.out.println("Name: " + info[i].getName());
  19. System.out.println("Description: " +
  20. info[i].getDescription());
  21.  
  22. MidiDevice device =
  23. MidiSystem.getMidiDevice(info[i]);
  24. System.out.println("Device: " + device);
  25. }
  26.  
  27. }
  28.  
  29. public void send(MidiMessage msg,
  30. long time) {
  31. System.out.println("Received message " + msg);
  32. }
  33.  
  34. public void close() {
  35. System.out.println("Closing");
  36. }
  37. }
  38.  
  39. Sequencer seq;
  40. Transmitter seqTrans;
  41. Synthesizer synth;
  42. Receiver synthRcvr;
  43. try {
  44. seq = MidiSystem.getSequencer();
  45. seqTrans = seq.getTransmitter();
  46. synth = MidiSystem.getSynthesizer();
  47. synthRcvr = synth.getReceiver();
  48. seqTrans.setReceiver(synthRcvr);
  49. } catch (MidiUnavailableException e) {
  50. // handle or throw exception
  51. }
  52.  
  53. package de.snke.dev;
  54.  
  55. import javax.sound.midi.*;
  56. import java.util.ArrayList;
  57. import java.util.List;
  58. import java.io.*;
  59.  
  60. public class Main
  61. {
  62.  
  63. public void Main()
  64. {
  65. MidiDevice device;
  66. MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
  67. for (int i = 0; i < infos.length; i++) {
  68. try {
  69. device = MidiSystem.getMidiDevice(infos[i]);
  70. //does the device have any transmitters?
  71. //if it does, add it to the device list
  72. System.out.println(infos[i]);
  73.  
  74. //get all transmitters
  75. List<Transmitter> transmitters = device.getTransmitters();
  76. //and for each transmitter
  77.  
  78. for(int j = 0; j<transmitters.size();j++) {
  79. //create a new receiver
  80. transmitters.get(j).setReceiver(
  81. //using my own MidiInputReceiver
  82. new MidiInputReceiver(device.getDeviceInfo().toString())
  83. );
  84. }
  85.  
  86. Transmitter trans = device.getTransmitter();
  87. trans.setReceiver(new MidiInputReceiver(device.getDeviceInfo().toString()));
  88.  
  89. //open each device
  90. device.open();
  91. //if code gets this far without throwing an exception
  92. //print a success message
  93. System.out.println(device.getDeviceInfo()+" Was Opened");
  94.  
  95.  
  96. } catch (MidiUnavailableException e) {}
  97. }
  98.  
  99.  
  100. }
  101. //tried to write my own class. I thought the send method handles an MidiEvents sent to it
  102. public class MidiInputReceiver implements Receiver {
  103. public String name;
  104. public MidiInputReceiver(String name) {
  105. this.name = name;
  106. }
  107. public void send(MidiMessage msg, long timeStamp) {
  108.  
  109.  
  110. byte[] aMsg = msg.getMessage();
  111. // take the MidiMessage msg and store it in a byte array
  112.  
  113. // msg.getLength() returns the length of the message in bytes
  114. for(int i=0;i<msg.getLength();i++){
  115. System.out.println(aMsg[i]);
  116. // aMsg[0] is something, velocity maybe? Not 100% sure.
  117. // aMsg[1] is the note value as an int. This is the important one.
  118. // aMsg[2] is pressed or not (0/100), it sends 100 when they key goes down,
  119. // and 0 when the key is back up again. With a better keyboard it could maybe
  120. // send continuous values between then for how quickly it's pressed?
  121. // I'm only using VMPK for testing on the go, so it's either
  122. // clicked or not.
  123. }
  124. System.out.println();
  125. }
  126. public void close() {}
  127. }
  128. }
Add Comment
Please, Sign In to add comment