Sound — An API for playing sound data from applications.

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:編程

  • 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知足了各類應用程序開發人員的需求。 潛在的應用領域包括: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

  • 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數據的支持。 這兩個主要功能模塊在單獨的包中提供:app

  • 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或運行時環境,而且在許多狀況下甚至不須要新的 發佈應用程序自己。

相關文章
相關標籤/搜索