Playing "Happy Birthday" in Java

Learning how to play sounds in Java may not be necessary, but it is certainly fun and interesting. For this program, an intermediate programmer should be able to understand these basic concepts. After completing these steps, you'll have created a program that plays the song we're all familiar with.

Step 1: Launching Eclipse

Open Eclipse. If you are asked about selecting a directory as your workspace, just take the default and click launch. Also, you may see a Welcome screen upon opening Eclipse for the first time. If so, uncheck the “Always show Welcome at start up” box in the lower right hand corner. Then, close out of the Welcome screen by clicking the “X” in the “Welcome” tab.

Step 2: Creating a Java Project

Create a new Java project by navigating to File > New > Java Project. Name the project “song” and click Finish.

Step 3: Creating a Java Class

Under your song project, create a new Java class by right clicking song in the package explorer and navigating to New > Class. Name the class “HappyBirthday” and click Finish.

Step 4: Navigating to the StdAudio Library

Open a separate internet browser and go to this URL: introcs.cs.princeton.edu/java/stdlib/StdAudio.java.html.

Step 5: Downloading the StdAudio Library

Download the StdAudio library by right clicking the “StdAudio.java” link and navigating to “Save link as...” Save the java file to your “Downloads” directory.

Step 6: Creating the Stdlib Package

Back in Eclipse, create a new package under your src directory by right clicking src in the package explorer and navigating to New > Package. Name the package “stdlib” and click Finish.

Step 7: Adding the StdAudio Library to Stdlib

In File Explorer, open your “Downloads” directory and click and drag the “StdAudio.java” file you downloaded earlier into your stdlib package in the package explorer in Eclipse. Click OK when the dialog box appears.

Step 8: Opening StdAudio in the Editor

Under your src directory in Eclipse’s package explorer, expand the stdlib package and double click the “StdAudio.java” class to open it in the editor.

Step 9: Editing the StdAudio Class

At the beginning of the class, click the plus sign to expand the comments in the class.

Then, hit your Enter key twice to create a couple of new lines. At the top of the class, type “package stdlib;” Hit Ctrl + S to save the changes you have made.

Step 10: Importing the StdAudio Class in the HappyBirthday Class

In your HappyBirthday class, import StdAudio by typing “import stdlib.StdAudio;” under the “package song;” statement. Ignore any unused import warnings.

Step 11: Creating the PlaySound Method

Create a method named “playSound” inside your HappyBirthday class and write it as such (copy and paste the code):

public static void playSound(double frequency, double duration) {

final int sliceCount = (int) (StdAudio.SAMPLE_RATE * duration);

final double[] slices = new double[sliceCount+1];

for (int i = 0; i <= sliceCount; i++) {

slices[i] = Math.sin(2 * Math.PI * i * frequency / StdAudio.SAMPLE_RATE);

}

StdAudio.play(slices);

}

Step 12: Creating the Main Method

After the playSound method, create your main method as such (copy and paste the code):

public static void main(String args[]) {

}

Step 13: Calling PlaySound

Inside your main method, call the playSound method 25 times with the following values (copy and paste the code):

playSound(196.00, 0.43); // G

playSound(0, 0.01);

playSound(196.00, 0.2); // G

playSound(220.00, 0.43); // A

playSound(196.00, 0.43); // G

playSound(261.63, 0.43); // C

playSound(246.94, 0.43); // B

playSound(0, 0.2);

playSound(196.00, 0.43); // G

playSound(0, 0.01);

playSound(196.00, 0.2); // G

playSound(220.00, 0.43); // A

playSound(196.00, 0.43); // G

playSound(293.66, 0.43); // D

playSound(261.63, 0.43); // C

playSound(0, 0.2);

playSound(196.00, 0.43); // G

playSound(0, 0.01);

playSound(196.00, 0.2); // G

playSound(392.00, 0.43); // G

playSound(329.63, 0.43); // E

playSound(261.63, 0.43); // C

playSound(246.94, 0.43); // B

playSound(220.00, 0.43); // A

playSound(0, 0.2);

playSound(349.23, 0.43); // F

playSound(0, 0.01);

playSound(349.23, 0.25); // F

playSound(329.63, 0.43); // E

playSound(261.63, 0.43); // C

playSound(293.66, 0.43); // D

playSound(261.63, 0.43); // C

Step 14: Closing the StdAudio Class

Close the StdAudio class by calling the close method after the calls to playSound as such (copy and paste the code):

StdAudio.close();

Step 15: Closing the System Class

Exit the system class after calling the close method as such (copy and paste the code):

System.exit(0);

Step 16: Running Your Program

Run your program by hitting the F11 key.

Share

    Recommendations

    • Build a Tool Contest

      Build a Tool Contest
    • Warm and Fuzzy Contest

      Warm and Fuzzy Contest
    • Organization Contest

      Organization Contest

    Discussions