gpt4 book ai didi

java.awt.Toolkit beep() 错误

转载 作者:行者123 更新时间:2023-12-01 10:25:16 25 4
gpt4 key购买 nike

好的,我会尽快完成。所以我将一段 C++ 代码快速移植到了 java,因为这个库无法在 ubuntu 上运行。我一直在寻找,我需要通过扬声器发出蜂鸣声。但它需要具有不同的频率。它连接到两个随机数生成器,根据这些随机数的结果,它应该以不同的频率发出蜂鸣声。这不太有效。有什么需要补充的吗?这是代码

import java.util.Random;
import java.awt.Toolkit;

public class RandomSinging {


public static void main(String args[]) {
// TODO code application logic here

Toolkit tk = Toolkit.getDefaultToolkit();

double c3 = 130.81;
double cS3 = 138.59;
double d3 = 146.83;
double dS3 = 155.56;
double e3 = 164.81;
double f3 = 174.61;
double fS3 = 185;
double g3 = 196;
double gS3 = 207.65;
double a3 = 220;
double aS3 = 233.08;
double b3 = 246.94;

double c4 = 261.63;
double cS4 = 277.18;
double d4 = 293.66;
double dS4 = 311.13;
double e4 = 329.63;
double f4 = 349.23;
double fS4 = 369.99;
double g4 = 392;
double gS4 = 415.3;
double a4 = 440;
double aS4 = 466.16;
double b4 = 493.88;

double c5 = 523.25;
double cS5 = 554.37;
double d5 = 587.33;
double dS5 = 622.25;
double e5 = 659.25;
double f5 = 698.46;
double fS5 = 739.99;
double g5 = 783.99;
double gS5 = 830.61;
double a5 = 880;
double aS5 = 962.33;
double b5 = 987.77;

int dummyInt = 0;

Random toneInt = new Random();
Random octInt = new Random();

while (dummyInt > -1){
toneInt.nextInt(1);
octInt.nextInt(1);

System.out.print("toneInt; ");
System.out.println(toneInt.nextInt(12));
System.out.print("octInt; ");
System.out.println(octInt.nextInt(3));

if (octInt.equals(0)){
if (toneInt.equals(0)) tk.beep();
else if (toneInt.equals(1)) tk.beep();
else if (toneInt.equals(2)) tk.beep();
else if (toneInt.equals(3)) tk.beep();
else if (toneInt.equals(4)) tk.beep();
else if (toneInt.equals(5)) tk.beep();
else if (toneInt.equals(6)) tk.beep();
else if (toneInt.equals(7)) tk.beep();
else if (toneInt.equals(8)) tk.beep();
else if (toneInt.equals(9)) tk.beep();
else if (toneInt.equals(10)) tk.beep();
else if (toneInt.equals(11)) tk.beep();
}

if (octInt.equals(1)){
if (toneInt.equals(0)) tk.beep();
else if (toneInt.equals(1)) tk.beep();
else if (toneInt.equals(2)) tk.beep();
else if (toneInt.equals(3)) tk.beep();
else if (toneInt.equals(4)) tk.beep();
else if (toneInt.equals(5)) tk.beep();
else if (toneInt.equals(6)) tk.beep();
else if (toneInt.equals(7)) tk.beep();
else if (toneInt.equals(8)) tk.beep();
else if (toneInt.equals(9)) tk.beep();
else if (toneInt.equals(10)) tk.beep();
else if (toneInt.equals(11)) tk.beep();
}
if (octInt.equals(2)){
if (toneInt.equals(0)) tk.beep();
else if (toneInt.equals(1)) tk.beep();
else if (toneInt.equals(2)) tk.beep();
else if (toneInt.equals(3)) tk.beep();
else if (toneInt.equals(4)) tk.beep();
else if (toneInt.equals(5)) tk.beep();
else if (toneInt.equals(6)) tk.beep();
else if (toneInt.equals(7)) tk.beep();
else if (toneInt.equals(8)) tk.beep();
else if (toneInt.equals(9)) tk.beep();
else if (toneInt.equals(10)) tk.beep();
else if (toneInt.equals(11)) tk.beep();
}
dummyInt++;
}

}

}

再次不确定我是否只是愚蠢,这无法与该语言一起使用,或者我是否缺少它需要的参数,但我已经查看了所有内容,但没有骰子。

最佳答案

我曾经也遇到过类似的问题。我找到了一个使用 aetherwalker 提到的 javax.sound 库生成曲调的示例。

 public void getSound(int f, int time) throws LineUnavailableException{
byte[] buf = new byte[ 1 ];
AudioFormat af = new AudioFormat( (float)44100, 8, 1, true, false );
SourceDataLine sdl = AudioSystem.getSourceDataLine( af );
sdl.open();
sdl.start();
for( int i = 0; i < 1000 * (float)time / 1000; i++ ) {
double angle = i / ((float)f / 440 ) * 2.0 * Math.PI;
buf[ 0 ] = (byte)( Math.sin(angle ) * 100 );
sdl.write( buf, 0, 1 );
}

sdl.drain();
sdl.stop();

}

它非常适合我的目的。因此,您可以将 tk.beep() 调用替换为 someNiceSoundClass.getSound(int,int) 和某些值(您可以根据随机数计算)。

关于java.awt.Toolkit beep() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35384309/

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