原始midi值和字节c ++

时间:2018-06-08 10:39:41

标签: android c++ midi usb-otg

我使用superpowered,我需要将midi音符发送到控制器midi 问题是我看到了一个函数send(int deviceID, unsigned char *data, int bytes); 源代码中的位置说:

  

deviceID:设备标识符。
  数据:原始MIDI数据   bytes:数量   字节。

我不知道我需要在数据和字节上使用的值。
0x80 - 0x48 - 0x00(C4音符开头,音高= 72,See values
字节1001nnnn0kkkkkkk0kkkkkkk(注意事件See values)例如?
这样的事情:

  

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

总是崩溃的问题,我无法调试或获取错误,因为我使用移动设备otg来复制错误。
当我找到解决方案时,我会尽快把它解决。

我是markdown的新手,抱歉有任何错误和我的英语语法。

编辑:我使用他们在GitHub上的示例项目进行测试,特别是simpleusb项目。 (source

我进行了一些小修改和工作,但具体来说,我尝试了很多方法,没有任何方法。我认为如果我很好地插入值,这个简单的macrochange至少可以工作 class simpleusb.cpp

#include <jni.h>
#include <math.h>
#include <SuperpoweredCPU.h>
#include <AndroidIO/SuperpoweredUSBAudio.h>
#include <malloc.h>
#include <pthread.h>

// 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<unsigned char *>(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;
}

另一个重要的课程,但我没有改变这个问题, 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");
    }






}

错误我最终无法构建应用

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<unsigned char *>(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<unsigned char *>(0x80 - 0x48 - 0x00), 100100010011100000000011);
        ~~~~~~~~~~~~~~~~~~~                                                                        ^~~~~~~~~~~~~~~~~~~~~~~~
1 warning and 1 error generated.
ninja: build stopped: subcommand failed.

对于文档来说,也许它是非常新手的jni,或者对我来说过于复杂,现在要100%理解。

2 个答案:

答案 0 :(得分:2)

好的,这就是driver = new ChromeDriver(); 所说的:

send

send(int deviceID, unsigned char *data, int bytes); 发送指向名为deviceId的缓冲区的指针,该缓冲区具有一定数量的data

所以当你说:     SuperpoweredUSBMIDI :: send(deviceID,reinterpret_cast(0x80 - 0x48 - 0x00),100100010011100000000011);

你基本上说的是“减去这3个数字:bytes”,然后将该数字重新解释为指向内存中某个缓冲区的指针。内存中的缓冲区包含我希望您阅读的0x80 - 0x48 - 0x00字节数据。

要解决此问题,我们会发送如下数据:

100100010011100000000011

答案 1 :(得分:0)

我认为midi在序列后附加了校验和值(字节)-是在您的代码中还是在库代码中完成的?

消息应该是一个无符号字符数组,并传递该数组的地址(名称) 嗯,这就是我编写midi时id在C中所做的事情。