Robot App Store Developer Program

robots-app-store-developer-program

Knowledge Base

How to Make Lego NXT to Play a Sound

  By Anna Sandler

Make sure you are familiar with "What is a Lego NXT Bluetooth Telegram" chapter before you continue to this tutorial.

Usages

How to play a sound on the NXT:
  • PlayTone command- Start a playback of a MIDI tone specified by frequency in Hz and duration is milliseconds;
  • PlaySoundFile command- Start a playback of a specified existing RSO file.
    You can either download a file to the NXT’s brick using Bluetooth,
    or you can use a file handling commands to transfer the file from your device programmatically;
  • Use the StopSoundPlayback command to stop the playback.
    In case of playing the sound file in a loop (or if you just want to stop the playing operation earlier).
    This command is valid for both PlayTone and PlaySoundFile;

Sound play command types

Sound play commands are part of the "Direct commands".
There are two relevant command types for a direct command:
  • 0x00 for a command with reply message, and
  • 0x80 for a command without the reply message.

Sound file name

Sound file name format consists from:
[name, up to 15 ASCII characters].rso[string NULL terminator 0x00].
The system is case insensitive, meaning uppercase characters are treated the same as lower case characters.

Frequency to note mapper


Leg NXT Programming Over Bluetooth Frequency Note Mapper
Lego NXT Programming Over Bluetooth Frequency Note Mapper



PlayTone command

This command is used to play a MIDI tone specified by frequency in Hz, and duration is milliseconds.

The playback won’t be stopped as the program/ robot-app is finished.
Use the StopSoundPlayback command to interrupt the playing process earlier.

Syntax

[0x06] [0x00] [0x00 or 0x80] [0x03] [Frequency byte LSB] [Frequency byte MSB] [Duration byte LSB] [Duration byte MSB]

Explanation:

  • Byte 0-1: Command length LSB first.
  • Byte 2: Command type- direct command.
    Since no error will be retrieved in any case, we can’t find a reason to ask for a response message,
    therefore, use always the 0x80 as a command type.
    If, for some reason, you are of those who just can’t live without unnecessary messages from the robot,
    in this case use the 0x00 as a command type.
  • Byte 3: Command- play a sound.
  • Byte 4-5: Frequency for the tone in Hz.
    Make sure the value range is 200-1400.
    There is no error message if the value is out of range. LSB first.
    Take a look at the “Frequency to note mapper” chart above to choose the relevant note.
    Make sure to round the Hz number when you send the value.
  • Byte 6-7: Duration of the tone is milliseconds. LSB first.

The reply message that is coming back (if requested) will always look like:

[0x03] [0x00] [0x02] [0x03] [0x00]

Explanation:

  • Byte 0-1: Message length LSB first.
  • Byte 2: Command type- reply message
  • Byte 3: Command that has requested for this reply.
  • Byte 4: Command status- 0x00 for success or a theoretic error number for failure.
    This command will not retrieve an error in any case.

Example:

Let’s play a Mi (C in the mapping chart) sound for half a second.

The command that is sent to NXT is:
0x06 0x00 0x00 0x03 0x0B 0x02 0xF4 0x01

The command that is received from NXT as a reply is:
0x03 0x00 0x02 0x03 0x00



PlaySoundFile command

This command is used to play a specified existing RSO file.

Make sure you use a valid rso file. The system will not notify in case of existing file from a different type.

The playback won’t be stopped as the program/ robot-app is finished.
Use the StopSoundPlayback command to interrupt the playing process earlier.

Syntax

[0x17] [0x00] [0x00 or 0x80] [0x02] [0x00 or 0x01] [fileName.rso]

Explanation:

  • Byte 0-1: Command length LSB first.
  • Byte 2: Command type- direct command.
    For direct command with response message use 0x00, otherwise, for direct command without the reply message, use 0x80.
  • Byte 3: Command- play a sound file.
  • Byte 4: Play in a loop.
    For playing the sound file only once use 0x0,
    otherwise, for playing the sound file indefinitely use 0x01.
  • Byte 5-24: Sound file name, including its extension- rso.
    The system is case insensitive.
    The bytes of file name and type characters should be ASCII encoded.
    NULL terminator must appear at the end ('\0').
    In case the name of the file is shorter than 15 characters,
    NULL terminator byte should be added to the end of the file name extension for each missing character.
    Total file name characters, including the NULL terminators, should always be 20.

The reply message that is coming back (if requested) looks like:

[0x03] [0x00] [0x02] [0x02] [0x00 or error]

Explanation:

  • Byte 0-1: Message length LSB first.
  • Byte 2: Command type- reply message
  • Byte 3: Command that has requested for this reply.
  • Byte 4: Command status- 0x00 for success or error number for failure. Check the NXT File Handling over Bluetooth- Introduction chapter for error numbers.

Example:

Play the Woops.rso sound file only once.

The command that is sent to NXT is:
0x17 0x00 0x00 0x02 0x00 woops.rso\0\0\0\0\0\0\0\0\0\0\0

The command that is received from NXT as a reply is:
0x03 0x00 0x02 0x02 0x00



StopSoundPlayback command

This command is used to stop the playback for both PlayTone and PlaySoundFile commands.

Syntax

[0x02] [0x00] [0x00 or 0x80] [0x0C]

Explanation:

  • Byte 0-1: Command length LSB first.
  • Byte 2: Command type- direct command.
    Since no error will be retrieved in any case, we can’t find a reason to ask for a response message,
    therefore, use always the 0x80 as a command type.
    If, for some reason, you are of those who just can’t live without unnecessary messages from the robot,
    in this case use the 0x00 as a command type.
  • Byte 3: Command- stop the sound.

The reply message that is coming back (if requested) will always look like:

[0x03] [0x00] [0x02] [0x0C] [0x00]

Explanation:

  • Byte 0-1: Message length LSB first.
  • Byte 2: Command type- reply message
  • Byte 3: Command that has requested for this reply.
  • Byte 4: Command status- 0x00 for success or a theoretic error number for failure.
    This command will not retrieve an error in any case.

Example:

PlayTone command has been sent with a very long duration,
or PlaySoundFile command has requested a playback to be in a loop.
In order to stop the playback, StopSoundPlayback command is being sent.

The command that is sent to NXT is:
0x02 0x00 0x00 0x0C

The command that is received from NXT as a reply is:
0x03 0x00 0x02 0x0C 0x00



What's next?

Let's move forward and learn how to program a pre-defined text to speech.

read find an affair click
canada drug pharmacy coupon go internet drug coupons







Related Robot-Apps™

Comments