Package stdlib
Class StdAudio
java.lang.Object
stdlib.StdAudio
Standard audio. This class provides a basic capability for
creating, reading, and saving audio.
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.
- Author:
- Robert Sedgewick, Kevin Wayne
-
Field Summary
Modifier and TypeFieldDescriptionprivate static final boolean
private static final int
private static byte[]
private static int
private static final int
private static SourceDataLine
private static final boolean
private static final int
private static final int
private static final int
static final int
The sample rate: 44,100 Hz for CD quality audio.private static final boolean
private static final int
private static final boolean
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
close()
Closes standard audio.private static AudioInputStream
getAudioInputStreamFromFile
(String filename) private static void
init()
static void
Deprecated.static void
loopInBackground
(String filename) Loops an audio file (in .wav, .mid, or .au format) in a background thread.static void
Test client - play an A major scale to standard audio.private static double[]
note
(double hz, double duration, double amplitude) Unit testsStdAudio
.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
Deprecated.replaced byplayInBackground(String filename)
static void
playInBackground
(String filename) Plays an audio file (in .wav, .mid, or .au format) in a background thread.static double[]
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
Saves the double array as an audio file (using .wav or .au format).private static void
stream
(AudioInputStream ais)
-
Field Details
-
SAMPLE_RATE
The sample rate: 44,100 Hz for CD quality audio.- See Also:
-
BYTES_PER_SAMPLE
- See Also:
-
BITS_PER_SAMPLE
- See Also:
-
MAX_16_BIT
- See Also:
-
SAMPLE_BUFFER_SIZE
- See Also:
-
MONO
- See Also:
-
STEREO
- See Also:
-
LITTLE_ENDIAN
- See Also:
-
BIG_ENDIAN
- See Also:
-
SIGNED
- See Also:
-
UNSIGNED
- See Also:
-
line
-
buffer
-
bufferSize
-
-
Constructor Details
-
StdAudio
private StdAudio()
-
-
Method Details
-
init
-
getAudioInputStreamFromFile
-
close
Closes standard audio. -
play
Writes one sample (between -1.0 and +1.0) to standard audio. If the sample is outside the range, it will be clipped.- Parameters:
sample
- the sample to play- Throws:
IllegalArgumentException
- if the sample isDouble.NaN
-
play
Writes the array of samples (between -1.0 and +1.0) to standard audio. If a sample is outside the range, it will be clipped.- Parameters:
samples
- the array of samples to play- Throws:
IllegalArgumentException
- if any sample isDouble.NaN
IllegalArgumentException
- ifsamples
isnull
-
read
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. The audio file must be 16-bit with a sampling rate of 44,100. It can be mono or stereo.- Parameters:
filename
- the name of the audio file- Returns:
- the array of samples
-
save
Saves the double array as an audio file (using .wav or .au format).- Parameters:
filename
- the name of the audio filesamples
- the array of samples- Throws:
IllegalArgumentException
- if unable to savefilename
IllegalArgumentException
- ifsamples
isnull
IllegalArgumentException
- iffilename
isnull
IllegalArgumentException
- iffilename
extension is not.wav
or.au
-
play
Deprecated.replaced byplayInBackground(String filename)
Plays an audio file (in .wav, .mid, or .au format) in a background thread.- Parameters:
filename
- the name of the audio file- Throws:
IllegalArgumentException
- if unable to playfilename
IllegalArgumentException
- iffilename
isnull
-
playInBackground
Plays an audio file (in .wav, .mid, or .au format) in a background thread.- Parameters:
filename
- the name of the audio file- Throws:
IllegalArgumentException
- if unable to playfilename
IllegalArgumentException
- iffilename
isnull
-
stream
-
loop
Deprecated.replaced byloopInBackground(String filename)
Loops an audio file (in .wav, .mid, or .au format) in a background thread.- Parameters:
filename
- the name of the audio file- Throws:
IllegalArgumentException
- iffilename
isnull
-
loopInBackground
Loops an audio file (in .wav, .mid, or .au format) in a background thread.- Parameters:
filename
- the name of the audio file- Throws:
IllegalArgumentException
- iffilename
isnull
-
note
Unit testsStdAudio
. -
main
Test client - play an A major scale to standard audio.- Parameters:
args
- the command-line arguments
-
loopInBackground(String filename)