gpt4 book ai didi

java - 计算两个 channel 的音频电平/幅度/db

转载 作者:行者123 更新时间:2023-12-01 17:41:08 26 4
gpt4 key购买 nike

我读过两篇关于从 AudioInputStream 中提取样本并将其转换为 dB 的文章。

https://stackoverflow.com/a/26576548/8428414

https://stackoverflow.com/a/26824664/8428414

据我了解 byte[] bytes; 的结构如下:

Index 0: Sample 0 (Left Channel)
Index 1: Sample 0 (Right Channel)
Index 2: Sample 1 (Left Channel)
Index 3: Sample 1 (Right Channel)
Index 4: Sample 2 (Left Channel)
Index 5: Sample 2 (Right Channel)

在第一篇文章中,它展示了如何从一个 channel (单声道)获取样本。

所以,我的问题是我想分别获取右声道和左声道的样本,以便计算左右声道的 dB。

这是代码。如何更改为分别获取左右声道?我无法理解索引 i 如何变化...

final byte[] buffer = new byte[2048];

float[] samples = new float[buffer.length / 2];

for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) {
line.write(buffer, 0, n);

for (int i = 0, sampleIndex = 0; i < n; ) {
int sample = 0;

sample |= buffer[i++] & 0xFF; // (reverse these two lines
sample |= buffer[i++] << 8; // if the format is big endian)

// normalize to range of +/-1.0f
samples[sampleIndex++] = sample / 32768f;
}

float rms = 0f;
for (float sample : samples) {
rms += sample * sample;
}

rms = (float) Math.sqrt(rms / samples.length);

希望你能帮助我。预先感谢您。

最佳答案

立体声信号保存的格式称为interleaved 。也就是说,正如您所描述的正确,它是 LLRRLLRRLLRR...。因此,您首先需要读取左侧样本,然后读取右侧样本,依此类推。

我已经编辑了您的代码以反射(reflect)这一点。然而,通过refactoring还有一些改进的空间。 .

注意:代码更改仅处理交错。我还没有检查你的其余代码。

final byte[] buffer = new byte[2048];

// create two buffers. One for the left, one for the right channel.
float[] leftSamples = new float[buffer.length / 4];
float[] rightSamples = new float[buffer.length / 4];

for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) {
line.write(buffer, 0, n);

for (int i = 0, sampleIndex = 0; i < n; ) {
int sample = 0;

leftSample |= buffer[i++] & 0xFF; // (reverse these two lines
leftSample |= buffer[i++] << 8; // if the format is big endian)

rightSample |= buffer[i++] & 0xFF; // (reverse these two lines
rightSample |= buffer[i++] << 8; // if the format is big endian)

// normalize to range of +/-1.0f
leftSamples[sampleIndex] = leftSample / 32768f;
rightSamples[sampleIndex] = rightSample / 32768f;

sampleIndex++;
}

// now compute RMS for left
float leftRMS = 0f;
for (float sample : leftSamples) {
leftRMS += sample * sample;
}

leftRMS = (float) Math.sqrt(leftRMS / leftSamples.length);

// ...and right
float rightRMS = 0f;
for (float sample : rightSamples) {
rightRMS += sample * sample;
}

rightRMS = (float) Math.sqrt(rightRMS / rightSamples.length);
}

关于java - 计算两个 channel 的音频电平/幅度/db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61149086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com