Skip to content

Commit

Permalink
Merge pull request #82 from ericahrens/api-18
Browse files Browse the repository at this point in the history
MCU/SSL fixed issue with non-ascii characters causing extension to crash due to illegal sysex values
  • Loading branch information
abique committed Feb 27, 2024
2 parents a793718 + 9f6bb70 commit 7174bac
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 176 deletions.
62 changes: 41 additions & 21 deletions src/main/java/com/bitwig/extensions/controllers/mcu/StringUtil.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
package com.bitwig.extensions.controllers.mcu;

public class StringUtil {

private static final int PAN_RANGE = 50;
private static final char[] SPECIALS = {'ä', 'ü', 'ö', 'Ä', 'Ü', 'Ö', 'ß', 'é', 'è', 'ê', 'â', 'á', 'à', //
'û', 'ú', 'ù', 'ô', 'ó', 'ò'};
private static final String[] REPLACE = {"a", "u", "o", "A", "U", "O", "ss", "e", "e", "e", "a", "a", "a", //
"u", "u", "u", "o", "o", "o"};

private static final char[] SPECIALS = {
'ä', 'ü', 'ö', 'Ä', 'Ü', 'Ö', 'ß', 'é', 'è', 'ê', 'â', 'á', 'à', //
'û', 'ú', 'ù', 'ô', 'ó', 'ò'
};
private static final String[] REPLACE = {
"a", "u", "o", "A", "U", "O", "ss", "e", "e", "e", "a", "a", "a", //
"u", "u", "u", "o", "o", "o"
};

private StringUtil() {
}

public static String toBarBeats(final double value) {
final int bars = (int) Math.floor(value);
final int beats = (int) Math.floor((value - bars) * 4);
return String.format("%02d:%02d", bars, beats);
}

public static String panToString(final double v) {
final int intv = (int) (v * PAN_RANGE * 2);
if (intv == PAN_RANGE) {
Expand All @@ -26,7 +30,7 @@ public static String panToString(final double v) {
}
return " " + (intv - PAN_RANGE) + "R";
}

/**
* Tailored to condense Volume value strings. Removes leading + and spaces.
*
Expand All @@ -45,27 +49,27 @@ public static String condenseVolumeValue(final String valueText, final int maxLe
}
return sb.toString();
}

public static String toTwoCharVal(final int value) {
if (value < 10) {
return " " + value;
}
return Integer.toString(value);
}

public static String toDisplayName(final String text) {
if (text.length() < 2) {
return text;
}
return text.charAt(0) + text.substring(1, Math.min(6, text.length())).toLowerCase();
}


public static String padString(final String text, final int pad) {
return " ".repeat(Math.max(0, pad)) + text;
}

public static String padEnd(final String text, int paddingLength) {
public static String padEnd(final String text, final int paddingLength) {
if (text.length() == paddingLength) {
return text;
}
Expand All @@ -74,19 +78,35 @@ public static String padEnd(final String text, int paddingLength) {
}
return text + " ".repeat(paddingLength - text.length());
}

public static String limit(final String value, final int max) {
return value.substring(0, Math.min(max, value.length()));
}

public static String reduceAscii(final String name, final int maxLen) {
String result = toAsciiDisplay(name, maxLen + 10);
final String result = toAsciiDisplay(name, maxLen + 10);
if (result.length() <= maxLen) {
return result;
}
return result.replace(" ", "");
}


public static String toAscii(final String value) {
final StringBuilder b = new StringBuilder();
for (int i = 0; i < value.length(); i++) {
final char c = value.charAt(i);
if (c < 128) {
b.append(c);
} else {
final int replacement = getReplace(c);
if (replacement >= 0) {
b.append(REPLACE[replacement]);
}
}
}
return b.toString();
}

public static String toAsciiDisplay(final String name, final int maxLen) {
final StringBuilder b = new StringBuilder();
for (int i = 0; i < name.length() && b.length() < maxLen; i++) {
Expand All @@ -105,7 +125,7 @@ public static String toAsciiDisplay(final String name, final int maxLen) {
}
return b.toString();
}

private static int getReplace(final char c) {
for (int i = 0; i < SPECIALS.length; i++) {
if (c == SPECIALS[i]) {
Expand All @@ -114,5 +134,5 @@ private static int getReplace(final char c) {
}
return -1;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
public class ParameterValueDisplayBinding extends AbstractDisplayBinding<Parameter> {
private final DoubleValueConverter converter;

public ParameterValueDisplayBinding(final DisplayManager target, final ControlMode mode,
final DisplayTarget displayTargetIndex, final Parameter parameter) {
this(target, mode, displayTargetIndex, parameter, value -> Double.toString(value));
}

public ParameterValueDisplayBinding(final DisplayManager target, final ControlMode mode,
final DisplayTarget displayTargetIndex, final Parameter parameter, final DoubleValueConverter converter) {
super(target, mode, displayTargetIndex, parameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.bitwig.extension.controller.api.BooleanValue;
import com.bitwig.extension.controller.api.StringValue;
import com.bitwig.extensions.controllers.mcu.StringUtil;
import com.bitwig.extensions.controllers.mcu.bindings.ResetableBinding;
import com.bitwig.extensions.controllers.mcu.display.DisplayManager;
import com.bitwig.extensions.controllers.mcu.layer.ControlMode;
Expand All @@ -24,7 +25,7 @@ public StringDisplayBinding(final DisplayManager target, final ControlMode mode,

public StringDisplayBinding(final DisplayManager target, final ControlMode mode,
final DisplayTarget displayTargetIndex, final StringValue stringValue, final BooleanValue existsValue) {
this(target, mode, displayTargetIndex, stringValue, existsValue, s -> s);
this(target, mode, displayTargetIndex, stringValue, existsValue, s -> StringUtil.toAsciiDisplay(s, 8));
}

private void handleValueChange(final String newValue) {
Expand All @@ -36,7 +37,7 @@ private void handleValueChange(final String newValue) {

@Override
public void reset() {
this.lastValue = getSource().get();
this.lastValue = stringConversion.convert(getSource().get());
if (isActive()) {
updateDisplay();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bitwig.extensions.controllers.mcu.bindings.display;

import com.bitwig.extension.controller.api.StringValue;
import com.bitwig.extensions.controllers.mcu.StringUtil;
import com.bitwig.extensions.controllers.mcu.display.DisplayManager;
import com.bitwig.extensions.controllers.mcu.display.DisplayRow;
import com.bitwig.extensions.controllers.mcu.layer.ControlMode;
Expand All @@ -12,15 +13,15 @@ public StringRowDisplayBinding(final DisplayManager target, final ControlMode mo
super(target, mode, DisplayTarget.of(row.getRowIndex(), -1, sectionIndex), stringValue);
exists = true;
stringValue.addValueObserver(this::handleValueChange);
this.lastValue = stringValue.get();
this.lastValue = StringUtil.toAscii(stringValue.get());
}

protected void updateDisplay() {
getTarget().sendText(controlMode, targetIndex.rowIndex(), lastValue);
}

private void handleValueChange(final String newValue) {
this.lastValue = newValue;
this.lastValue = StringUtil.toAscii(newValue);
if (isActive()) {
updateDisplay();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@
import com.bitwig.extension.controller.api.Parameter;

public interface DeviceManager {

//void initiateBrowsing(final BrowserConfiguration browser, Type type);

//void addBrowsing(final BrowserConfiguration browser, boolean after);

//void setInfoLayer(DisplayLayer infoLayer);

//void enableInfo(InfoSource type);

void disableInfo();

//InfoSource getInfoSource();

Parameter getParameter(int index);

//ParameterPage getParameterPage(int index);

void navigateDeviceParameters(final int direction);

//void handleResetInvoked(final int index, final ModifierValueObject modifier);

DeviceTypeFollower getCurrentFollower();

DeviceTypeFollower getDeviceFollower();
boolean isSpecificDevicePresent();

int getPageCount();

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.bitwig.extensions.controllers.mcu.devices;

import com.bitwig.extension.callback.BooleanValueChangedCallback;
import com.bitwig.extension.controller.api.*;
import com.bitwig.extension.controller.api.BooleanValue;
import com.bitwig.extension.controller.api.CursorTrack;
import com.bitwig.extension.controller.api.Device;
import com.bitwig.extension.controller.api.DeviceBank;
import com.bitwig.extension.controller.api.DeviceMatcher;
import com.bitwig.extension.controller.api.PinnableCursorDevice;
import com.bitwig.extensions.controllers.mcu.CursorDeviceControl;
import com.bitwig.extensions.controllers.mcu.VPotMode;
import com.bitwig.extensions.controllers.mcu.value.BasicIntValue;
import com.bitwig.extensions.framework.values.BooleanValueObject;

public class DeviceTypeFollower {
Expand All @@ -12,20 +18,24 @@ public class DeviceTypeFollower {
private final Device focusDevice;
private final CursorDeviceControl cursorDeviceControl;
private final BooleanValue cursorOnDevice;
private final BasicIntValue trackIndex = new BasicIntValue();

private int chainIndex = -1;

public DeviceTypeFollower(final CursorDeviceControl cursorDeviceControl, final DeviceMatcher matcher,
final VPotMode potMode) {
final VPotMode potMode) {
this.cursorDeviceControl = cursorDeviceControl;
final CursorTrack cursorTrack = cursorDeviceControl.getCursorTrack();
deviceBank = cursorTrack.createDeviceBank(1);
this.potMode = potMode;
deviceBank.setDeviceMatcher(matcher);

focusDevice = deviceBank.getItemAt(0);
focusDevice.exists().markInterested();
focusDevice.name().markInterested();


cursorTrack.position().addValueObserver(pos -> trackIndex.set(pos));

final PinnableCursorDevice cursorDevice = cursorDeviceControl.getCursorDevice();
if (potMode.getAssign() == VPotMode.BitwigType.DEVICE) {
final BooleanValueObject matchesTyp = new BooleanValueObject();
Expand All @@ -50,48 +60,50 @@ public DeviceTypeFollower(final CursorDeviceControl cursorDeviceControl, final D
}
});
}

public void addOnCursorListener(final BooleanValueChangedCallback booleanValueChangedCallback) {
cursorOnDevice.addValueObserver(booleanValueChangedCallback);
}

public Device getFocusDevice() {
return focusDevice;
}

public Device getCurrentDevice() {
return cursorDeviceControl.getCursorDevice();
}

public void addNewDeviceAfter() {
if (focusDevice.exists().get()) {
focusDevice.afterDeviceInsertionPoint().browse();
} else {
deviceBank.browseToInsertDevice(0);
}
}

public void addNewDeviceBefore() {
if (focusDevice.exists().get()) {
focusDevice.beforeDeviceInsertionPoint().browse();
} else {
deviceBank.browseToInsertDevice(0);
}
}

public void initiateBrowsing() {
if (focusDevice.exists().get()) {
focusDevice.replaceDeviceInsertionPoint().browse();
} else {
deviceBank.browseToInsertDevice(0);
}
}

public void ensurePosition() {
final int currentDiveChainLocation = cursorDeviceControl.getCursorDevice().position().get();
if (!cursorOnDevice.get()) {
cursorDeviceControl.getCursorDevice().selectDevice(focusDevice);
}
}


public BasicIntValue getTrackIndex() {
return trackIndex;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public BooleanValue getExists() {
}

@Override
public DeviceTypeFollower getCurrentFollower() {
public DeviceTypeFollower getDeviceFollower() {
return deviceFollower;
}

Expand Down
Loading

0 comments on commit 7174bac

Please sign in to comment.