Small Steps II — Making Things

Waiting for a sound to finish

Wait for a sound to finish.

What you will learn

Functions ending in AndWait wait for the sound to finish before returning.

Try it

Example · example2.cpp
void small_main()
{
    beep_and_wait(440, 0.25);
    beep_and_wait(550, 0.25);
    beep_and_wait(660, 0.4);

    play_sound_and_wait(Sound::Win);
}

beep_and_wait's first value is the frequency in Hz, which sets pitch; the second is duration in seconds. Three rising tones at 440, 550, and 660 are followed by a sound effect.

When a game screen must keep moving, non-waiting play_sound or beep is convenient.

Exercise

Use beep_and_wait to play three tones of different frequencies in sequence. Choose any frequencies and durations.

Exercise starter · exercise2_starter.cpp
void small_main()
{
    // Make a three-note sound.
}

Hint

For example, play 440, 550, and 660 Hz for about 0.2 seconds each.

show solution
void small_main()
{
    beep_and_wait(440, 0.2);
    beep_and_wait(550, 0.2);
    beep_and_wait(660, 0.3);
}
← Previous lessonNext lesson →