Jingle Bell Music using Java program (javax.sound.sampled package)

This article will explain how to generate basic sound in Java using javax.sound.sampled package. We will use it further to generate ‘Jingle Bell’ music.

Java and sound

Java package javax.sound.sampled provides facility to record & playback of sound by interacting with audio devices of system. For this article, we will use 

Basic sound generation

In simplest way, sound can be visualized in format of sine wave of different amplitudes. We can use this simplest idea to create a basic sound in Java.

  1. We can iterate through angles 0 to 360 & take sin(angle) & pass it to javax.sound.sampled library to generate basic sound.
  2. Just to make sound audible, we will multiply sin(angle) by 1000 so that audio becomes audible. 
  3. Since single iteration from 0 to 360 will be very brief to hear properly, we will do this multiple times using another iteration to have 100 cycles of above iteration.

Sound properties

Above program creates one random sound. But different sounds have different properties which are used to generate specific sounds. Here is quick description of those properties.

  • Frequency: Different sounds are identified using the frequency of its wave. This is measured in cycles per seconds or Hertz i.e. “cycles per seconds”.
  • Angular frequency: If frequency is measured in circular rotation rate format, it is noted in angular frequency i.e. angles passed in given time. Angular frequency = Normal Frequency * 2 * PI  & unit is “radians per second”

In digital world, the sound units are not measured against time like above definitions. In digital world frequencies need to be converted to “per sample” instead of “per seconds”. This is done using something called “Sampling rate” which is “samples per second” & resulting frequency is called “Normalized frequency”

  • Normalized frequency [aka digital frequency]:
    • Normalized frequency =  Frequency / Sample rate
    • Measured in “cycles per sample”
    • We use most common sample rate which is 44,100_Hz in our examples.
  • Normalized Angular frequency
    • Normalized angular frequency = (Angular frequency / Sample rate) * 2 * PI
    • Measured in “radians per sample”

So using these properties & conversion mechanism we modify earlier program to generate a musical note C in octave 5 which has normal frequency 523 Hz.  We will convert this frequency into digital frequency using above formulas & generate sound. 

Musical notes representation.

As you might know, music is comprised of different musical notes. Each musical note has a specific frequency. Refer music note frequencies here @ Table of Musical Notes Frequencies.

Lets create an enum to represent each note & its frequencies in different octaves as shown below. PAUSE is not a musical note, but added it to give a gap between music notes in simpler way.

Now we will modify earlier program to take Musical Note as input, do conversions & play musical note sound. We will also add pause facility. This is how it will look now.

 

Jingle Bell

Now that we have program ready to play musical notes, let’s compose track for “Jingle Bell” using musical notes. Musical notes for jingle bell can be found in several musical websites. Here is one with due credit & appreciation.

Now we just put all these notes with proper pauses in method calls to generateMusicalNotes in a java main method then run program & play the track !! Now you can enjoy Jingle bell track in basic sounds generated through Java.

You can adjust OCTAVE, VOLUME, MILLSECONDS to try different varieties. Or even better, you can add your own musical notes & enjoy the music !

 

You can find complete source code here JingleBell.java.   MERRY CHRISTMAS !!!!

Leave a Reply

Your email address will not be published.