MIDI beginner - need to play one note
Asked Answered
H

2

16

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.

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.

Heffron answered 9/5, 2013 at 13:22 Comment(4)
What tutorial have you gone through so far and how is it not working?Oilcan
Go through the resources linked from the Java Sound info. page. There is also a short source that should play a MIDI tune.Mordred
"Head First Java" by Sierra and Bates has example code for this.Conference
None of these comments is helpful. (Please do not punish newbies by answering their questions literally, saying RTFM, or refusing to think about what they're really asking.)Megathere
A
25

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 ) {
            e.printStackTrace();
        }
        mChannels[0].noteOff(60);//turn of the note
    
    
      } catch (MidiUnavailableException e) {
         e.printStackTrace();
      }
   }

}    

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/

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

Features nonworking code written by a 15 year old. This was - surprisingly - the most helpful thing I found.

Amber answered 7/4, 2016 at 4:31 Comment(0)
R
3

Here you go :

MIDI Tag Info on stackoverflow

  1. UnderStanding MIDI
  2. Tutorial on Oracle
  3. Accessing MIDI
  4. MIdi Synthesis
Reticle answered 9/5, 2013 at 13:33 Comment(1)
I also want to add another link that was very helpful for me: cs.cmu.edu/~music/cmsip/readings/…Myosotis

© 2022 - 2024 — McMap. All rights reserved.