[英]Generated sound played with SouceDataLine is fuzzy
我正在嘗試實時生成一組同時發出的音調。 但是程序產生的所有聲音都是“模糊的”,或者是“靜態的”,甚至在后台聽起來像是“尖叫”。 這在較低音調的聲音中尤其明顯。 這是代碼:
static final long bufferLength = 44100;
static final AudioFormat af = new AudioFormat(bufferLength, 8, 1, true, false);
static boolean go = true; //to be changed somewhere else
static void startSound(double[] hertz) {
if (hertz.length == 0) {return;}
try {
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl.open();
sdl.start();
int i = 0;
//iterate as long as the sound must play
do {
//create a new buffer
double[] buf = new double[128]; //arbitrary number
final int startI = i;
//iterate through each of the tones
for (int k = 0; k < hertz.length; k++) {
i = startI;
//iterate through each of the samples for this buffer
for (int j = 0; j < buf.length; j++) {
double x = (double)i/bufferLength*hertz[k]*2*Math.PI;
double wave1 = Math.sin(x);
//decrease volume with increasing pitch
double volume = Math.min(Math.max(300 - hertz[k], 50d), 126d);
buf[j] += wave1*volume;
i++;
if (i == 9999999) { //prevent i from getting too big
i = 0;
}
}
}
final byte[] finalBuffer = new byte[buf.length];
//copy the double buffer into byte buffer
for (int j = 0; j < buf.length; j++) {
//divide by hertz.length to prevent simultaneous sounds
// from becoming too loud
finalBuffer[j] = (byte)(buf[j]/hertz.length);
}
//play the sound
sdl.write(finalBuffer, 0, finalBuffer.length);
} while (go);
sdl.flush();
sdl.stop();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
//play some deep example tones
startSound(new double[]{65.4064, 58.2705, 48.9995});
我試過錄制從該程序輸出的聲音,並且波形確實有些參差不齊。 但是,當我直接從程序中打印出生成的波形時,它們看起來非常平滑。 我產生的聲音似乎與揚聲器發出的聲音不匹配。 誰能知道我做錯了什么?
根據我的評論,我認為您由於8位音頻而聽到量化錯誤,因此您應該切換到16位。 量化誤差有時被稱為噪聲,但是平方諧波失真的一種,是您聽到的細微泛音的來源。
8位有時對於語音之類的東西來說是可以接受的,它聽起來更像是噪音。 純音會使失真更明顯。
class SoundTest {
static final int bufferLength = 44100;
static final AudioFormat af8 = new AudioFormat(bufferLength, 8, 1, true, false);
static final AudioFormat af16 = new AudioFormat(bufferLength, 16, 1, true, false);
static volatile boolean go = true; //to be changed somewhere else
static void startSound8(double[] hertz) {
if (hertz.length == 0) {return;}
try {
SourceDataLine sdl = AudioSystem.getSourceDataLine(af8);
sdl.open();
sdl.start();
int i = 0;
//iterate as long as the sound must play
do {
//create a new buffer
double[] buf = new double[128]; //arbitrary number
final int startI = i;
//iterate through each of the tones
for (int k = 0; k < hertz.length; k++) {
i = startI;
//iterate through each of the samples for this buffer
for (int j = 0; j < buf.length; j++) {
double x = (double)i/bufferLength*hertz[k]*2*Math.PI;
double wave1 = Math.sin(x);
//decrease volume with increasing pitch
// double volume = Math.min(Math.max(300 - hertz[k], 50d), 126d);
double volume = 64;
buf[j] += wave1*volume;
i++;
if (i == 9999999) { //prevent i from getting too big
i = 0;
}
}
}
final byte[] finalBuffer = new byte[buf.length];
//copy the double buffer into byte buffer
for (int j = 0; j < buf.length; j++) {
//divide by hertz.length to prevent simultaneous sounds
// from becoming too loud
finalBuffer[j] = (byte)(buf[j]/hertz.length);
}
//play the sound
sdl.write(finalBuffer, 0, finalBuffer.length);
} while (go);
sdl.flush();
sdl.stop();
synchronized (SoundTest.class) {
SoundTest.class.notifyAll();
}
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
static void startSound16(double[] hertz) {
if (hertz.length == 0) {return;}
try {
SourceDataLine sdl = AudioSystem.getSourceDataLine(af16);
sdl.open();
sdl.start();
int i = 0;
//iterate as long as the sound must play
do {
//create a new buffer
double[] buf = new double[128]; //arbitrary number
final int startI = i;
//iterate through each of the tones
for (int k = 0; k < hertz.length; k++) {
i = startI;
//iterate through each of the samples for this buffer
for (int j = 0; j < buf.length; j++) {
double x = (double)i/bufferLength*hertz[k]*2*Math.PI;
double wave1 = Math.sin(x);
//decrease volume with increasing pitch
// double volume = Math.min(Math.max(300 - hertz[k], 50d), 126d);
double volume = 16384;
buf[j] += wave1*volume;
i++;
if (i == 9999999) { //prevent i from getting too big
i = 0;
}
}
}
final byte[] finalBuffer = new byte[buf.length * 2];
//copy the double buffer into byte buffer
for (int j = 0; j < buf.length; j++) {
//divide by hertz.length to prevent simultaneous sounds
// from becoming too loud
int a = (int) (buf[j] / hertz.length);
finalBuffer[j * 2] = (byte) a;
finalBuffer[(j * 2) + 1] = (byte) (a >>> 8);
}
//play the sound
sdl.write(finalBuffer, 0, finalBuffer.length);
} while (go);
sdl.flush();
sdl.stop();
synchronized (SoundTest.class) {
SoundTest.class.notifyAll();
}
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
static void playTone(final double hz, final boolean fewBits) {
go = true;
new Thread() {
@Override
public void run() {
if (fewBits) {
startSound8(new double[] {hz});
} else {
startSound16(new double[] {hz});
}
}
}.start();
try {
Thread.sleep(5000);
} catch (InterruptedException x) {
x.printStackTrace();
} finally {
go = false;
synchronized (SoundTest.class) {
try {
SoundTest.class.wait();
} catch (InterruptedException x) {
x.printStackTrace();
}
}
}
}
public static void main(String[] args) {
playTone(220, true);
playTone(220, false);
}
}
我將在此處討論用於打包16位字節數組的位操作的概念,並提供示例代碼。
還值得一提的是,如果出於某種原因專業應用程序需要使用8位,則可能在量化之前添加抖動 ,這聽起來比純量化誤差更好。 (就此而言,與16位相同,但除非累積了16位量化誤差,否則聽不到。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.