How To Control Music

This article will show you how to manipulate music in SwinGame.

howtocontrolmusic

Level

Intermediate

Read these first

How To Play Music

Details

Music can be used to highlight important points in your game to great effect. SwinGame includes a number of features that mean you can control the music in your game.

Once your music has been loaded into your project you can play it by calling it. Many of the Audio functions allow you to control volume, fade music in and out, pause and resume the track.

Only a single music file can be played at one time (different to sound effects)

The screen shot to above shows how to control the example shown in this how to. Download and run it while working through the code. This is a good example of using both Audio and Input in your SwinGame project.

Source Code Widget

  • program HowToControlMusic;
    uses SwinGame,sgTypes;
    
    procedure Main();
    
    begin
        OpenAudio();
        OpenGraphicsWindow('How To Control Music' ,320 ,240 );
        LoadDefaultColors();
        LoadMusic('game.ogg' );
        LoadMusic('diving-turtle.mp3' );
        LoadMusic('fast.mp3' );
        LoadMusic('gentle-thoughts-1.mp3' );
        LoadMusic('morning-workout.mp3' );
        LoadMusic('saber.ogg' );
        SetMusicVolume(1 );
        repeat
            ProcessEvents();
            ClearScreen(ColorWhite );
            if  KeyDown(vk_RCTRL )  or KeyDown(vk_LCTRL )    then
            begin
                if  KeyTyped(vk_1 )    then
                    FadeMusicIn('game.ogg' ,-1 ,10000 );
                if  KeyTyped(vk_2 )    then
                    FadeMusicIn('fast.mp3' ,-1 ,10000 );
                if  KeyTyped(vk_3 )    then
                    FadeMusicIn('gentle-thoughts-1.mp3' ,-1 ,10000 );
                if  KeyTyped(vk_4 )    then
                    FadeMusicIn('morning-workout.mp3' ,-1 ,10000 );
                if  KeyTyped(vk_5 )    then
                    FadeMusicIn('saber.ogg' ,-1 ,10000 );
                if  KeyTyped(vk_6 )    then
                    FadeMusicIn('diving-turtle.mp3' ,-1 ,10000 );
            end 
            else
                if  KeyDown(vk_RALT )  or KeyDown(vk_LALT )    then
                begin
                    if  KeyTyped(vk_1 )    then
                        PlayMusic('game.ogg' );
                    FadeMusicOut(10000 );
                    if  KeyTyped(vk_2 )    then
                        PlayMusic('fast.mp3' );
                    FadeMusicOut(10000 );
                    if  KeyTyped(vk_3 )    then
                        PlayMusic('gentle-thoughts-1.mp3' );
                    FadeMusicOut(10000 );
                    if  KeyTyped(vk_4 )    then
                        PlayMusic('morning-workout.mp3' );
                    FadeMusicOut(10000 );
                    if  KeyTyped(vk_5 )    then
                        PlayMusic('saber.ogg' );
                    FadeMusicOut(10000 );
                    if  KeyTyped(vk_6 )    then
                        PlayMusic('diving-turtle.mp3' );
                    FadeMusicOut(10000 );
                end 
                else
                begin
                    if  KeyTyped(vk_1 )    then
                        PlayMusic('game.ogg' );
                    if  KeyTyped(vk_2 )    then
                        PlayMusic('fast.mp3' );
                    if  KeyTyped(vk_3 )    then
                        PlayMusic('gentle-thoughts-1.mp3' );
                    if  KeyTyped(vk_4 )    then
                        PlayMusic('morning-workout.mp3' );
                    if  KeyTyped(vk_5 )    then
                        PlayMusic('saber.ogg' );
                    if  KeyTyped(vk_6 )    then
                        PlayMusic('diving-turtle.mp3' );
                    if  KeyTyped(vk_p )    then
                        PauseMusic();
                    if  KeyTyped(vk_r )    then
                        ResumeMusic();
                    if  KeyTyped(vk_KP_PLUS )  and (MusicVolume()  < 1 )   then
                        SetMusicVolume(MusicVolume()  + 0.1 );
                    if  KeyTyped(vk_KP_MINUS )  and (MusicVolume()  > 0 )   then
                        SetMusicVolume(MusicVolume()  - 0.1 );
                    if  KeyTyped(vk_s )    then
                        if  MusicPlaying()    then
                            StopMusic();
                end;
            DrawText('Control Music (Escape or q to quit)' ,ColorRed ,'Arial' ,18 ,15 ,15 );
            DrawText('1-6 to play different music' ,ColorBlue ,'Arial' ,14 ,20 ,50 );
            DrawText('CTRL + (1-6) to Fade Music In' ,ColorBlue ,'Arial' ,14 ,20 ,75 );
            DrawText('Alt + (1-6) to Fade Music Out' ,ColorBlue ,'Arial' ,14 ,20 ,100 );
            DrawText('p to pause music' ,ColorBlue ,'Arial' ,14 ,20 ,125 );
            DrawText('r to resume music' ,ColorBlue ,'Arial' ,14 ,20 ,150 );
            DrawText('+ or - increase volume by 10%' ,ColorBlue ,'Arial' ,14 ,20 ,175 );
            DrawText('s to stop playing music' ,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() 
    {
    
        open_audio();
        open_graphics_window("How To Control Music" ,320 ,240 );
        load_default_colors();
        load_music("game.ogg" );
        load_music("diving-turtle.mp3" );
        load_music("fast.mp3" );
        load_music("gentle-thoughts-1.mp3" );
        load_music("morning-workout.mp3" );
        load_music("saber.ogg" );
        set_music_volume(1 );
        do 
        {
            process_events();
            clear_screen(color_white );
            if ( key_down(VK_RCTRL ) || key_down(VK_LCTRL )  )
            {
                if ( key_typed(VK_1 )  )
                    fade_music_in("game.ogg" ,-1 ,10000 );
                if ( key_typed(VK_2 )  )
                    fade_music_in("fast.mp3" ,-1 ,10000 );
                if ( key_typed(VK_3 )  )
                    fade_music_in("gentle-thoughts-1.mp3" ,-1 ,10000 );
                if ( key_typed(VK_4 )  )
                    fade_music_in("morning-workout.mp3" ,-1 ,10000 );
                if ( key_typed(VK_5 )  )
                    fade_music_in("saber.ogg" ,-1 ,10000 );
                if ( key_typed(VK_6 )  )
                    fade_music_in("diving-turtle.mp3" ,-1 ,10000 );
            }
            else
                if ( key_down(VK_RALT ) || key_down(VK_LALT )  )
                {
                    if ( key_typed(VK_1 )  )
                        play_music("game.ogg" );
                    fade_music_out(10000 );
                    if ( key_typed(VK_2 )  )
                        play_music("fast.mp3" );
                    fade_music_out(10000 );
                    if ( key_typed(VK_3 )  )
                        play_music("gentle-thoughts-1.mp3" );
                    fade_music_out(10000 );
                    if ( key_typed(VK_4 )  )
                        play_music("morning-workout.mp3" );
                    fade_music_out(10000 );
                    if ( key_typed(VK_5 )  )
                        play_music("saber.ogg" );
                    fade_music_out(10000 );
                    if ( key_typed(VK_6 )  )
                        play_music("diving-turtle.mp3" );
                    fade_music_out(10000 );
                }
                else
                {
                    if ( key_typed(VK_1 )  )
                        play_music("game.ogg" );
                    if ( key_typed(VK_2 )  )
                        play_music("fast.mp3" );
                    if ( key_typed(VK_3 )  )
                        play_music("gentle-thoughts-1.mp3" );
                    if ( key_typed(VK_4 )  )
                        play_music("morning-workout.mp3" );
                    if ( key_typed(VK_5 )  )
                        play_music("saber.ogg" );
                    if ( key_typed(VK_6 )  )
                        play_music("diving-turtle.mp3" );
                    if ( key_typed(VK_P )  )
                        pause_music();
                    if ( key_typed(VK_R )  )
                        resume_music();
                    if ( key_typed(VK_KP_PLUS ) && (music_volume() < 1 )  )
                        set_music_volume(music_volume() + 0.1 );
                    if ( key_typed(VK_KP_MINUS ) && (music_volume() > 0 )  )
                        set_music_volume(music_volume() - 0.1 );
                    if ( key_typed(VK_S )  )
                        if ( music_playing()  )
                            stop_music();
                }
            draw_text("Control Music (Escape or q to quit)" ,color_red ,"Arial" ,18 ,15 ,15 );
            draw_text("1-6 to play different music" ,color_blue ,"Arial" ,14 ,20 ,50 );
            draw_text("CTRL + (1-6) to Fade Music In" ,color_blue ,"Arial" ,14 ,20 ,75 );
            draw_text("Alt + (1-6) to Fade Music Out" ,color_blue ,"Arial" ,14 ,20 ,100 );
            draw_text("p to pause music" ,color_blue ,"Arial" ,14 ,20 ,125 );
            draw_text("r to resume music" ,color_blue ,"Arial" ,14 ,20 ,150 );
            draw_text("+ or - increase volume by 10%" ,color_blue ,"Arial" ,14 ,20 ,175 );
            draw_text("s to stop playing music" ,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;
    }
    
    Download Now"
  • 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

  • Enhance realism and immersion in gaming by playing music, fade in and fade out at different stage of your program.
  • Interactivity between player and in game object using music.