NXT Functions Sound

From ROBOTC API Guide
Jump to: navigation, search


ROBOTC provides a comprehensive suite of functions for controlling the NXT speaker.


ROBOTC firmware queues up to 10 sound items for playback. This allows user programs to initiate playback of a sound item and continue execution without having to wait for a sound item to finish; this mode of operatio nis very desirable for robots where, if you waited for the sound to complete playback, you could be delayed in updating the robot’s motors in reaction to a change in sensors.


ROBOTC plays both uncompressed and compressed sound files. The ROBOTC development environment provides a command to compress sound files. It is located in the menu “Windows -> NXT Brick -> File Management”.


Color Key
Function:
Variable:


bPlaySounds

word bPlaySounds
(word) Boolean flag. Indicates whether new sound requests should be accepted or discarded. Note that this is defined as a word but is treated as a bool.
bPlaySounds = true;   // ACCEPT new sound requests
bPlaySounds = false;  // DISCARD new sound requests


bSoundActive

const bool bSoundActive
(bool) Boolean flag. If true indicates sound system is actively playing sound.
while(bSoundActive)  // while a sound is actively playing:
{
  // do not continue until finished playing sound
}


bSoundQueueAvailable

const bool bSoundQueueAvailable
(bool) Boolean flag. Indicates whether there is space available in the sound queue for another item.
if(bSoundQueueAvailable)  // if there is still space in the Sound Queue:
{
  PlayImmediateTone(SensorValue[lightSensor] * 80, 50);  // play tone according to light sensor readings
}
else                      // if there is no longer any space in the Sound Queue:
{
  nxtDisplayCenteredTextLine(3, "Sound Queue Full!");    // display a centered text on line 3 of the NXT LCD
}


ClearSounds

void ClearSounds()
(void) Clears all existing and buffered sound commands.
ClearSounds();  // clear the sound buffer


MuteSound

void MuteSound()
(void) Mutes all subsequent sound commands.
MuteSound();  // MUTE


nVolume

word nVolume
(word) Sound volume. Range 0 to 4 (loudest).
nVolume = 1;  // set the volume to 1, a nice and comfortable volume for a quiet location


PlayImmediateTone

void PlayImmediateTone(const int frequency, const int durationIn10MsecTicks)
(void) Immediately play tone at frequency & duration ahead of queued requests.
Parameter Explanation Data Type
frequency The frequency of the tone to play int
durationIn10MsecTicks How long to play the tone (measured in units of 10 milliseconds) int
while(true)
{
  PlayImmediateTone(SensorValue[lightSensor] * 80, 50);  // play tone according to light sensor readings
  wait1Msec(200);                                        // wait 200 milliseconds before checking again
}


PlaySound

void PlaySound(TSounds sound)
(void) Play one of the system predefined sounds (buzz, beep, click, ...)
Parameter Explanation Data Type
sound A predefined system sound TSounds


Predefined Sounds:

  • soundBlip
  • soundBeepBeep
  • soundDownwardTones
  • soundUpwardTones
  • soundLowBuzz
  • soundFastUpwardTones
  • soundShortBlip
  • soundException
  • soundLowBuzzShort
PlaySound(soundBeepBeep);  // play the sound, 'soundBeepBeep'


PlaySoundFile

void PlaySoundFile(const string &sFileName)
(void) Plays a sound file from the NXT file system. File must be present on the NXT. RobotC will automatically download required files with user program.
Parameter Explanation Data Type
sFileName A sound file to play string
PlaySoundFile("Woops.rso");  // play the sound file, 'Woops.rso'


PlayTone

void PlayTone(const int frequency, const int durationIn10MsecTicks)
(void) Plays a constant tone at the specified frequency and duration.
Parameter Explanation Data Type
frequency The frequency of the tone to play int
durationIn10MsecTicks How long to play the tone (measured in units of 10 milliseconds) int
PlayTone(784, 15);  // play a tone at a frequency of 784 for 150 milliseconds


UnmuteSound

void UnmuteSound()
(void) Restores sound playback volume.
UnmuteSound();  // UN-MUTE