Difference between revisions of "VEX2 Functions Sound"

From ROBOTC API Guide
Jump to: navigation, search
(bSoundQueueAvailable)
 
(4 intermediate revisions by one user not shown)
Line 1: Line 1:
{| style="font-family:Verdana, Genega, sans-sarif; font-size:80%;color:gray;" width="100%" cellpadding="0%" cellspacing="0" border="0"
+
<yambe:breadcrumb self="Sound">VEX2_Functions_and_Variables|Functions and Variables</yambe:breadcrumb>
|-
+
<br />
|
+
''[[Main_Page|Main]] >> [[VEX2_Main|CORTEX]] >> [[VEX2: Functions| Functions and Variables]] >> [[VEX2:_Functions_-_Sound|Sound]] ''
+
|-
+
|}
+
 
+
  
 
{| class="wikiText"
 
{| class="wikiText"
Line 16: Line 11:
 
|-
 
|-
 
|}
 
|}
 +
<br />
  
 
+
{{tl|1|1}}
{|
+
<br />
|-
+
|style="vertical-align:top"| __TOC__
+
|style="vertical-align:top"|
+
{| width="100" cellpadding="2" cellspacing="0" style="border-collapse: collapse; border-width: 1px; border-style: solid; border-color: #000"
+
!colspan="2" class="wikiHeader"|Color Key
+
|-
+
|class="wikiText" width="75%" style="border-style: solid; border-width: 1px 0px 0px 0px"|Function:
+
|width="25%" style="border-style: solid; border-width: 1px 0px 0px 0px;" class="colorKeyFunc"|
+
|-
+
|class="wikiText" width="75%" style="border-style: solid; border-width: 0px 0px 0px 0px"|Variable:
+
|width="25%" style="border-style: solid; border-width: 0px 0px 0px 0px;" class="colorKeyVar"|
+
|}
+
|-
+
|}
+
 
+
  
 
== bPlaySounds ==
 
== bPlaySounds ==
Line 231: Line 212:
 
   |<syntaxhighlight lang="ROBOTC">
 
   |<syntaxhighlight lang="ROBOTC">
 
PlaySound(soundBeepBeep);  // play the sound, 'soundBeepBeep'
 
PlaySound(soundBeepBeep);  // play the sound, 'soundBeepBeep'
</syntaxhighlight>
 
  |-
 
  |}
 
|-
 
|}
 
<br />
 
 
== PlaySoundFile ==
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
| class="functionType"| <span class="bigKeywordBI">void </span><span class="bigKeywordB">PlaySoundFile</span><span class="bigCodePunc">(</span><span class="bigKeywordBI">const string </span><span class="bigCodeBasic">&sFileName</span><span class="bigCodePunc">)</span>
 
|-
 
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|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.
 
|-
 
|
 
  {| class="parameterTable" cellpadding="5%" width="100%"
 
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
 
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
 
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
 
  |-
 
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|''sFileName''
 
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|A sound file to play
 
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_string|string]]
 
  |-
 
  |}
 
|-
 
|
 
  {|
 
  |-
 
  |<syntaxhighlight lang="ROBOTC">
 
PlaySoundFile("Woops.rso");  // play the sound file, 'Woops.rso'
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-

Latest revision as of 16:10, 30 April 2012


ROBOTC provides a comprehensive suite of functions for controlling an optional speaker connected to the first motor port on a VEX controller.

The 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 operation is 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.

There are several sample programs illustrating sound playback on a VEX controller. The file “songs.c” contains several complete songs that can be played as a background task while your robot continues to execute its main “mission” task.


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
}


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'


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