MIDI beginner - you need to play one note

I don’t know much about the Java MIDI function. In fact, it amazes me completely. however, I would like to just build a simple application that will play one note.

How to play a single MIDI note using Java Sound?

There is virtually no support for this on the Internet, and I am completely at a loss.

+7
source share
2 answers

I know this is a really old question, but as a novice programmer, it was very difficult for me to figure out how to do this, so I decided to share the next welcome program in a world in which Java play one mid-note to help someone to begin.

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 created mainly by cutting, pasting and messing with the code found in several online tutorials. Here are the most useful guides I've found:

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

http://patater.com/gbaguy/javamidi.htm Features of non-working code written by a 15-year-old. It was amazing - the most useful thing I found.

Good luck to you. Greetings.

+7
source

Here you go:

MIDI tag stackoverflow info

There is no harm in research in the first place.

+3
source

All Articles