C H A P T E R 1 4
Sound
Using Sound
14-7
Synchronous and Asynchronous Sound
14
When a sound is played asynchronously, the playback can be intermixed with other
tasks because the system does not wait for the sound to finish before beginning
another task (such as updating the user interface, allowing user feedback; for
example with buttons, or playing a subsequent sound).
When playback must be allowed to complete, use the
PlaySoundSync,
PlaySoundAtVolume
, or
PlaySoundIrregardless
to guarantee
uninterrupted playback. Synchronous playback is generally preferred unless the
sound is so long as to be tedious or the application requires a high degree of
responsiveness to the user. The NewtonScript interpreter can do nothing else until
it completes synchronous playback.
Both approaches have benefits and drawbacks: synchronous playback can block
other NewtonScript code from running when it's inconvenient to do so; on the
other hand, asynchronous playback is never guaranteed to complete. Your use of
synchronous or asynchronous sound playback depends on your application's needs.
Differences Between Synchronous Asynchronous Playback
14
The following code example demonstrates the difference between asynchronous
playback and synchronous playback. To hear the demonstration of the two types
of sound playback, type following code example into the Inspector as it is shown
here, select all of these lines, and press Enter:
print ("Synchronous sound demo");
call func()
begin
for i := 0 to 20 do
PlaySoundSync(ROM_simplebeep);
end with();
print ("Async sound demo");
call func()
begin
for i := 0 to 20 do
PlaySoundSync(ROM_simplebeep);
end with();
The synchronous sound playback example plays the
ROM_simplebeep
sound
twenty times; the sound plays its entire length each time. Twenty repetitions may
seem a bit laborious until you hear how quickly the same calls are made in
asynchronous mode.
Note that the asynchronous version can call the sound chip so fast that the sound
does not have enough time to finish playing; as a result, part of the playback is