Libraries.Robots.Lego.Sound Documentation

This class is an object representation of the LEGO EV3 Mindstorms sound controller. It is used to control volume, to play tones, and to play music.

Example Code

use Libraries.Robots.Lego.Sound
use Libraries.Robots.Lego.Motor
use Libraries.Robots.Lego.ColorSensor
use Libraries.Robots.Lego.Utility

Sound sound
Motor motor
ColorSensor colorSensor
Utility utility

colorSensor:SetPort(colorSensor:PORT_3)
motor:RotateForward(motor:MOTOR_B)
motor:RotateForward(motor:MOTOR_C)        //two motors to move the robot, connected to ports B and C
text color = ""
repeat 20 times
    color = colorSensor:GetColor()
    if color = "red"
        sound:PlayTone(200, 100)
    elseif color = "blue"
        sound:PlayTone(800, 100)
    elseif color = "green"
        sound:PlayTone(1600, 100)
    elseif color = "yellow"
        sound:PlayTone(3200, 100)
    end
    utility:DelayMilliseconds(100)
end

Inherits from: Libraries.Language.Object

Summary

Actions Summary Table

ActionsDescription
Beep()This action tells the robot to play a beep noise.
BeepSequenceDown()This action tells the robot to play a sequence of 4 beeps with each tone lower than the previous.
BeepSequenceUp()This action tells the robot to play a sequence of 4 beeps with each tone higher than the previous.
BeepTwice()This action tells the robot to beep twice.
Buzz()This action tells the robot to make a low pitch buzz noise.
Compare(Libraries.Language.Object object)This action compares two object hash codes and returns an integer.
Equals(Libraries.Language.Object object)This action determines if two objects are equal based on their hash code values.
GetHashCode()This action gets the hash code for an object.
GetVolume()This action gets the volume of the robot.
PlayAudio(Libraries.System.File audioFile)This action plays an audio file stored on the robot.
PlayAudio(text audioFile)This action plays an audio file stored on the robot.
PlayTone(integer frequency, integer duration)This action plays a specified tone for a an amount of time.
SetVolume(integer volume)This action sets the volume of the robot.

Actions Documentation

Beep()

This action tells the robot to play a beep noise.

Example Code

use Libraries.Robots.Lego.Sound
    use Libraries.Robots.Lego.Motor
    use Libraries.Robots.Lego.ColorSensor
    use Libraries.Robots.Lego.Utility

    Sound sound
    Motor motor
    ColorSensor colorSensor
    Utility utility
    
    colorSensor:SetPort(3)
    motor:RotateForward(motor:MOTOR_B)
    motor:RotateForward(motor:MOTOR_C)    //two motors used to move the robot, connected to ports B and C
    repeat 5 times
        if colorSensor:GetColor() = "red"
            sound:BeepTwice()
        else
            sound:Buzz()
        end
        utility:DelayMilliseconds(1000)
    end

BeepSequenceDown()

This action tells the robot to play a sequence of 4 beeps with each tone lower than the previous.

Example Code

use Libraries.Robots.Lego.Sound
    use Libraries.Robots.Lego.Motor

    Sound sound
    Motor motor
    
    motor:SetSpeed(motor:MOTOR_A, 180)
    sound:BeepSequenceUp()                      //indicate that the motor is about to start moving
    motor:RotateByDegrees(motor:MOTOR_A, 720)   //rotate the motor connected to port A
    sound:BeepSequenceDown()                    //indicate the motor has stopped moving

BeepSequenceUp()

This action tells the robot to play a sequence of 4 beeps with each tone higher than the previous.

Example Code

use Libraries.Robots.Lego.Sound
    use Libraries.Robots.Lego.Motor

    Sound sound
    Motor motor
    
    motor:SetSpeed(motor:MOTOR_A, 180)
    sound:BeepSequenceUp()                      //indicate the motor is about to start moving
    motor:RotateByDegrees(motor:MOTOR_A, 720)   //rotate the motor connected to port A
    sound:BeepSequenceDown()                    //indicate the motor has stopped moving

BeepTwice()

This action tells the robot to beep twice.

Example Code

use Libraries.Robots.Lego.Sound
    use Libraries.Robots.Lego.Motor
    use Libraries.Robots.Lego.ColorSensor
    use Libraries.Robots.Lego.Utility

    Sound sound
    Motor motor
    ColorSensor colorSensor
    Utility utility
    
    colorSensor:SetPort(3)
    motor:RotateForward(motor:MOTOR_B)
    motor:RotateForward(motor:MOTOR_C)    //two motors used to move the robot, connected to ports B and C
    repeat 5 times
        if colorSensor:GetColor() = "blue"
            sound:BeepTwice()
        else
            sound:Buzz()
        end
        utility:DelayMilliseconds(1000)
    end

Buzz()

This action tells the robot to make a low pitch buzz noise.

Example Code

use Libraries.Robots.Lego.Sound
    use Libraries.Robots.Lego.InfraredSensor
    use Libraries.Robots.Lego.Motor
    use Libraries.Robots.Lego.Utility

    Sound sound
    InfraredSensor infraredSensor
    Motor motor
    Utility utility
    
    infraredSensor:SetPort(infraredSensor:PORT_4)
    motor:RotateForward(motor:MOTOR_B)
    motor:RotateForward(motor:MOTOR_C)                //two motors used to move the robot, connected to ports B and C
    repeat until infraredSensor:GetDistance() < 50
        sound:Beep()
        utility:DelayMilliseconds(200)
    end
    sound:Buzz()    //the sensor found a nearby object
    motor:Stop(motor:MOTOR_B)
    motor:Stop(motor:MOTOR_C)

Compare(Libraries.Language.Object object)

This action compares two object hash codes and returns an integer. The result is larger if this hash code is larger than the object passed as a parameter, smaller, or equal. In this case, -1 means smaller, 0 means equal, and 1 means larger. This action was changed in Quorum 7 to return an integer, instead of a CompareResult object, because the previous implementation was causing efficiency issues.

Example Code

Object o
        Object t
        integer result = o:Compare(t) //1 (larger), 0 (equal), or -1 (smaller)

Parameters

Return

integer: The Compare result, Smaller, Equal, or Larger.

Equals(Libraries.Language.Object object)

This action determines if two objects are equal based on their hash code values.

Example Code

use Libraries.Language.Object
        use Libraries.Language.Types.Text
        Object o
        Text t
        boolean result = o:Equals(t)

Parameters

Return

boolean: True if the hash codes are equal and false if they are not equal.

GetHashCode()

This action gets the hash code for an object.

Example Code

Object o
        integer hash = o:GetHashCode()

Return

integer: The integer hash code of the object.

GetVolume()

This action gets the volume of the robot.

Example Code

use Libraries.Robots.Lego.Sound
    use Libraries.Robots.Lego.Screen
    use Libraries.Robots.Lego.Utility

    Sound sound
    Screen screen
    Utility utility
    
    integer volume = sound:GetVolume()
    screen:OutputCenter("Initial volume: " + volume, 2)
    if volume > 30
        sound:SetVolume(30) //limit the maximum volume
    elseif volume < 10
        sound:SetVolume(10) //limit the minimum volume
    end
    screen:OutputCenter("New volume: " + sound:GetVolume(), 4)
    sound:BeepSequenceUp()
    utility:DelayMilliseconds(250)
    sound:BeepSequenceDown()

Return

integer: how loud the robot is set to be. This value will be between 0 (lowest) and 100 (highest).

PlayAudio(Libraries.System.File audioFile)

This action plays an audio file stored on the robot. The audio file must be in .wav format and use a single channel (mono).

Example Code

use Libraries.Robots.Lego.Sound
    use Libraries.Robots.Lego.Motor
    use Libraries.Robots.Lego.Utility
    use Libraries.System.File
    
    Sound sound
    Motor motor
    Utility utility
    File audioFile
    
    audioFile:SetPath("sound.wav")      //requires a file named sound.wav already stored on the robot
    motor:RotateForward(motor:MOTOR_B)    //two motors used to move the robot, connected to ports B and C
    motor:RotateBackward(motor:MOTOR_C)   //both motors go in opposite directions, causing the robot to spin in place
    sound:SetVolume(100)
    sound:PlayAudio(audioFile)
    repeat while sound:IsPlaying()
        utility:DelayMilliseconds(100)  //check every 100 milliseconds if the robot is still playing the audio
    end
    motor:Stop(motor:MOTOR_B)
    motor:Stop(motor:MOTOR_C)

Parameters

PlayAudio(text audioFile)

This action plays an audio file stored on the robot. The audio file must be in .wav format and use a single channel (mono).

Example Code

use Libraries.Robots.Lego.Sound
    use Libraries.Robots.Lego.Motor
    use Libraries.Robots.Lego.Utility
    use Libraries.System.File
    
    Sound sound
    Motor motor
    Utility utility

    text audioFile = "sound.wav"          //assumes a file named sound.wav exists in the current directory
    motor:RotateForward(motor:MOTOR_B)    //two motors used to move the robot, connected to ports B and C
    motor:RotateBackward(motor:MOTOR_C)   //both motors go in opposite directions, causing the robot to spin in place
    sound:SetVolume(100)
    sound:PlayAudio(audioFile)
    repeat while sound:IsPlaying()
        utility:DelayMilliseconds(100)  //check every 100 milliseconds if the robot is still playing the audio
    end
    motor:Stop(motor:MOTOR_B)
    motor:Stop(motor:MOTOR_C)

Parameters

PlayTone(integer frequency, integer duration)

This action plays a specified tone for a an amount of time.

Example Code

use Libraries.Robots.Lego.Sound
    use Libraries.Robots.Lego.Utility
    
    Sound sound
    Utility utility
    
    integer frequency = 100
    repeat 5 times
        sound:PlayTone(frequency, 200)
        utility:DelayMilliseconds(200)
        frequency = frequency * 3
    end

Parameters

SetVolume(integer volume)

This action sets the volume of the robot.

Example Code

use Libraries.Robots.Lego.Sound
    use Libraries.Robots.Lego.Utility

    Sound sound
    Utility utility
    
    sound:SetVolume(20)
    sound:PlayTone(777, 200)
    utility:DelayMilliseconds(400)
    sound:SetVolume(40)
    sound:PlayTone(777, 200)
    utility:DelayMilliseconds(400)
    sound:SetVolume(60)
    sound:PlayTone(777, 200)
    utility:DelayMilliseconds(400)
    sound:SetVolume(80)
    sound:PlayTone(777, 200)
    utility:DelayMilliseconds(400)
    sound:SetVolume(100)
    sound:PlayTone(777, 200)
    utility:DelayMilliseconds(400)

Parameters