在许多编程语言中,可以使用特定的库或函数来产生不同频率的蜂鸣声。以下是几个常用的编程语言的示例代码:
import winsound
import time
# 定义不同频率的蜂鸣声
frequencies = [261, 294, 329, 349, 392, 440, 493, 523]
# 播放不同频率的蜂鸣声
for frequency in frequencies:
winsound.Beep(frequency, 1000)
time.sleep(0.5)
#include
#include
int main() {
// 定义不同频率的蜂鸣声
int frequencies[] = {261, 294, 329, 349, 392, 440, 493, 523};
// 播放不同频率的蜂鸣声
for (int i = 0; i < sizeof(frequencies)/sizeof(frequencies[0]); i++) {
Beep(frequencies[i], 1000);
Sleep(500);
}
return 0;
}
import javax.sound.sampled.*;
public class Beeper {
public static void main(String[] args) throws LineUnavailableException {
// 定义不同频率的蜂鸣声
int[] frequencies = {261, 294, 329, 349, 392, 440, 493, 523};
// 播放不同频率的蜂鸣声
for (int frequency : frequencies) {
beep(frequency, 1000);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void beep(int frequency, int duration) throws LineUnavailableException {
byte[] buf = new byte[1];
AudioFormat af = new AudioFormat((float)44100, 8, 1, true, false);
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl.open(af);
sdl.start();
for (int i = 0; i < duration * (float)44100 / 1000; i++) {
double angle = i / ((float)44100 / frequency) * 2.0 * Math.PI;
buf[0] = (byte)(Math.sin(angle) * 100);
sdl.write(buf, 0, 1);
}
sdl.drain();
sdl.stop();
sdl.close();
}
}
以上是三种常用的编程语言的示例代码,可根据需要选择适合自己的代码来产生不同频率的蜂鸣声。
下一篇:不同频率的时序上的布尔掩码