从Java的MIDI控制器接收com.sun.media.sound.FastShortMessage,该如何解码? [英] Receiving com.sun.media.sound.FastShortMessage from MIDI Controller in Java, how to decode?

查看:156
本文介绍了从Java的MIDI控制器接收com.sun.media.sound.FastShortMessage,该如何解码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个Java程序,该程序从控制器接收MIDI输入,并且根据发送的Midi音符,理想情况下可以做不同的事情(不一定与播放合成器的输出有关).

I have a java program set up that takes MIDI input from a controller and ideally does different things (not necessarily related to playing synthesizer output) depending on which midi note is sent.

我的代码很大程度上基于以下SO问题中的代码: Java从Java获取输入MIDI键盘,特别是我正在使用整个MidiInputReceiver类.我已经对其进行了修改,以执行System.out.println(msg)并打印收到的MIDI",并且它似乎在每次我按控制器上的键时都起作用,它会检测到MIDI并打印midi msg ,但我什至不知道如何将输出解码为可以解密的内容.

My code is heavily based on the code in this SO question: Java getting input from MIDI keyboard, specifically I'm using the entire MidiInputReceiver class. I've modified it to do System.out.println(msg) as well as printing "MIDI received, and it appears to work in as far as every time I press a key from my controller it detects the MIDI and prints the midi msg, but I don't know how to decode the output into something I can decipher, if that's even possible.

我得到的输出是:

midi received
com.sun.media.sound.FastShortMessage@75b0e2c3
midi received
com.sun.media.sound.FastShortMessage@2ff7ac92
midi received
com.sun.media.sound.FastShortMessage@2d62bdd8
midi received
com.sun.media.sound.FastShortMessage@2d9dc72f

我一直在尝试使用Java类 http://www.jsresources .org/examples/DumpReceiver.java.html 来解码消息,但它仅解码ShortMessages,而不解码FastShortMessages,而且我在网上找不到任何有关FastShortMessage是什么的文档,更不用说如何从将FSM转换为SM.有人有什么想法吗?有比我正在做的事更简单的方法吗?

I've been trying to use this Java class http://www.jsresources.org/examples/DumpReceiver.java.html to decode the message, but it only decodes ShortMessages, not FastShortMessages, and I can't find any documentation online about what a FastShortMessage is, much less how to convert from an FSM to an SM. Does anybody have any ideas? Is there an easier way than what I'm doing?

这可能不是最好的方法,但是我想出了一种可行的方法,我无法在8个小时内回答自己的帖子,但如果有人需要,我会在这里发布.

This might not be the best way, but I just came up with a way that works, I can't answer my own post for 8 hours but I'll post it here in case anybody else needs it.

我使用下面的代码设法解决了自己的问题.

I just managed to solve my own problem, with the code below.

public void send(MidiMessage msg, long timeStamp) {
    // Print to confirm signal arrival
    System.out.println("midi received");

    byte[] aMsg = msg.getMessage();
    // take the MidiMessage msg and store it in a byte array

    // msg.getLength() returns the length of the message in bytes
    for(int i=0;i<msg.getLength();i++){
        System.out.println(aMsg[i]);
        // aMsg[0] is something, velocity maybe? Not 100% sure.
        // aMsg[1] is the note value as an int. This is the important one.
        // aMsg[2] is pressed or not (0/100), it sends 100 when they key goes down,  
        // and 0 when the key is back up again. With a better keyboard it could maybe
        // send continuous values between then for how quickly it's pressed? 
        // I'm only using VMPK for testing on the go, so it's either 
        // clicked or not.
    }
    System.out.println();
}

两个按键的示例输出:

midi received 
-103
71
100

midi received
-119
71
0

midi received
-103
52
100

midi received
-119
52
0

因此aMsg [1]保留每个音符的Midi编号,可以在网上的任何地方引用该编号,由于重复,我无法发布链接.

So aMsg[1] holds the Midi number for each note, which can be referenced anywhere online, I can't post a link due to rep.

getMessage()和getLength()方法来自

The getMessage() and getLength() methods come from http://docs.oracle.com/javase/7/docs/api/javax/sound/midi/MidiMessage.html, I still haven't figured out what a FastShortMessage is, but it might just be leftover legacy code or something? It's com.sun stuff so it's got to be pretty old.

从这里开始,我可以在aMsg [1]上执行switch()语句,并根据所按下的键的大小写不同,这正是我要执行的操作,它向后兼容1.6,因为它是整数.

From here I can do a switch() statement on the aMsg[1] with a different case depending on which key is pressed, which is exactly what I was aiming to do, which will be backwards compatible to 1.6 since it's an integer.

推荐答案

FastShortMessage(间接)从MidiMessage派生;就像一个人一样处理.

FastShortMessage is (indirectly) derived from MidiMessage; just handle it like one.

拥有ShortMessage时,应使用getCommand/getChannel/getData1/2函数,这些函数比临时字节数组更易于使用.

When you have a ShortMessage, you should use the getCommand/getChannel/getData1/2 functions, which are easier to use than a temporary byte array.

这篇关于从Java的MIDI控制器接收com.sun.media.sound.FastShortMessage,该如何解码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆