Documentation

The Java™ Tutorials

Trail: Sound

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. Java声音API是一种低级API,用于影响和控制声音媒体的输入和输出,包括音频和乐器数字接口(MIDI)数据。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. JavaSoundAPI在一个促进可扩展性和灵活性的框架中提供了对声音输入和输出通常需要的功能的显式控制。

The Java Sound API fulfills the needs of a wide range of application developers. Java Sound API满足了广泛应用程序开发人员的需求。Potential application areas include:潜在的应用领域包括:

The Java Sound API provides the lowest level of sound support on the Java platform. Java声音API在Java平台上提供最低级别的声音支持。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. 例如,Java Sound API提供用于安装、访问和操作系统资源的机制,如音频混音器、MIDI合成器、其他音频或MIDI设备、文件读取器和写入器以及声音格式转换器。The Java Sound API does not include sophisticated sound editors or graphical tools, but it provides capabilities upon which such programs can be built. Java声音API不包括复杂的声音编辑器或图形工具,但它提供了构建此类程序的功能。It emphasizes low-level control beyond that commonly expected by the end user.它强调最终用户通常期望的低级别控制。

The Java Sound API includes support for both digital audio and MIDI data. Java声音API包括对数字音频和MIDI数据的支持。These two major modules of functionality are provided in separate packages:这两个主要功能模块在单独的软件包中提供:

Two other packages permit service providers (as opposed to application developers) to create custom software components that extend the capabilities of an implementation of the Java Sound API:另外两个包允许服务提供商(而不是应用程序开发人员)创建自定义软件组件,以扩展Java Sound API实现的功能:

This page introduces the sampled-audio system, the MIDI system, and the SPI packages. 本页介绍采样音频系统、MIDI系统和SPI软件包。Each package is then discussed in greater detail later in the tutorial. 然后,在本教程的后面部分将对每个包进行更详细的讨论。


Note: 

There are other Java platform APIs that also have sound-related elements. 还有其他Java平台API也有与声音相关的元素。The Java Media Framework API (JMF) is a higher-level API that is currently available as a Standard Extension to the Java platform. Java媒体框架API(JMF)是一种更高级别的API,目前作为Java平台的标准扩展提供。JMF specifies a unified architecture, messaging protocol, and programming interface for capturing and playing back time-based media. JMF为捕获和播放基于时间的媒体指定了一致性的体系结构、消息传递协议和编程接口。JMF provides a simpler solution for basic media-player application programs, and it enables synchronization between different media types, such as audio and video. JMF为基本媒体播放器应用程序提供了一个更简单的解决方案,它支持不同媒体类型(如音频和视频)之间的同步。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. 另一方面,专注于声音的程序可以从Java sound API中受益,特别是当它们需要更高级的功能时,例如仔细控制缓冲音频播放或直接操作MIDI合成器的能力。其他具有声音特性的Java API包括Java3D和用于电话和语音的API。An implementation of any of these APIs might use an implementation of the Java Sound API internally, but is not required to do so.这些API中任何一个的实现都可能在内部使用Java声音API的实现,但不需要这样做。


What is Sampled Audio?什么是采样音频?

The javax.sound.sampled package handles digital audio data, which the Java Sound API refers to as sampled audio. 包处理数字音频数据,Java声音API将其称为采样音频。Samples are successive snapshots of a signal. 采样是信号的连续快照。In the case of audio, the signal is a sound wave. 就音频而言,信号是声波。A microphone converts the acoustic signal into a corresponding analog electrical signal, and an analog-to-digital converter transforms that analog signal into a sampled digital form. 麦克风将声音信号转换为相应的模拟电信号,模数转换器将该模拟信号转换为采样数字形式。The following figure shows a brief moment in a sound recording.下图显示了录音中的一个简短瞬间。

A Sampled Sound Wave

A Sampled Sound Wave取样声波

This graph plots sound pressure (amplitude) on the vertical axis, and time on the horizontal axis. 此图在纵轴上绘制声压(振幅),在横轴上绘制时间。The amplitude of the analog sound wave is measured periodically at a certain rate, resulting in the discrete samples (the red data points in the figure) that comprise the digital audio signal. 模拟声波的振幅以一定的速率周期性地测量,从而产生构成数字音频信号的离散样本(图中的红色数据点)。The center horizontal line indicates zero amplitude; points above the line 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.作为参考点,为存储在光盘上而录制的音频每秒采样44100次,并用每个采样16位表示。

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."声波可以以离散的间隔进行采样,同时保持模拟形式。然而,就Java声音API而言,“采样音频”等同于“数字音频”

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.术语“采样音频”指的是数据的类型,而不是其来源。

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. Java Sound API不采用特定的音频硬件配置;它的设计目的是允许在系统上安装不同种类的音频组件,并通过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:Java Sound API支持常见功能,例如声卡的输入和输出(例如,用于录制和播放声音文件)以及多个音频流的混合。以下是典型音频架构的一个示例:

The following context describes this figure

A Typical Audio Architecture典型的音频架构

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. 混音器可以接收从文件读取、从网络传输、由应用程序动态生成或由MIDI合成器生成的数据。The mixer combines all its audio inputs into a single stream, which can be sent to an output device for rendering.混音器将其所有音频输入组合成一个流,该流可以发送到输出设备进行渲染。

What is MIDI?

The javax.sound.midi package contains APIs for transporting and sequencing MIDI events, and for synthesizing sound from those events.javax.sound.midi包包含用于传输和排序MIDI事件以及从这些事件合成声音的API。

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数据可以被认为是创建声音的方法,尤其是音乐声音。MIDI data, unlike audio data, does not describe sound directly. 与音频数据不同,MIDI数据不直接描述声音。Instead, it describes events that affect the sounds (or actions) performed by a MIDI-enabled device or instrument, such as a synthesizer. 相反,它描述了影响MIDI设备或乐器(如合成器)执行的声音(或动作)的事件。MIDI data is analogous to a graphical user interface's keyboard and mouse events. MIDI数据类似于图形用户界面的键盘和鼠标事件。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. 在MIDI的情况下,事件可以被认为是音乐键盘上的动作,以及该乐器上各种踏板、滑块、开关和旋钮上的动作。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. 这些活动实际上不需要起源于硬件乐器;它们可以在软件中模拟,也可以存储在MIDI文件中。A program that can create, edit, and perform these files is called a sequencer. 可以创建、编辑和执行这些文件的程序称为sequencer。Many computer sound cards include MIDI-controllable music synthesizer chips to which sequencers can send their MIDI events. 许多计算机声卡包括MIDI可控制的音乐合成器芯片,音序器可以将MIDI事件发送到该芯片。Synthesizers can also be implemented entirely in software. 合成器也可以完全用软件实现。The synthesizers interpret the MIDI events that they receive and produce audio output. 合成器解释它们接收的MIDI事件并产生音频输出。Usually the sound synthesized from MIDI data is musical sound (as opposed to speech, for example). 通常,从MIDI数据合成的声音是音乐声音(例如,与语音相反)。MIDI synthesizers are also capable of generating various kinds of sound effects.MIDI合成器还能够产生各种声音效果。

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. 一些声卡包括MIDI输入和输出端口,外部MIDI硬件设备(如键盘合成器或其他乐器)可以连接到这些端口。From a MIDI input port, an application program can receive events generated by an external MIDI-equipped musical instrument. 从MIDI输入端口,应用程序可以接收由配备MIDI的外部乐器生成的事件。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. 该程序可以使用计算机的内部合成器播放音乐表演,将其作为MIDI文件保存到磁盘,或将其转换为乐谱。A program might use a MIDI output port to play an external instrument, or to control other external devices such as recording equipment.程序可以使用MIDI输出端口来播放外部乐器,或控制其他外部设备,如录音设备。

The following diagram illustrates the functional relationships between the major components in a possible MIDI configuration based on the Java Sound API. 下图说明了基于Java Sound API的可能MIDI配置中主要组件之间的功能关系。(As with audio, the Java Sound API permits a variety of MIDI software devices to be installed and interconnected. The system shown here is just one potential scenario.) (与音频一样,Java Sound API允许安装和互连各种MIDI软件设备。此处显示的系统只是一种可能的场景。)The flow of data between components is indicated by arrows. 组件之间的数据流由箭头指示。The data can be in a standard file format, or (as indicated by the key in the lower right corner of the diagram), it can be audio, raw MIDI bytes, or time-tagged MIDI messages.数据可以是标准文件格式,也可以是音频、原始MIDI字节或带有时间标记的MIDI消息(如图右下角的键所示)。

The following context describes this figure.

A Possible MIDI Configuration可能的MIDI配置

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). 在本例中,应用程序通过加载作为标准MIDI文件存储在磁盘(图的左侧)上的乐谱来准备音乐表演。Standard MIDI files contain tracks, each of which is a list of time-tagged MIDI events. 标准MIDI文件包含曲目,每个曲目都是带有时间标记的MIDI事件列表。Most of the events represent musical notes (pitches and rhythms). This MIDI file is read and then "performed" by a software sequencer. 大多数活动都代表音符(音高和节奏)。这个MIDI文件被读取,然后由软件序列器“执行”。A sequencer performs its music by sending MIDI messages to some other device, such as an internal or external synthesizer. 音序器通过向其他设备(如内部或外部合成器)发送MIDI消息来执行音乐。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 it.如果没有,合成器将使用已加载到MIDI文件中的任何乐器声音播放MIDI文件中存储的音符。

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 MIDI instrument. 如图所示,在通过MIDI输出端口发送到外部MIDI乐器之前,MIDI事件必须转换为原始(无时间标记)MIDI。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.类似地,从外部MIDI源(图中的键盘乐器)进入计算机的原始MIDI数据被转换为带时间标签的MIDI消息,这些消息可以控制合成器,或者音序器可以存储以供以后使用。

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. javax.sound.sampled.spijavax.sound.midi.spi包包含API,允许软件开发人员创建新的音频或midi资源,这些资源可以单独提供给用户,并“插入”到Java sound API的现有实现中。Here are some examples of services (resources) that can be added in this way:以下是一些可以通过这种方式添加的服务(资源)示例:

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.例如,合成器或混音器可以是声卡芯片的接口,也可以在根本不需要任何硬件支持的情况下实现。

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. Java Sound API的实现包含一组基本的服务,但是服务提供者接口(SPI)包允许第三方创建新的服务。These third-party services are integrated into the system in the same way as the built-in services. 这些第三方服务以与内置服务相同的方式集成到系统中。The AudioSystem class and the MidiSystem class act as coordinators that let application programs access the services explicitly or implicitly. AudioSystem类和MidiSystem类充当协调器,让应用程序显式或隐式地访问服务。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 JDK or runtime environment, and, in many cases, without even requiring a new release of the application program itself.服务提供者机制使基于Java Sound API的应用程序用户受益,因为可以向程序添加新的声音功能,而无需JDK或运行时环境的新版本,在许多情况下甚至无需应用程序本身的新版本。


Previous page: Beginning of Tutorial
Next page: Overview of the Sampled Package