public final class StdAudio extends Object
The audio format uses a sampling rate of 44,100 Hz, 16-bit, monaural.
For additional documentation, see Section 1.5 of Computer Science: An Interdisciplinary Approach by Robert Sedgewick and Kevin Wayne.
Modifier and Type | Field and Description |
---|---|
static int |
SAMPLE_RATE
The sample rate: 44,100 Hz for CD quality audio.
|
Modifier and Type | Method and Description |
---|---|
static void |
close()
Closes standard audio.
|
static void |
loop(String filename)
Loops an audio file (in .wav, .mid, or .au format) in a background thread.
|
static void |
main(String[] args)
Test client - play an A major scale to standard audio.
|
static void |
play(double sample)
Writes one sample (between -1.0 and +1.0) to standard audio.
|
static void |
play(double[] samples)
Writes the array of samples (between -1.0 and +1.0) to standard audio.
|
static void |
play(String filename)
Plays an audio file (in .wav, .mid, or .au format) in a background thread.
|
static double[] |
read(String filename)
Reads audio samples from a file (in .wav or .au format) and returns
them as a double array with values between -1.0 and +1.0.
|
static void |
save(String filename,
double[] samples)
Saves the double array as an audio file (using .wav or .au format).
|
public static final int SAMPLE_RATE
public static void close()
public static void play(double sample)
sample
- the sample to playIllegalArgumentException
- if the sample is Double.NaN
public static void play(double[] samples)
samples
- the array of samples to playIllegalArgumentException
- if any sample is Double.NaN
IllegalArgumentException
- if samples
is null
public static double[] read(String filename)
filename
- the name of the audio filepublic static void save(String filename, double[] samples)
filename
- the name of the audio filesamples
- the array of samplesIllegalArgumentException
- if unable to save filename
IllegalArgumentException
- if samples
is null
IllegalArgumentException
- if filename
is null
IllegalArgumentException
- if filename
extension is not .wav
or .au
public static void play(String filename)
filename
- the name of the audio fileIllegalArgumentException
- if unable to play filename
IllegalArgumentException
- if filename
is null
public static void loop(String filename)
filename
- the name of the audio fileIllegalArgumentException
- if filename
is null
public static void main(String[] args)
args
- the command-line arguments