Java sound midi tutorial (java sound)

Home arrow Java Tutorials arrow Java Essentials arrow Java sound midi tutorial (java sound)
Java sound midi tutorial (java sound) Print E-mail
Contributed by Joe   
Sunday, 25 June 2006
What is Java Sound?

The Java Sound API specifies mechanisms for capturing, processing, and playing back audio and MIDI (Musical Instrument Digital Interface) data in a framework that promotes extensibility and flexibility.

Sun's reference implementation for the Java Sound API, also known as the Java Sound engine, is a 64 channel audio rendering and MIDI controlled sound synthesis engine which offers reliable, high quality sound on all Java 2 Platforms. This implementation also supports a set of high-quality General MIDI sound banks.

Who needs Java Sound?

Anyone who is interested in adding sound capabilities into their Java applets and applications will benefit from the Java Sound API. Java Sound will enable a wide variety of markets, such as education, advertising and telecommunications, to take advantage of its high quality audio to make communication more effective and accessible.

Where is Java Sound available?

The full featured Java Sound API is available as a core API in the Java 2 SDK, Standard Edition, v. 1.3 and above. The full featured Java Sound API is also available as part of the Java Media Framework 2.x for use on older JDK 1.x platforms.

Please note that the Java 2 Runtime Environment, Standard Edition, v. 1.3 release for Win32 does not include a soundbank. To use the software synthesizer with this release bundle, you must manually install a soundbank. For more information, refer to the Soundbanks page.

This API provides comprehensive access to the features of the Java Sound engine, and allows musicians and sound designers to integrate their work into Java applets and applications.

The Java Sound engine has been integrated into previous releases of the Java 2 platform (formerly known as JDK 1.2), and uses the existing AudioClip interface to access it. This means AudioClips may be created from any of the supported file formats using any of the supported data types.

What are the benefits of Java Sound?

Consistent and Reliable Sound:
Java Sound allows consistent, reliable, high-quality audio on all Java-enabled platforms. Developers have the assurance their audio content will sound great across all platforms, with the only requirement being a simple digital to audio converter (DAC).

CPU Efficient:
Java Sound uses only a small amount of a system's CPU to process sound files. For example: a 24-voice MIDI file uses only 20% of the CPU on a Pentium 90 MHz system.

Complete Audio API:
The fully featured Java Sound API allows total access to the underlying synthesis and rendering engine. This provides musicians with the capability to create new musical instruments and engineers the ability to capture microphone data for telephony or audio conferencing applications.

What audio formats does Java Sound support?

Java Sound supports the following audio file formats: AIFF, AU and WAV. It also supports the following MIDI based song file formats: SMF type 0 (Standard MIDI File, aka .mid files), SMF type 1 and RMF.

The Java Sound engine can render 8 or 16 bit audio data, in mono or stereo, with sample rates from 8KHz to 48KHz, that might be found in streaming audio or any of the supported file formats.

How do you stream audio using Java Sound?

Java Sound can begin playing as soon as it starts getting audio samples or MIDI requests -- there is no need to wait for the entire audio file to be loaded into memory.

Java Sound can be used in conjunction with the Java Media Framework API software (JMF) to stream audio data over the network. Third parties can also use Java Sound to present audio within their own streaming framework.

What other APIs that Sun is creating that relate to Java Sound?

Java Sound is part of a family of APIs that work together to provide customers with enhanced multimedia capabilities.

These APIs include:

Java 2D - two dimensional graphics and imaging
Java 3D - three dimensional graphics and imaging
Java Advanced Imaging - additional graphics and imaging operations which extend Java 2D
Java Media Framework - allows the capture, streaming and playback of time-based media such as audio and video.
Java Shared Data Toolkit - provides collaboration support for Java applets and applications.
Java Speech - speech synthesis and recognition (API only)
FreeTTS free speech synthesizer in Java - speech synthesis (implementation only)

Description of javax.sound.midi package(Java sound midi tutorial)

Provides interfaces and classes for I/O, sequencing, and synthesis of MIDI (Musical Instrument Digital Interface) data.


How to load and play Midi Audio using Sequence ? (Java sound midi Sequence tutorial)

A Sequence is a data structure containing musical information (often an entire song or composition) that can be played back by a Sequencer object. Specifically, the Sequence contains timing information and one or more tracks. Each track consists of a series of MIDI events (such as note-ons, note-offs, program changes, and meta-events). The sequence's timing information specifies the type of unit that is used to time-stamp the events in the sequence.

A Sequence can be created from a MIDI file by reading the file into an input stream and invoking one of the getSequence methods of MidiSystem. A sequence can also be built from scratch by adding new Tracks to an empty Sequence, and adding MidiEvent objects to these Tracks.

 Playing Streaming Midi Audio
Supported audio file formats: mid, rmf

        Sequencer sequencer = MidiSystem.getSequencer();
        sequencer.open();
       InputStream is = new BufferedInputStream(
            new URL("
http://hotdir.biz/rmffile").openStream());   
        sequencer.setSequence(is);
        sequencer.start();


 

 

 

 

Using MidiFileFormat to Determine the File Format of a Midi Audio File (Java sound midi tutorial)

 A MidiFileFormat object encapsulates a MIDI file's type, as well as its length and timing information.

    // From file
        MidiFileFormat fformat = MidiSystem.getMidiFileFormat(new File("midifile"));
   
        // From URL
        fformat = MidiSystem.getMidiFileFormat(new URL("http://hostname/midifile"));
   
        // Get file format
        switch (fformat.getType()) {
          case 0:
            // mid
            break;
          case 1:
            // rmf
            break;
        }


Using controlChange(int controller,int value) method to set the Volume of Playing Midi Audio (Java sound midi tutorial)

The controlChange method reacts to a change in the specified controller's value. A controller is some control other than a keyboard key, such as a switch, slider, pedal, wheel, or breath-pressure sensor. The MIDI 1.0 Specification provides standard numbers for typical controllers on MIDI devices, and describes the intended effect for some of the controllers. The way in which an Instrument reacts to a controller change may be specific to the Instrument.
The MIDI 1.0 Specification defines both 7-bit controllers and 14-bit controllers. Continuous controllers, such as wheels and sliders, typically have 14 bits (two MIDI bytes), while discrete controllers, such as switches, typically have 7 bits (one MIDI byte). Refer to the specification to see the expected resolution for each type of control.

Controllers 64 through 95 (0x40 - 0x5F) allow 7-bit precision. The value of a 7-bit controller is set completely by the value argument. An additional set of controllers provide 14-bit precision by using two controller numbers, one for the most significant 7 bits and another for the least significant 7 bits. Controller numbers 0 through 31 (0x00 - 0x1F) control the most significant 7 bits of 14-bit controllers; controller numbers 32 through 63 (0x20 - 0x3F) control the least significant 7 bits of these controllers. For example, controller number 7 (0x07) controls the upper 7 bits of the channel volume controller, and controller number 39 (0x27) controls the lower 7 bits. The value of a 14-bit controller is determined by the interaction of the two halves. When the most significant 7 bits of a controller are set (using controller numbers 0 through 31), the lower 7 bits are automatically set to 0. The corresponding controller number for the lower 7 bits may then be used to further modulate the controller value.

if (sequencer instanceof Synthesizer) {
        Synthesizer synthesizer = (Synthesizer)sequencer;
        MidiChannel[] channels = synthesizer.getChannels();
   
        // gain is a value between 0 and 1 (loudest)
        double gain = 0.9D;
        for (int i=0; i<channels.length; i++) {
            channels[i].controlChange(7, (int)(gain * 127.0));
        }
    }


Usegull article:(Java sound midi tutorial)

Add MP3 capabilities to Java Sound with SPI

Summary

The Service Provider Interface (SPI), a new feature in Java 2 1.3, allows developers to transparently add new functions to the JVM. That allows older Java programs, even Java 1.02 programs, to take advantage of the newly added functions with no changes and no recompiling. For instance, Java Sound uses the SPI at runtime to provide sound mixers, file readers and writers, and format conversion utilities to a Java sound program. Indeed, more functions can be added to Java Sound to take advantage of new file formats. In this article, Dan Becker introduces the SPI by way of a real-world example: MP3 sound files.


Some java open source projects related to JAVA SOUND

PartyJukebox:

Features:

  • Party mode
    Allows you to select a limit for the number of songs and/or play time. You can also provide a play list, which prevents that the music will stop, if you have lazy party people.

  • Scans detailed Information's about the files.
    Reads ID3 Tags and if none is found, it tries to extract data form the filename.

  • Powerful Search Engine
    Which also supports Regular Expressions! (under JRE 1.4 and above)

  • Platform independent
    Completely written in Java (tm) . So if Java is available for your platform, your platform is supported. (e.g. Windows, Linux, Solaris, Mac OS)
  • Easy tree navigation
  • Supports mp3 and ogg
  • Supports internal and external playback.
  • Cross fade
  • Creation and modifying of ID3 Tags .
  • Creation and modifying of play lists.
  • Multiple Language support.
  • Choose able font sizes for disabled people.
  • Preparations for copies to other media devices.
  • Help to find duplicates.
  • Volume and Balance Controls.
Last Updated ( Sunday, 25 June 2006 )

  home              contact us

 

©2006-2012 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground