Tutorial: Using Sounds with LEGO™ Robots

This tutorial helps explain the use of sounds with LEGO™ Robots.

Using Sounds with LEGO™ Robots in Quorum

In this tutorial, we will talk about the different ways a robot can produce sound. In particular, we will discuss:

  • Gerenal information about sounds
  • Playing Tones
  • Playing Pre-defined Beeps
  • Playing Audio Clips
use Libraries.Robots.Lego.Sound

Sound sound
sound:PlayTone(440, 200)     //plays a tone corresponding to 440 Hz for 200 milliseconds

General Information About Sounds

The brick has a single speaker on its side, which all sounds are played out of. Quorum has two actions that are used for controlling the volume of the sounds:

GetVolume and SetVolume. Volume levels are represented as percentages of the maximum volume, and thus the range that volume actions work with is between 0 and 100.

With the Sound class, we are able to play tones, pre-defined beeps, and even audio clips. These sounds can be used to indicate that a robot is doing something, has reached a certain part in the program, or to simply get the attention of others.

Playing Tones

Tones are sounds played at a certain frequency, measured in hertz (Hz). For humans, the audible range for these tones is from around 20 Hz to 20,000 Hz where lower frequencies generate lower pitch sounds and higher frequencies generate higher pitch sounds. An example of how to play a tone in a program is as follows:

From the comment in the example, we can see that the PlayTone action takes two parameters: the frequency of the tone we want to play and the amount of time, in milliseconds, that we want it to play for.

Playing Pre-Defined Beeps

Quorum has a set of actions to play different types of beeps. The different actions are as follows:

  • Beep
  • BeepTwice
  • BeepSequenceUp
  • BeepSequenceDown
  • Buzz

The Beep and BeepTwice actions do what you might think: beep and beep twice, respectively. The BeepSequenceUp plays four tones in quick succession, with each tone being a higher frequency than the last. On the other hand, BeepSequenceDown plays the same tones in reverse. The buzz action plays a lower frequency tone than any of the others for a short amount of time.

Playing Audio Clips

Custom audio clips can be played through the Sound class's PlayAudio action. The types of sound files that can be played are limited to .wav files and must be in mono format (as opposed to stereo). The easiest way to put a sound on the robot is to physically pull the SD-card out of the robot and place it into a card reader in the computer. If your computer does not have an SD card reader, you may need to purchase one.

On the SD card, there are two partitions, or structures for storing files, one being named FAT32 and the other named EXT4. In our case, the EXT4 partition is where Quorum programs live. We want to transfer our files into the same folder as the Quorum files, which is in the folder /home/lejos/programs. In order to read EXT4, you need to either use Linux or a program to allow you to read that partition format. More information on that can be found at the links below for Mac or PC:

    Once you have the file on the robot, the following code will allow the robot to play it:

    use Libraries.Robots.Lego.Sound
    use Libraries.System.File
    
    File audioFile
    Sound sound
    
    audioFile:SetPath("sound.wav")
    sound:PlayAudio(audioFile)

    Additional Information

    For more information on the Sound class, see the Sound class documentation.

    Next Tutorial

    In the next tutorial, we will discuss Screen, which describes how to use lego robots screen.