1. 程式人生 > >Sound — An API for playing sound data from applications.

Sound — An API for playing sound data from applications.

https://docs.oracle.com/javase/tutorial/sound/index.html

 

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 Sound API是一個底層API,用於實現和控制聲音媒體的輸入和輸出,包括音訊和樂器數字介面(MIDI)資料。 Java Sound API在一個促進可擴充套件性和靈活性的框架中提供對聲音輸入和輸出通常所需功能的顯式控制。

 

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

Java Sound 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:

  • 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.

Java Sound API包括對數字音訊和MIDI資料的支援。 這兩個主要功能模組在單獨的包中提供:

  • javax.sound.sampled - 此包指定用於捕獲,混合和回放數字(取樣)音訊的介面。
  • javax.sound.midi - 此包提供MIDI合成,排序和事件傳輸的介面。

 

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實現的功能:

  • javax.sound.sampled.spi
  • javax.sound.midi.spi

 

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將其稱為取樣音訊。 樣本是訊號的連續快照。 在音訊的情況下,訊號是聲波。 麥克風將聲學訊號轉換為相應的模擬電訊號,並且模擬 - 數字轉換器將該模擬訊號轉換為取樣的數字形式。 下圖顯示了錄音中的短暫時刻。

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.

該圖描繪了垂直軸上的聲壓(振幅)和水平軸上的時間。 以一定速率週期性地測量模擬聲波的幅度,從而產生包括數字音訊訊號的離散樣本(圖中的紅色資料點)。 中心水平線表示零幅度; 線上方的點是正值樣本,下面的點是負數。 模擬訊號的數字近似的精確度取決於其在時間上的解析度(取樣率)及其量化,或幅度解析度(用於表示每個樣本的位元數)。 作為參考,記錄儲存在光碟上的音訊每秒取樣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支援常見功能,例如音效卡的輸入和輸出(例如,用於錄製和回放聲音檔案)以及混合多個音訊流。 以下是典型音訊架構的一個示例:

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. 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資訊。

A Possible MIDI Configuration

 

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。 以下是可以通過這種方式新增的服務(資源)的一些示例:

 

  • 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
  • 音訊混音器
  • MIDI合成器
  • 一種檔案解析器,可以讀取或寫入新型別的音訊或MIDI檔案
  • 轉換器,可在不同的聲音資料格式之間進行轉換

 

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或執行時環境,並且在許多情況下甚至不需要新的 釋出應用程式本身。