android studio raw midi,android - Raw midi values and bytes c++ - Stack Overflow

博客作者在尝试使用Superpowered库通过USB MIDI控制器发送MIDI信号时遇到问题。代码尝试将特定的MIDI数据(如C4音符的开始)作为参数传递给`send`函数,但遇到了整数溢出错误。作者提到已尝试多种方式,但应用总是崩溃,并且由于使用的是移动设备,无法直接进行调试。他们提供了修改过的GitHub示例项目的代码,并附带了构建错误信息。
摘要由CSDN通过智能技术生成

I use superpowered, I need send midi note to a controller midi.

The problem is that I saw a function send(int deviceID, unsigned char *data, int bytes);

Where in their source code say:

deviceID: Device identifier.

data: Raw MIDI data.

bytes: Number of

bytes.

I don't know the values that I need put exactly on data and bytes to work.

The raw midi could be 0x80 - 0x48 - 0x00(start of C4 note, pitch= 72, See values)

And the bytes 1001nnnn0kkkkkkk0kkkkkkk(note on event See values) for example?

Something like that:

SuperpoweredUSBMIDI::send(deviceID, reinterpret_cast(0x80 - 0x48 - 0x00), 1001nnnn0kkkkkkk0kkkkkkk);

The problem that always crash, and I can't debug or get the error for the reason that I use the mobile with otg to replicate the error.

When I find a solution, I will put it as soon as I can.

I'm newbie with markdown, sorry for any mistakes and my English grammar.

Edit: I'm using the example project that they have on GitHub for testing purposes, specifically the simpleusb project. (source)

I make a small modifications and work, but with this specifically I try with many ways and nothing. I think this simple macrochange at least could work if I insert well the values

class simpleusb.cpp:

#include

#include

#include

#include

#include

#include

// Called when the application is initialized. You can initialize SuperpoweredUSBSystem

// at any time btw. Although this function is marked __unused, it's due Android Studio's

// annoying warning only. It's definitely used.

__unused jint JNI_OnLoad (

JavaVM * __unused vm,

void * __unused reserved

) {

SuperpoweredUSBSystem::initialize(NULL, NULL, NULL, NULL, NULL);

return JNI_VERSION_1_6;

}

// Called when the application is closed. You can destroy SuperpoweredUSBSystem at any time btw.

// Although this function is marked __unused, it's due Android Studio's annoying warning only.

// It's definitely used.

__unused void JNI_OnUnload (

JavaVM * __unused vm,

void * __unused reserved

) {

SuperpoweredUSBSystem::destroy();

}

// A helper structure for sine wave output.

typedef struct sineWaveOutput {

float mul;

unsigned int step;

} sineWaveOutput;

// This is called periodically for audio I/O. Audio is always 32-bit floating point,

// regardless of the bit depth preference. (SuperpoweredUSBAudioProcessingCallback)

static bool audioProcessing (

void *clientdata,

int __unused deviceID,

float *audioIO,

int numberOfSamples,

int samplerate,

int __unused numInputChannels,

int numOutputChannels

) {

// If audioIO is NULL, then it's the very last call, IO is closing.

if (!audioIO) {

// Free memory for sine wave struct.

free(clientdata);

return true;

}

sineWaveOutput *swo = (sineWaveOutput *)clientdata;

if (swo->mul == 0.0f) swo->mul = (2.0f * float(M_PI) * 300.0f) / float(samplerate);

// Output sine wave on all output channels.

for (int n = 0; n < numberOfSamples; n++) {

float v = sinf(swo->step++ * swo->mul) * 0.5f;

for (int c = 0; c < numOutputChannels; c++) *audioIO++ = v;

}

return true; // Return false for silence, true if we put audio output into audioIO.

}

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

static int latestMidiCommand = -1;

static int latestMidiChannel = 0;

static int latestMidiNumber = 0;

static int latestMidiValue = 0;

// This is called when some MIDI data is coming in.

// We are doing some primitive MIDI data processing here.

static void onMidiReceived (

void * __unused clientdata,

int __unused deviceID,

unsigned char *data,

int bytes

) {

while (bytes > 0) {

if (*data > 127) {

int command = *data >> 4;

switch (command) {

case 8: // note off

case 9: // note on

case 11: // control change

pthread_mutex_lock(&mutex);

// store incoming MIDI data

latestMidiCommand = command;

latestMidiChannel = *data++ & 15;

latestMidiNumber = *data++;

latestMidiValue = *data++;

pthread_mutex_unlock(&mutex);

bytes -= 3;

break;

default:

data++;

bytes--;

}

} else {

data++;

bytes--;

}

}

}

// Beautifying the ugly Java-C++ bridge (JNI) with these macros.

#define PID com_superpowered_simpleusb_SuperpoweredUSBAudio // Java package name and class name. Don't forget to update when you copy this code.

#define MAKE_JNI_FUNCTION(r, n, p) extern "C" JNIEXPORT r JNICALL Java_ ## p ## _ ## n

#define JNI(r, n, p) MAKE_JNI_FUNCTION(r, n, p)

// This is called by the SuperpoweredUSBAudio Java object when a USB device is connected.

JNI(jint, onConnect, PID) (

JNIEnv *env,

jobject __unused obj,

jint deviceID,

jint fd,

jbyteArray rawDescriptor

) {

jbyte *rd = env->GetByteArrayElements(rawDescriptor, NULL);

int dataBytes = env->GetArrayLength(rawDescriptor);

int r = SuperpoweredUSBSystem::onConnect(deviceID, fd, (unsigned char *)rd, dataBytes);

env->ReleaseByteArrayElements(rawDescriptor, rd, JNI_ABORT);

// r is 0 if SuperpoweredUSBSystem can't do anything with the connected device.

// r & 2 is true if the device has MIDI. Start receiving events.

if (r & 2) {

SuperpoweredUSBMIDI::startIO(deviceID, NULL, onMidiReceived);

//TODO HERE IT'S THE PROBLEM: error: integer literal is too large to be represented in any integer type

SuperpoweredUSBMIDI::send(deviceID, reinterpret_cast(0x80 - 0x48 - 0x00), 100100010011100000000011);

//FINISH PROBLEM

}

// r & 1 is true if the device has audio. Start output.

if (r & 1) {

// allocate struct for sine wave oscillator

sineWaveOutput *swo = (sineWaveOutput *)malloc(sizeof(sineWaveOutput));

if (swo) {

swo->mul = 0.0f;

swo->step = 0;

SuperpoweredCPU::setSustainedPerformanceMode(true);

// Our preferred settings: 44100 Hz, 16 bits, 0 input channels, 256 output channels,

// low latency. Superpowered will set up the audio device as close as it can to these.

SuperpoweredUSBAudio::easyIO (

deviceID, // deviceID

44100, // sampling rate

16, // bits per sample

0, // numInputChannels

256, // numOutputChannels

SuperpoweredUSBLatency_Low, // latency

swo, // clientData

audioProcessing // SuperpoweredUSBAudioProcessingCallback

);

}

}

return r;

}

// This is called by the SuperpoweredUSBAudio Java object when a USB device is disconnected.

JNI(void, onDisconnect, PID) (

JNIEnv * __unused env,

jobject __unused obj,

jint deviceID

) {

SuperpoweredUSBSystem::onDisconnect(deviceID);

SuperpoweredCPU::setSustainedPerformanceMode(false);

}

#undef PID

#define PID com_superpowered_simpleusb_MainActivity

// This is called by the MainActivity Java object periodically.

JNI(jintArray, getLatestMidiMessage, PID) (

JNIEnv *env,

jobject __unused obj

) {

jintArray ints = env->NewIntArray(4);

jint *i = env->GetIntArrayElements(ints, 0);

pthread_mutex_lock(&mutex);

i[0] = latestMidiCommand;

i[1] = latestMidiChannel;

i[2] = latestMidiNumber;

i[3] = latestMidiValue;

pthread_mutex_unlock(&mutex);

env->ReleaseIntArrayElements(ints, i, 0);

return ints;

}

The other important class but I don't change on this problem, MainActivity:

@RequiresApi(api = Build.VERSION_CODES.M)

public class MainActivity extends AppCompatActivity implements SuperpoweredUSBAudioHandler {

private Handler handler;

private TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textView = findViewById(R.id.text);

SuperpoweredUSBAudio usbAudio = new SuperpoweredUSBAudio(getApplicationContext(), this);

usbAudio.check();

// Update UI every 40 ms.

Runnable runnable = new Runnable() {

@Override

public void run() {

int[] midi = getLatestMidiMessage();

switch (midi[0]) {

case 8: textView.setText(String.format(Locale.ENGLISH, "Note Off, CH %d, %d, %d",

midi[1] + 1, midi[2], midi[3]));

break;

case 9: textView.setText(String.format(Locale.ENGLISH, "Note On, CH %d, %d, %d",

midi[1] + 1, midi[2], midi[3]));

break;

case 11: textView.setText(String.format(Locale.ENGLISH, "Control Change, CH %d, %d, %d",

midi[1] + 1, midi[2], midi[3]));

break;

}

handler.postDelayed(this, 40);

}

};

handler = new Handler();

handler.postDelayed(runnable, 40);

/*Not look, only for testing purposes and for remember what use.

byte[] buffer = new byte[32];

int numBytes = 0;

int channel = 6; // MIDI channels 1-16 are encoded as 0-15.

buffer[numBytes++] = (byte)(0x90 + (channel - 1)); // note on

buffer[numBytes++] = (byte)60; // pitch is middle C

buffer[numBytes++] = (byte)127; // max velocity

int offset = 0;*/

}

public void onUSBAudioDeviceAttached(int deviceIdentifier) {

}

public void onUSBMIDIDeviceAttached(int deviceIdentifier) {

}

public void onUSBDeviceDetached(int deviceIdentifier) {

}

// Function implemented in the native library.

private native int[] getLatestMidiMessage();

static {

System.loadLibrary("SuperpoweredExample");

}

}

Error that I can't build app finally:

Build command failed.

Error while executing process D:\Users\ramoc\AppData\Local\Android\sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build F:\PROYECTOFIN\SuperpoweredUSBExample\simpleusb\.externalNativeBuild\cmake\debug\arm64-v8a --target SuperpoweredExample}

[1/2] Building CXX object CMakeFiles/SuperpoweredExample.dir/simpleusb.cpp.o

FAILED: D:\Users\ramoc\AppData\Local\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android --gcc-toolchain=D:/Users/ramoc/AppData/Local/Android/sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=D:/Users/ramoc/AppData/Local/Android/sdk/ndk-bundle/sysroot -DSuperpoweredExample_EXPORTS -IF:/PROYECTOFIN/SuperpoweredUSBExample/simpleusb/src/main/jni/src/main/jni -IF:/PROYECTOFIN/SuperpoweredUSBExample/simpleusb/../../../Superpowered -isystem D:/Users/ramoc/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include -isystem D:/Users/ramoc/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/arm64-v8a/include -isystem D:/Users/ramoc/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include/backward -isystem D:/Users/ramoc/AppData/Local/Android/sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fsigned-char -IF:\PROYECTOFIN\SuperpoweredUSBExample\simpleusb\..\..\..\Superpowered -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/SuperpoweredExample.dir/simpleusb.cpp.o -MF CMakeFiles\SuperpoweredExample.dir\simpleusb.cpp.o.d -o CMakeFiles/SuperpoweredExample.dir/simpleusb.cpp.o -c F:\PROYECTOFIN\SuperpoweredUSBExample\simpleusb\src\main\jni\simpleusb.cpp

F:\PROYECTOFIN\SuperpoweredUSBExample\simpleusb\src\main\jni\simpleusb.cpp:129:100: error: integer literal is too large to be represented in any integer type

SuperpoweredUSBMIDI::send(deviceID, reinterpret_cast(0x80 - 0x48 - 0x00), 100100010011100000000011);

^

F:\PROYECTOFIN\SuperpoweredUSBExample\simpleusb\src\main\jni\simpleusb.cpp:129:100: warning: implicit conversion from 'unsigned long long' to 'int' changes value from 7976667151972931595 to 887068683 [-Wconstant-conversion]

SuperpoweredUSBMIDI::send(deviceID, reinterpret_cast(0x80 - 0x48 - 0x00), 100100010011100000000011);

~~~~~~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~

1 warning and 1 error generated.

ninja: build stopped: subcommand failed.

Maybe it's for the documentation, very newbie with jni or too complex to me for now to understand 100%.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值