001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package stdlib;
/* ***********************************************************************
 *  Compilation:  javac StdAudio.java
 *  Execution:    java StdAudio
 *
 *  Simple library for reading, writing, and manipulating .wav files.

 *
 *  Limitations
 *  -----------
 *    - Does not seem to work properly when reading .wav files from a .jar file.
 *    - Assumes the audio is monaural, with sampling rate of 44,100.
 *
 *************************************************************************/

import java.applet.*;
import java.io.*;
import java.net.*;
import javax.sound.sampled.*;

/**
 *  <i>Standard audio</i>. This class provides a basic capability for
 *  creating, reading, and saving audio.
 *  <p>
 *  The audio format uses a sampling rate of 44,100 (CD quality audio), 16-bit, monaural.
 *
 *  <p>
 *  For additional documentation, see <a href="http://introcs.cs.princeton.edu/15inout">Section 1.5</a> of
 *  <i>Introduction to Programming in Java: An Interdisciplinary Approach</i> by Robert Sedgewick and Kevin Wayne.
 */
public final class StdAudio {

  /**
   *  The sample rate - 44,100 Hz for CD quality audio.
   */
  public static final int SAMPLE_RATE = 44100;

  private static final int BYTES_PER_SAMPLE = 2;                // 16-bit audio
  private static final int BITS_PER_SAMPLE = 16;                // 16-bit audio
  private static final double MAX_16_BIT = Short.MAX_VALUE;     // 32,767
  private static final int SAMPLE_BUFFER_SIZE = 4096;


  private static SourceDataLine line;   // to play the sound
  private static byte[] buffer;         // our internal buffer
  private static int bufferSize = 0;    // number of samples currently in internal buffer

  // do not instantiate
  private StdAudio() { }


  // static initializer
  static { init(); }

  // open up an audio stream
  private static void init() {
    try {
      // 44,100 samples per second, 16-bit audio, mono, signed PCM, little Endian
      AudioFormat format = new AudioFormat(SAMPLE_RATE, BITS_PER_SAMPLE, 1, true, false);
      DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

      line = (SourceDataLine) AudioSystem.getLine(info);
      line.open(format, SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE);

      // the internal buffer is a fraction of the actual buffer size, this choice is arbitrary
      // it gets divided because we can't expect the buffered data to line up exactly with when
      // the sound card decides to push out its samples.
      buffer = new byte[SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE/3];
    } catch (Exception e) {
      System.out.println(e.getMessage());
      System.exit(1);
    }

    // no sound gets made before this call
    line.start();
  }


  /**
   * Close standard audio.
   */
  public static void close() {
    line.drain();
    line.stop();
  }

  /**
   * Write one sample (between -1.0 and +1.0) to standard audio. If the sample
   * is outside the range, it will be clipped.
   */
  public static void play(double in) {

    // clip if outside [-1, +1]
    if (in < -1.0) in = -1.0;
    if (in > +1.0) in = +1.0;

    // convert to bytes
    short s = (short) (MAX_16_BIT * in);
    buffer[bufferSize++] = (byte) s;
    buffer[bufferSize++] = (byte) (s >> 8);   // little Endian

    // send to sound card if buffer is full
    if (bufferSize >= buffer.length) {
      line.write(buffer, 0, buffer.length);
      bufferSize = 0;
    }
  }

  /**
   * Write an array of samples (between -1.0 and +1.0) to standard audio. If a sample
   * is outside the range, it will be clipped.
   */
  public static void play(double[] input) {
    for (int i = 0; i < input.length; i++) {
      play(input[i]);
    }
  }

  /**
   * Read audio samples from a file (in .wav or .au format) and return them as a double array
   * with values between -1.0 and +1.0.
   */
  public static double[] read(String filename) {
    byte[] data = readByte(filename);
    int N = data.length;
    double[] d = new double[N/2];
    for (int i = 0; i < N/2; i++) {
      d[i] = ((short) (((data[2*i+1] & 0xFF) << 8) + (data[2*i] & 0xFF))) / (MAX_16_BIT);
    }
    return d;
  }




  /**
   * Play a sound file (in .wav, .mid, or .au format) in a background thread.
   */
  public static void play(String filename) {
    URL url = null;
    try {
      File file = new File(filename);
      if (file.canRead()) url = file.toURI().toURL();
    }
    catch (MalformedURLException e) { e.printStackTrace(); }
    // URL url = StdAudio.class.getResource(filename);
    if (url == null) throw new RuntimeException("audio " + filename + " not found");
    AudioClip clip = Applet.newAudioClip(url);
    clip.play();
  }

  /**
   * Loop a sound file (in .wav, .mid, or .au format) in a background thread.
   */
  public static void loop(String filename) {
    URL url = null;
    try {
      File file = new File(filename);
      if (file.canRead()) url = file.toURI().toURL();
    }
    catch (MalformedURLException e) { e.printStackTrace(); }
    // URL url = StdAudio.class.getResource(filename);
    if (url == null) throw new RuntimeException("audio " + filename + " not found");
    AudioClip clip = Applet.newAudioClip(url);
    clip.loop();
  }


  // return data as a byte array
  private static byte[] readByte(String filename) {
    byte[] data = null;
    AudioInputStream ais = null;
    try {

      // try to read from file
      File file = new File(filename);
      if (file.exists()) {
        ais = AudioSystem.getAudioInputStream(file);
        data = new byte[ais.available()];
        ais.read(data);
      }

      // try to read from URL
      else {
        URL url = StdAudio.class.getResource(filename);
        ais = AudioSystem.getAudioInputStream(url);
        data = new byte[ais.available()];
        ais.read(data);
      }
    }
    catch (Exception e) {
      System.out.println(e.getMessage());
      throw new RuntimeException("Could not read " + filename);
    }

    return data;
  }



  /**
   * Save the double array as a sound file (using .wav or .au format).
   */
  public static void save(String filename, double[] input) {

    // assumes 44,100 samples per second
    // use 16-bit audio, mono, signed PCM, little Endian
    AudioFormat format = new AudioFormat(SAMPLE_RATE, 16, 1, true, false);
    byte[] data = new byte[2 * input.length];
    for (int i = 0; i < input.length; i++) {
      int temp = (short) (input[i] * MAX_16_BIT);
      data[2*i + 0] = (byte) temp;
      data[2*i + 1] = (byte) (temp >> 8);
    }

    // now save the file
    try {
      ByteArrayInputStream bais = new ByteArrayInputStream(data);
      AudioInputStream ais = new AudioInputStream(bais, format, input.length);
      if (filename.endsWith(".wav") || filename.endsWith(".WAV")) {
        AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File(filename));
      }
      else if (filename.endsWith(".au") || filename.endsWith(".AU")) {
        AudioSystem.write(ais, AudioFileFormat.Type.AU, new File(filename));
      }
      else {
        throw new RuntimeException("File format not supported: " + filename);
      }
    }
    catch (Exception e) {
      System.out.println(e);
      System.exit(1);
    }
  }




  /* *********************************************************************
   * sample test client
   ***********************************************************************/

  // create a note (sine wave) of the given frequency (Hz), for the given
  // duration (seconds) scaled to the given volume (amplitude)
  private static double[] note(double hz, double duration, double amplitude) {
    int N = (int) (StdAudio.SAMPLE_RATE * duration);
    double[] a = new double[N+1];
    for (int i = 0; i <= N; i++)
      a[i] = amplitude * Math.sin(2 * Math.PI * i * hz / StdAudio.SAMPLE_RATE);
    return a;
  }

  /**
   * Test client - play an A major scale to standard audio.
   */
  public static void main(String[] args) {

    // 440 Hz for 1 sec
    double freq = 440.0;
    for (int i = 0; i <= StdAudio.SAMPLE_RATE; i++) {
      StdAudio.play(0.5 * Math.sin(2*Math.PI * freq * i / StdAudio.SAMPLE_RATE));
    }

    // scale increments
    int[] steps = { 0, 2, 4, 5, 7, 9, 11, 12 };
    for (int i = 0; i < steps.length; i++) {
      double hz = 440.0 * Math.pow(2, steps[i] / 12.0);
      StdAudio.play(note(hz, 1.0, 0.5));
    }


    // need to call this in non-interactive stuff so the program doesn't terminate
    // until all the sound leaves the speaker.
    StdAudio.close();

    // need to terminate a Java program with sound
    System.exit(0);
  }
}