Digital Sound & Music: Concepts, Applications, & Science, Chapter 2, last updated 6/25/2013
stored in two bytes (i.e., 16 bits), ranges from 32768 to 32767. To put the value into this range,
we multiply by 32767 and, with the floor function, round down.
The three frequencies are created and concatenated into one array of audio values. The
write function has the device ID, the name of the buffer for storing the sound data, and the size
of the buffer as its parameters. This function sends the sound data to the sound card to be
played. The three frequencies together produce a harmonious chord in the key of C. In Chapter
3, we‟ll explore what makes these frequencies harmonious.
The program requires some header files for definitions of constants like
O_WRONLY (restricting access to the sound device to writing) and
SOUND_PCM_WRITE_BITS. After you install the sound libraries, you‟ll need
to locate the appropriate header files and adjust the #include statement
accordingly. You‟ll also need to check the way your compiler handles the math
and sound libraries. You may need to include the option –lm on the compile line
to include the math library, or the –lasound option for the ALSA library.
This program introduces you to the notion that sound must be converted to
a numeric format that is communicable to a computer. The solution to the
programming assignment given as a learning supplement has an explanation of the
variables and constants in this program. A full understanding of the program
requires that you know something about sampling and quantization, the two main steps in
analog-to-digital conversion, a topic that we‟ll examine in depth in Chapter 5.
2.3.13 Modeling Sound in Java
The Java environment allows the programmer to take advantage of Java libraries for sound and
to benefit from object-oriented programming features like encapsulation, inheritance, and
interfaces. In this chapter, we are going to use the package javax.sound.sampled. This package
provides functionality to capture, mix, and play sounds with classes such as SourceDataLine,
AudioFormat, AudioSystem, and LineUnvailableException.
Program 2.5 uses a SourceDataLine object. This is the object to which we write audio
data. Before doing that, we must set up the data line object with a specified audio format object.
(See line 30.) The AudioFormat class specifies a certain arrangement of data in the sound
stream, including the sampling rate, sample size in bits, and number of channels. A
SourceDataLine object is created with the specified format, which in the example is 44,100
samples per second, eight bits per sample, and one channel for mono. With this setting, the line
gets the required system resource and becomes operational. After the SourceDataLine is
opened, data is written to the mixer using a buffer that contains data generated by a sine function.
Notice that we don‟t directly access the Sound Device because we are using a SourceDataLine
object to deliver data bytes to the mixer. The mixer mixes the samples and finally delivers the
samples to an audio output device on a sound card.
1 import javax.sound.sampled.AudioFormat;
2 import javax.sound.sampled.AudioSystem;
3 import javax.sound.sampled.SourceDataLine;
4 import javax.sound.sampled.LineUnavailableException;
6 public class ExampleTone1{
8 public static void main(String[] args){
Sound Waves
Previous Page Next Page