java 改变音频的振幅_使用JAVA从wav文件中提取振幅数组

该博客介绍了如何使用Java从WAV文件中读取和处理音频振幅。通过WaveFile类,可以获取指定帧的振幅值,并提供了播放音频的功能。此外,还提到了WAV文件的组成以及对不同位深音频文件的处理限制。
摘要由CSDN通过智能技术生成

这是一个可以使用的帮助类. getSampleInt()方法是你需要得到的幅度:

File file = ...;

WavFile wav = new WavFile(file);

int amplitudeExample = wav.getSampleInt(140); // 140th amplitude value.

for (int i = 0; i < wav.getFramesCount(); i++) {

int amplitude = wav.getSampleInt(i);

// Plot.

}

它也可以播放文件,以便您可以测试它,但只有8位或16位文件.对于其他情况,您只能阅读它们.

另外,请查看these diagrams查看WAV文件的组成,并更好地了解此类的作用.

public class WaveFile {

public final int NOT_SPECIFIED = AudioSystem.NOT_SPECIFIED; // -1

public final int INT_SIZE = 4;

private int sampleSize = NOT_SPECIFIED;

private long framesCount = NOT_SPECIFIED;

private int sampleRate = NOT_SPECIFIED;

private int channelsNum;

private byte[] data; // wav bytes

private AudioInputStream ais;

private AudioFormat af;

private Clip clip;

private boolean canPlay;

public WaveFile(File file) throws UnsupportedAudioFileException, IOException {

if (!file.exists()) {

throw new FileNotFoundException(file.getAbsolutePath());

}

ais = AudioSystem.getAudioInputStream(file);

af = ais.getFormat();

framesCount = ais.getFrameLength();

sampleRate = (int) af.getSampleRate();

sampleSize = af.getSampleSizeInBits() / 8;

channelsNum = af.getChannels();

long dataLength = framesCount * af.getSampleSizeInBits() * af.getChannels() / 8;

data = new byte[(int) dataLength];

ais.read(data);

AudioInputStream aisForPlay = AudioSystem.getAudioInputStream(file);

try {

clip = AudioSystem.getClip();

clip.open(aisForPlay);

clip.setFramePosition(0);

canPlay = true;

} catch (LineUnavailableException e) {

canPlay = false;

System.out.println("I can play only 8bit and 16bit music.");

}

}

public boolean isCanPlay() {

return canPlay;

}

public void play() {

clip.start();

}

public void stop() {

clip.stop();

}

public AudioFormat getAudioFormat() {

return af;

}

public int getSampleSize() {

return sampleSize;

}

public double getDurationTime() {

return getFramesCount() / getAudioFormat().getFrameRate();

}

public long getFramesCount() {

return framesCount;

}

/**

* Returns sample (amplitude value). Note that in case of stereo samples

* go one after another. I.e. 0 - first sample of left channel, 1 - first

* sample of the right channel, 2 - second sample of the left channel, 3 -

* second sample of the rigth channel, etc.

*/

public int getSampleInt(int sampleNumber) {

if (sampleNumber < 0 || sampleNumber >= data.length / sampleSize) {

throw new IllegalArgumentException(

"sample number can't be < 0 or >= data.length/"

+ sampleSize);

}

byte[] sampleBytes = new byte[4]; //4byte = int

for (int i = 0; i < sampleSize; i++) {

sampleBytes[i] = data[sampleNumber * sampleSize * channelsNum + i];

}

int sample = ByteBuffer.wrap(sampleBytes)

.order(ByteOrder.LITTLE_ENDIAN).getInt();

return sample;

}

public int getSampleRate() {

return sampleRate;

}

public Clip getClip() {

return clip;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值