Pages

Java Sound : generate play sine wave - source code

Working source code example on how to generate and play sine wave in Java :
View my previous post  for playing any PCM amplitude array.

Generate Sine wave of a particular frequency :
    private static byte[] generateSineWavefreq(int frequencyOfSignal, int seconds) {
        // total samples = (duration in second) * (samples per second)
        byte[] sin = new byte[seconds * sampleRate];
        double samplingInterval = (double) (sampleRate / frequencyOfSignal);
        System.out.println("Sampling Frequency  : "+sampleRate);
        System.out.println("Frequency of Signal : "+frequencyOfSignal);
        System.out.println("Sampling Interval   : "+samplingInterval);
        for (int i = 0; i < sin.length; i++) {
            double angle = (2.0 * Math.PI * i) / samplingInterval;
            sin[i] = (byte) (Math.sin(angle) * 127);
            System.out.println("" + sin[i]);
        }
        return sin;
    }

Generated Sequence Plot
Generated Sine wave plot

CODE For Playing The Generated Sequence:

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

public class PlayAnArray {
    private static int sampleRate = 8000;
    public static void main(String[] args) {
        final AudioFormat af = new AudioFormat(sampleRate, 16, 1, true, true);
        try {
            SourceDataLine line = AudioSystem.getSourceDataLine(af);
            line.open(af);
            line.start();
            //play Frequency = 200 Hz for 1 seconds
            play(line, generateSineWavefreq(200,1));
            line.drain();
            line.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static byte[] generateSineWavefreq(int frequencyOfSignal, int seconds) {
        // total samples = (duration in second) * (samples per second)
        byte[] sin = new byte[seconds * sampleRate];
        double samplingInterval = (double) (sampleRate / frequencyOfSignal);
        System.out.println("Sampling Frequency  : "+sampleRate);
        System.out.println("Frequency of Signal : "+frequencyOfSignal);
        System.out.println("Sampling Interval   : "+samplingInterval);
        for (int i = 0; i < sin.length; i++) {
            double angle = (2.0 * Math.PI * i) / samplingInterval;
            sin[i] = (byte) (Math.sin(angle) * 127);
            //System.out.println("" + sin[i]);
        }
        return sin;
    }
    private static void play(SourceDataLine line, byte[] array) {
        int length = sampleRate * array.length / 1000;
        line.write(array, 0, array.length);
    }
}


4 comments:

  1. nice article. It works great.

    ReplyDelete
  2. Thanks for the tutorial it helped me a lot on my project!

    ReplyDelete
  3. the sine wave plot is happening fr me....after running the code i cud hear the tone but i cannot c the plot of sine wave.....any suggestions??????

    ReplyDelete
    Replies
    1. yes the program doesnt plot the wave it only produces the sound. the author must have recorded the sound and created the waveform image manually using some audio program. if you wanted to do that using java, it would be a much bigger program.

      Delete

Your Comment and Question will help to make this blog better...