FutureBasic Logo

<<    Index    >> FutureBasic 5

sound <frequency>   statement



Syntax:
sound pitch,duration [,[volume][,async]]

Description:
This statement plays a tone of the given pitch, duration and volume.
The pitch is expressed as a frequency in cycles per second. The frequency that you specify is converted to the nearest "MIDI note value," which determines the actual note that's played. Middle "C" corresponds to a frequency of 261.625.
The duration is expressed in ticks, and can range from 0 to 32,767. However, the toolbox sound commands require FutureBasic to translate the ticks into an integer to represent half-milliseconds. This means you can play a note that is no longer than 32.8 seconds.
the volume can range from 0 through 127. Specifying 0 will result in silence, and 127 will play the sound at the maximum volume specified in the "Sound" control panel. If you omit this parameter, it is treated as 127.
If async is _zTrue, the sound will play asynchronously. If async is _false, or you omit the parameter, the sound plays synchronously. When you play asynchronously, your program starts executing the next statement immediately while the sound is playing in the background. When you play synchronously, the next statement in your program does not execute until the sound has finished playing.
If you are playing notes asynchronously, and your execute a second sound statement while another sound is still playing in the background, the new sound won't start playing until the first sound finishes. Note that on some machines, this technique can result in lost sounds. When playing asynchronously it's better to use the sound% function to determine when one sound has ended, before attempting to play the next sound.
One way to play sound frequencies is to use negative numbers (from -1 through -127) to represent the note that you wish to play. The table below shows how to use these values.

AA#BCC#DD#EFF#GG#
Octave 1012345678
Octave 291011121314151617181920
Octave 3212223242526272829303132
Octave 4333435363738394041424344
Octave 5454647484950515253545556
Octave 6575859606162636465666768
Octave 7697071727374757677787980
Octave 8818283848586878889909192
Octave 993949596979899100101102103104
Octave 10105106107108109110111112113114115116
Octave 11117118119120121122123124125126127

Using the midi table for a guideline, we can create a version of "pop goes the weasel" as follows:
print "Pop! "; : sound -70, 45 ,,_false
print "Goes "; : sound -64, 30 ,,_false
print "the "; : sound -67, 15 ,,_false
print "wea"; : sound -66, 40 ,,_false
print "sel "; : sound -62, 45 ,,_false


See Also:
sound%; sound end; sound <snd>