Open In App

Java Sound API

Last Updated : 17 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

JavaSound is a collection of classes and interfaces for effecting and controlling sound media in java. It consists of two packages.

  • javax.sound.sampled: This package provides an interface for the capture, mixing digital audio.
  • javax.sound.midi: This package provides an interface for MIDI (Musical Instrument Digital Interface) synthesis, sequencing, and event transport.

What is MIDI?

We will be exploring MIDI more about MIDI here. So MIDI is like a sheet of notes that the midi capable instrument can play, think of it as an HTML document and midi capable instrument as a web browser. MIDI file has information about how a song should be played, just like an instruction sheet for a guitar player. MIDI devices know how to read a midi file and generate sound.

In order to make actual sound we need four things for this:

  1. Instrument (that plays the music) SEQUENCER
  2. Song (the music to be played)  SEQUENCE
  3. Track (which holds the notes)
  4. Notes (the actual music information)  MIDI EVENTS

JavaSound API got all of these covered.

SEQUENCER  â‡’  SEQUENCE ⇒ TRACK ⇒ MIDI EVENTS

Procedure:

Step 1: Get a Sequencer and open it

// Make a sequencer named player and open it
Sequencer player = MIDISystem.getSequencer();
player.open();

Step 2: Make a new Sequence

// Make a new sequence 
Sequence seq = new Sequence(Sequence.PPQ, 4);

Step 3: Get a new Track from the Sequence

// Creating new Track
Track t = seq.createTrack();

Step 4: Fill the Track with MIDIEVENTS  

// Filling the Track with MidiEvent and
// giving the Sequence to the Sequencer

t.add(myMidiEvent1);
player.setSequence(seq);

// Play it using start
player.start();

Implementation:

Example

Java




// Java Program to Illustrate JAva Sound API
 
// Importing classes from
// javax.sound package
import javax.sound.midi.*;
 
// Main class
// MiniMusicApp
public class GFG {
 
    // Method 1
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of class inside main()
        GFG minimusic = new GFG();
 
        // Calling method 2 to play the sound
        minimusic.play();
 
        // Display message on the console for
        // successful execution of program
        System.out.print(
            "Successfully compiled and executed");
    }
 
    // Method 2
    // To play the sound
    public void play()
    {
 
        // Try block to check for exceptions
        try {
            // Getting a sequencer and open it
            Sequencer player = MidiSystem.getSequencer();
            player.open();
 
            // Making 1a new Sequence
            Sequence seq = new Sequence(Sequence.PPQ, 4);
 
            // Creating a new track
            Track track = seq.createTrack();
 
            // Making a Message
            ShortMessage a = new ShortMessage();
 
            // Put the Instruction in the Message
            a.setMessage(144, 1, 44, 100);
 
            // Make a new MidiEvent
            MidiEvent noteOn = new MidiEvent(a, 1);
 
            // Add MidiEvent to the Track
            track.add(noteOn);
 
            ShortMessage b = new ShortMessage();
            b.setMessage(128, 1, 44, 100);
            MidiEvent noteOff = new MidiEvent(b, 16);
            track.add(noteOff);
 
            // Giving sequence to Sequencer
            player.setSequence(seq);
 
            // Start the Sequencer using start() method
            player.start();
        }
 
        // Catch block to handle exceptions
        catch (Exception ex) {
            // Display the exception on console
            // along with line number
            ex.printStackTrace();
        }
    }
}


Output:

Successfully compiled and executed


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads