## Standard audio.

StdAudio is a library that you can use to play and manipulate sound files. It allows you to play .wav files, to write programs to create and manipulate arrays of double values, and to read and write them as .wav files: We first introduce some some basic concepts behind one of the oldest and most important areas of computer science and scientific computing, which is known as digital signal processing.
• Concert A. Sound is the vibration of matter that we perceive, in particular, by the vibration of our eardrums, so oscillation is the key to understanding sound. Perhaps the simplest place to start is to consider the musical note A above middle C, which is known as concert A. This note is nothing more than a sine wave, scaled to oscillate at a frequency of 440 times per second. The function sin(t) repeats itself once every 2π units on the x axis, so if we measure t in seconds and plot the function sin(2πt × 440) we get a curve that oscillates 440 times per second. We measure frequency in hertz (cycles per second). When you double or halve the frequency, you move up or down an octave on the scale. For example 880 hertz is one octave above concert A and 110 hertz is two octaves below concert A. For reference, the frequency range of human hearing is about 20 to 20,000 hertz.The amplitude (y value) corresponds to the volume. We assume it is scaled to be between -1 and +1. The following code fragment plays concert A for 1 second.
 ```int SAMPLE_RATE = 44100; double[] a = new double[SAMPLE_RATE + 1]; for (int i = 0; i <= SAMPLE_RATE; i++) { a[i] = Math.sin(2 * Math.PI * i * 440 / SAMPLE_RATE); } StdAudio.play(a); ```
• Other notes. A simple mathematical formula characterizes the other notes on the chromatic scale. They are divided equally on a logarithmic (base 2) scale: there are twelve notes on the chromatic scale, and we get the i th note above a given note by multiplying its frequency by the (i/12)th power of 2. In other words, the frequency of each note in the chromatic scale is precisely the frequency of the previous note in the scale multiplied by the twelfth root of two. This information suffices to create music! For example, to play the tune Frere Jacques, we just need to play each of the notes A B C A by producing sine waves of the appropriate frequency for about half a second each and then repeat the pattern. • Chords and harmonics. Notes like concert A have a pure sound that is a not very musical, because the sounds that you are accustomed to hearing have many other components. The sound from the guitar string echos off the wooden part of the instrument, the walls of the room that you are in, and so forth. You may think of such effects as modifying the basic sine wave. For example, most musical instruments produce harmonics (the same note in different octaves and not as loud), or you might play chords (multiple notes at the same time). To combine multiple sounds, we can simply add their waves together (and rescale to make sure they stay between -1 and +1). When we add sine waves of different frequency, we can get arbitrarily complicated waves. Indeed, one of the triumphs of 19th century mathematics was the development of the ideas that any periodic function can be expressed as a sum of sine and cosine waves, known as a Fourier series. This mathematical idea corresponds to the idea that we can create a large range of sounds with musical instruments or our vocal chords and that all sound consists of a composition of various oscillating curves. Any sound corresponds to a curve and any curve corresponds to a sound.

• Sampling. For digital sound, we represent a curve by sampling it at regular intervals, in precisely the same manner as when we plot function graphs. We sample sufficiently often that we have an accurate representation of the curve - a widely used sampling rate for digital sound is 44,100 samples per second. For concert A, that rate corresponds to plotting each cycle of the sine wave by sampling it at about 100 points. Since we sample at regular intervals, we only need to compute the y coordinates of the sample points. It is that simple: we represent sound as an array of numbers (double values that are between -1 and +1).  • Play that tune. PlayThatTune.java is an example that shows how easily we can create music with StdAudio. It takes notes from standard input, indexed on the chromatic scale from concert A, and plays them on standard audio.