Java Sound API


Java sound is a bunch of classes and interfaces. It is a low-level API which is used for effecting and controlling the input and output of sound media and Musical Instrument Digital Interface (MIDI) data. It offers explicit control over the capabilities generally needed for sound input and output, in a framework that promotes extensibility and flexibility. It consists of two packages namely, javax.sound.sampled and javax.sound.midi.

Who is the Java Sound API designed for?

The Java Sound API satisfies the requirements of a broad spectrum of application developers because sound is such a crucial component. Possible application domains comprise −

  • Frameworks for communication, including telephone and conferencing

  • Technologies for delivering user-generated content, such as media players and streaming music

  • Interactive application programs, such as games and dynamically generated websites

  • Editing and producing content

  • Utilities, equipment and tool kits.

How do other interfaces relate to the Java Sound API?

The Java platform offers the simplest level of sound functionality with the Java Sound API. It is flexible and gives application programs extensive control over sound operations. The Java Sound API, for instance, offers methods for setting up, gaining access to, and modifying system resources including audio mixers, MIDI synthesizers, other audio or MIDI devices, file readers and writers, and sound format converters.

Although the Java Sound API does not come with advanced sound editors or graphical tools, it does offer the building blocks for such programs. Beyond what is often anticipated by the end user, it emphasizes low level control.

Other Java Platform APIs provide components related to sound. A more advanced API for the Java Platform is the Java Media Framework (JMF), which is presently offered as a standard extension. For recording and playing back time-based material, JMf defines a unified architecture, communications protocol, and programming interface. JMF permits synchronization between various media kinds, such as audio and video.

It offers a straighter forward solution for fundamental media player application programs. The Java Sound API, on the other hand, can be useful for programs that are primarily concerned with sound, especially if they need more sophisticated capabilities like the ability to precisely regulate delayed audio playback or directly control a MIDI synthesizer.

Java 3D APIs, telephony APIs, and Vice APIs are additional Java APIs with sound related features. Although it is not needed, an implementation of any of these APIs may internally utilize an implementation of the Java Sound API.

Packages in Java Sound API

Both digital audio and MIDI Data are supported by the Java Sound API. These two key functional elements are offered in distinct packages −

  • javax.sound.sampled − specifies interfaces for capture, mixing and playback of digital audio.

  • javax.sound.midi − specifies interfaces for MIDI synthesis, sequencing, and event transport.

What is Sampled Audio?

Digital audio data is also known as sampled audio by the Java Sound API. It is handled by the javax.sound.sampled package. A signal is captured in a series of samples. A sound wave constitutes the signal in audio. An analog to digital converter changes the analog electrical signal produced by the acoustic signal produced by the microphone into a sampled digital form. The image below depicts a brief segment of a sound recording.

The visual representation presented here informs us about capturing sounds within digital systems accurately. Specifically, as per this graph depiction - Time flows from left to right along with X-axis and on Y-axis - Audio levels corresponds to Sound Pressure (Amplitude.) The Red data points visible indicate Discrete Samples yielding Digital Audio Signals that capture Analog Sound Wave measurements regularly every predetermined interval slowing down each Peak or Trough resulting precisely documented Data Points for analysis with reference to +ve values located above & their -ve counterparts underneath said Red Line significant giving meaning to data collected. The horizontal center line denotes zero amplitude. The sampling rate, which determines the resolution in time, and the quantization, or resolution in amplitude, which determines the number of bits used to represent each sample. For comparison, the audio stored on compact CDs is sampled at a rate of 44,100 samples per second, with a 16 bit per sample representation.

Here the phrase “sampled audio” is used quite loosely. A sound wave might be preserved in analog form and sampled at regular intervals. The Java Sound API, however, treats “sampled audio” as being the same as “digital audio”.

Normally, sampled audio on computers is taken from sound recordings, although the sound might also be artificially produced (for instance, to simulate touch tone phone noises). The word “sampled audio” describes the kind of data, not where it came from.

Audio Configurations

The Java Sound API is intended to allow multiple types of audio components to be installed on a system and accessible by the API; it does not presuppose a certain audio hardware setup. The Java Sound API enables standard features including mixing several audio streams as well as input and output from a sound card (for example, for recording and playing back sound files). here is a typical audio architecture illustration −

In this illustration, mixing is offered by the program and is accomplished via a device with several input and output ports, such as sound card. Data may be read from a file, transmitted over a network, created instantly by an application program or synthesized by a MIDI synthesizer and sent to the mixer.

(The javax.sound.midi package, which will be covered later, offers a Java Language Interface for synthesizers). a single stream created by the mixer from all of its audio inputs can be transmitted to an output device for rendering.

What is MIDI?

MIDI data can be viewed as a recipe for producing a sound, particularly a musical sound, as opposed to sampled audio, which is a direct depiction of a sound itself. Unlike audio data, MIDI data does not directly describe sound. Instead, it talks about things that have an impact on the sound a synthesizer is producing. The keyboard and mouse events of a graphical user interface are comparable to MIDI data.

When it comes to MIDI, the event may be compared to keyboard actions as well as keyboard actions on different pedals, sliders, switches, and knobs. These activities don’t always need to start with a physical musical instrument’ they may be emulated in software and saved as MIDI files. A sequencer is a program that can produce, modify and perform these files. Sequencers can convey their MIDI events to the MIDI controllable music synthesizer circuits found in many computer sound cards.

Additionally, synthesizers can be fully implemented in software. The synthesizers generate audio output by interpreting the MIDI signals they receive. As contrast to, say speech, MIDI data is often used to create musical sound. Different sound effects may be produced using MIDI synthesizers as well.

MIDI Configurations

The functional links between the key elements of a potential MIDI configuration based on the Java Sound API are depicted in the figure below. (Like with audio, the Java Sound API enables the installation and connection of a range of MIDI software devices. One possible situation is the setup displayed above). Arrows show the direction of the data as it moves between components. The information may be in a common file format, as shown by the key in the diagram’s lower right corner.

In this illustration, the application program loads a musical score that is kept as a common MIDI file on a disc (left side of the figure) to prepare a musical performance. Each track in a typical MIDI file is a collection of time tagged MIDI events. The majority of the happenings are likened to musical notes (pitch and rhythm). a software sequencer reads this MIDI file and “performs” it after that. Sending MIDI data to another device, such as an internal or external synthesizer, allows a sequencer to execute its music. A sound bank file with instructions for simulating the sounds of certain musical instruments may be read by the synthesizer itself. If not, the synthesizer will use whichever instrument sounds are currently loaded into the synthesizer to play the notes specified in the MIDI file.

Example for using a Java Sound API

These are all addressed by Java Sound API −

SEQUENCER >> SEQUENCE >> TRACK >> MIDI EVENTS

Approach

  • Step 1 − Get a sequencer, then start it

// make a sequencer named play and open it 
Sequencer play = MIDISystem.getSequencer():
play.open();
  • Step 2 − Create a new Sequence

// make a new sequence
Sequence seq = new Sequence (Sequence.PPQ, 4);
  • Step 3 − Get a new Track in the sequence

// creating a new track
Track t = seq.createTrack();
  • Step 4 − Put MIDIEVENTS throughout the track

// filing the track with MIDIEVENTS and
// giving the Sequence to the Sequencer

t.add(myMIDIEvent1); 
play.setSequence(seq);

// play it using start
play.start();

Example

// Java Program to Illustrate Java Sound API

import javax.sound.midi.*;
public class Example {

   public static void main (String [] args) {
      Example minimusic = new Example ();
      minimusic.play();
      System.out.print("Successfully compiled and executed");
   }

   // Method 2
   public void play() {
      try {
         Sequencer player = MidiSystem.getSequencer();
         play.open();

         Sequence seq = new Sequence(Sequence.PPQ, 4);
         Track track = seq.createTrack();
         ShortMessage a = new ShortMessage();
         a.setMessage(144, 1, 44, 100);

         MidiEvent noteOn = new MidiEvent(a, 1);
         track.add(noteOn);

         ShortMessage b = new ShortMessage();
         b.setMessage(128, 1, 44, 100);
         MidiEvent noteOff = new MidiEvent(b, 16);
         track.add(noteOff);

         play.setSequence(seq);

         play.start();
      }

      catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

Successfully compiled and executed

Conclusion

Java Sound API is a low-level API. It is used for effecting and controlling the input and output of sound media and MIDI data. It provides explicit control over the capabilities. The package serves a variety of application developers' requirements. It comes in two packages, specifically javax.sound.sampled and javax.sounds.midi.

Updated on: 01-Aug-2023

287 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements