How To Control Sound

This intermediate level how to explains the process of controlling sound effects in your SwinGame project. Sound Effects can be played, paused, volume controlled, rewound and set to play continuously. The Audio API explains these functions.

howtocontrolsound

Level

Intermediate

Read these first

How To Play Sound Effect

Details

In your SwinGame project you will often need to play a sound effect. Once your audio files have been loaded you can control properties such as looping and volume. You can test if the effect is playing, has started or stopped.

To be thorough, you can test the audio capacity of the device you are using and let SwinGame test for audio on the device by calling TryOpenAudio.

The code below shows a number of sound effects being loaded. It also uses Input to control them. This is a good program to highlight both the use of Audio and Input.

Source Code Widget

  • program HowToControlSound;
    uses SwinGame,sgTypes;
    
    procedure Main();
    var
        sndEffect : SoundEffect;
    begin
        OpenAudio();
        OpenGraphicsWindow('How To Control Sound' ,320 ,240 );
        LoadDefaultColors();
        sndEffect := LoadSoundEffect('chipmunk.ogg' );
        LoadSoundEffect('bells.ogg' );
        LoadSoundEffect('camera.ogg' );
        LoadSoundEffect('comedy_boing.ogg' );
        LoadSoundEffect('dinosaur.ogg' );
        LoadSoundEffect('dog_bark.ogg' );
        repeat
            ProcessEvents();
            if  KeyDown(vk_RCTRL )  or KeyDown(vk_LCTRL )    then
            begin
                if  KeyTyped(vk_1 )    then
                    sndEffect := SoundEffectNamed('chipmunk.ogg' );
                if  KeyTyped(vk_2 )    then
                    sndEffect := SoundEffectNamed('bells.ogg' );
                if  KeyTyped(vk_3 )    then
                    sndEffect := SoundEffectNamed('camera.ogg' );
                if  KeyTyped(vk_4 )    then
                    sndEffect := SoundEffectNamed('comedy_boing.ogg' );
                if  KeyTyped(vk_5 )    then
                    sndEffect := SoundEffectNamed('dinosaur.ogg' );
                if  KeyTyped(vk_6 )    then
                    sndEffect := SoundEffectNamed('dog_bark.ogg' );
            end 
            else
            begin
                if  KeyTyped(vk_1 )    then
                    PlaySoundEffect(sndEffect );
                if  KeyTyped(vk_2 )    then
                    PlaySoundEffect(sndEffect ,0.5 );
                if  KeyTyped(vk_3 )    then
                    PlaySoundEffect(sndEffect ,3 ,0.25 );
                if  KeyTyped(vk_4 )    then
                    PlaySoundEffect(sndEffect ,-1 ,0.1 );
                if  KeyTyped(vk_5 )    then
                    if  SoundEffectPlaying(sndEffect )    then
                        StopSoundEffect(sndEffect );
            end;
            ClearScreen(ColorWhite );
            DrawText('Control Sound (Escape or q to quit)' ,ColorRed ,'Arial' ,18 ,15 ,15 );
            DrawText('1: Play Sound At Full Volume' ,ColorBlue ,'Arial' ,14 ,20 ,50 );
            DrawText('2: Play Sound At 50% Volume' ,ColorBlue ,'Arial' ,14 ,20 ,80 );
            DrawText('3: Play Sound At 25% Volume 3 Times' ,ColorBlue ,'Arial' ,14 ,20 ,110 );
            DrawText('4: Play Sound Continuously at 10%' ,ColorBlue ,'Arial' ,14 ,20 ,140 );
            DrawText('5: Stop Playing Current Sound' ,ColorBlue ,'Arial' ,14 ,20 ,170 );
            DrawText('CTRL + (1-6) load different sound effects' ,ColorBlue ,'Arial' ,14 ,20 ,200 );
            RefreshScreen(60 );
        
        until WindowCloseRequested()  or KeyTyped(vk_ESCAPE )  or KeyTyped(vk_q );
        CloseAudio();
        ReleaseAllResources();
    end;
    begin
        Main();
    end.
    Download Now"
  • #include 
    #include "SwinGame.h"
    int main() 
    {
        sound_effect  snd_effect;
        open_audio();
        open_graphics_window("How To Control Sound" ,320 ,240 );
        load_default_colors();
        snd_effect = load_sound_effect("chipmunk.ogg" ) ;
        load_sound_effect("bells.ogg" );
        load_sound_effect("camera.ogg" );
        load_sound_effect("comedy_boing.ogg" );
        load_sound_effect("dinosaur.ogg" );
        load_sound_effect("dog_bark.ogg" );
        do 
        {
            process_events();
            if ( key_down(VK_RCTRL ) || key_down(VK_LCTRL )  )
            {
                if ( key_typed(VK_1 )  )
                    snd_effect = sound_effect_named("chipmunk.ogg" ) ;
                if ( key_typed(VK_2 )  )
                    snd_effect = sound_effect_named("bells.ogg" ) ;
                if ( key_typed(VK_3 )  )
                    snd_effect = sound_effect_named("camera.ogg" ) ;
                if ( key_typed(VK_4 )  )
                    snd_effect = sound_effect_named("comedy_boing.ogg" ) ;
                if ( key_typed(VK_5 )  )
                    snd_effect = sound_effect_named("dinosaur.ogg" ) ;
                if ( key_typed(VK_6 )  )
                    snd_effect = sound_effect_named("dog_bark.ogg" ) ;
            }
            else
            {
                if ( key_typed(VK_1 )  )
                    play_sound_effect(snd_effect );
                if ( key_typed(VK_2 )  )
                    play_sound_effect(snd_effect ,0.5 );
                if ( key_typed(VK_3 )  )
                    play_sound_effect(snd_effect ,3 ,0.25 );
                if ( key_typed(VK_4 )  )
                    play_sound_effect(snd_effect ,-1 ,0.1 );
                if ( key_typed(VK_5 )  )
                    if ( sound_effect_playing(snd_effect )  )
                        stop_sound_effect(snd_effect );
            }
            clear_screen(color_white );
            draw_text("Control Sound (Escape or q to quit)" ,color_red ,"Arial" ,18 ,15 ,15 );
            draw_text("1: Play Sound At Full Volume" ,color_blue ,"Arial" ,14 ,20 ,50 );
            draw_text("2: Play Sound At 50% Volume" ,color_blue ,"Arial" ,14 ,20 ,80 );
            draw_text("3: Play Sound At 25% Volume 3 Times" ,color_blue ,"Arial" ,14 ,20 ,110 );
            draw_text("4: Play Sound Continuously at 10%" ,color_blue ,"Arial" ,14 ,20 ,140 );
            draw_text("5: Stop Playing Current Sound" ,color_blue ,"Arial" ,14 ,20 ,170 );
            draw_text("CTRL + (1-6) load different sound effects" ,color_blue ,"Arial" ,14 ,20 ,200 );
            refresh_screen(60 );
        }
        while ( ! (window_close_requested() || key_typed(VK_ESCAPE ) || key_typed(VK_Q ) ) );
        close_audio();
        release_all_resources();
        return 0;
    }
    
  • Source Code Coming Soon

  • Source Code Coming Soon

  • Source Code Coming Soon

Under Windows a window need to be open in order for it to play audio

Possibilities

  • Create special sound effect in game such as explosion, bullet flying.
  • Interactivity between player and in game object using sound effect.
  • Allow your game program to play different sound at different volume more than one.