|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object ch.aplu.util.FunctionPlayer
public class FunctionPlayer
Class to play audio clips based on mathematical functions.
To define your own audio function, create a class that implements Waveform
and load an instance using load(). Be sure that the method f() has the
right signature and is bounded to -1..1. See the following example:
public class MyWaveform implements Waveform
{
public double f(double t, double freq)
{
double amplitude = 0.8;
double omega = 2 * Math.PI * freq;
double[] overtones =
{
1.0, 1.0 / 2, 1.0 / 3, 1.0 / 4, 1.0 / 5, 1.0 / 6, 1.0 / 7, 1.0 / 8, 1.0 / 9
};
double value = 0;
for (int n = 0; n < overtones.length; n++)
value = value + overtones[n] * Math.sin(n * omega * t);
return amplitude * value;
}
}
To check what you have done, you may display the function using Waveform.WavePlot.
Waveform also contains some predefined waveforms like sine, square, sawtooth,
triangle, etc.
Waveform
Constructor Summary | |
---|---|
FunctionPlayer()
Creates a FunctionPlayer with format Audioformats.dvd_mono. |
|
FunctionPlayer(javax.sound.sampled.AudioFormat audioFormat)
Creates a FunctionPlayer with given audio format. |
Method Summary | |
---|---|
void |
load(Waveform wf,
double duration,
double frequency)
Loads the audio data using the function from given Waveform. |
boolean |
loop()
Starts playing the sound clip continously and returns immediately. |
boolean |
save(java.io.File file)
Save the loaded sound data to the given WAV file using the current audio format. |
boolean |
start()
Starts playing the sound clip and returns immediately. |
boolean |
start(boolean blocking)
Starts playing the sound clip and blocks until finished if blocking is true. |
void |
stop()
Stops playing the sound clip and discards all remaining data. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public FunctionPlayer(javax.sound.sampled.AudioFormat audioFormat)
audioFormat
- the audio format to use.AudioFormats
public FunctionPlayer()
AudioFormats
Method Detail |
---|
public void load(Waveform wf, double duration, double frequency)
wf
- the Waveform that defines the function to playduration
- the time in seconds the clip is playedfrequency
- the frequency in Hertzpublic boolean save(java.io.File file)
file
- the WAV file to be created. If it already exists, it is overwritten.
public boolean start()
public boolean start(boolean blocking)
blocking
- if true, the method blocks until the clip is finished
public boolean loop()
public void stop()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |