Player.java


Below is the syntax highlighted version of Player.java from §1.5 Input and Output.


import java.io.File;
import java.io.ByteArrayInputStream;
import java.io.BufferedInputStream;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;


public class Player {
    private AudioFormat format;
    private int frames;         // total number of frames
    private int frameSize;      // bytes per frame (1 for 8-bit, 2 for 16-bit)
    private int bytes;          // total number of bytes
    private float frameRate;    // frames per second (44100 for CD audio)
    private double seconds;     // duration in seconds
    private byte[] data;

    // create new object from wav file
    public Player(String filename) {
        File file = new File(filename);
        try {
            AudioInputStream ais = AudioSystem.getAudioInputStream(file);
            AudioFileFormat  aff = AudioSystem.getAudioFileFormat(file);

            // get info
            format    = ais.getFormat();
            frames    = aff.getFrameLength();
            frameSize = format.getFrameSize();
            frameRate = format.getFrameRate();
            bytes     = frames * frameSize;
            seconds   = frames / frameRate;

            // read data
            data = new byte[bytes];
            ais.read(data);
        }
        catch (Exception e)  {
            e.printStackTrace();
            return;
        }
    }

    // create new object from wav file
    public Player(byte[] data) {
        this.data = data.clone();
        // 44100Hz, 8 bit, 1 channel, PCM signed, little endian
        format = new AudioFormat(44100, 8, 1, true, false);
        frames    = data.length;
        frameSize = 1;   // 8-bit
        frameRate = 44100;
        bytes     = data.length;
        seconds   = frames / frameRate;
    }


    // play the sound on the sound card
    public void play() {
        SourceDataLine line = null;
        DataLine.Info info  = new DataLine.Info(SourceDataLine.class, format);
        try {
            line = (SourceDataLine) AudioSystem.getLine(info);
            line.open(format);
        }
        catch (Exception e) {
            e.printStackTrace();
            return;
        }
        line.start();
        line.write(data, 0, bytes);
        line.drain();
        line.close();
    }

    // save to a wav file
    public void save(String filename) {
        File file = new File(filename);
        AudioFileFormat.Type type = AudioFileFormat.Type.WAVE;
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(data);
            BufferedInputStream  bis  = new BufferedInputStream(bais);
            AudioInputStream ais = new AudioInputStream(bis, format, (long) frames);
            AudioSystem.write(ais, type, file);
        }
        catch (Exception e) {
            e.printStackTrace();
            return;
        }
    }


    // return duration
    public double duration() {
        return seconds;
    }

    // return string representation
    public String toString() {
        return "" + format;
    }


    public static void main(String[] args) {
        Player player = new Player(args[0]);
        System.out.println(player);
        player.play();
        player.play();

        byte[] data = new byte[44100];
        double hz = 659.25;   // E
        for (int i = 0; i < 44100; i++)
            data[i] = (byte) (100 * Math.sin(2 * Math.PI * i * hz / 44100));
        player = new Player(data);
        player.play();
        player.play();
        player.save("temp.wav");
        System.exit(0);
    }

}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.