Small Steps II — Making Things

Play Sounds

Play Sounds

Explore one topic in small steps. Try the examples one at a time and take a break whenever you need. Choose how many extra exercises you want to try.

Playing sound effects

What you will learn

Playing sound takes time. After a function call, the program may continue to its next task before the sound ends.

Try it

Example · example1.cpp
void small_main()
{
    print("Pop!");
    play_sound(Sound::Pop);

    sleep(0.5);

    print("Coin!");
    play_sound(Sound::Coin);

    sleep(1.0);
}

play_sound starts playback and returns immediately. Sound::Pop and Sound::Coin name built-in sound effects.

The example uses sleep to keep the program from ending immediately. Listen for the two effects starting in sequence.

Waiting for a sound to finish

What you will learn

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

Try it

Example · sound_2_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

Play Click, Coin, and Win in order. Start each sound only after the previous one finishes.

Exercise starter · exercise1_starter.cpp
void small_main()
{
    // Play Click, Coin, and Win in order.
}

Hint

Use play_sound_and_wait three times.

show solution
void small_main()
{
    play_sound_and_wait(Sound::Click);
    play_sound_and_wait(Sound::Coin);
    play_sound_and_wait(Sound::Win);
}
Try More (Optional)

Exercise

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

Exercise starter · sound_2_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);
}

Your own WAV sounds

You can also pass a file path: play_sound("sound.wav");. Use a 16-bit PCM WAV file (mono or stereo). Put it in the program folder; add it with Publish → Add Files when sharing. The first call loads in the background and later calls reuse the cached sound. For timing-sensitive effects, optionally call preload_sound("sound.wav"); before gameplay. This waits for loading without playing. play_sound_and_wait("sound.wav"); waits for playback.

void small_main()
{
    preload_sound("sound.wav"); // Optional
    play_sound("sound.wav");
    print("The program keeps running.");
    sleep(1);
    play_sound_and_wait("sound.wav");
}

A ready-to-run project with an original sound.wav is included in examples/projects/WavSound. Open that folder as a project.

← Previous lessonNext lesson →