类 StdAudio
- java.lang.Object
-
- edu.princeton.cs.algs4.StdAudio
-
public final class StdAudio extends java.lang.ObjectStandard 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.
-
-
字段概要
字段 修饰符和类型 字段 说明 static intSAMPLE_RATEThe sample rate: 44,100 Hz for CD quality audio.
-
方法概要
修饰符和类型 方法 说明 static voidclose()Closes standard audio.static voidloop(java.lang.String filename)Loops an audio file (in .wav, .mid, or .au format) in a background thread.static voidmain(java.lang.String[] args)Test client - play an A major scale to standard audio.static voidplay(double sample)Writes one sample (between -1.0 and +1.0) to standard audio.static voidplay(double[] samples)Writes the array of samples (between -1.0 and +1.0) to standard audio.static voidplay(java.lang.String filename)Plays an audio file (in .wav, .mid, or .au format) in a background thread.static double[]read(java.lang.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 voidsave(java.lang.String filename, double[] samples)Saves the double array as an audio file (using .wav or .au format).
-
-
-
字段详细资料
-
SAMPLE_RATE
public static final int SAMPLE_RATE
The sample rate: 44,100 Hz for CD quality audio.- 另请参阅:
- 常量字段值
-
-
方法详细资料
-
close
public static void close()
Closes standard audio.
-
play
public static void play(double sample)
Writes one sample (between -1.0 and +1.0) to standard audio. If the sample is outside the range, it will be clipped.- 参数:
sample- the sample to play- 抛出:
java.lang.IllegalArgumentException- if the sample isDouble.NaN
-
play
public static void play(double[] samples)
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.- 参数:
samples- the array of samples to play- 抛出:
java.lang.IllegalArgumentException- if any sample isDouble.NaNjava.lang.IllegalArgumentException- ifsamplesisnull
-
read
public static double[] read(java.lang.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. The audio file must be 16-bit with a sampling rate of 44,100. It can be mono or stereo.- 参数:
filename- the name of the audio file- 返回:
- the array of samples
-
save
public static void save(java.lang.String filename, double[] samples)Saves the double array as an audio file (using .wav or .au format).- 参数:
filename- the name of the audio filesamples- the array of samples- 抛出:
java.lang.IllegalArgumentException- if unable to savefilenamejava.lang.IllegalArgumentException- ifsamplesisnulljava.lang.IllegalArgumentException- iffilenameisnulljava.lang.IllegalArgumentException- iffilenameextension is not.wavor.au
-
play
public static void play(java.lang.String filename)
Plays an audio file (in .wav, .mid, or .au format) in a background thread.- 参数:
filename- the name of the audio file- 抛出:
java.lang.IllegalArgumentException- if unable to playfilenamejava.lang.IllegalArgumentException- iffilenameisnull
-
loop
public static void loop(java.lang.String filename)
Loops an audio file (in .wav, .mid, or .au format) in a background thread.- 参数:
filename- the name of the audio file- 抛出:
java.lang.IllegalArgumentException- iffilenameisnull
-
main
public static void main(java.lang.String[] args)
Test client - play an A major scale to standard audio.- 参数:
args- the command-line arguments
-
-