Class Sound
- java.lang.Object
-
- lejos.hardware.Sound
-
-
Field Summary
Fields Modifier and Type Field Description protected static Audioaudiostatic intC2static java.lang.StringVOL_SETTING-
Fields inherited from interface lejos.hardware.Sounds
ASCENDING, BEEP, BUZZ, DESCENDING, DOUBLE_BEEP, FLUTE, PIANO, VOL_MAX, XYLOPHONE
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidbeep()Beeps once.static voidbeepSequence()Downward tones.static voidbeepSequenceUp()Upward tones.static voidbuzz()Low buzzstatic intgetTime()Returns the number of milliseconds remaining of the current tone or sample.static intgetVolume()Get the current master volume levelstatic voidloadSettings()Load the current system settings associated with this class.static voidpause(int t)static voidplayNote(int[] inst, int freq, int len)Play a note with attack, decay, sustain and release shape, which is known as a ADSR envelope.static intplaySample(byte[] data, int offset, int len, int freq, int vol)Queue a series of PCM samples to play at the specified volume and sample rate.static intplaySample(java.io.File file)Play a wav file.static intplaySample(java.io.File file, int vol)Play a wav file.static voidplayTone(int freq, int duration)Plays a tone, given its frequency and duration.static voidplayTone(int aFrequency, int aDuration, int aVolume)Plays a tone, given its frequency and duration.static voidsetVolume(int vol)Set the master volume levelstatic voidsystemSound(boolean aQueued, int aCode)Play a system sound.static voidtwoBeeps()Beeps twice.
-
-
-
Field Detail
-
audio
protected static final Audio audio
-
VOL_SETTING
public static final java.lang.String VOL_SETTING
- See Also:
- Constant Field Values
-
C2
public static int C2
-
-
Method Detail
-
systemSound
public static void systemSound(boolean aQueued, int aCode)Play a system sound.aCode Resulting Sound 0 short beep 1 double beep 2 descending arpeggio 3 ascending arpeggio 4 long, low buzz
-
beep
public static void beep()
Beeps once.
-
twoBeeps
public static void twoBeeps()
Beeps twice.
-
beepSequence
public static void beepSequence()
Downward tones.
-
beepSequenceUp
public static void beepSequenceUp()
Upward tones.
-
buzz
public static void buzz()
Low buzz
-
pause
public static void pause(int t)
-
getTime
public static int getTime()
Returns the number of milliseconds remaining of the current tone or sample.- Returns:
- milliseconds remaining
-
playTone
public static void playTone(int aFrequency, int aDuration, int aVolume)Plays a tone, given its frequency and duration.- Parameters:
aFrequency- The frequency of the tone in Hertz (Hz).aDuration- The duration of the tone, in milliseconds.aVolume- The volume of the playback 100 corresponds to 100%
-
playTone
public static void playTone(int freq, int duration)Plays a tone, given its frequency and duration.- Parameters:
freq- The frequency of the tone in Hertz (Hz).duration- The duration of the tone, in milliseconds.
-
playSample
public static int playSample(java.io.File file, int vol)Play a wav file. Must be mono, from 8kHz to 48kHz, and 8-bit or 16-bit.- Parameters:
file- the 8-bit or 16-bit PWM (WAV) sample filevol- the volume percentage 0 - 100- Returns:
- The number of milliseconds the sample will play for or < 0 if there is an error.
- Throws:
java.io.FileNotFoundException
-
playSample
public static int playSample(java.io.File file)
Play a wav file. Must be mono, from 8kHz to 48kHz, and 8-bit or 16-bit.- Parameters:
file- the 8-bit or 16-bit PWM (WAV) sample file- Returns:
- The number of milliseconds the sample will play for or < 0 if there is an error.
- Throws:
java.io.FileNotFoundException
-
playSample
public static int playSample(byte[] data, int offset, int len, int freq, int vol)Queue a series of PCM samples to play at the specified volume and sample rate.- Parameters:
data- Buffer containing the samplesoffset- Offset of the first sample in the bufferlen- Number of samples to queuefreq- Sample ratevol- playback volume- Returns:
- Number of samples actually queued
-
playNote
public static void playNote(int[] inst, int freq, int len)Play a note with attack, decay, sustain and release shape, which is known as a ADSR envelope. This function plays a more musical sounding note compared to playTone(). It uses a set of supplied "instrument" parameters to define the shape of the note's envelope.
Instruments are defined in the Sounds interface, which is inherited by this class. For example, the piano instrument array looks like this:
public final static int[] PIANO = new int[]{4, 25, 500, 7000, 5};The parameter len is the total time of the note to be played val 0 is the attack time the time for the volume to go from near zero to 100% val 1 is the decay time during this period the volume decreases by val[2]/100 to the sustain level. the sustain period is (len - val[0] - val[1] - val[4]) during this period the volume decreases by val[3]/100. the final decay period is val[4] and the volume reduces to zero. All of the times are units of 2mS (except for len which is in mS).
Because values equal 2ms, the piano array numbers are doubled:
1. take 8mS to increase the volume from zero to 100%
2. take 50mS to decrese the volume by 5%
3. take len - 8 - 25 - 10 mS to decrease the volume by 70%
4. take 10mS to decrease the volume to 0- Parameters:
inst- Instrument definition (5 ints in an array).freq- The note to play (in Hz)len- The duration of the note (in ms)
-
setVolume
public static void setVolume(int vol)
Set the master volume level- Parameters:
vol- 0-100
-
getVolume
public static int getVolume()
Get the current master volume level- Returns:
- the current master volume 0-100
-
loadSettings
public static void loadSettings()
Load the current system settings associated with this class. Called automatically to initialize the class. May be called if it is required to reload any settings.
-
-