Skip to content

Commit

Permalink
Merge branch 'release/v0.0.11'
Browse files Browse the repository at this point in the history
  • Loading branch information
kshoji committed Feb 25, 2022
2 parents 200aadb + c32eac2 commit 503b28e
Show file tree
Hide file tree
Showing 16 changed files with 169 additions and 31 deletions.
2 changes: 1 addition & 1 deletion BLE-MIDI-library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ group = 'jp.kshoji'
uploadArchives {
repositories.mavenDeployer {
repository url: 'file://' + file('../library/repository').absolutePath
pom.version = '0.0.10'
pom.version = '0.0.11'
pom.artifactId = 'ble-midi'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ public void onServicesDiscovered(final BluetoothGatt gatt, int status) {

// find MIDI Output device
synchronized (midiOutputDevicesMap) {
Set<MidiOutputDevice> midiOutputDevices = midiOutputDevicesMap.get(gattDeviceAddress);
if (midiOutputDevices != null) {
for (MidiOutputDevice midiOutputDevice : midiOutputDevices) {
midiOutputDevice.stop();
}
}
midiOutputDevicesMap.remove(gattDeviceAddress);
}

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

for (MidiOutputDevice midiOutputDevice : midiOutputDevices) {
midiOutputDevice.stop();
if (midiDeviceDetachedListener != null) {
midiDeviceDetachedListener.onMidiOutputDeviceDetached(midiOutputDevice);
}
Expand Down Expand Up @@ -366,6 +373,13 @@ public void terminate() {
}

synchronized (midiOutputDevicesMap) {
for (Set<MidiOutputDevice> midiOutputDevices : midiOutputDevicesMap.values()) {
for (MidiOutputDevice midiOutputDevice : midiOutputDevices) {
midiOutputDevice.stop();
}

midiOutputDevices.clear();
}
midiOutputDevicesMap.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import android.support.annotation.NonNull;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
* Represents BLE MIDI Output Device
*
Expand All @@ -11,6 +14,8 @@ public abstract class MidiOutputDevice {

public static final int MAX_TIMESTAMP = 8192;

final ByteArrayOutputStream transferDataStream = new ByteArrayOutputStream();

/**
* Transfer data
*
Expand Down Expand Up @@ -40,16 +45,67 @@ public final String toString() {
return getDeviceName();
}

volatile boolean transferDataThreadAlive;
final Thread transferDataThread = new Thread(new Runnable() {
@Override
public void run() {
transferDataThreadAlive = true;

while (transferDataThreadAlive) {
synchronized (transferDataStream) {
if (writtenDataCount > 0) {
transferData(transferDataStream.toByteArray());
transferDataStream.reset();
writtenDataCount = 0;
}
}

try {
Thread.sleep(10);
} catch (InterruptedException ignored) {
}
}
}
});

protected MidiOutputDevice() {
transferDataThread.start();
}

/**
* Stops transfer thread
*/
public void stop() {
transferDataThreadAlive = false;
}

transient int writtenDataCount;
private void storeTransferData(byte[] data) {
synchronized (transferDataStream) {
long timestamp = System.currentTimeMillis() % MAX_TIMESTAMP;
if (writtenDataCount == 0) {
// Store timestamp high
transferDataStream.write((byte) (0x80 | ((timestamp >> 7) & 0x3f)));
writtenDataCount++;
}
// timestamp low
transferDataStream.write((byte) (0x80 | (timestamp & 0x7f)));
writtenDataCount++;
try {
transferDataStream.write(data);
writtenDataCount += data.length;
} catch (IOException ignored) {
}
}
}

/**
* Sends MIDI message to output device.
*
* @param byte1 the first byte
*/
private void sendMidiMessage(int byte1) {
long timestamp = System.currentTimeMillis() % MAX_TIMESTAMP;
byte[] writeBuffer = new byte[] { (byte) (0x80 | ((timestamp >> 7) & 0x3f)), (byte) (0x80 | (timestamp & 0x7f)), (byte) byte1 };

transferData(writeBuffer);
storeTransferData(new byte[] { (byte) byte1 });
}

/**
Expand All @@ -59,15 +115,7 @@ private void sendMidiMessage(int byte1) {
* @param byte2 the second byte
*/
private void sendMidiMessage(int byte1, int byte2) {
byte[] writeBuffer = new byte[4];
long timestamp = System.currentTimeMillis() % MAX_TIMESTAMP;

writeBuffer[0] = (byte) (0x80 | ((timestamp >> 7) & 0x3f));
writeBuffer[1] = (byte) (0x80 | (timestamp & 0x7f));
writeBuffer[2] = (byte) byte1;
writeBuffer[3] = (byte) byte2;

transferData(writeBuffer);
storeTransferData(new byte[] { (byte) byte1, (byte) byte2 });
}

/**
Expand All @@ -78,16 +126,7 @@ private void sendMidiMessage(int byte1, int byte2) {
* @param byte3 the third byte
*/
private void sendMidiMessage(int byte1, int byte2, int byte3) {
byte[] writeBuffer = new byte[5];
long timestamp = System.currentTimeMillis() % MAX_TIMESTAMP;

writeBuffer[0] = (byte) (0x80 | ((timestamp >> 7) & 0x3f));
writeBuffer[1] = (byte) (0x80 | (timestamp & 0x7f));
writeBuffer[2] = (byte) byte1;
writeBuffer[3] = (byte) byte2;
writeBuffer[4] = (byte) byte3;

transferData(writeBuffer);
storeTransferData(new byte[] { (byte) byte1, (byte) byte2, (byte) byte3 });
}

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

// immediately transfer data
transferData(writeBuffer);

timestamp = System.currentTimeMillis() % MAX_TIMESTAMP;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ public void run() {
@Override
public void run() {
if (midiInputEventListener != null) {
if (midiEventVelocity == 0) {
if (getArg3() == 0) {
midiInputEventListener.onMidiNoteOff(sender, getArg1() & 0xf, getArg2(), getArg3());
} else {
midiInputEventListener.onMidiNoteOn(sender, getArg1() & 0xf, getArg2(), getArg3());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import jp.kshoji.blemidi.listener.OnMidiDeviceAttachedListener;
import jp.kshoji.blemidi.listener.OnMidiDeviceDetachedListener;
import jp.kshoji.blemidi.listener.OnMidiInputEventListener;
import jp.kshoji.blemidi.peripheral.BleMidiPeripheralProvider;

/**
* BLE MIDI Plugin for Unity
Expand Down Expand Up @@ -379,6 +380,7 @@ public void sendMidiReset(String serializedMidiMessage) {
}
}

private BleMidiPeripheralProvider bleMidiPeripheralProvider;
private BleMidiCentralProvider bleMidiCentralProvider;
HashMap<String, MidiOutputDevice> midiOutputDeviceMap = new HashMap<>();

Expand Down Expand Up @@ -410,6 +412,33 @@ public void onMidiOutputDeviceDetached(@NonNull MidiOutputDevice midiOutputDevic
UnityPlayer.UnitySendMessage(GAME_OBJECT_NAME, "OnMidiOutputDeviceDetached", midiOutputDevice.getDeviceAddress());
}
});

bleMidiPeripheralProvider = new BleMidiPeripheralProvider(context);
bleMidiPeripheralProvider.setOnMidiDeviceAttachedListener(new OnMidiDeviceAttachedListener() {
@Override
public void onMidiInputDeviceAttached(@NonNull MidiInputDevice midiInputDevice) {
midiInputDevice.setOnMidiInputEventListener(midiInputEventListener);
UnityPlayer.UnitySendMessage(GAME_OBJECT_NAME, "OnMidiInputDeviceAttached", midiInputDevice.getDeviceAddress());
}

@Override
public void onMidiOutputDeviceAttached(@NonNull MidiOutputDevice midiOutputDevice) {
midiOutputDeviceMap.put(midiOutputDevice.getDeviceAddress(), midiOutputDevice);
UnityPlayer.UnitySendMessage(GAME_OBJECT_NAME, "OnMidiOutputDeviceAttached", midiOutputDevice.getDeviceAddress());
}
});
bleMidiPeripheralProvider.setOnMidiDeviceDetachedListener(new OnMidiDeviceDetachedListener() {
@Override
public void onMidiInputDeviceDetached(@NonNull MidiInputDevice midiInputDevice) {
UnityPlayer.UnitySendMessage(GAME_OBJECT_NAME, "OnMidiInputDeviceDetached", midiInputDevice.getDeviceAddress());
}

@Override
public void onMidiOutputDeviceDetached(@NonNull MidiOutputDevice midiOutputDevice) {
midiOutputDeviceMap.remove(midiOutputDevice.getDeviceAddress());
UnityPlayer.UnitySendMessage(GAME_OBJECT_NAME, "OnMidiOutputDeviceDetached", midiOutputDevice.getDeviceAddress());
}
});
}

/**
Expand All @@ -427,6 +456,21 @@ public void stopScanDevice() {
bleMidiCentralProvider.stopScanDevice();
}

/**
* Starts advertising
*/
public void startAdvertising()
{
bleMidiPeripheralProvider.startAdvertising();
}

/**
* Stops advertising
*/
public void stopAdvertising()
{
bleMidiPeripheralProvider.stopAdvertising();
}

/**
* Obtains device name for deviceId
Expand All @@ -448,5 +492,9 @@ public void terminate() {
bleMidiCentralProvider.stopScanDevice();
bleMidiCentralProvider.terminate();
}
if (bleMidiPeripheralProvider != null) {
bleMidiPeripheralProvider.stopAdvertising();
bleMidiPeripheralProvider.terminate();
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9a733e978e9c0b602e34b599cf1eb6f4
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6a520cdde120aec9d106450f72530f0792c0cfe8
31 changes: 31 additions & 0 deletions library/repository/jp/kshoji/ble-midi/0.0.11/ble-midi-0.0.11.pom
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>jp.kshoji</groupId>
<artifactId>ble-midi</artifactId>
<version>0.0.11</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
<groupId>jp.kshoji</groupId>
<artifactId>javax-sound-midi</artifactId>
<version>0.0.4</version>
<type>aar</type>
<classifier></classifier>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.android.support</groupId>
<artifactId>support-annotations</artifactId>
<version>28.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
842c04f5dca84b0b09158819aed08428
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4d81bf9c63b0f0ad6a24b5b1a99227cc4b7d05a7
5 changes: 3 additions & 2 deletions library/repository/jp/kshoji/ble-midi/maven-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<groupId>jp.kshoji</groupId>
<artifactId>ble-midi</artifactId>
<versioning>
<release>0.0.10</release>
<release>0.0.11</release>
<versions>
<version>0.0.1</version>
<version>0.0.2-SNAPSHOT</version>
Expand All @@ -21,7 +21,8 @@
<version>0.0.9</version>
<version>0.0.10-SNAPSHOT</version>
<version>0.0.10</version>
<version>0.0.11</version>
</versions>
<lastUpdated>20210701045819</lastUpdated>
<lastUpdated>20220225031500</lastUpdated>
</versioning>
</metadata>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
61544fe1b3c07f84260f37db7a37e98f
9567512c905ff0ea48dc379b0b88ed50
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a5b2073808b3c6155b96d4521f7f2b5e8613e2e8
edd99ebaf46a87e24d47c32f34b405ddeabe9d3b
2 changes: 1 addition & 1 deletion sample/src/main/res/menu/central.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<item android:id="@+id/action_toggle_scan"
android:title="@string/start_scan"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
android:showAsAction="ifRoom" />
</menu>
2 changes: 1 addition & 1 deletion sample/src/main/res/menu/peripheral.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<item android:id="@+id/action_toggle_advertise"
android:title="@string/start_advertise"
android:orderInCategory="100"
app:showAsAction="ifRoom" />
android:showAsAction="ifRoom" />
</menu>

0 comments on commit 503b28e

Please sign in to comment.