JavaMIDIキーボードの打鍵を音にするプログラム。

 

2つの方法がある。

(1) MIDI-Keyboard(transmitter)--->(receiver)SoftSynthesizer--->Speaker

(この方法は、MACでは動作するが、Windowsでは動作しない、原因不明)

 

(2) MIDI-Keyboard(transmitter)--->(receiver)Windows MIDI_MAPPER--->Speaker

(これはWindows専用)

 

準備:

(0)MACの準備

準備1:MAC OS Xの場合、

mmjをダウンロードし、libmmj.jnlib mmj.jar /Library/Java/Extensions/ の中に入れる。

 

(1)(2)共通の準備

準備2:MIDIキーボードをPCに接続

(私の場合、MIDIキーボードはUSB接続できる)

デバイスドライバをインストールする必要なし

 

準備3:PCに実装されているMIDIデバイスの情報を取得

DeviceLIst.javaを走らせると、一覧が出てくる。下記(3)参照。

求めた番号を下記プログラムの定数に代入する。

(例:私のMACでは)

static final int DEVICE_IN  = 0;

static final int DEVICE_OUT = 3;

(例:私のWindowsでは)

static final int DEVICE_IN  = 0;

static final int DEVICE_OUT = 1;//1or2

 

(1)、(1)の実装

基本思想:midiキーボードにトランスミッタ、synthesizerにレシーバを付け、それらを接続。

 

import javax.sound.midi.*;

class MidiKeyboardM {

    static final int DEVICE_IN  = 0;

    static final int DEVICE_OUT = 3;

    static MidiDevice device_input = null;

    static MidiDevice device_output= null;

 

    public static void main (String[] args) {

        MidiDevice.Info[] info=MidiSystem.getMidiDeviceInfo();

       try{

          device_input  = MidiSystem.getMidiDevice(info[DEVICE_IN]);

          device_output = MidiSystem.getMidiDevice(info[DEVICE_OUT]);

       }catch(Throwable t){};

 

        if (!(device_output instanceof Synthesizer)) {

            throw new IllegalArgumentException("not a Synthesizer!");

        }

 

        try { // connet midikeyboard transmitter to Software synthesizer

            Transmitter trans

                = device_input.getTransmitter();

            Receiver recv

                = device_output.getReceiver();

 

            if (!device_output.isOpen()) {

                device_output.open();

            }

            trans.setReceiver(recv);

        } catch (MidiUnavailableException e) {

            System.err.println(e.getMessage());

            System.exit(0);

        }

    }

}

 

 

(2)、(2)の実装

基本思想:JavaプログラムにReceiverをインプリメントすると(implements Receiver)打鍵のたびにsend()メソッドが動く。

 

import javax.sound.midi.*;

public class MidiKeyboardW implements Receiver

{

              static final int DEVICE_IN  = 0;

              static final int DEVICE_OUT = 1;//ここを修正、私の場合1or2

              private MidiDevice.Info[] info;

              private MidiDevice in_device = null;

              private MidiDevice out_device = null;

             

              public static void main(String[] args)          {

                            new MidiKeyboardW();

              }

             

              public MidiKeyboardW(){

 

                            info = MidiSystem.getMidiDeviceInfo();

                            MidiDevice dev=null;

 

                            try{ dev=MidiSystem.getMidiDevice(info[DEVICE_IN]);

                            }catch(Throwable t){}

                            connectMidiIn(dev);

 

                            try{ dev=MidiSystem.getMidiDevice(info[DEVICE_OUT]);

                            }catch(Throwable t){}

                            connectMidiOut(dev);

              }

             

              private void connectMidiIn( MidiDevice device ) {

                            try {

                                          device.open();

                                          Transmitter tx = device.getTransmitter();

                                          tx.setReceiver(this);

                                          in_device = device;

                            } catch(MidiUnavailableException e) {

                                          e.printStackTrace();

                            }

 

    }

   

              private void connectMidiOut( MidiDevice device ) {

                            try {

                                          device.open();

                                          out_device = device;

                            } catch(MidiUnavailableException e) {

                                          e.printStackTrace();

                            }

              }

             

              public void close() {}

              public void send(MidiMessage message, long timeStamp) {

                           

                            if ( message instanceof ShortMessage ) {

                                          ShortMessage sm = (ShortMessage)message;

                                          switch ( sm.getCommand() ) {

                                                        case ShortMessage.NOTE_ON:

                                                                      System.out.println(sm.getData1() +" ");

                                                        break;

                                                        //case ShortMessage.NOTE_OFF:

                                                        //break;

                                                        default:

                                          }

                            }

                           

                            try {

                                          out_device.getReceiver().send(message ,timeStamp);

                            } catch(MidiUnavailableException e) {}

              }

}

 

 

(3)MIDIデバイス番号を求めるプログラム

import javax.sound.midi.*;

 

public class DeviceList {

    public static void main(String[] args) throws Exception {

        MidiDevice.Info[] info = MidiSystem.getMidiDeviceInfo();

        for (int i = 0; i < info.length; i++) {

            System.err.println("[" + i + "]");

            System.err.println("  Description:" + info[i].getDescription());

            System.err.println("  Name:" + info[i].getName());

            System.err.println("  Vendor:" + info[i].getVendor());

            MidiDevice device = MidiSystem.getMidiDevice(info[i]);

            if (device instanceof Sequencer) {

                System.err.println("SEQUENCER");

            }

            if (device instanceof Synthesizer) {

                System.err.println("SYNTHESIZER");

            }

            System.err.println();

        }

    }

}

 

(実行例1)私のWindows環境では、

[0]Description:No details available

Name:nanoKEY

Vender:Unknown vender

 

[1]Description:Windows MIDI_MAPPER

Name:Microsoft MIDI Mapper

Vender:Unknown vender

 

[2]Description:Internal software synthesizer

Name:Microsoft GS Wavetable Synth

Vender:Unknown vender

 

[3]Description:External MIDI Port

Name:nanoKEY

Vender:Unknown vender

 

[4]Description:Software sequencer

Name:Real Time Sequencer

Vender:Sun Microsystems

SEQUENCER

 

[5]Description:Software wavetable ...

Name:Java Sound Synthesizer

Vender:Sun Microsystems

SYNTHESIZER

 

(実行例2)私のMAC環境では、

[0]Description:nanoKEY-KEYBOARD

Name:nanoKEY

Vender:KORG INC.

 

[1]Description:nanoKEY-CTRL

Name:nanoKEY-CTRL

Vender:KORG INC.

 

[2]Description:Software Sequencer

Name:Real Time Sequencer

Vender:Sun Microsystems

SEQUENCER

 

[3]Description:Software Wabetable...

Name:Java Sound Synthesizer

Vender:Sun Microsystems

SYNTHESIZER