Docsity

javasound, Notas de estudo de Informática

Pré-visualização parcial do texto

Baixe javasound e outras Notas de estudo em PDF para Informática, somente na Docsity!

i

Java™ Sound API

Programmer’s Guide

ii Java Sound API Programmer’s Guide

iv Java Sound API Programmer’s Guide

v

Contents

Preface.................................................. xi

Who this Guide Is For.................................. xi

What this Guide Describes.............................. xi

For More Information.................................. xii

Chapter 1 Introduction to the Java Sound API................ 1

Design Goals........................................... 1

Who is the Java Sound API For?.................... 1

How Does the Java Sound API Relate

to Other Interfaces?............................ 2

Packages......................................... 2

Sampled Audio......................................... 3

What Is Sampled Audio?.......................... 3

Audio Configurations............................. 4

MIDI.................................................. 5

What Is MIDI?................................... 5

MIDI Configurations.............................. 6

Service Provider Interfaces............................... 7

Part I: Sampled Audio..................................... 9

Chapter 2 Overview of the Sampled Package................ 11

Design Goals.......................................... 11

A Focus on Data Transport....................... 11

Buffered and Unbuffered Handling of Audio........ 12

  • The Essentials: Format, Mixer, and Line.
    • What Is Formatted Audio Data?.
    • What Is a Mixer?..
    • What Is a Line?.
    • Lines in an Audio-output Configuration.
    • Lines in an Audio-input Configuration.
    • The Line Interface Hierarchy.
  • Chapter 3 Accessing Audio System Resources.
    • The AudioSystem Class.
    • Information Objects.
    • Getting a Mixer.
    • Getting a Line of a Desired Type.
      • Getting a Line Directly from the AudioSystem.
      • Getting a Line from a Mixer.
    • Selecting Input and Output Ports..
    • Permission to Use Audio Resources.
  • Chapter 4 Playing Back Audio.
    • Using a Clip.
      • Setting Up the Clip for Playback.
      • Starting and Stopping Playback.
    • Using a SourceDataLine.
      • Setting Up the SourceDataLine for Playback.
      • Starting and Stopping Playback.
    • Monitoring a Line’s Status.
    • Synchronizing Playback on Multiple Lines.
    • Processing the Outgoing Audio.
  • Chapter 5 Capturing Audio.
    • Setting Up a TargetDataLine..
    • Reading the Data from the TargetDataLine.
    • Monitoring the Line’s Status..
    • Processing the Incoming Audio.
    • Chapter 6 Processing Audio with Controls Contents vii - Introduction to Controls - Getting a Line that Has the Desired Controls - Getting the Controls from the Line - Using a Control to Change the Audio Signal - Controlling a Line’s Mute State - Changing a Line’s Volume - Selecting among Various Reverberation Presets. - Manipulating the Audio Data Directly.
      • Chapter 7 Using Files and Format Converters
        • Reading Sound Files
        • Writing Sound Files
        • Converting File and Data Formats - Converting from One File Format to Another - Converting Audio between Different Data Formats - Learning What Conversions Are Available.
  • Part II: MIDI
  • Chapter 8 Overview of the MIDI Package - A MIDI Refresher: Wires and Files - Streaming Data in the MIDI Wire Protocol - Sequenced Data in Standard MIDI Files - The Java Sound API’s Representation of MIDI Data - MIDI Messages. - MIDI Events - Sequences and Tracks - The Java Sound API’s Representation of MIDI Devices - The MidiDevice Interface - Transmitters and Receivers - Sequencers - Synthesizers
  • Chapter 9 Accessing MIDI System Resources. viii Java Sound API Programmer’s Guide
    • The MidiSystem Class.
    • Obtaining Default Devices.
    • Learning What Devices Are Installed.
    • Obtaining a Desired Device.
    • Opening Devices..
  • Chapter 10 Transmitting and Receiving MIDI Messages.
    • Understanding Devices, Transmitters, and Receivers.
      • Using a Transmitter. Sending a Message to a Receiver without
    • Understanding Time Stamps. - Time Stamps on Messages Sent to Devices.
    • Connecting Transmitters to Receivers.. - Connecting to a Single Device. - Connecting to More than One Device. - Closing Connections.
    • MIDI Sequences. Chapter 11 Playing, Recording, and Editing
    • Introduction to Sequencers. - When to Use a Sequencer.
    • Understanding Sequence Data. - Sequences and Tracks. - MidiEvents and Ticks.
    • Overview of Sequencer Methods.
    • Obtaining a Sequencer.
    • Loading a Sequence.
    • Playing a Sequence.
    • Recording and Saving Sequences..
    • Editing a Sequence.
    • Using Advanced Sequencer Features. - Moving to an Arbitrary Position in the Sequence. - Changing the Playback Speed Contents ix - Tracks in the Sequence. Muting or Soloing Individual - Synchronizing with Other MIDI Devices. - Specifying Special Event Listeners
  • Chapter 12 Synthesizing Sound.
    • Understanding MIDI Synthesis - Instruments. - Channels - Soundbanks and Patches - Voices.
    • Managing Instruments and Soundbanks - Learning What Instruments Are Loaded - Loading Different Instruments - Unloading Instruments. - Accessing Soundbank Resources
      • Current State Querying the Synthesizer’s Capabilities and
    • Using Channels - Using a Sequencer Controlling the Synthesizer without - Getting a Channel’s Current State. - Muting and Soloing a Channel
    • Permission to Play Synthesized Sound.
  • Part III: Service Provider Interfaces
  • Chapter 13 Introduction to the Service Provider Interfaces
    • What Are Services?
    • How Services Work
    • How Providers Prepare New Services
    • How Users Install New Services
  • Chapter 14 Providing Sampled-Audio Services
    • Introduction
    • Providing Audio File-Writing Services
    • Providing Audio File-Reading Services. x Java Sound API Programmer’s Guide
    • Providing Format-Conversion Services.
    • Providing New Types of Mixers.
  • Chapter 15 Providing MIDI Services..
    • Introduction..
    • Providing MIDI File-Writing Services..
    • Providing MIDI File-Reading Services.
    • Providing Particular MIDI Devices.
    • Providing Soundbank File-Reading Services.
  • Index..

xi

Preface

Who this Guide Is For

This guide is intended for three groups of readers:

  • Application developers: software programmers who want to write programs in the Java™ language (whether applications or applets) that use audio or MIDI. Most readers will be in this category.
  • Service providers: developers of software modules that extend the capabilities of an implementation of the Java Sound application programming interface (API). For example, a vendor might provide a new audio mixer or MIDI synthesizer, or the ability to read and write a new file format. The Java Sound API is designed to let programs automatically get access to all such “plug-in” modules that are available on a particular system.
  • API implementors: developers creating a new implementation of the Java Sound API.

It is assumed that the reader has a basic knowledge of programming in the Java language. Familiarity with audio and MIDI is helpful, but not assumed.

What this Guide Describes

This is a largely conceptual description of the Java Sound API, with some code snippets as programming examples. The Java Sound API specifies a software layer that allows application programs to communicate with an audio and MIDI engine. The Java Sound API is part of the Java™ 2 Platform, Standard Edition (J2SE), version 1.3, which is the version that this guide describes. The sound API is included in both the Java 2

1

Chapter 1

Introduction to the Java

Sound API

Design Goals

The Java™ Sound API is a low-level API for effecting and controlling the input and output of sound media, including both audio and Musical Instrument Digital Interface (MIDI) data. The Java Sound API provides explicit control over the capabilities normally required for sound input and output, in a framework that promotes extensibility and flexibility.

Who is the Java Sound API For?

Because sound is so fundamental, the Java Sound API fulfills the needs of a wide range of application developers. Potential application areas include:

  • Communication frameworks, such as conferencing and telephony
  • End-user content delivery systems, such as media players and music using streamed content
  • Interactive application programs, such as games and Web sites that use dynamic content
  • Content creation and editing
  • Tools, toolkits, and utilities

2 Java Sound API Programmer’s Guide

How Does the Java Sound API Relate to Other Interfaces?

The Java Sound API provides the lowest level of sound support on the Java platform. It provides application programs with a great amount of control over sound operations, and it is extensible. For example, the Java Sound API supplies mechanisms for installing, accessing, and manipulating system resources such as audio mixers, MIDI synthesizers, other audio or MIDI devices, file readers and writers, and sound format converters. The Java Sound API does not include sophisticated sound editors or graphical tools, but it provides capabilities upon which such programs can be built. It emphasizes low-level control beyond that commonly expected by the end user.

There are other Java platform APIs that have sound-related elements. The Java Media Framework (JMF) is a higher-level API that is currently available as a Standard Extension to the Java platform. JMF specifies a unified architecture, messaging protocol, and programming interface for capturing and playing back time-based media. JMF provides a simpler solution for basic media-player application programs, and it enables synchronization between different media types, such as audio and video. On the other hand, programs that focus on sound can benefit from the Java Sound API, especially if they require more advanced features, such as the ability to carefully control buffered audio playback or directly manipulate a MIDI synthesizer. Other Java APIs with sound aspects include Java 3D and APIs for telephony and speech. An implementation of any of these APIs might use an implementation of the Java Sound API internally, but is not required to do so.

Packages

The Java Sound API includes support for both digital audio and MIDI data. These two major modules of functionality are provided in separate packages:

  • javax.sound.sampled This package specifies interfaces for capture, mixing, and playback of digital (sampled) audio.
  • javax.sound.midi This package provides interfaces for MIDI synthesis, sequencing, and event transport.

4 Java Sound API Programmer’s Guide

are positive-valued samples, and points below are negative. The accuracy of the digital approximation of the analog signal depends on its resolution in time (the sampling rate ) and its quantization , or resolution in amplitude (the number of bits used to represent each sample). As a point of reference, the audio recorded for storage on compact discs is sampled 44,100 times per second and represented with 16 bits per sample.

The term “sampled audio” is used here slightly loosely. A sound wave could be sampled at discrete intervals while being left in an analog form. For purposes of the Java Sound API, however, “sampled audio” is equivalent to “digital audio.”

Typically, sampled audio on a computer comes from a sound recording, but the sound could instead be synthetically generated (for example, to create the sounds of a touch-tone telephone). The term “sampled audio” refers to the type of data, not its origin.

Further information about the structure of digital audio data is given under “What Is Formatted Audio Data?” in Chapter 2, “Overview of the Sampled Package.”

Audio Configurations The Java Sound API does not assume a specific audio hardware configuration; it is designed to allow different sorts of audio components to be installed on a system and accessed by the API. The Java Sound API supports common functionality such as input and output from a sound card (for example, for recording and playback of sound files) as well as mixing of multiple streams of audio. Here is one example of a typical audio architecture:

A Typical Audio Architecture

Chapter 1 Introduction to the Java Sound API 5

In this example, a device such as a sound card has various input and output ports, and mixing is provided in the software. The mixer might receive data that has been read from a file, streamed from a network, generated on the fly by an application program, or produced by a MIDI synthesizer. (The javax.sound.midi package, discussed next, supplies a Java language interface for synthesizers.) The mixer combines all its audio inputs into a single stream, which can be sent to an output device for rendering.

MIDI

The javax.sound.midi package contains APIs for transporting and sequencing MIDI events, and for synthesizing sound from those events.

What Is MIDI?

Whereas sampled audio is a direct representation of a sound itself, MIDI data can be thought of as a recipe for creating a sound, especially a musical sound. MIDI data, unlike audio data, does not describe sound directly. Instead, it describes events that affect the sound a synthesizer is making. MIDI data is analogous to a graphical user interface’s keyboard and mouse events. In the case of MIDI, the events can be thought of as actions upon a musical keyboard, along with actions on various pedals, sliders, switches, and knobs on that musical instrument. These events need not actually originate with a hardware musical instrument; they can be simulated in software, and they can be stored in MIDI files. A program that can create, edit, and perform these files is called a sequencer. Many computer sound cards include MIDI-controllable music synthesizer chips to which sequencers can send their MIDI events. Synthesizers can also be implemented entirely in software. The synthesizers interpret the MIDI events that they receive and produce audio output. Usually the sound synthesized from MIDI data is musical sound (as opposed to speech, for example). MIDI synthesizers are also capable of generating various kinds of sound effects.

Some sound cards include MIDI input and output ports to which external MIDI hardware devices (such as keyboard synthesizers or other instruments) can be connected. From a MIDI input port, an application program can receive events generated by an external MIDI-equipped musical instrument. The program might play the musical performance using the computer’s internal synthesizer, save it to disk as a MIDI file, or render it into musical notation. A program might use a MIDI output port

Chapter 1 Introduction to the Java Sound API 7

In this example, the application program prepares a musical performance by loading a musical score that’s stored as a standard MIDI file on a disk (left side of the diagram). Standard MIDI files contain tracks, each of which is a list of time-tagged MIDI events. Most of the events represent musical notes (pitches and rhythms). This MIDI file is read and then “performed” by a software sequencer. A sequencer performs its music by sending MIDI messages to some other device, such as an internal or external synthesizer. The synthesizer itself may read a soundbank file containing instructions for emulating the sounds of certain musical instruments. If not, the synthesizer will play the notes stored in the MIDI file using whatever instrument sounds are already loaded into the synthesizer.

As illustrated, the MIDI events must be translated into raw (non-time- tagged) MIDI before being sent through a MIDI output port to an external synthesizer. Similarly, raw MIDI data coming into the computer from an external MIDI source (a keyboard instrument, in the diagram) is translated into time-tagged MIDI messages that can control a synthesizer, or that a sequencer can store for later use. All these aspects of MIDI data flow are explained in detail in the subsequent chapters on MIDI (see Part II of this guide).

Service Provider Interfaces

The javax.sound.sampled.spi and javax.sound.midi.spi packages contain APIs that let software developers create new audio or MIDI resources that can be provided separately to the user and “plugged in” to an existing implementation of the Java Sound API. Here are some examples of services (resources) that can be added in this way:

  • An audio mixer
  • A MIDI synthesizer
  • A file parser that can read or write a new type of audio or MIDI file
  • A converter that translates between different sound data formats

In some cases, services are software interfaces to the capabilities of hardware devices, such as sound cards, and the service provider might be the same as the vendor of the hardware. In other cases, the services exist purely in software. For example, a synthesizer or a mixer could be an interface to a chip on a sound card, or it could be implemented without any hardware support at all.

8 Java Sound API Programmer’s Guide

An implementation of the Java Sound API contains a basic set of services, but the service provider interface (SPI) packages allow third parties to create new services. These third-party services are integrated into the system in the same way as the built-in services. The AudioSystem class in the sampled package and the MidiSystem class in the midi package act as coordinators that let application programs access the services explicitly or implicitly. Often the existence of a service is completely transparent to an application program that uses it. The service-provider mechanism benefits users of application programs based on the Java Sound API, because new sound features can be added to a program without requiring a new release of the Java SDK or runtime environment, and, in many cases, without even requiring a new release of the application program itself.