Dynamic music

  • #1, by Simon_ASATuesday, 07. July 2015, 16:48 9 years ago
    Hi, until now in my games I used a standard looping soundtrack for each environment. So I used to use the background music in the scene properties in VS. No problem with this, working fine.
    However I'd like to improve this, and I tried to create an action script in order to choose randomly between 3 musics, and add a pause of x seconds between each music. It's working well and I'd love to use this in all the game.

    Unfortunately I have a big problem : with this action script, the music is stopped everytime I change of scene ! I have been working 2 days trying to fix this and I tried a lot of things, but nothing worked.
    I was wondering if there is a way to solve this with lua?
    For example : can I get the current playing music, save the current playtime before I quit the scene, and then load it again at the exact same playtime in the next scene? Might create a pause in the music, but I thought it might still work. However I don't know how to do that.

    Any other ideas?

    Great Poster

    321 Posts


  • #2, by afrlmeTuesday, 07. July 2015, 16:54 9 years ago
    Yes if you sort out the entire music stuff with the Lua script openAL sound engine functions. There would be a pause between music playing between scenes. Also the saved offset might be a little off because it's likely the music would have carried on briefly before the scene changes so it would end up jumping backwards slightly.

    I believe though that it's possible to script the currently linked background music file for each scene. You could just keep swapping that out & before end of scene get current background music & replace the one in the next scene with current one, which should allow music to continue fluidly I think. Well it's a theory at this point, but it would be interesting to see if it works.

    Imperator

    7278 Posts

  • #3, by Simon_ASATuesday, 07. July 2015, 17:53 9 years ago
    I believe though that it's possible to script the currently linked background music file for each scene. You could just keep swapping that out & before end of scene get current background music & replace the one in the next scene with current one, which should allow music to continue fluidly I think.


    I like this idea quite a lot, but how can I script the background music specifically?

    Great Poster

    321 Posts

  • #4, by afrlmeTuesday, 07. July 2015, 18:35 9 years ago
    Here's a definition script with a few functions.
    local bgm = nil
    
    -- * function for storing background music * --
    function getBGM()
     bgm = game.CurrentScene.BackgroundMusic
    end
    
    -- * function for replacing background music of specified scene with stored background music * -- 
    function setBGM(s)
     if bgm ~= nil then Scenes[s].BackgroundMusic = bgm end
    end
    
    -- * function for setting a new background music manually for a specified scene * --
    function newBGM(s, p)
     Scenes[s].BackgroundMusic = p
    end
    

    Now here is how to use them (via execute a script actions)

    Storing currently playing bg music...
    getBGM()
    

    Replacing specified scenes bg music with recently stored music...
    setBGM("scene_name")
    

    Replacing specified scenes bg music by directly linking to a new audio file (relative to .ved path)
    newBGM("scene_name", "data/music/concerto.ogg")
    


    Simple enough no? If you replace the background music of next scene with current scenes background music before changing to the next scene, then it should continue without pausing. (untested).

    Imperator

    7278 Posts

  • #5, by Simon_ASATuesday, 07. July 2015, 18:39 9 years ago
    You're amazing, many thanks!
    I will try this asap and I will post my feedback to let you know if it works.

    Great Poster

    321 Posts

  • #6, by Simon_ASAWednesday, 08. July 2015, 11:45 9 years ago
    Well, your script is working fine, but it doesn't work in my case roll
    The problem is that I use the background music in the scene property for the looping sound ambiance (rain, etc).
    Why? Because I don't use it for the music. In order to have dynamc music as explained before, I am using the action to play a sound.

    Here basically is the action script I use:

    I create a random value named "choice" that goes between 1 and 3 because I have 3 musics

    I create a random value called "random_music" with a value between 20 and 60 (because I want the music to stop randomly between 20 and 60 seconds)

    
    if value "choice" = 1
     play sound landing.ogg
     wait until sound landing.ogg is finished
     pause (for "random_music" seconds)
     jump to action part #1
    end if
    
    if value "choice" = 2
     play sound stinger1.ogg
     wait until sound stinger1.ogg is finished
     pause (for "random_music" seconds)
     jump to action part #1
    end if
    
    if value "choice" = 3
     play sound stinger2.ogg
     wait until sound stinger2.ogg is finished
     pause (for "random_music" seconds)
     jump to action part #1
    end if
    


    This script is working very well, but it is not using the background music channel. And the problem is that the music is stopped everytime I change of scene roll

    Maybe I can put the music again in the background music property and use your script, then I could use this action script to play the ambiant sound. There would still be a pause between each scene though.

    Great Poster

    321 Posts

  • #7, by afrlmeWednesday, 08. July 2015, 13:08 9 years ago
    For me, what would be more ideal is if we could specify say 4 background music files for each scene. That way we can cover environmental sounds too. The data structure would have to be reworked though, so I'm not sure if the devs would be willing to do that. Failing that, they might be able to add some kind of setting to the scene properties tab, that allows you to determine if sounds playing in previous scene should keep playing into the next scene.

    The only thing I can think for you to do is to combine your environment sounds into your music sounds & then when music isn't playing you switch it purely back to the environment sounds. It's a bit of a crude method though I think.

    Imperator

    7278 Posts

  • #8, by Simon_ASAWednesday, 08. July 2015, 14:39 9 years ago
    Yes it would be perfect if we could manage around 4 background audio files.

    The problem of mixing music and sound effects is that I can't play the music randomly or the sfx will stop between each music: when the first random_music ends, the sfx will not continue during the silence.

    I was wondering : could there be a way to use the VS audio player like an audio player in the background? I mean: starting any music in the background of the game (as if it was playing in Windows Media Player). If I could manage my own playlist in the background and start/stop it when I want, it would be fine, because it would be independant of the scenes.
    That is what I was trying to do with the above action script, but unfortunately it stops when I change of scene. It seems that except for the problem of scene changing, there is everything we need in Visionaire.

    If I tried a lua script to reproduce the action script above, could it work?

    Great Poster

    321 Posts

  • #9, by afrlmeWednesday, 08. July 2015, 15:06 9 years ago
    You can use os.execute() function to launch external files. I don't know about actually getting it to play a specific file in windows media player or whatever, though. The main issue with os.execute is that it minimizes the game to desktop to open the executable instead of doing it in the background.

    Imperator

    7278 Posts

  • #10, by Simon_ASAWednesday, 08. July 2015, 22:47 9 years ago
    Hmm it still doesn't work with the os.execute. However I think I have found a solution with the background music. Is there a way to constantly refresh an action script? I would like to always check the script (every 1 or 2 seconds) instead of checking only once at the beginning of the scene.

    Great Poster

    321 Posts

  • #11, by afrlmeWednesday, 08. July 2015, 23:34 9 years ago
    Yes you could create a loop in the editor using the jump to x action part function or you could create a mainLoop event function with Lua script.

    If you use the editor method then you need to make sure you create it inside of a called by other action block & quit the action inside of an at end of scene action to kill the loop.

    Imperator

    7278 Posts