Package trclib.driverio
Class TrcTone
java.lang.Object
trclib.driverio.TrcTone
- Direct Known Subclasses:
FtcAndroidTone
This class implements a platform independent tone player that can play a tone with specified waveform, frequency,
duration and volume. This class should be extended by a platform dependent class that will implement the tone
playing abstract methods.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThis enum type specifies the sound waveform to be used in playing a note. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final TrcTone.Waveformprivate final Stringfinal TrcDbgTrace -
Constructor Summary
ConstructorsConstructorDescriptionTrcTone(String instanceName, TrcTone.Waveform defWaveform) Constructor: Create and initialize an instance of the object. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidapplySoundEnvelope(short[] buffer, int sampleRate, double attack, double decay, double sustain, double release) This method applies the sound envelope to the sound data.protected voidgenSineWave(short[] buffer, int sampleRate, double frequency, double volume) This method generates the wave data for a sine wave with the specified frequency and volume.protected voidgenSquareWave(short[] buffer, int sampleRate, double frequency, double volume) This method generates the wave data for a square wave with the specified frequency and volume.protected voidgenTriangleWave(short[] buffer, int sampleRate, double frequency, double volume) This method generates the wave data for a triangle wave with the specified frequency and volume.abstract booleanThis method determines if the tone is still playing.voidplayTone(double frequency, double duration) This method plays a tone with the specified frequency and duration using default waveform and full volume.voidplayTone(double frequency, double duration, double volume) This method plays a tone with the specified frequency, duration and volume using default waveform.abstract voidplayTone(TrcTone.Waveform waveform, double frequency, double duration, double volume) This method plays a tone with the specified waveform, frequency, duration and volume.private voidscaleData(short[] buffer, int startIndex, int length, double slope, double zeroIntercept) This method scales the sound data using the envelope segment specified by the equation y = mx + b where m is the slope and b is the zeroIntercept.abstract voidstop()This method stops the playing tone.
-
Field Details
-
tracer
-
instanceName
-
defWaveform
-
-
Constructor Details
-
TrcTone
Constructor: Create and initialize an instance of the object.- Parameters:
instanceName- specifies the instance name.defWaveform- specifies the default waveform type.
-
-
Method Details
-
playTone
public abstract void playTone(TrcTone.Waveform waveform, double frequency, double duration, double volume) This method plays a tone with the specified waveform, frequency, duration and volume.- Parameters:
waveform- specifies the waveform type.frequency- specifies the tone frequency in Hz.duration- specifies the duration in seconds.volume- specifies the volume in the range 0.0 to 1.0.
-
stop
public abstract void stop()This method stops the playing tone. -
isPlaying
public abstract boolean isPlaying()This method determines if the tone is still playing.- Returns:
- true if tone is still playing, false otherwise.
-
playTone
public void playTone(double frequency, double duration, double volume) This method plays a tone with the specified frequency, duration and volume using default waveform.- Parameters:
frequency- specifies the tone frequency in Hz.duration- specifies the duration in seconds.volume- specifies the volume in the range 0.0 to 1.0.
-
playTone
public void playTone(double frequency, double duration) This method plays a tone with the specified frequency and duration using default waveform and full volume.- Parameters:
frequency- specifies the tone frequency in Hz.duration- specifies the duration in seconds.
-
genSineWave
protected void genSineWave(short[] buffer, int sampleRate, double frequency, double volume) This method generates the wave data for a sine wave with the specified frequency and volume. It is intended to be called by the platform dependent wave player that extends this class.- Parameters:
buffer- specifies the buffer to hold the generated waveform data.sampleRate- specifies the sampling rate of tbe waveform.frequency- specifies the tone frequency in Hz.volume- specifies the volume in the range 0.0 to 1.0.
-
genSquareWave
protected void genSquareWave(short[] buffer, int sampleRate, double frequency, double volume) This method generates the wave data for a square wave with the specified frequency and volume. It is intended to be called by the platform dependent wave player that extends this class.- Parameters:
buffer- specifies the buffer to hold the generated waveform data.sampleRate- specifies the sampling rate of tbe waveform.frequency- specifies the tone frequency in Hz.volume- specifies the volume in the range 0.0 to 1.0.
-
genTriangleWave
protected void genTriangleWave(short[] buffer, int sampleRate, double frequency, double volume) This method generates the wave data for a triangle wave with the specified frequency and volume. It is intended to be called by the platform dependent wave player that extends this class.- Parameters:
buffer- specifies the buffer to hold the generated waveform data.sampleRate- specifies the sampling rate of tbe waveform.frequency- specifies the tone frequency in Hz.volume- specifies the volume in the range 0.0 to 1.0.
-
applySoundEnvelope
protected void applySoundEnvelope(short[] buffer, int sampleRate, double attack, double decay, double sustain, double release) This method applies the sound envelope to the sound data.- Parameters:
buffer- specifies the buffer that contains the wave data.sampleRate- specifies the sampling rate of tbe waveform.attack- specifies the attack time in seconds.decay- specifies the decay time in seconds.sustain- specifies the sustain level in proportion (0.0 to 1.0).release- specifies the release time in seconds.
-
scaleData
private void scaleData(short[] buffer, int startIndex, int length, double slope, double zeroIntercept) This method scales the sound data using the envelope segment specified by the equation y = mx + b where m is the slope and b is the zeroIntercept.- Parameters:
buffer- specifies the buffer contains the wave data.startIndex- specifies the start index of the envelope segment.length- specifies the length of the envelope segment.slope- specifies the slope of the envelope segment.zeroIntercept- specifies the zero intercept of the envelope segment.
-