How generates and directly outputs a rectangle-wave of a given frequency.

Before writing the java code code will write raw binary data into the OS audio buffer to generate sound. It’s seriously important to understand the limitations and necessary calculations to generating sound in this format. Playback is no doubt a basic instantaneous, calculations need to be performed at almost real-time. As such this method is unusable for more complicated sound-sampling. For such purposes using specialized tools is the better approach.

The method below generates and directly outputs a rectangle-wave of a given frequency in a given volume for a given duration.

NOTE : You can also go almost bare-metal when producing sound with java. This code will write raw binary data into the OS audio buffer to generate sound. It’s extremely important to understand the limitations and necessary calculations to generating sound like this. Since playback is basically instantaneous, calculations need to be performed at almost real-time.

No doubt this method is unusable for more complicated sound-sampling and for such purposes using specialized tools is the better approach.

  public void myRectangleWave(byte volume, int hertz, int msecs) {

        final SourceDataLine dataLine;

// 24 kHz x 8bit, single-channel, signed little endian AudioFormat

        AudioFormat af = new AudioFormat(24_000, 8, 1, true, false);

        try {

            dataLine = AudioSystem.getSourceDataLine(af);

            dataLine.open(af, 10_000); // audio buffer size: 10k samples

        } catch (LineUnavailableException e) {

            throw new RuntimeException(e);

        }



        int waveHalf = 24_000 / hertz; // samples for half a period

        byte[] buffer = new byte[waveHalf * 20];

        int samples = msecs * (24_000 / 1000); // 24k (samples / sec) / 1000 (ms/sec) * time(ms)



        dataLine.start(); // starts playback

        int sign = 1;



        for (int i = 0; i < samples; i += buffer.length) {

            for (int j = 0; j < 20; j++) { // generate 10 waves into buffer

                sign *= -1;

// fill from the jth wave-half to the j+1th wave-half with volume

                Arrays.fill(buffer, waveHalf * j, waveHalf * (j+1), (byte) (volume * sign));

            }

            dataLine.write(buffer, 0, buffer.length); //

        }

        dataLine.drain(); // forces buffer drain to hardware

        dataLine.stop(); // ends playback

    }

In order to to generate different soundwaves ,sinus calculations and possibly larger sample sizes are necessary. This results in significantly very complex code. I hope you now know how to generates and directly outputs a rectangle-wave of a given frequency. Look out for more interesting articles

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: