Generate Random Music In Java

In this tutorial, we will learn to generate random music using java. This means that we will write a code to play sound using java. We will generate a tune using a sine wave. The nature of the tune will depend on the parameters passed by us.

For generating tune using java we need to add the jar file ie. JSyn. JSyn allows us to generate a random computer interactive tone.

Link to download JSyn::http://www.softsynth.com/jsyn/developers/download.php

While generating tone we can adjust the pitch, frequency, and voice from the speakers of our computer(ie. left speaker and right speaker).

In this code, we will use Threads. Threads can be referred to as light-weighted tasks. We use multithreading and multitasking both for multiprocessing. But we can say that multithreading is better than multitasking. In Multithreading, threads share a common memory area whereas in multitasking different memory area is allocated to different tasks.

To Generate random music using java

We can generate java projects using any IDE ie. Eclipse, Netbeans, Notepad++, Notepad, etc but generating code using Netbeans and Eclipse helps us a lot as it becomes easy for us to load the required jar files using Netbeans and Eclipse. And these IDE’s generates most of the code for us.

This is not the same in the case of Notepad, Notepad++. While using Notepad, Notepad++ we need to add the jar files in the classpath of our computer.

Code::

package music;
import com.jsyn.JSyn;
import com.jsyn.Synthesizer;
import com.jsyn.unitgen.LineOut;
import com.jsyn.unitgen.SineOscillator;
public class jsyn {
  
    public static void playSound() {
        Synthesizer synth = JSyn.createSynthesizer();
        try {
            synth.start();
            SineOscillator sineOsc1 = new SineOscillator(1000, 10);
            SineOscillator sineOsc2 = new SineOscillator(1000, 10);
            LineOut lineOut= new LineOut();

            synth.add(lineOut);
            synth.add(sineOsc1);
            synth.add(sineOsc2);

            sineOsc1.output.connect(0, lineOut.input, 0); 
            sineOsc2.output.connect(0, lineOut.input, 1);

            lineOut.start();

            sineOsc1.start(); 
            sleep(500);
            sineOsc2.start(); 
            sleep(500);
            sineOsc1.stop();
            sleep(500);
            sineOsc2.stop(); 
            lineOut.stop();
        } finally {
            synth.stop();
        }
    }

    private static void sleep(int ms) {
        try {
            Thread.sleep(ms);
        } catch (Exception e) {
        	System.out.println(e);
        }
    }
    public static void main(String args[]) {
    System.out.println("sound started");	
  	  playSound();
  	System.out.println("sound ended");
  	  
  	 
  }
}

In the above code music is the name of the package. We will import the required packages.

jsyn is the name of our class. Synthesizer is an interface that we use to load or unload instruments from soundbank.

We instantiated Synthesizer with the instance synth. We can say that synth is the variable of Synthesizer.

SineOscillator is used to generate a frequency controlled by Sinewave. Parameters passed in SineOscillator are used to increase the pitch and frequency of the wave( or we can say the sound produced).

The LineOut is used to add a signal to the stereo and then send it to audio lines.

Then we created a sleep method. Sleep method is used while working with threads to delay the working of threads with some milliseconds. In the sleep method, we can pass milliseconds as the parameter of method.

We use finally block in case of Exceptions as compiler always executes finally block.

We use try-catch block to handle theĀ  Exceptions. Exception disturbs the normal flow of the code.

In the main method of our class first, we printed sound started. Then after printing the message, we called the playSound() method which starts playing the sound. After playing sound we printed sound ended.

Output::

Below is the result given:

JSyn

Leave a Reply