https://docs.oracle.com/javase/tutorial/sound/index.htmlhtml
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.java
Java Sound API是一个底层API,用于实现和控制声音媒体的输入和输出,包括音频和乐器数字接口(MIDI)数据。 Java Sound API在一个促进可扩展性和灵活性的框架中提供对声音输入和输出一般所需功能的显式控制。git
The Java Sound API fulfills the needs of a wide range of application developers. Potential application areas include:编程
Java Sound API知足了各类应用程序开发人员的需求。 潜在的应用领域包括:api
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.网络
Java Sound API在Java平台上提供最低级别的声音支持。 它为应用程序提供了对声音操做的大量控制,而且是可扩展的。 例如,Java Sound API提供了安装,访问和操做系统资源的机制,如音频混合器,MIDI合成器,其余音频或MIDI设备,文件读取器和写入器以及声音格式转换器。 Java Sound API不包含复杂的声音编辑器或图形工具,但它提供了构建此类程序的功能。 它强调低级控制,超出最终用户一般指望的控制。架构
The Java Sound API includes support for both digital audio and MIDI data. These two major modules of functionality are provided in separate packages:oracle
Java Sound API包括对数字音频和MIDI数据的支持。 这两个主要功能模块在单独的包中提供:app
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. Each package is then discussed in greater detail later in the tutorial.
本页介绍了采样音频系统,MIDI系统和SPI包。 而后在本教程的后面更详细地讨论每一个包。
Note:
There are other Java platform APIs that also have sound-related elements. The Java Media Framework API (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.
注意:
还有其余Java平台API也具备与声音相关的元素。 Java Media Framework API(JMF)是一种更高级别的API,目前可用做Java平台的标准扩展。 JMF指定用于捕获和回放基于时间的媒体的统一架构,消息传递协议和编程接口。 JMF为基本的媒体播放器应用程序提供了一种更简单的解决方案,它能够实现不一样媒体类型(如音频和视频)之间的同步。 另外一方面,专一于声音的程序能够从Java Sound API中受益,特别是若是它们须要更高级的功能,例如可以仔细控制缓冲的音频播放或直接操做MIDI合成器。 其余具备声音方面的Java API包括用于电话和语音的Java 3D和API。 任何这些API的实现均可能在内部使用Java Sound API的实现,但不是必须这样作。
What is Sampled Audio?
The javax.sound.sampled package handles digital audio data, which the Java Sound API refers to as sampled audio. 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.
什么是Sampled Audio?
javax.sound.sampled包处理数字音频数据,Java Sound API将其称为采样音频。 样本是信号的连续快照。 在音频的状况下,信号是声波。 麦克风将声学信号转换为相应的模拟电信号,而且模拟 - 数字转换器将该模拟信号转换为采样的数字形式。 下图显示了录音中的短暂时刻。
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.
该图描绘了垂直轴上的声压(振幅)和水平轴上的时间。 以必定速率周期性地测量模拟声波的幅度,从而产生包括数字音频信号的离散样本(图中的红色数据点)。 中心水平线表示零幅度; 线上方的点是正值样本,下面的点是负数。 模拟信号的数字近似的精确度取决于其在时间上的分辨率(采样率)及其量化,或幅度分辨率(用于表示每一个样本的比特数)。 做为参考,记录存储在光盘上的音频每秒采样44,100次,每一个采样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 Sound 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. 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不承担特定的音频硬件配置; 它旨在容许在系统上安装不一样种类的音频组件并由API访问。 Java Sound API支持常见功能,例如声卡的输入和输出(例如,用于录制和回放声音文件)以及混合多个音频流。 如下是典型音频架构的一个示例:
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 mixer combines all its audio inputs into a single stream, which can be sent to an output device for rendering.
在该示例中,诸如声卡的设备具备各类输入和输出端口,而且在软件中提供混合。 混音器可能接收从文件读取,从网络流式传输,应用程序即时生成或由MIDI合成器生成的数据。 混音器将其全部音频输入组合成单个流,能够将其发送到输出设备进行渲染。
What is MIDI?
The javax.sound.midi package contains APIs for transporting and sequencing MIDI events, and for synthesizing sound from those events.
什么是MIDI?
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 data, unlike audio data, does not describe sound directly. Instead, it describes events that affect the sounds (or actions) performed by a MIDI-enabled device or instrument, such as a synthesizer. 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.
虽然采样音频是声音自己的直接表示,但MIDI数据能够被认为是用于建立声音的配方,尤为是音乐声音。与音频数据不一样,MIDI数据不直接描述声音。相反,它描述了影响由启用MIDI的设备或乐器(例如合成器)执行的声音(或动做)的事件。 MIDI数据相似于图形用户界面的键盘和鼠标事件。在MIDI的状况下,事件能够被认为是对音乐键盘的动做,以及对该乐器上的各类踏板,滑块,开关和旋钮的动做。这些事件实际上不须要硬件乐器;它们能够用软件模拟,也能够存储在MIDI文件中。能够建立,编辑和执行这些文件的程序称为顺控程序。许多计算机声卡包括MIDI可控音乐合成器芯片,音序器能够向其发送其MIDI事件。合成器也能够彻底用软件实现。合成器解释它们接收的MIDI事件并产生音频输出。一般,从MIDI数据合成的声音是音乐声音(例如,与语音相反)。 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. 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 to play an external instrument, or to control other external devices such as recording equipment.
某些声卡包括MIDI输入和输出端口,能够链接外部MIDI硬件设备(如键盘合成器或其余乐器)。 从MIDI输入端口,应用程序能够接收由配备有外部MIDI的乐器生成的事件。 该程序可使用计算机的内部合成器播放音乐表演,将其做为MIDI文件保存到磁盘,或将其呈现为曲谱。 程序可能使用MIDI输出端口播放外部乐器,或控制其余外部设备,如录音设备。
The following diagram illustrates the functional relationships between the major components in a possible MIDI configuration based on the Java Sound API. (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.) 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.
下图说明了基于Java Sound API的可能MIDI配置中主要组件之间的功能关系。 (与音频同样,Java Sound API容许安装和链接各类MIDI软件设备。此处显示的系统只是一种可能的状况。)组件之间的数据流用箭头表示。 数据能够采用标准文件格式,或者(如图中右下角的键所示),能够是音频,原始MIDI字节或带时间标记的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). 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 it.
在该示例中,应用程序经过将存储为标准MIDI文件的曲谱加载到盘(图的左侧)来准备音乐演奏。 标准MIDI文件包含轨道,每一个轨道都是带时间标记的MIDI事件列表。 大多数事件表明音符(音高和节奏)。 读取该MIDI文件,而后由软件定序器“执行”。 音序器经过将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. 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乐器以前转换为原始(非时间标记)MIDI。 相似地,从外部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. Here are some examples of services (resources) that can be added in this way:
服务提供者接口
javax.sound.sampled.spi和javax.sound.midi.spi包中包含的API容许软件开发人员建立新的音频或MIDI资源,这些资源能够单独提供给用户并“插入”Java Sound的现有实现API。 如下是能够经过这种方式添加的服务(资源)的一些示例:
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. 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. 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的实现包含一组基本服务,但服务提供者接口(SPI)包容许第三方建立新服务。 这些第三方服务以与内置服务相同的方式集成到系统中。 AudioSystem类和MidiSystem类充当协调器,容许应用程序显式或隐式地访问服务。 一般,服务的存在对于使用它的应用程序是彻底透明的。 服务提供程序机制使基于Java Sound API的应用程序用户受益,由于能够将新的声音功能添加到程序中而无需新版本的JDK或运行时环境,而且在许多状况下甚至不须要新的 发布应用程序自己。