Java中的正弦波声音发生器

mil*_*ilo 13 java audio trigonometry javasound wave

在Java中以任何频率生成正弦波声音的最简单方法是什么?样本大小超过2个字节会有所帮助,但这并不重要.

谢谢

And*_*son 11

请参阅Beeper自包含示例.


也许更简单的事情?

如链接答案顶部所示,51行代码段(重复低于 - 单行和在线注释的间隔)就像生成音调一样简单(好吧,你可以取5行以上)谐波).

人们似乎认为应该是工具包内置的一种方法来产生纯音.它不是,并且需要一点计算来制作一个.

/** Generates a tone, and assigns it to the Clip. */
public void generateTone()
    throws LineUnavailableException {
    if ( clip!=null ) {
        clip.stop();
        clip.close();
    } else {
        clip = AudioSystem.getClip();
    }
    boolean addHarmonic = harmonic.isSelected();

    int intSR = ((Integer)sampleRate.getSelectedItem()).intValue();
    int intFPW = framesPerWavelength.getValue();

    float sampleRate = (float)intSR;

    // oddly, the sound does not loop well for less than
    // around 5 or so, wavelengths
    int wavelengths = 20;
    byte[] buf = new byte[2*intFPW*wavelengths];
    AudioFormat af = new AudioFormat(
        sampleRate,
        8,  // sample size in bits
        2,  // channels
        true,  // signed
        false  // bigendian
        );

    int maxVol = 127;
    for(int i=0; i<intFPW*wavelengths; i++){
        double angle = ((float)(i*2)/((float)intFPW))*(Math.PI);
        buf[i*2]=getByteValue(angle);
        if(addHarmonic) {
            buf[(i*2)+1]=getByteValue(2*angle);
        } else {
            buf[(i*2)+1] = buf[i*2];
        }
    }

    try {
        byte[] b = buf;
        AudioInputStream ais = new AudioInputStream(
            new ByteArrayInputStream(b),
            af,
            buf.length/2 );

        clip.open( ais );
    } catch(Exception e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)


zie*_*mer 8

使用Java Sound API,并Math.sin创建实际的wave级别.

http://www.developer.com/java/other/article.php/2226701有一个很好的教程,我前面提到过. http://jsresources.org/examples/是另一个有用的参考.


Thu*_*mbz 8

如果您想要一些简单的代码来帮助您入门,这应该会有所帮助

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

public class SinSynth {
    //
   protected static final int SAMPLE_RATE = 16 * 1024;


   public static byte[] createSinWaveBuffer(double freq, int ms) {
       int samples = (int)((ms * SAMPLE_RATE) / 1000);
       byte[] output = new byte[samples];
           //
       double period = (double)SAMPLE_RATE / freq;
       for (int i = 0; i < output.length; i++) {
           double angle = 2.0 * Math.PI * i / period;
           output[i] = (byte)(Math.sin(angle) * 127f);  }

       return output;
   }



   public static void main(String[] args) throws LineUnavailableException {
       final AudioFormat af = new AudioFormat(SAMPLE_RATE, 8, 1, true, true);
       SourceDataLine line = AudioSystem.getSourceDataLine(af);
       line.open(af, SAMPLE_RATE);
       line.start();

       boolean forwardNotBack = true;

       for(double freq = 400; freq <= 800;)  {
           byte [] toneBuffer = createSinWaveBuffer(freq, 50);
           int count = line.write(toneBuffer, 0, toneBuffer.length);

           if(forwardNotBack)  {
               freq += 20;  
               forwardNotBack = false;  }
           else  {
               freq -= 10;
               forwardNotBack = true;  
       }   }

       line.drain();
       line.close();
    }

}
Run Code Online (Sandbox Code Playgroud)

  • @nont sin 返回一个值 -1.0 到 1.0,将它乘以 127 会得到一个满量程为一个字节 -127 到 +127 的正弦波,(它被转换为一个字节) (2认同)