MIDI初学者 - 需要播放一个音符 [英] MIDI beginner - need to play one note

查看:179
本文介绍了MIDI初学者 - 需要播放一个音符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解Java的MIDI功能。事实上,它完全让我感到困惑。然而,我想做的是只是构建一个简单的应用程序,它将播放一个音符。

I don't know very much about Java's MIDI function. In fact, it utterly bewilders me. what I'd like to do however is just build a simple application that will play one note.

如何使用Java Sound播放单个MIDI音符?

How to play a single MIDI note using Java Sound?

在网上支持这个音符是几乎不存在,我完全不知所措。

The support for this out on the web is almost nonexistent, and I am totally at a loss.

推荐答案

我知道这是一个非常古老的问题,但作为一名新手程序员,我很难搞清楚如何要做到这一点,所以我想我会分享下面的hello-world风格的程序,让Java播放一个midi音符,以帮助其他任何人开始。

I know this is a really old question, but, as a novice programmer, I had a very difficult time figuring out how to do this, so I thought I would share the following hello-world-style program that gets Java to play a single midi note in order to help anyone else getting started.

import javax.sound.midi.*;

public class MidiTest{

    public static void main(String[] args) { 
      try{
        /* Create a new Sythesizer and open it. Most of 
         * the methods you will want to use to expand on this 
         * example can be found in the Java documentation here: 
         * https://docs.oracle.com/javase/7/docs/api/javax/sound/midi/Synthesizer.html
         */
        Synthesizer midiSynth = MidiSystem.getSynthesizer(); 
        midiSynth.open();

        //get and load default instrument and channel lists
        Instrument[] instr = midiSynth.getDefaultSoundbank().getInstruments();
        MidiChannel[] mChannels = midiSynth.getChannels();

        midiSynth.loadInstrument(instr[0]);//load an instrument


        mChannels[0].noteOn(60, 100);//On channel 0, play note number 60 with velocity 100 
        try { Thread.sleep(1000); // wait time in milliseconds to control duration
        } catch( InterruptedException e ) { }
        mChannels[0].noteOff(60);//turn of the note


      } catch (MidiUnavailableException e) {}
   }

}    

上面的代码主要是通过剪切,粘贴和搞乱几个在线教程中的代码来创建的。以下是我发现的最有用的教程:

The above code was primarily created by cutting, pasting, and messing around with code found in several online tutorials. Here are the most helpful tutorials that I found:

http://www.ibm.com/developerworks/library/it/it-0801art38/
这是一个很棒的教程,可能有你想要的一切;然而,一开始可能有点压倒性。

http://www.ibm.com/developerworks/library/it/it-0801art38/ This is a great tutorial and probably has everything you are looking for; however, it may be a little overwhelming at first.

http://patater.com/gbaguy/javamidi.htm
包含15岁的非工作代码。这是 - 令人惊讶的 - 这是我找到的最有用的东西。

http://patater.com/gbaguy/javamidi.htm Features nonworking code written by a 15 year old. This was - surprisingly - the most helpful thing I found.

祝你好运。
干杯。

Best of luck to you. Cheers.

这篇关于MIDI初学者 - 需要播放一个音符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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