The leJOS Tutorial

| Start of Tutorial > Start of Trail

Trail: Essential leJOS classes
Lesson: Controlling the Hardware

# Controlling the Hardware: Sound

The RCX contains a little speaker which is able to generate many simple sounds.
leJOS offers access to this audio capabilities via the `josx.rcx.platform.Sound` class.
This class is static; hence a typical call would be

                Sound.beep();

There are several methods available for producing sounds:

- `public static void beep()`: beeps once.
- `public static void twoBeeps()`: beeps twice.
- `public static void beepSequence()`: a downwards sequence of beeps.
- `public static void buzz()`: low buzz.
- `public static void playTone(int aFrequency,int aDuration)`: plays a sound of the given frequency (in hertz) and the given duration (in centiseconds!), where the maximum duration is 256 (= 2.56 seconds). Note that frequencies below 31 Hz and above 25,000 Hz are not audible by most human beings (you may call your dog with the RCX, though).
- `public static void systemSound(boolean isQueued,int aCode)`: produces one of currently six possible system sounds (code see API below), which is queued or not by the ROM and hence played in background.

Note that the above calls (save systemSound()) return immediately; hence the program flow continues while the RCX is playing the sound(s) in question.

The variety of sounds proved to be pretty useful for means of error seeking: just play a special sound or sound sequence at a point in your code when you want to get sure that this part is actually executed.

## An Example that uses the Sound class

                /**
                * plays some sounds
                */
                import josx.platform.rcx.*;
                class SoundExample {
                    public static void main(String[] args)
                        throws InterruptedException {
                        // play tone
                        Sound.playTone(4000,100);
                        // wait a bit
                        Thread.sleep(1500);
                        // play system sound (long low beep)
                        Sound.systemSound(false,4);
                        // wait a bit
                        Thread.sleep(2000);
                    } // main()
                } // class SoundExample

## The Sound API

may be found here.

The leJOS Tutorial

| Start of Tutorial > Start of Trail