Skip to content

Commit 503b28e

Browse files
committed
Merge branch 'release/v0.0.11'
2 parents 200aadb + c32eac2 commit 503b28e

16 files changed

+169
-31
lines changed

BLE-MIDI-library/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ group = 'jp.kshoji'
3939
uploadArchives {
4040
repositories.mavenDeployer {
4141
repository url: 'file://' + file('../library/repository').absolutePath
42-
pom.version = '0.0.10'
42+
pom.version = '0.0.11'
4343
pom.artifactId = 'ble-midi'
4444
}
4545
}

BLE-MIDI-library/src/main/java/jp/kshoji/blemidi/central/BleMidiCallback.java

+14
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ public void onServicesDiscovered(final BluetoothGatt gatt, int status) {
167167

168168
// find MIDI Output device
169169
synchronized (midiOutputDevicesMap) {
170+
Set<MidiOutputDevice> midiOutputDevices = midiOutputDevicesMap.get(gattDeviceAddress);
171+
if (midiOutputDevices != null) {
172+
for (MidiOutputDevice midiOutputDevice : midiOutputDevices) {
173+
midiOutputDevice.stop();
174+
}
175+
}
170176
midiOutputDevicesMap.remove(gattDeviceAddress);
171177
}
172178

@@ -328,6 +334,7 @@ private void disconnectByDeviceAddress(@NonNull String deviceAddress) {
328334
midiOutputDevicesMap.remove(deviceAddress);
329335

330336
for (MidiOutputDevice midiOutputDevice : midiOutputDevices) {
337+
midiOutputDevice.stop();
331338
if (midiDeviceDetachedListener != null) {
332339
midiDeviceDetachedListener.onMidiOutputDeviceDetached(midiOutputDevice);
333340
}
@@ -366,6 +373,13 @@ public void terminate() {
366373
}
367374

368375
synchronized (midiOutputDevicesMap) {
376+
for (Set<MidiOutputDevice> midiOutputDevices : midiOutputDevicesMap.values()) {
377+
for (MidiOutputDevice midiOutputDevice : midiOutputDevices) {
378+
midiOutputDevice.stop();
379+
}
380+
381+
midiOutputDevices.clear();
382+
}
369383
midiOutputDevicesMap.clear();
370384
}
371385

BLE-MIDI-library/src/main/java/jp/kshoji/blemidi/device/MidiOutputDevice.java

+63-23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import android.support.annotation.NonNull;
44

5+
import java.io.ByteArrayOutputStream;
6+
import java.io.IOException;
7+
58
/**
69
* Represents BLE MIDI Output Device
710
*
@@ -11,6 +14,8 @@ public abstract class MidiOutputDevice {
1114

1215
public static final int MAX_TIMESTAMP = 8192;
1316

17+
final ByteArrayOutputStream transferDataStream = new ByteArrayOutputStream();
18+
1419
/**
1520
* Transfer data
1621
*
@@ -40,16 +45,67 @@ public final String toString() {
4045
return getDeviceName();
4146
}
4247

48+
volatile boolean transferDataThreadAlive;
49+
final Thread transferDataThread = new Thread(new Runnable() {
50+
@Override
51+
public void run() {
52+
transferDataThreadAlive = true;
53+
54+
while (transferDataThreadAlive) {
55+
synchronized (transferDataStream) {
56+
if (writtenDataCount > 0) {
57+
transferData(transferDataStream.toByteArray());
58+
transferDataStream.reset();
59+
writtenDataCount = 0;
60+
}
61+
}
62+
63+
try {
64+
Thread.sleep(10);
65+
} catch (InterruptedException ignored) {
66+
}
67+
}
68+
}
69+
});
70+
71+
protected MidiOutputDevice() {
72+
transferDataThread.start();
73+
}
74+
75+
/**
76+
* Stops transfer thread
77+
*/
78+
public void stop() {
79+
transferDataThreadAlive = false;
80+
}
81+
82+
transient int writtenDataCount;
83+
private void storeTransferData(byte[] data) {
84+
synchronized (transferDataStream) {
85+
long timestamp = System.currentTimeMillis() % MAX_TIMESTAMP;
86+
if (writtenDataCount == 0) {
87+
// Store timestamp high
88+
transferDataStream.write((byte) (0x80 | ((timestamp >> 7) & 0x3f)));
89+
writtenDataCount++;
90+
}
91+
// timestamp low
92+
transferDataStream.write((byte) (0x80 | (timestamp & 0x7f)));
93+
writtenDataCount++;
94+
try {
95+
transferDataStream.write(data);
96+
writtenDataCount += data.length;
97+
} catch (IOException ignored) {
98+
}
99+
}
100+
}
101+
43102
/**
44103
* Sends MIDI message to output device.
45104
*
46105
* @param byte1 the first byte
47106
*/
48107
private void sendMidiMessage(int byte1) {
49-
long timestamp = System.currentTimeMillis() % MAX_TIMESTAMP;
50-
byte[] writeBuffer = new byte[] { (byte) (0x80 | ((timestamp >> 7) & 0x3f)), (byte) (0x80 | (timestamp & 0x7f)), (byte) byte1 };
51-
52-
transferData(writeBuffer);
108+
storeTransferData(new byte[] { (byte) byte1 });
53109
}
54110

55111
/**
@@ -59,15 +115,7 @@ private void sendMidiMessage(int byte1) {
59115
* @param byte2 the second byte
60116
*/
61117
private void sendMidiMessage(int byte1, int byte2) {
62-
byte[] writeBuffer = new byte[4];
63-
long timestamp = System.currentTimeMillis() % MAX_TIMESTAMP;
64-
65-
writeBuffer[0] = (byte) (0x80 | ((timestamp >> 7) & 0x3f));
66-
writeBuffer[1] = (byte) (0x80 | (timestamp & 0x7f));
67-
writeBuffer[2] = (byte) byte1;
68-
writeBuffer[3] = (byte) byte2;
69-
70-
transferData(writeBuffer);
118+
storeTransferData(new byte[] { (byte) byte1, (byte) byte2 });
71119
}
72120

73121
/**
@@ -78,16 +126,7 @@ private void sendMidiMessage(int byte1, int byte2) {
78126
* @param byte3 the third byte
79127
*/
80128
private void sendMidiMessage(int byte1, int byte2, int byte3) {
81-
byte[] writeBuffer = new byte[5];
82-
long timestamp = System.currentTimeMillis() % MAX_TIMESTAMP;
83-
84-
writeBuffer[0] = (byte) (0x80 | ((timestamp >> 7) & 0x3f));
85-
writeBuffer[1] = (byte) (0x80 | (timestamp & 0x7f));
86-
writeBuffer[2] = (byte) byte1;
87-
writeBuffer[3] = (byte) byte2;
88-
writeBuffer[4] = (byte) byte3;
89-
90-
transferData(writeBuffer);
129+
storeTransferData(new byte[] { (byte) byte1, (byte) byte2, (byte) byte3 });
91130
}
92131

93132
/**
@@ -124,6 +163,7 @@ public final void sendMidiSystemExclusive(@NonNull byte[] systemExclusive) {
124163
// timestamp MSB
125164
writeBuffer[0] = (byte) (0x80 | ((timestamp >> 7) & 0x3f));
126165

166+
// immediately transfer data
127167
transferData(writeBuffer);
128168

129169
timestamp = System.currentTimeMillis() % MAX_TIMESTAMP;

BLE-MIDI-library/src/main/java/jp/kshoji/blemidi/util/BleMidiParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ public void run() {
565565
@Override
566566
public void run() {
567567
if (midiInputEventListener != null) {
568-
if (midiEventVelocity == 0) {
568+
if (getArg3() == 0) {
569569
midiInputEventListener.onMidiNoteOff(sender, getArg1() & 0xf, getArg2(), getArg3());
570570
} else {
571571
midiInputEventListener.onMidiNoteOn(sender, getArg1() & 0xf, getArg2(), getArg3());

BLE-MIDI-library/src/main/java/jp/kshoji/unity/midi/BleMidiUnityPlugin.java

+48
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import jp.kshoji.blemidi.listener.OnMidiDeviceAttachedListener;
1515
import jp.kshoji.blemidi.listener.OnMidiDeviceDetachedListener;
1616
import jp.kshoji.blemidi.listener.OnMidiInputEventListener;
17+
import jp.kshoji.blemidi.peripheral.BleMidiPeripheralProvider;
1718

1819
/**
1920
* BLE MIDI Plugin for Unity
@@ -379,6 +380,7 @@ public void sendMidiReset(String serializedMidiMessage) {
379380
}
380381
}
381382

383+
private BleMidiPeripheralProvider bleMidiPeripheralProvider;
382384
private BleMidiCentralProvider bleMidiCentralProvider;
383385
HashMap<String, MidiOutputDevice> midiOutputDeviceMap = new HashMap<>();
384386

@@ -410,6 +412,33 @@ public void onMidiOutputDeviceDetached(@NonNull MidiOutputDevice midiOutputDevic
410412
UnityPlayer.UnitySendMessage(GAME_OBJECT_NAME, "OnMidiOutputDeviceDetached", midiOutputDevice.getDeviceAddress());
411413
}
412414
});
415+
416+
bleMidiPeripheralProvider = new BleMidiPeripheralProvider(context);
417+
bleMidiPeripheralProvider.setOnMidiDeviceAttachedListener(new OnMidiDeviceAttachedListener() {
418+
@Override
419+
public void onMidiInputDeviceAttached(@NonNull MidiInputDevice midiInputDevice) {
420+
midiInputDevice.setOnMidiInputEventListener(midiInputEventListener);
421+
UnityPlayer.UnitySendMessage(GAME_OBJECT_NAME, "OnMidiInputDeviceAttached", midiInputDevice.getDeviceAddress());
422+
}
423+
424+
@Override
425+
public void onMidiOutputDeviceAttached(@NonNull MidiOutputDevice midiOutputDevice) {
426+
midiOutputDeviceMap.put(midiOutputDevice.getDeviceAddress(), midiOutputDevice);
427+
UnityPlayer.UnitySendMessage(GAME_OBJECT_NAME, "OnMidiOutputDeviceAttached", midiOutputDevice.getDeviceAddress());
428+
}
429+
});
430+
bleMidiPeripheralProvider.setOnMidiDeviceDetachedListener(new OnMidiDeviceDetachedListener() {
431+
@Override
432+
public void onMidiInputDeviceDetached(@NonNull MidiInputDevice midiInputDevice) {
433+
UnityPlayer.UnitySendMessage(GAME_OBJECT_NAME, "OnMidiInputDeviceDetached", midiInputDevice.getDeviceAddress());
434+
}
435+
436+
@Override
437+
public void onMidiOutputDeviceDetached(@NonNull MidiOutputDevice midiOutputDevice) {
438+
midiOutputDeviceMap.remove(midiOutputDevice.getDeviceAddress());
439+
UnityPlayer.UnitySendMessage(GAME_OBJECT_NAME, "OnMidiOutputDeviceDetached", midiOutputDevice.getDeviceAddress());
440+
}
441+
});
413442
}
414443

415444
/**
@@ -427,6 +456,21 @@ public void stopScanDevice() {
427456
bleMidiCentralProvider.stopScanDevice();
428457
}
429458

459+
/**
460+
* Starts advertising
461+
*/
462+
public void startAdvertising()
463+
{
464+
bleMidiPeripheralProvider.startAdvertising();
465+
}
466+
467+
/**
468+
* Stops advertising
469+
*/
470+
public void stopAdvertising()
471+
{
472+
bleMidiPeripheralProvider.stopAdvertising();
473+
}
430474

431475
/**
432476
* Obtains device name for deviceId
@@ -448,5 +492,9 @@ public void terminate() {
448492
bleMidiCentralProvider.stopScanDevice();
449493
bleMidiCentralProvider.terminate();
450494
}
495+
if (bleMidiPeripheralProvider != null) {
496+
bleMidiPeripheralProvider.stopAdvertising();
497+
bleMidiPeripheralProvider.terminate();
498+
}
451499
}
452500
}
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9a733e978e9c0b602e34b599cf1eb6f4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6a520cdde120aec9d106450f72530f0792c0cfe8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>jp.kshoji</groupId>
6+
<artifactId>ble-midi</artifactId>
7+
<version>0.0.11</version>
8+
<packaging>aar</packaging>
9+
<dependencies>
10+
<dependency>
11+
<groupId>jp.kshoji</groupId>
12+
<artifactId>javax-sound-midi</artifactId>
13+
<version>0.0.4</version>
14+
<type>aar</type>
15+
<classifier></classifier>
16+
<scope>compile</scope>
17+
<exclusions>
18+
<exclusion>
19+
<artifactId>*</artifactId>
20+
<groupId>*</groupId>
21+
</exclusion>
22+
</exclusions>
23+
</dependency>
24+
<dependency>
25+
<groupId>com.android.support</groupId>
26+
<artifactId>support-annotations</artifactId>
27+
<version>28.0.0</version>
28+
<scope>compile</scope>
29+
</dependency>
30+
</dependencies>
31+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
842c04f5dca84b0b09158819aed08428
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4d81bf9c63b0f0ad6a24b5b1a99227cc4b7d05a7

library/repository/jp/kshoji/ble-midi/maven-metadata.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<groupId>jp.kshoji</groupId>
44
<artifactId>ble-midi</artifactId>
55
<versioning>
6-
<release>0.0.10</release>
6+
<release>0.0.11</release>
77
<versions>
88
<version>0.0.1</version>
99
<version>0.0.2-SNAPSHOT</version>
@@ -21,7 +21,8 @@
2121
<version>0.0.9</version>
2222
<version>0.0.10-SNAPSHOT</version>
2323
<version>0.0.10</version>
24+
<version>0.0.11</version>
2425
</versions>
25-
<lastUpdated>20210701045819</lastUpdated>
26+
<lastUpdated>20220225031500</lastUpdated>
2627
</versioning>
2728
</metadata>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
61544fe1b3c07f84260f37db7a37e98f
1+
9567512c905ff0ea48dc379b0b88ed50
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a5b2073808b3c6155b96d4521f7f2b5e8613e2e8
1+
edd99ebaf46a87e24d47c32f34b405ddeabe9d3b

sample/src/main/res/menu/central.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
<item android:id="@+id/action_toggle_scan"
66
android:title="@string/start_scan"
77
android:orderInCategory="100"
8-
app:showAsAction="ifRoom" />
8+
android:showAsAction="ifRoom" />
99
</menu>

sample/src/main/res/menu/peripheral.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
<item android:id="@+id/action_toggle_advertise"
66
android:title="@string/start_advertise"
77
android:orderInCategory="100"
8-
app:showAsAction="ifRoom" />
8+
android:showAsAction="ifRoom" />
99
</menu>

0 commit comments

Comments
 (0)