Java生成声音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1932490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:34:14  来源:igfitidea点击:

Java generating sound

javaaudio

提问by Hamza Yerlikaya

I created a pong clone and I would like to add some sound effects when collisions occur. My problem is that every example I could find about synthesizing sound takes about 30 lines of code, considering my whole application has only 90 lines of code. I am looking for a simpler approach. Is there a simple way to create a beep sound of different tones? Duration does not matter. I just want a series of beeps with different tones.

我创建了一个乒乓球克隆,我想在发生碰撞时添加一些声音效果。我的问题是,考虑到我的整个应用程序只有 90 行代码,我能找到的关于合成声音的每个示例都需要大约 30 行代码。我正在寻找一种更简单的方法。有没有一种简单的方法来创建不同音调的哔声?持续时间无关紧要。我只想要一系列不同音调的哔哔声。

采纳答案by tangens

Here's a small example taken (and shortened) from Java Sound - Example: Code to generate audio tone

这是从Java Sound 中获取(并缩短)的一个小示例- 示例:生成音频音调的代码

    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 )44100 / 1000; i++ ) {
        double angle = i / ( (float )44100 / 440 ) * 2.0 * Math.PI;
        buf[ 0 ] = (byte )( Math.sin( angle ) * 100 );
        sdl.write( buf, 0, 1 );
    }
    sdl.drain();
    sdl.stop();

回答by Sev

java.awt.Toolkit.getDefaultToolkit().beep()

java.awt.Toolkit.getDefaultToolkit().beep()

series of beeps?

一连串的哔哔声?

int numbeeps = 10;

for(int x=0;x<numbeeps;x++)
{
  java.awt.Toolkit.getDefaultToolkit().beep();
}

回答by Martijn Courteaux

You can use JSyn. This is a lib you have to install (with a .DLLand a .JAR). But very simple to create diffrent tones.

您可以使用 JSyn。这是您必须安装的库(带有 a.DLL和 a .JAR)。但是很容易创建不同的音调。

Link(Also tutorials available)

链接(也提供教程)

This is an example:

这是一个例子:

public static void main(String[] args) throws Exception {
    SawtoothOscillatorBL osc;
    LineOut lineOut;
    // Start JSyn synthesizer.
    Synth.startEngine(0);

    // Create some unit generators.
    osc = new SawtoothOscillatorBL();
    lineOut = new LineOut();

    // Connect oscillator to both left and right channels of output.
    osc.output.connect(0, lineOut.input, 0);
    osc.output.connect(0, lineOut.input, 1);

    // Start the unit generators so they make sound.
    osc.start();
    lineOut.start();

    // Set the frequency of the oscillator to 200 Hz.
    osc.frequency.set(200.0);
    osc.amplitude.set(0.8);

    // Sleep for awhile so we can hear the sound.
    Synth.sleepForTicks(400);

    // Change the frequency of the oscillator.
    osc.frequency.set(300.0);
    Synth.sleepForTicks(400);

    // Stop units and delete them to reclaim their resources.
    osc.stop();
    lineOut.stop();
    osc.delete();
    lineOut.delete();

    // Stop JSyn synthesizer.
    Synth.stopEngine();
}

Martijn

马丁

回答by Bojan Vukasovic

Here is same code as above with a bit of description in 16 bits

这是与上面相同的代码,其中有一些 16 位的描述

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;

public class MakeSound {
  public static void main(String[] args) throws LineUnavailableException {
    System.out.println("Make sound");
    byte[] buf = new byte[2];
    int frequency = 44100; //44100 sample points per 1 second
    AudioFormat af = new AudioFormat((float) frequency, 16, 1, true, false);
    SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
    sdl.open();
    sdl.start();
    int durationMs = 5000;
    int numberOfTimesFullSinFuncPerSec = 441; //number of times in 1sec sin function repeats
    for (int i = 0; i < durationMs * (float) 44100 / 1000; i++) { //1000 ms in 1 second
      float numberOfSamplesToRepresentFullSin= (float) frequency / numberOfTimesFullSinFuncPerSec;
      double angle = i / (numberOfSamplesToRepresentFullSin/ 2.0) * Math.PI;  // /divide with 2 since sin goes 0PI to 2PI
      short a = (short) (Math.sin(angle) * 32767);  //32767 - max value for sample to take (-32767 to 32767)
      buf[0] = (byte) (a & 0xFF); //write 8bits ________WWWWWWWW out of 16
      buf[1] = (byte) (a >> 8); //write 8bits WWWWWWWW________ out of 16
      sdl.write(buf, 0, 2);
    }
    sdl.drain();
    sdl.stop();
  }
}