move Pd MIDI bridge to btmidi repository
[pdlib:btmidi.git] / PdMidiBridge / src / com / noisepages / nettoyeur / bluetooth / midi / pd / PdMidiBridge.java
1 /**
2  * 
3  * For information on usage and redistribution, and for a DISCLAIMER OF ALL
4  * WARRANTIES, see the file, "LICENSE.txt," in this distribution.
5  * 
6  */
7
8 package com.noisepages.nettoyeur.bluetooth.midi.pd;
9
10 import java.io.IOException;
11
12 import org.puredata.core.PdBase;
13 import org.puredata.core.PdMidiReceiver;
14
15 import android.bluetooth.BluetoothDevice;
16 import android.util.Log;
17
18 import com.noisepages.nettoyeur.bluetooth.BluetoothSppObserver;
19 import com.noisepages.nettoyeur.bluetooth.midi.BluetoothMidiReceiver;
20 import com.noisepages.nettoyeur.bluetooth.midi.BluetoothMidiService;
21
22 /**
23  * This class establishes a link between libpd and BluetoothMidi.  It only hooks up and registers the
24  * callbacks for handling MIDI and Bluetooth connection events.  Everything else, e.g., starting and
25  * stopping services, remains the responsibility of the client code.
26  *
27  * @author Peter Brinkmann (peter.brinkmann@gmail.com)
28  */
29 public class PdMidiBridge implements BluetoothMidiReceiver, PdMidiReceiver {
30
31         /**
32          * Establishes a connection that sends MIDI events from Bluetooth to libpd and vice versa.
33          * It calls the init method of the MIDI service, and so no further initialization is necessary.
34          * 
35          * @param service initialized but unconnected MIDI service
36          * @param observer callbacks for handling Bluetooth connection events
37          * @throws IOException
38          */
39         public static void establishMidiBridge(BluetoothMidiService service, BluetoothSppObserver observer) throws IOException {
40                 PdMidiBridge bridge = new PdMidiBridge(service, observer);
41                 service.init(bridge);
42                 PdBase.setMidiReceiver(bridge);
43         }
44         
45         private final static String TAG = "BluetoothMidiBridge";
46
47         private final BluetoothMidiService service;
48         private final BluetoothSppObserver observer;
49
50         private PdMidiBridge(BluetoothMidiService service, BluetoothSppObserver observer) {
51                 this.service = service;
52                 this.observer = observer;
53         }
54
55         @Override
56         public void receiveProgramChange(int channel, int value) {
57                 try {
58                         service.sendProgramChange(channel, value);
59                 } catch (IOException e) {
60                         Log.e(TAG, e.getMessage());
61                 }
62         }
63
64         @Override
65         public void receivePolyAftertouch(int channel, int pitch, int value) {
66                 try {
67                         service.sendPolyAftertouch(channel, pitch, value);
68                 } catch (IOException e) {
69                         Log.e(TAG, e.getMessage());
70                 }
71         }
72
73         @Override
74         public void receivePitchBend(int channel, int value) {
75                 try {
76                         service.sendPitchbend(channel, value);
77                 } catch (IOException e) {
78                         Log.e(TAG, e.getMessage());
79                 }
80         }
81
82         @Override
83         public void receiveNoteOn(int channel, int pitch, int velocity) {
84                 try {
85                         service.sendNoteOn(channel, pitch, velocity);
86                 } catch (IOException e) {
87                         Log.e(TAG, e.getMessage());
88                 }
89         }
90
91         @Override
92         public void receiveControlChange(int channel, int controller, int value) {
93                 try {
94                         service.sendControlChange(channel, controller, value);
95                 } catch (IOException e) {
96                         Log.e(TAG, e.getMessage());
97                 }
98         }
99
100         @Override
101         public void receiveAftertouch(int channel, int value) {
102                 try {
103                         service.sendAftertouch(channel, value);
104                 } catch (IOException e) {
105                         Log.e(TAG, e.getMessage());
106                 }
107         }
108
109         @Override
110         public void receiveMidiByte(int port, int value) {
111                 try {
112                         service.sendRawByte(value);  // We ignore the port number.
113                 } catch (IOException e) {
114                         Log.e(TAG, e.getMessage());
115                 }
116         }
117         
118         @Override
119         public void onAftertouch(int channel, int velocity) {
120                 PdBase.sendAftertouch(channel, velocity);
121         }
122
123         @Override
124         public void onControlChange(int channel, int controller, int value) {
125                 PdBase.sendControlChange(channel, controller, value);
126         }
127
128         @Override
129         public void onNoteOff(int channel, int key, int velocity) {
130                 PdBase.sendNoteOn(channel, key, 0);
131         }
132
133         @Override
134         public void onNoteOn(int channel, int key, int velocity) {
135                 PdBase.sendNoteOn(channel, key, velocity);
136         }
137
138         @Override
139         public void onPitchBend(int channel, int value) {
140                 PdBase.sendPitchBend(channel, value);
141         }
142
143         @Override
144         public void onPolyAftertouch(int channel, int key, int velocity) {
145                 PdBase.sendPolyAftertouch(channel, key, velocity);
146         }
147
148         @Override
149         public void onProgramChange(int channel, int program) {
150                 PdBase.sendProgramChange(channel, program);
151         }
152
153         @Override
154         public void onRawByte(int value) {
155                 if (value >= 0xf8) {
156                         PdBase.sendSysRealTime(0, value);
157                 } else {
158                         PdBase.sendMidiByte(0, value);
159                         // Note that we send raw bytes straight to [midiout], without attempting to parse
160                         // sysex messages.
161                 }
162         }
163         
164         @Override
165         public void onConnectionFailed() {
166                 observer.onConnectionFailed();
167         }
168
169         @Override
170         public void onConnectionLost() {
171                 observer.onConnectionLost();
172         }
173
174         @Override
175         public void onDeviceConnected(BluetoothDevice device) {
176                 observer.onDeviceConnected(device);
177         }
178 }