Class StdMidi


  • public class StdMidi
    extends Object
    The StdMidi class provides easy-to-use static methods for playing musical notes in real time using MIDI. It also supports reading and writing audio files in the MIDI format. The Musical Instrument Digital Interface (MIDI) standard is a communication protocol that allows computers, musical instruments, and other hardware to communicate. StdMidi is built on top of Java's Sound API, a powerful framework for audio playback, recording, mixing, MIDI synthesis, and more. The goal is to make real-time MIDI synthesis accessible to novice programmers. Advanced features of MIDI (such as sequencing or multiple instruments at the same time) are beyond the scope of this library.

    Getting started. To use this class, you must have StdMidi in your Java classpath. Here are three possible ways to do this:

    • If you ran our autoinstaller, use the commands javac-introcs and java-introcs (or javac-algs4 and java-algs4) when compiling and executing. These commands add stdlib.jar (or algs4.jar) to the Java classpath, which provides access to StdMidi.
    • Download stdlib.jar (or algs4.jar) and add it to the Java classpath.
    • Download StdMidi.java and put it in the working directory.

    As a test, cut-and-paste the following short program into your editor:

      public class AxelF {
            StdMidi.setInstrument(StdMidi.SYNTH_BASS_1);
            StdMidi.setTempo(220);
    
            int[] pitches1  = { F4, REST, AF4, REST,  F4,  F4, BF4, F4, EF4 };
            double[] beats1 = { QN,   QN,  QN,   EN,  QN,  EN,  QN, QN,  QN };
            int[] pitches2  = { F4, REST,  C5, REST,  F4,  F4, DF5, C5, AF4 };
            double[] beats2 = { QN,   QN,  QN,   EN,  QN,  EN,  QN, QN,  QN };
            int[] pitches3  = { F4,   C5,  F5,   F4, EF4, EF4,  C4, G4,  F4, REST };
            double[] beats3 = { QN,   QN,  QN,   EN,  QN,  EN,  QN, QN, DQN, WN   };
    
            for (int i = 0; i < pitches1.length; i++)
                StdMidi.playNote(pitches1[i], beats1[i]);
            for (int i = 0; i < pitches2.length; i++)
                StdMidi.playNote(pitches2[i], beats2[i]);
            for (int i = 0; i < pitches3.length; i++)
                StdMidi.playNote(pitches3[i], beats3[i]);
          }
      }
      

    If you compile and execute the program, you should hear the first few notes of the electronic instrumental track Axel F by Harold Faltermeyer.

    Playing pitched instruments. You can use the following method to play an individual MIDI note with a given pitch::

    The MIDI note number is an integer between 0 and 127 (60 = Middle C) that specifies the pitch. The method plays the specified note for the specified duration (measured in beats). The special pitch StdMidi.REST corresponds to a rest. A rest has a duration but produces no sound.

    Playing unpitched percussion instruments. Unpitched instruments are not tuned to identifiable frequencies. You can use the following method to play unpitched percussion instruments (such as drums and cymbals):

    The method plays the specified percussive instrument for the specified duration (measured in beats). The percussionInstrument is an integer between 35 (acoustic bass drum) and 81 (open triangle).

    Durations. The durations are measured in beats, with one beat corresponding to a quarter note. The length of a beat is determined by the tempo, which is measured in beats per minute. The default tempo is 120 beats per minute, so each beat (or quarter note) lasts 0.5 seconds.

    There are a number of predefined constants for common tempos, ranging from StdMidi.LARGHISSIMO (20 beats per minute) to StdMidi.PRESTISSIMO (200 beats per minute). You can set the tempo using the method:

    The StdMidi class provides predefined constants for standard musical durations (measured in beats), including StdMidi.QUARTER_NOTE (1 beat), StdMidi.HALF_NOTE (2 beats), StdMidi.WHOLE_NOTE (4 beats), StdMidi.EIGHTH_NOTE (1/2 beat), and StdMidi.SIXTEENTH_NOTE (1/4 beat). For brevity, you can also use StdMidi.QN, StdMidi.HN, StdMidi.WN, StdMidi.EN, and StdMidi.SN.

    Key velocities. The key-down velocity indiciates the force with which a note is played. It controls the note's volume and/or brightness. Velocities range from 0 (silent) to 127 (loudest). The default MIDI velocity is 96. You can use change the key-down velocity using the method:

    Instruments. The default MIDI instrument is an Acousic Grand Piano. You can use change the instrument using the method:

    Subsequent notes will be synthesized using that instrument. The instrument argument must be an integer between 1 and 128. The instrument is identified using the General MIDI standard, which specifies 128 individual instruments and numbers them from 1 (Acoustic Grand Piano) to 128 (Gunshot). The StdMidi class provides pre-defined constants for these instruments, such as StdMidi.ACOUSTIC_GRAND_PIANO and StdMidi.GUNSHOT. Depending on the soundfont, each instrument may sound different.

    Playing multiple notes at the same time. For added control, you can use the following methods to play several notes (of different durations) at the same time.

    If a note has a natural decay (such as a piano or bass drum), it is not strictly necessary to call noteOff() or percussionOff(). Nevertheless, it is good practice to do so in order to avoid allocating unnecessary resources for a note that is no longer making sound. Also, some instruments have limited polyphony (number of notes you can play at the same time), so you may exceed this limit if you don't explicitly turn off the notes.

    Playing MIDI files. You can use the following method to play a MIDI file:

    The play() method plays the MIDI file and waits until the audio file finishes playing before continuing. The playInBackground() method plays the MIDI file in a background thread (e.g., as a background score in your program). The filename must have the extension .mid or .midi.

    Saving MIDI files. You can use the following method to save the sequence of notes to a MIDI file:

    The filename must have the extension .mid or .midi.

    Soundfonts. A soundfont stores samples of musical instruments for MIDI playback. This determines how each musical instruments sounds. We recommend FluidR3, pro-quality soundfont developed by Frank Wen and released under an open-source license. If you ran our autoinstaller, it should be installed and configured automatically. If not, Java will default to an internal soundfont, known as Gervill. (On OS X, it is located in ~/.gervill/soundbank-emg.sf2.) Alternatively, you can download FluidR3_GM2-2.sf2 and install it at /usr/local/lift/Soundfonts/FluidR3_GM2-2.sf2.

    Author:
    Kevin Wayne
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int A_1
      The note A in octave -1.
      static int A0
      The note A in octave 0.
      static int A1
      The note A in octave 1.
      static int A2
      The note A in octave 2.
      static int A3
      The note A in octave 3.
      static int A4
      The note A in octave 4.
      static int A5
      The note A in octave 5.
      static int A6
      The note A in octave 6.
      static int A7
      The note A in octave 7.
      static int A8
      The note A in octave 8.
      static int ACCORDION
      The instrument Accordion.
      static int ACOUSTIC_BASS
      The instrument Acoustic Bass.
      static int ACOUSTIC_BASS_DRUM
      The percussion instrument Acoustic Bass Drum.
      static int ACOUSTIC_GRAND_PIANO
      The instrument Acoustic Grand Piano.
      static int ACOUSTIC_SNARE
      The percussion instrument Acoustic Snare.
      static int ADAGIETTO
      The tempo adagietto (74 beats per minute).
      static int ADAGIO
      The tempo adagio (70 beats per minute).
      static int AF_1
      The note A♭ in octave -1.
      static int AF0
      The note A♭ in octave 0.
      static int AF1
      The note A♭ in octave 1.
      static int AF2
      The note A♭ in octave 2.
      static int AF3
      The note A♭ in octave 3.
      static int AF4
      The note A♭ in octave 4.
      static int AF5
      The note A♭ in octave 5.
      static int AF6
      The note A♭ in octave 6.
      static int AF7
      The note A♭ in octave 7.
      static int AF8
      The note A♭ in octave 8.
      static int AGOGÔ
      The instrument Agogô.
      static int AHH_CHOIR
      The instrument Ahh Choir.
      static int ALLEGRO
      The tempo allegro (140 beats per minute).
      static int ALTO_SAX
      The instrument Alto Sax.
      static int ANDANTE
      The tempo andante (90 beats per minute).
      static int ANDANTINO
      The tempo andantino (96 beats per minute).
      static int APPLAUSE
      The instrument Applause.
      static int AS_1
      The note A♯ in octave -1.
      static int AS0
      The note A♯ in octave 0.
      static int AS1
      The note A♯ in octave 1.
      static int AS2
      The note A♯ in octave 2.
      static int AS3
      The note A♯ in octave 3.
      static int AS4
      The note A♯ in octave 4.
      static int AS5
      The note A♯ in octave 5.
      static int AS6
      The note A♯ in octave 6.
      static int AS7
      The note A♯ in octave 7.
      static int AS8
      The note A♯ in octave 8.
      static int ATMOSPHERE
      The instrument Atmosphere.
      static int B_1
      The note B in octave -1.
      static int B0
      The note B in octave 0.
      static int B1
      The note B in octave 1.
      static int B2
      The note B in octave 2.
      static int B3
      The note B in octave 3.
      static int B4
      The note B in octave 4.
      static int B5
      The note B in octave 5.
      static int B6
      The note B in octave 6.
      static int B7
      The note B in octave 7.
      static int B8
      The note B in octave 8.
      static int BAGPIPE
      The instrument Bagpipe.
      static int BANDONEON
      The instrument Bandoneon.
      static int BANJO
      The instrument Banjo.
      static int BARITONE_SAX
      The instrument Baritone Sax.
      static int BASS_AND_LEAD
      The instrument Bass and Lead.
      static int BASSOON
      The instrument Bassoon.
      static int BF_1
      The note B♭ in octave -1.
      static int BF0
      The note B♭ in octave 0.
      static int BF1
      The note B♭ in octave 1.
      static int BF2
      The note B♭ in octave 2.
      static int BF3
      The note B♭ in octave 3.
      static int BF4
      The note B♭ in octave 4.
      static int BF5
      The note B♭ in octave 5.
      static int BF6
      The note B♭ in octave 6.
      static int BF7
      The note B♭ in octave 7.
      static int BF8
      The note B♭ in octave 8.
      static int BIRD_TWEET
      The instrument Bird Tweet.
      static int BOTTLE_CHIFF
      The instrument Bottle Chiff.
      static int BOWED_GLASS
      The instrument Bowed Glass.
      static int BRASS_SECTION
      The instrument Brass Section.
      static int BREATH_NOISE
      The instrument Breath Noise.
      static int BRIGHT_ACOUSTIC_PIANO
      The instrument Bright Acoustic Piano.
      static int BRIGHTNESS
      The instrument Brightness.
      static int C_1
      The note C in octave -1.
      static int C0
      The note C in octave 0.
      static int C1
      The note C in octave 1.
      static int C2
      The note C in octave 2.
      static int C3
      The note C in octave 3.
      static int C4
      The note C in octave 4.
      static int C5
      The note C in octave 5.
      static int C6
      The note C in octave 6.
      static int C7
      The note C in octave 7.
      static int C8
      The note C in octave 8.
      static int C9
      The note C in octave 9.
      static int CABASA
      The percussion instrument Cabasa.
      static int CALLIOPE_LEAD
      The instrument Calliope Lead.
      static int CELESTA
      The instrument Celesta.
      static int CELLO
      The instrument Cello.
      static int CHARANG
      The instrument Charang.
      static int CHIFFER_LEAD
      The instrument Chiffer Lead.
      static int CHINESE_CYMBAL
      The percussion instrument Chinese Cymbal.
      static int CHURCH_ORGAN
      The instrument Church Organ.
      static int CLARINET
      The instrument Clarinet.
      static int CLAVES
      The percussion instrument Claves.
      static int CLAVINET
      The instrument Clavinet.
      static int CLEAN_GUITAR
      The instrument Clean Guitar.
      static int CLOSED_HI_HAT
      The percussion instrument Closed Hi Hat.
      static int CONCERT_A
      The note A in octave 4, also known as Concert A and A440.
      static int CONTRABASS
      The instrument Contrabass.
      static int COWBELL
      The percussion instrument Cowbell.
      static int CRASH_CYMBAL_1
      The percussion instrument Crash Cymbal 1.
      static int CRASH_CYMBAL_2
      The percussion instrument Crash Cymbal 2.
      static int CRYSTAL
      The instrument Crystal.
      static int CS_1
      The note C♯ in octave -1.
      static int CS0
      The note C♯ in octave 0.
      static int CS1
      The note C♯ in octave 1.
      static int CS2
      The note C♯ in octave 2.
      static int CS3
      The note C♯ in octave 3.
      static int CS4
      The note C♯ in octave 4.
      static int CS5
      The note C♯ in octave 5.
      static int CS6
      The note C♯ in octave 6.
      static int CS7
      The note C♯ in octave 7.
      static int CS8
      The note C♯ in octave 8.
      static int CS9
      The note C♯ in octave 9.
      static int D_1
      The note D in octave -1.
      static int D0
      The note D in octave 0.
      static int D1
      The note D in octave 1.
      static int D2
      The note D in octave 2.
      static int D3
      The note D in octave 3.
      static int D4
      The note D in octave 4.
      static int D5
      The note D in octave 5.
      static int D6
      The note D in octave 6.
      static int D7
      The note D in octave 7.
      static int D8
      The note D in octave 8.
      static int D9
      The note D in octave 9.
      static double DDEN
      The duration of a double dotted eighth note (7/8 beat).
      static double DDHN
      The duration of a double dotted half note (7/2 beats).
      static double DDQN
      The duration of a double dotted quarter note (7/4 beats).
      static int DEFAULT_INSTRUMENT
      The default MIDI instrument (Acoustic Grand Piano).
      static int DEFAULT_TEMPO
      The default MIDI tempo (120 beats per minute).
      static int DEFAULT_VELOCITY
      The default MIDI velocity (96).
      static double DEN
      The duration of a double dotted eighth note (3/4 beats).
      static int DF_1
      The note D♭ in octave -1.
      static int DF0
      The note D♭ in octave 0.
      static int DF1
      The note D♭ in octave 1.
      static int DF2
      The note D♭ in octave 2.
      static int DF3
      The note D♭ in octave 3.
      static int DF4
      The note D♭ in octave 4.
      static int DF5
      The note D♭ in octave 5.
      static int DF6
      The note D♭ in octave 6.
      static int DF7
      The note D♭ in octave 7.
      static int DF8
      The note D♭ in octave 8.
      static int DF9
      The note D♭ in octave 9.
      static double DHN
      The duration of a dotted half note (3 beats).
      static int DISTORTION_GUITAR
      The instrument Distortion Guitar.
      static double DOTTED_EIGHTH_NOTE
      The duration of a double dotted eighth note (3/4 beats).
      static double DOTTED_HALF_NOTE
      The duration of a dotted half note (3 beats).
      static double DOTTED_QUARTER_NOTE
      The duration of a dotted quarter note (3/2 beats).
      static double DOTTED_SIXTEENTH_NOTE
      The duration of a dotted sixteenth note (3/8 beat).
      static double DOUBLE_DOTTED_EIGHTH_NOTE
      The duration of a double dotted eighth note (7/8 beat).
      static double DOUBLE_DOTTED_HALF_NOTE
      The duration of a double dotted half note (7/2 beats).
      static double DOUBLE_DOTTED_QUARTER_NOTE
      The duration of a double dotted quarter note (7/4 beats).
      static double DQN
      The duration of a dotted quarter note (3/2 beats).
      static int DRAWBAR_ORGAN
      The instrument Drawbar Organ.
      static int DS_1
      The note D♯ in octave -1.
      static int DS0
      The note D♯ in octave 0.
      static int DS1
      The note D♯ in octave 1.
      static int DS2
      The note D♯ in octave 2.
      static int DS3
      The note D♯ in octave 3.
      static int DS4
      The note D♯ in octave 4.
      static int DS5
      The note D♯ in octave 5.
      static int DS6
      The note D♯ in octave 6.
      static int DS7
      The note D♯ in octave 7.
      static int DS8
      The note D♯ in octave 8.
      static int DS9
      The note D♯ in octave 9.
      static double DSN
      The duration of a dotted sixteenth note (3/8 beat).
      static int DULCIMER
      The instrument Dulcimer.
      static int E_1
      The note E in octave -1.
      static int E0
      The note E in octave 0.
      static int E1
      The note E in octave 1.
      static int E2
      The note E in octave 2.
      static int E3
      The note E in octave 3.
      static int E4
      The note E in octave 4.
      static int E5
      The note E in octave 5.
      static int E6
      The note E in octave 6.
      static int E7
      The note E in octave 7.
      static int E8
      The note E in octave 8.
      static int E9
      The note E in octave 9.
      static int ECHO_DROPS
      The instrument Echo Drops.
      static int EF_1
      The note E♭ in octave -1.
      static int EF0
      The note E♭ in octave 0.
      static int EF1
      The note E♭ in octave 1.
      static int EF2
      The note E♭ in octave 2.
      static int EF3
      The note E♭ in octave 3.
      static int EF4
      The note E♭ in octave 4.
      static int EF5
      The note E♭ in octave 5.
      static int EF6
      The note E♭ in octave 6.
      static int EF7
      The note E♭ in octave 7.
      static int EF8
      The note E♭ in octave 8.
      static int EF9
      The note E♭ in octave 9.
      static double EIGHTH_NOTE
      The duration of an eighth note (1/2 beat).
      static double EIGHTH_NOTE_TRIPLET
      The duration of an eighth note triplet (1/3 beat).
      static int ELECTRIC_BASS_DRUM
      The percussion instrument Electric Bass Drum.
      static int ELECTRIC_GRAND_PIANO
      The instrument Electric Grand Piano.
      static int ELECTRIC_PIANO_1
      The instrument Electric Piano 1.
      static int ELECTRIC_PIANO_2
      The instrument Electric Piano 2.
      static int ELECTRIC_SNARE
      The percussion instrument Electric Snare.
      static double EN
      The duration of an eighth note (1/2 beat).
      static int ENGLISH_HORN
      The instrument English Horn.
      static double ENT
      The duration of an eighth note triplet (1/3 beat).
      static int F_1
      The note F in octave -1.
      static int F0
      The note F in octave 0.
      static int F1
      The note F in octave 1.
      static int F2
      The note F in octave 2.
      static int F3
      The note F in octave 3.
      static int F4
      The note F in octave 4.
      static int F5
      The note F in octave 5.
      static int F6
      The note F in octave 6.
      static int F7
      The note F in octave 7.
      static int F8
      The note F in octave 8.
      static int F9
      The note F in octave 9.
      static int FANTASIA
      The instrument Fantasia.
      static int FIDDLE
      The instrument Fiddle.
      static int FIFTH_SAWTOOTH_WAVE
      The instrument Fifth Sawtooth Wave.
      static int FINGERED_BASS
      The instrument Fingered Bass.
      static int FLUTE
      The instrument Flute.
      static int FORTE
      The velocity forte (85).
      static int FORTISSIMO
      The velocity fortissimo (100).
      static int FORTISSISSIMO
      The velocity fortississimo (120).
      static int FRENCH_HORN
      The instrument French Horn.
      static int FRET_NOISE
      The instrument Fret Noise.
      static int FRETLESS_BASS
      The instrument Fretless Bass.
      static int FS_1
      The note F♯ in octave -1.
      static int FS0
      The note F♯ in octave 0.
      static int FS1
      The note F♯ in octave 1.
      static int FS2
      The note F♯ in octave 2.
      static int FS3
      The note F♯ in octave 3.
      static int FS4
      The note F♯ in octave 4.
      static int FS5
      The note F♯ in octave 5.
      static int FS6
      The note F♯ in octave 6.
      static int FS7
      The note F♯ in octave 7.
      static int FS8
      The note F♯ in octave 8.
      static int FS9
      The note F♯ in octave 9.
      static int G_1
      The note G in octave -1.
      static int G0
      The note G in octave 0.
      static int G1
      The note G in octave 1.
      static int G2
      The note G in octave 2.
      static int G3
      The note G in octave 3.
      static int G4
      The note G in octave 4.
      static int G5
      The note G in octave 5.
      static int G6
      The note G in octave 6.
      static int G7
      The note G in octave 7.
      static int G8
      The note G in octave 8.
      static int G9
      The note G in octave 9.
      static int GF_1
      The note G♭ in octave -1.
      static int GF0
      The note G♭ in octave 0.
      static int GF1
      The note G♭ in octave 1.
      static int GF2
      The note G♭ in octave 2.
      static int GF3
      The note G♭ in octave 3.
      static int GF4
      The note G♭ in octave 4.
      static int GF5
      The note G♭ in octave 5.
      static int GF6
      The note G♭ in octave 6.
      static int GF7
      The note G♭ in octave 7.
      static int GF8
      The note G♭ in octave 8.
      static int GF9
      The note G♭ in octave 9.
      static int GLOCKENSPIEL
      The instrument Glockenspiel.
      static int GOBLIN
      The instrument Goblin.
      static int GRAVE
      The tempo grave (40 beats per minute).
      static int GS_1
      The note G♯ in octave -1.
      static int GS0
      The note G♯ in octave 0.
      static int GS1
      The note G♯ in octave 1.
      static int GS2
      The note G♯ in octave 2.
      static int GS3
      The note G♯ in octave 3.
      static int GS4
      The note G♯ in octave 4.
      static int GS5
      The note G♯ in octave 5.
      static int GS6
      The note G♯ in octave 6.
      static int GS7
      The note G♯ in octave 7.
      static int GS8
      The note G♯ in octave 8.
      static int GUITAR_HARMONICS
      The instrument Guitar Harmonics.
      static int GUNSHOT
      The instrument Gunshot.
      static double HALF_NOTE
      The duration of a half note (2 beats).
      static double HALF_NOTE_TRIPLET
      The duration of a half note triplet (4/3 beats).
      static int HALO_PAD
      The instrument Halo Pad.
      static int HAND_CLAP
      The percussion instrument Hand Clap.
      static int HARMONICA
      The instrument Harmonica.
      static int HARP
      The instrument Harp.
      static int HARPSICHORD
      The instrument Harpsichord.
      static int HELICOPTER
      The instrument Helicopter.
      static int HIGH_AGOGÔ
      The percussion instrument High Agogô.
      static int HIGH_BONGO
      The percussion instrument High Bongo.
      static int HIGH_FLOOR_TOM
      The percussion instrument High Floor Tom.
      static int HIGH_MID_TOM
      The percussion instrument High Mid Tom.
      static int HIGH_TIMBALE
      The percussion instrument High Timbale.
      static int HIGH_TOM
      The percussion instrument High Tom.
      static int HIGH_WOODBLOCK
      The percussion instrument High Woodblock.
      static double HN
      The duration of a half note (2 beats).
      static double HNT
      The duration of a half note triplet (4/3 beats).
      static int HONKY_TONK_PIANO
      The instrument Honky Tonk Piano.
      static int ICE_RAIN
      The instrument Ice Rain.
      static int JAZZ_GUITAR
      The instrument Jazz Guitar.
      static int KALIMBA
      The instrument Kalimba.
      static int KOTO
      The instrument Koto.
      static int LARGHETTO
      The tempo larghetto (60 beats per minute).
      static int LARGHISSIMO
      The tempo larghissimo (20 beats per minute).
      static int LARGO
      The tempo largo (50 beats per minute).
      static int LONG_GUIRO
      The percussion instrument Long Guiro.
      static int LONG_WHISTLE
      The percussion instrument Long Whistle.
      static int LOW_AGOGÔ
      The percussion instrument Low Agogô.
      static int LOW_BONGO
      The percussion instrument Low Bongo.
      static int LOW_CONGA
      The percussion instrument Low Conga.
      static int LOW_FLOOR_TOM
      The percussion instrument Low Floor Tom.
      static int LOW_MID_TOM
      The percussion instrument Low Mid Tom.
      static int LOW_TIMBALE
      The percussion instrument Low Timbale.
      static int LOW_TOM
      The percussion instrument Low Tom.
      static int LOW_WOODBLOCK
      The percussion instrument Low Woodblock.
      static int MARACAS
      The percussion instrument Maracas.
      static int MARIMBA
      The instrument Marimba.
      static int MELODIC_TOM
      The instrument Melodic Tom.
      static int METAL_PAD
      The instrument Metal Pad.
      static int MEZZO_FORTE
      The velocity mezzo forte (70).
      static int MEZZO_PIANO
      The velocity mezzo piano (60).
      static int MIDDLE_C
      The note C in octave 4, also known as Middle C.
      static int MODERATO
      The tempo moderato (110 beats per minute).
      static int MUSIC_BOX
      The instrument Music Box.
      static int MUTE_CUICA
      The percussion instrument Mute Cuica.
      static int MUTE_HI_CONGA
      The percussion instrument Mute Hi Conga.
      static int MUTE_TRIANGLE
      The percussion instrument Mute Triangle.
      static int MUTED_TRUMPET
      The instrument Muted Trumpet.
      static int NYLON_STRING_GUITAR
      The instrument Nylon String Guitar.
      static int OBOE
      The instrument Oboe.
      static int OCARINA
      The instrument Ocarina.
      static int OHH_VOICES
      The instrument Ohh Voices.
      static int OPEN_CUICA
      The percussion instrument Open Cuica.
      static int OPEN_HI_CONGA
      The percussion instrument Open Hi Conga.
      static int OPEN_HI_HAT
      The percussion instrument Open Hi Hat.
      static int OPEN_TRIANGLE
      The percussion instrument Open Triangle.
      static int ORCHESTRA_HIT
      The instrument Orchestra Hit.
      static int OVERDRIVE_GUITAR
      The instrument Overdrive Guitar.
      static int PALM_MUTED_GUITAR
      The instrument Palm Muted Guitar.
      static int PAN_FLUTE
      The instrument Pan Flute.
      static int PEDAL_HI_HAT
      The percussion instrument Pedal Hi Hat.
      static int PERCUSSIVE_ORGAN
      The instrument Percussive Organ.
      static int PIANISSIMO
      The velocity pianissimo (25).
      static int PIANISSISSIMO
      The velocity pianississimo (10).
      static int PIANO
      The velocity piano (50).
      static int PICCOLO
      The instrument Piccolo.
      static int PICKED_BASS
      The instrument Picked Bass.
      static int PIZZICATO_STRINGS
      The instrument Pizzicato Strings.
      static int POLYSYNTH
      The instrument Polysynth.
      static int POP_BASS
      The instrument Pop Bass.
      static int PRESTISSIMO
      The tempo prestissimo (200 beats per minute).
      static int PRESTO
      The tempo presto (180 beats per minute).
      static double QN
      The duration of a quarter note (1 beat).
      static double QNT
      The duration of a quarter note triplet (2/3 beat).
      static double QUARTER_NOTE
      The duration of a quarter note (1 beat).
      static double QUARTER_NOTE_TRIPLET
      The duration of a quarter note triplet (2/3 beat).
      static int RECORDER
      The instrument Recorder.
      static int REED_ORGAN
      The instrument Reed Organ.
      static int REST
      The note number corresponding to a rest.
      static int REVERSE_CYMBAL
      The instrument Reverse Cymbal.
      static int RIDE_BELL
      The percussion instrument Ride Bell.
      static int RIDE_CYMBAL_1
      The percussion instrument Ride Cymbal 1.
      static int RIDE_CYMBAL_2
      The percussion instrument Ride Cymbal 2.
      static int ROCK_ORGAN
      The instrument Rock Organ.
      static int SAW_WAVE
      The instrument Saw Wave.
      static int SEA_SHORE
      The instrument Sea Shore.
      static int SHAKUHACHI
      The instrument Shakuhachi.
      static int SHAMISEN
      The instrument Shamisen.
      static int SHEHNAI
      The instrument Shehnai.
      static int SHORT_GUIRO
      The percussion instrument Short Guiro.
      static int SHORT_WHISTLE
      The percussion instrument Short Whistle.
      static int SIDE_STICK
      The percussion instrument Side Stick.
      static int SILENT
      The velocity 0.
      static int SITAR
      The instrument Sitar.
      static double SIXTEENTH_NOTE
      The duration of a sixteenth note (1/4 beat).
      static double SIXTEENTH_NOTE_TRIPLET
      The duration of a sixteenth note triplet (1/6 beat).
      static int SLAP_BASS
      The instrument Slap Bass.
      static int SLOW_STRINGS
      The instrument Slow Strings.
      static double SN
      The duration of a sixteenth note (1/4 beat).
      static double SNT
      The duration of a sixteenth note triplet (1/6 beat).
      static int SOLO_VOX
      The instrument Solo Vox.
      static int SOPRANO_SAX
      The instrument Soprano Sax.
      static int SOUNDTRACK
      The instrument Soundtrack.
      static int SPACE_VOICE
      The instrument Space Voice.
      static int SPLASH_CYMBAL
      The percussion instrument Splash Cymbal.
      static int SQUARE_LEAD
      The instrument Square Lead.
      static int STAR_THEME
      The instrument Star Theme.
      static int STEEL_DRUMS
      The instrument Steel Drums.
      static int STEEL_STRING_GUITAR
      The instrument Steel String Guitar.
      static int STRINGS
      The instrument Strings.
      static int SWEEP_PAD
      The instrument Sweep Pad.
      static int SYNTH_BASS_1
      The instrument Synth Bass 1.
      static int SYNTH_BASS_2
      The instrument Synth Bass 2.
      static int SYNTH_BRASS_1
      The instrument Synth Brass 1.
      static int SYNTH_BRASS_2
      The instrument Synth Brass 2.
      static int SYNTH_DRUM
      The instrument Synth Drum.
      static int SYNTH_STRINGS_1
      The instrument Synth Strings 1.
      static int SYNTH_STRINGS_2
      The instrument Synth Strings 2.
      static int SYNTH_VOICE
      The instrument Synth Voice.
      static int TAIKO_DRUM
      The instrument Taiko Drum.
      static int TAMBOURINE
      The percussion instrument Tambourine.
      static int TELEPHONE
      The instrument Telephone.
      static int TENOR_SAX
      The instrument Tenor Sax.
      static double THIRTYSECOND_NOTE
      The duration of a thirty-second note (1/8 beat).
      static double THIRTYSECOND_NOTE_TRIPLET
      The duration of a thirty-second note triplet (1/12 beat).
      static int TIMPANI
      The instrument Timpani.
      static int TINKLE_BELL
      The instrument Tinkle Bell.
      static double TN
      The duration of a thirty-second note (1/8 beat).
      static double TNT
      The duration of a thirty-second note (1/8 beat).
      static int TREMOLO_STRINGS
      The instrument Tremolo Strings.
      static int TROMBONE
      The instrument Trombone.
      static int TRUMPET
      The instrument Trumpet.
      static int TUBA
      The instrument Tuba.
      static int TUBULAR_BELLS
      The instrument Tubular Bells.
      static int VIBRAPHONE
      The instrument Vibraphone.
      static int VIBRASLAP
      The percussion instrument Vibraslap.
      static int VIOLA
      The instrument Viola.
      static int VIOLIN
      The instrument Violin.
      static int VIVACE
      The tempo vivace (166 beats per minute).
      static int VIVACISSIMO
      The tempo vivacissimo (174 beats per minute).
      static int WARM_PAD
      The instrument Warm Pad.
      static int WHISTLE
      The instrument Whistle.
      static double WHOLE_NOTE
      The duration of a whole note (4 beats).
      static double WN
      The duration of a whole note (4 beats).
      static int WOODBLOCK
      The instrument Woodblock.
      static int XYLOPHONE
      The instrument Xylophone.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void allNotesOff()
      Turns all notes off, but allows decaying notes to complete.
      static void allSoundOff()
      Turns all notes off immediately.
      static int getInstrument()
      Returns the MIDI instrument number (between 1 and 128).
      static void main​(String[] args)
      Test client - plays the first few notes from Axel F by Harold Faltermeyer.
      static void noteOff​(int note)
      Turns the specified note off.
      static void noteOn​(int note)
      Turns the specified note on.
      static void pause​(double beats)
      Pauses for the specified duration.
      static void percussionOff​(int instrument)  
      static void percussionOn​(int instrument)  
      static void percussionOn​(int instrument, int velocity)  
      static void play​(String filename)
      Plays the specified MIDI file and waits until the audio file finishes playing before continuing.
      static void playInBackground​(String filename)
      Plays the specified MIDI file in a background thread.
      static void playNote​(int note, double beats)
      Plays the specified note for the given duration (measured in beats).
      static void playNotes​(int[] notes, double beats)
      Plays the specified notes for the given duration (measured in beats).
      static void playPercussion​(int instrument, double beats)
      Plays the specified percussion instrument for the given duration (measured in beats).
      static void save​(String filename)
      Saves the sequence of notes to the specified MIDI file.
      static void setInstrument​(int instrument)
      Sets the MIDI instrument to the specified value.
      static void setTempo​(int beatsPerMinute)
      Sets the tempo to the specified number of beats per minute.
      static void setVelocity​(int val)
      Sets the velocity to the specified value between 0 (silent) and 127 (loudest).
    • Method Detail

      • setInstrument

        public static void setInstrument​(int instrument)
        Sets the MIDI instrument to the specified value. For example 1 corresponds to an acoustic grand piano and 39 corresponds to a synthetic bass. You can specify the instrument numbers using predefined constants, such as StdMidi.ACOUSTIC_GRAND_PIANO and StdMidi.SYNTH_BASS_1.
        Parameters:
        instrument - the integer corresponding to the MIDI instrument
        Throws:
        IllegalArgumentException - unless instrument is between 1 and 128
      • getInstrument

        public static int getInstrument()
        Returns the MIDI instrument number (between 1 and 128).
        Returns:
        the integer corresponding to the MIDI instrument
      • setVelocity

        public static void setVelocity​(int val)
        Sets the velocity to the specified value between 0 (silent) and 127 (loudest). The key-down velocity indicates the force with which a note is played. It controls the note's volume and/or brightness.
        Parameters:
        val - the velocity of the note
        Throws:
        IllegalArgumentException - unless velocity is between 0 and 127
      • setTempo

        public static void setTempo​(int beatsPerMinute)
        Sets the tempo to the specified number of beats per minute.
        Parameters:
        beatsPerMinute - the number of beats per minute
        Throws:
        IllegalArgumentException - unless beatsPerMinute is a positive integer
      • playNote

        public static void playNote​(int note,
                                    double beats)
        Plays the specified note for the given duration (measured in beats). Uses the current instrument, velocity, and tempo. The call playNote(note, beats) is equivalent to the sequence of calls noteOn(note), pause(beats), and noteOff(note).
        Parameters:
        note - the MIDI note number (between 0 and 127)
        beats - the duration, measured in beats (quarter note = 1 beat)
        Throws:
        IllegalArgumentException - unless note is between 0 and 127
        IllegalArgumentException - unless beats is non-negative
      • noteOn

        public static void noteOn​(int note)
        Turns the specified note on.
        Parameters:
        note - the MIDI note number (between 0 and 127)
        Throws:
        IllegalArgumentException - unless note is between 0 and 127
      • noteOff

        public static void noteOff​(int note)
        Turns the specified note off.
        Parameters:
        note - the MIDI note number (between 0 and 127)
        Throws:
        IllegalArgumentException - unless note is between 0 and 127
      • pause

        public static void pause​(double beats)
        Pauses for the specified duration. The duration is measured in beats, where a quarter note is one beat.
        Parameters:
        beats - the duration, measured in beats (quarter note = 1 beat)
        Throws:
        IllegalArgumentException - unless beats is non-negative
      • playNotes

        public static void playNotes​(int[] notes,
                                     double beats)
        Plays the specified notes for the given duration (measured in beats). All notes must have the same duration. Uses the current instrument, velocity, and tempo.
        Parameters:
        notes - the MIDI note numbers (between 0 and 127)
        beats - the duration, measured in beats (quarter note = 1 beat)
        Throws:
        IllegalArgumentException - unless note is between 0 and 127
        IllegalArgumentException - unless beats is non-negative
      • playPercussion

        public static void playPercussion​(int instrument,
                                          double beats)
        Plays the specified percussion instrument for the given duration (measured in beats). Uses the current velocity and tempo.
        Parameters:
        instrument - the MIDI percussion instrument number (between 35 and 81)
        beats - the duration, measured in beats (quarter note = 1 beat)
        Throws:
        IllegalArgumentException - unless instrument is between 35 and 81
        IllegalArgumentException - unless beats is non-negative
      • percussionOn

        public static void percussionOn​(int instrument)
      • percussionOn

        public static void percussionOn​(int instrument,
                                        int velocity)
      • percussionOff

        public static void percussionOff​(int instrument)
      • allNotesOff

        public static void allNotesOff()
        Turns all notes off, but allows decaying notes to complete.
      • allSoundOff

        public static void allSoundOff()
        Turns all notes off immediately.
      • playInBackground

        public static void playInBackground​(String filename)
        Plays the specified MIDI file in a background thread. It is possible to play multiple MIDI files at the same time, e.g., a background musical score and sound effects.
        Parameters:
        filename - the name of the MIDI file
        Throws:
        IllegalArgumentException - if filename is null
        IllegalArgumentException - if filename is not a MIDI file
        IllegalArgumentException - if filename cannot be read
      • main

        public static void main​(String[] args)
        Test client - plays the first few notes from Axel F by Harold Faltermeyer.
        Parameters:
        args - the command-line arguments (none should be specified)