ch.aplu.util
Class FunctionPlayer

java.lang.Object
  extended by ch.aplu.util.FunctionPlayer

public class FunctionPlayer
extends java.lang.Object

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.01.0 / 21.0 / 31.0 / 41.0 / 51.0 / 61.0 / 71.0 / 81.0 / 9
    };
    double value = 0;
    for (int n = 0; n < overtones.length; n++)
      value = value + overtones[n] * Math.sin(* 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.

See Also:
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

FunctionPlayer

public FunctionPlayer(javax.sound.sampled.AudioFormat audioFormat)
Creates a FunctionPlayer with given audio format. There are simple predefined formats in class ch.aplu.util.AudioFormats.

Parameters:
audioFormat - the audio format to use.
See Also:
AudioFormats

FunctionPlayer

public FunctionPlayer()
Creates a FunctionPlayer with format Audioformats.dvd_mono.

See Also:
AudioFormats
Method Detail

load

public void load(Waveform wf,
                 double duration,
                 double frequency)
Loads the audio data using the function from given Waveform. The clip will be played during the given duration with given frequency and amplitude. To prevent audio distortion the user defined audio function should be bounded to -1..1.

Parameters:
wf - the Waveform that defines the function to play
duration - the time in seconds the clip is played
frequency - the frequency in Hertz

save

public boolean save(java.io.File file)
Save the loaded sound data to the given WAV file using the current audio format.

Parameters:
file - the WAV file to be created. If it already exists, it is overwritten.
Returns:
true, if successful; otherwise false

start

public boolean start()
Starts playing the sound clip and returns immediately.

Returns:
true, if successfull; otherwise false (IO errors, sound card errors)

start

public boolean start(boolean blocking)
Starts playing the sound clip and blocks until finished if blocking is true.

Parameters:
blocking - if true, the method blocks until the clip is finished
Returns:
true, if successfull; otherwise false (IO errors, sound card errors)

loop

public boolean loop()
Starts playing the sound clip continously and returns immediately.

Returns:
true, if successfull; otherwise false

stop

public void stop()
Stops playing the sound clip and discards all remaining data.