日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

java sound api_Java Sound API

發(fā)布時(shí)間:2023/12/15 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java sound api_Java Sound API 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Java Sound API是javaSE平臺(tái)提供底層的(low-level)處理聲音接口。

例外,java也提供了簡單的實(shí)用的高層媒體接口(higher-level) - JMF(Java Media Framework)。

Java Sound API 將需要處理的數(shù)字音頻分為:simpled-audio和midi,

分別提供Package來處理它們:

javax.sound.simpled

javax.sound.midi

同時(shí)SOUND API還提供了第三方的擴(kuò)展接口:

javax.sound.simpled.spi

javax.sound.midi.spi

*注:spi : service provider interface

Sampled Audio

采樣音頻(simpled-audio)不僅包含從模擬信號(hào)采樣來的數(shù)字音頻,還包括電腦合成的。

稱作digital-audio更為合適。

為了能夠讓設(shè)備播放采樣聲音,程序需要處理 audio input, output device, audio data buffers。

還有混音處理(mix multiple streams of audio into one stream)。

SOUND API 可以使用3種方式傳輸聲音數(shù)據(jù):stream, buffered fashion, in-memory unbuffered fashion。

第三種方式適合數(shù)據(jù)量不大,能夠一次載入的所有數(shù)據(jù)的情形。這樣,聲音的響應(yīng)較快,循環(huán)和隨機(jī)定位也會(huì)很簡單。

使用SOUND API播放聲音至少需要3樣?xùn)|西:

lformatted audio data,

la mixer,

la line.

Mixer

調(diào)音臺(tái)

technically the Mixer itself is also a kind of Line

Line

音頻數(shù)據(jù)管道。

Clip extends Line

將需要播放的音頻數(shù)據(jù)裝載進(jìn)來。

preloads audio data from a sound file into clips

A Clip is a data line into which audio data can be loaded prior to playback. Because the data is

pre-loaded rather than streamed, the clip‘s duration is known before playback, and you can choose any

starting position in the media. Clips can be looped, meaning that upon playback, all the data between two

specified loop points will repeat a specified number of times, or indefinitely.

SourceDataLine extends Line

accept real-time stream of audio data

feed audio to the Mixer

A SourceDataLine receives audio data for playback. It provides methods for writing data to the

source data line‘s buffer for playback, and for determining how much data the line is prepared to receive

without blocking.

TargetDataLine

A TargetDataLine receives audio data from a mixer. Commonly, the mixer has captured audio data

from a port such as a microphone; it might process or mix this captured audio before placing the data in

the target data line‘s buffer. The TargetDataLine interface provides methods for reading the data

from the target data line‘s buffer and for determining how much data is currently available for reading.

Port extends Line

simple Line

Line接口的繼承關(guān)系圖

AudioSystem

AudioSystem提供音頻資源的訪問服務(wù)。

通過AudioSystem,可以知道什么樣的資源可以被識(shí)別。

可從AudioSystem獲得的資源:

lMixers, AudioSystem類可以提供一張已經(jīng)安裝的Mixer列表

lLines

lFormat conversions

lFiles and streams

Mixer的獲得

Mixer.Info

AudioSystem.getMixerInfo():Mixer.Info

可以獲得一張Mixer信息列表。

每個(gè)Mixer.Info包含如下關(guān)鍵信息:

lName

lVersion

lVendor

lDescription

我機(jī)器上的Mixer列表,WinXP,JDK_1.4.2

[INFO 0]

INFO.NAME:Java Sound Audio Engine

INFO.VERSION:1.0

INFO.VERDOR:Sun Microsystems

INFO.DESCRIPTION:Software mixer and synthesizer

[INFO 1]

INFO.NAME:Microsoft ?ù??????÷

INFO.VERSION:Unknown Version

INFO.VERDOR:Unknown Vendor

INFO.DESCRIPTION:No details available

[INFO 2]

INFO.NAME:Realtek AC97 Audio

INFO.VERSION:Unknown Version

INFO.VERDOR:Unknown Vendor

INFO.DESCRIPTION:No details available

[INFO 3]

INFO.NAME:Realtek AC97 Audio

INFO.VERSION:5.10

INFO.VERDOR:Unknown Vendor

INFO.DESCRIPTION:Unknown Description

獲取Mixer

AudioSystem.getMixer( MixerInfo ):Mixer

如果只從Mixer.Info提供的信息無法確定需要的Mixer,

不妨創(chuàng)建出所有的Mixer,使用時(shí)檢查它們的能力,使用合適那個(gè)。

例如,你可能需要一個(gè)Mixer,能夠?qū)⒒煲艉玫臄?shù)據(jù)同時(shí)寫入一定數(shù)目的目標(biāo)數(shù)據(jù)管道(TargetDataLine).

使用Mixer.getMaxLines(Line.Info info):int來了解Mixer的輸出能力。info就是指定的TargetDataLine

獲得指定類型Line

2種方法獲得Line

l直接由AudioSystem獲得,AudioSystem.getLine( Line.Info ):Line

l由Mixer獲得

從AudioSystem直接獲得Line

static Line AudioSystem.getLine( Line.Info )

不同于Mixer.Info,Line.Info不是文本信息,而是Line的類信息。

Line.Info是抽象類,所以使用它的子類DataLine.Info,Port.Info。

下面是通過Line.Info獲得Line的示例:

TargetDataLine line;

DataLine.Info info = new DataLine.Info(TargetDataLine.class,

format); // format is an AudioFormat object

if (!AudioSystem.isLineSupported(info)) {

// Handle the error.

}

// Obtain and open the line.

try {

line = (TargetDataLine) AudioSystem.getLine(info);

line.open(format);

} catch (LineUnavailableException ex) {

// Handle the error.

//...

}

Port.Info定義一系列靜態(tài)的Port.Info對象,MICROPHONE,SPEAKER,etc.

從Mixer獲得Line

getSourceDataLine()

getTargetDataLine()

getLine()

AudioSystem對象模型

AudioPermission

音頻資源訪問許可。

利用JAVA-SOUND-API播放聲音

可以使用2種Line來播放聲音,Clip,SourceDataLine。

Clip一次載入需要播放的聲音資源,而SourceDataLine以流的方式傳輸聲音數(shù)據(jù)。

使用Clip

當(dāng)使用getLine()獲得Clip后,還要保證其他的程序在你播放前不能獲取它,調(diào)用open()方法獨(dú)占它:

void open( AudioInputStream stream );

void open( AudioFormat, byte[] data, int offset, int bufferSize );

Clip默認(rèn)從音頻的開頭開始播放,除非使用setFramePosition(),setMicroSecondPosition()設(shè)定其他位置。

Clip.start()播放,Clip.stop()暫停。

getLevel(),獲得聲音高度。

isActive(),Clip是否正在播放。

使用SourceDataLine

open(AudioFormat),打開source dataLine,但不指定數(shù)據(jù),使用默認(rèn)的buffer size。

open(AudioFormat, bufferSize),指定buffer size.

合理設(shè)置buffer size,保證開始載入的延時(shí)能夠被接受,又盡量減少IO訪問。

open()之后,調(diào)用start()容許SourceDataLine一有數(shù)據(jù)就開始播放,使用write()不停的輸入數(shù)據(jù)。

void write( byte[] b, int offset, int length );

SourceDataLine開始播放后,向Mixer傳輸數(shù)據(jù),當(dāng)Mixer開始向target傳輸數(shù)據(jù)時(shí),SourceDataLine產(chǎn)生一個(gè)START事件。

這是SourceDataLine被認(rèn)為是活動(dòng)的(active)。

isRunning()表明Line是否start()了。

isActive()表明Line是否開始播放。

write()方法向buffer size中寫入數(shù)據(jù),如果buffer已滿,還剩下一些數(shù)據(jù),該方法會(huì)阻塞;否則,是不阻塞的。

可以使用DataLine.available()知道buffer size還能寫入多少數(shù)據(jù)。

事實(shí)上,可以另開線程去使用write(),而不用考慮阻塞的問題。

drain()方法在所有數(shù)據(jù)播放完后返回。

所有在寫完數(shù)據(jù)后,調(diào)用drain(),到它返回時(shí)再是否Line。

line.write(b, offset, numBytesToWrite);

//this is the final invocation of write

line.drain();

line.stop();

line.close();

line = null;

flush()清空buffer中的剩余數(shù)據(jù),Line在stop時(shí)才能調(diào)用。

有如下情形,Line會(huì)產(chǎn)生STOP事件:

l調(diào)用drain()

l調(diào)用stop()

l調(diào)用flush()

l輸出完舊的數(shù)據(jù),而新的數(shù)據(jù)未到時(shí)。

STOP事件意味著isActive()返回false.

start()調(diào)用之后,isRunning()都會(huì)返回true,知道stop()被調(diào)用。它不是依據(jù)STOP事件產(chǎn)生返回值的。

而isActive()是依據(jù)START和STOP事件的。

監(jiān)視Line的狀態(tài)

使用LineListener響應(yīng)Line的事件。

void Line.addLineListener( LineListener );

當(dāng)調(diào)用open(),close(),start(),stop()會(huì)產(chǎn)生OPEN,CLOSE,START,STOP事件。

多個(gè)Line同步播放

有些Mixer提供方便的同步功能,對一組Lines使用open(),close(),start(),stop(),保證它們的同步。

可以使用如下方法,檢查Mixer是否支持同步功能:

boolean isSynchronizationSupported( Line[] lines, boolean maintainSync )

第二個(gè)參數(shù)表明同步精度,是采樣同步,還是只是start(),stop()保持同步,并不維護(hù)播放過程同步。

AudioFormat

音頻采樣的格式,不是音頻文件的格式。

l編碼技術(shù),一般都是PCM( pulse code modulation )

l聲道數(shù)目(1,單聲道;2,雙聲道;等等)

l采樣頻率sample rate

l樣本的位數(shù)number of bits per sample

l幀速率Frame rate

lFrame Size in bytes

lByte Order( big-endian or little-endian )

AudioFileFormat:

音頻文件格式。

The file type( WAV,AIFF,etc )

The file length in bytes

The length, in frames, of the audio data contained in the file

An AudioFormat that specifies data format of the audio data in the file

AudioInputStream extends InputStream

無須考慮文件的格式,就能操作Samples。

讀取音頻文件

AudioSystem提供2中方法讀取音頻文件:

l根據(jù)音頻文件中音頻數(shù)據(jù)的格式信息

l使用一個(gè)指定了音頻數(shù)據(jù)格式的流

使用如下方法獲得音頻文件中音頻數(shù)據(jù)的格式信息:

static AudioFileFormat getAudioFileFormat(File);

static AudioFileFormat getAudioFileFormat(InputStream);

static AudioFileFormat getAudioFileFormat(URL);

利用如下方法獲得第二種方法提到的音頻數(shù)據(jù)流:

static AudioInputStream getAudioInputStream(File)

static AudioInputStream getAudioInputStream(InputStream)

static AudioInputStream getAudioInputStream(URL)

讀取音頻文件數(shù)據(jù)的步驟:

1)獲得AudioInputStream對象

2)創(chuàng)建一個(gè)字節(jié)數(shù)組,存放一次讀入的數(shù)據(jù)塊

3)不斷地從audio流中讀入數(shù)據(jù),播放或處理數(shù)據(jù)。

示例代碼如下:

int totalFramesRead = 0;

File fileIn = new File(somePathName);

// somePathName is a pre-existing string whose value was

// based on a user selection.

try {

AudioInputStream audioInputStream =

AudioSystem.getAudioInputStream(fileIn);

int bytesPerFrame =

audioInputStream.getFormat().getFrameSize();

// Set an arbitrary buffer size of 1024 frames.

int numBytes = 1024 * bytesPerFrame;

byte[] audioBytes = new byte[numBytes];

try {

int numBytesRead = 0;

int numFramesRead = 0;

// Try to read numBytes bytes from the file.

while ((numBytesRead =

audioInputStream.read(audioBytes)) != -1) {

// Calculate the number of frames actually read.

numFramesRead = numBytesRead / bytesPerFrame;

totalFramesRead += numFramesRead;

// Here, do something useful

// with the audio data that‘s

// now in the audioBytes array...

}

} catch (Exception ex) {

// Handle the error...

}

} catch (Exception e) {

// Handle the error...

}

寫音頻文件

通過下列方法知道AudioSystem支持哪些音頻文件格式寫入:

static boolean isFileTypeSupported( AudioFileFormat.Type,AudioInputStream );

static AudioFileFormat.Type[] getAudioFileTypes();

static AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream);

利用AudioSystem.write()方法向文件寫入指定格式的音頻數(shù)據(jù):

static int write( AudioInputStream, AudioFileFormat.Type, File );

文件或數(shù)據(jù)格式轉(zhuǎn)換

“Java Sound Programmer Guide” – chapter 7

Audio File Format Convertion

Audio Data Format Convertion

PCM

PCM脈沖編碼調(diào)制是Pulse Code Modulation的縮寫。

PCM通過抽樣、量化、編碼三個(gè)步驟將連續(xù)變化的模擬信號(hào)轉(zhuǎn)換為數(shù)字編碼。

PCM是數(shù)字音頻中最佳的保真水準(zhǔn),近似看成“無損”編碼。

PCM編碼的優(yōu)點(diǎn)是音質(zhì)好,缺點(diǎn)是數(shù)據(jù)量大。

JAVA SOUND API對于其它編碼格式,在播放前都會(huì)轉(zhuǎn)換成PCM格式。

DAC:digital-to-analog converter,數(shù)模轉(zhuǎn)換器

Decibel:分貝。pl.decibels

PAN:聲象,該通道信號(hào)在左右音箱之間的立體聲位置。

GAIN:增益

REVERB:數(shù)字混響。

Acoustics:聲學(xué)

資源

《Java Sound programmer guide》

總結(jié)

以上是生活随笔為你收集整理的java sound api_Java Sound API的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。