Seamlessly changing background music loops

  • #1, by trepnMonday, 11. June 2018, 22:25 2 years ago
    Is it possible to change backgroundmusic loops seamlessly? Like the iMuse does in Monkey Island 2.

    Newbie

    60 Posts


  • #2, by afrlmeMonday, 11. June 2018, 23:17 2 years ago
    I believe changing the background music causes it to fade out the current track & fade in the next. The only method I can think of is to play them as sounds. You could play it once then use a wait until sound is finished then do an if query to check if it should play again or change to something else.

    Actually the new sound engine has some interesting things, you can assign sounds to specific mix containers. Play & loop a bunch of sounds all at the same time & then fade in/out the mix containers as needed. I believe this is what you are after? I just googled monkey 2 & imuse & it seems to be different variations of the same song.

    Quick note: fading to next scene instead of instantly changing will automatically fade out & kill all sounds - except background music if the next scene has the continue music from previous scene option enabled or the same audio file as the previous scene linked to it - so if you want to be able to seamlessly change between scenes while having the audio files carry on playing then you need to change to the next scene instantly & if you want to fade the scene then you will have to do it manually with a shader, an image or interface or the recently added start color overlay action part.

    Imperator

    6842 Posts

  • #3, by afrlmeTuesday, 12. June 2018, 23:06 2 years ago

    Had a little mess around. I didn't bother searching for too long for variations of the same music track to test with but found 2 on dig.ccmixter.org that are basically different versions of the same song. Anyway...

    There would be a perfect solution for fading one track into the next if the fade sound action part actually worked the way I expected it to work. It can fade from whatever the current volume value is to a value higher or lower, but if you try using it again on the same sound then it seems to fade the sound out completely regardless of what value you use.

    Next I tried to wrap my head around the new sound engine & mixer container things, but I couldn't figure out how to tweak the volume values for them with Lua script. I just kept generating issues.

    Finally I decided to try the openAL sound engine via Lua script & I managed to get that working after a bit of hassle.

    C (released) will toggle between scene 101 & 102.
    T (released) will toggle between song A  & song B.
    Q (released) will quit the game.

    P.S: like I said, I didn't have perfect tracks to test with like the ones in monkey island 2, so the timings aren't perfect between the 2 tracks, but at least you can see that it's possible to play multiple tracks at the same time, toggle between scene while the music carries on playing & fade one track into another. Hopefully Simon can make the fade sound action part do what I expected it to do or add an action part for tweening sound volumes without stopping the sound after fading to the new value.

    * edit: managed to find an mp3 version of the woodtick tune from monkey 2. I used Audacity to chop up 2 segments of the track with the same duration. I also managed to get the Mix busses to work in the new sound engine, though they gave me a bit of grief for a while as I couldn't seem to get the first track to fade out as it wasn't registering the first automation I added to it. For some reason it started working after I created a second automation. It still worked after deleting the second automation, so all good now I guess. smile


    I didn't add the flourish transitions, but I assume they can be done via the sync/switch containers, but I have no idea how to use them at the minute.

    Imperator

    6842 Posts

  • #4, by stered86Saturday, 16. June 2018, 09:32 2 years ago
    That's. EXACTLY. What I was trying to achieve.

    I'll test it on my game right away, thank you very much!

    Newbie

    62 Posts

  • #5, by trepnMonday, 02. July 2018, 17:23 2 years ago
    Sorry bit late reply. Tnx for the examples!

    It's good to see that multiple tracks can be played. But I could fix it with only using one channel. I will look if I can make an example file later to share, for now here the piece of code that does seamless changing of loops between two scenes:

    local currentMusicId = nil

    local function checkMusicEnded()
      --keep checking every cycle for music end and if so start new loop
      if getSoundProperty(currentMusicId, "playing") == false then
        if game.CurrentScene:getName() == "camping_campsite" then
          currentMusicId = startSound('vispath:assets/music/loop_a.wav', {flags=1, loop=true})
        else
          currentMusicId = startSound('vispath:assets/music/loop_b.wav', {flags=1, loop=true})
        end
        removeMainLoopListener(checkMusicEnded)
      end
    end

    local function sceneChangedListener(oldScene, newScene)
      if newScene == "camping_campsite" then
        if currentMusicId == nil then
          currentMusicId = startSound('vispath:assets/music/loop_a.wav', {flags=1, loop=true})
        else
          setSoundProperty(currentMusicId, {flags=1, loop=false})
          --custom method that calls the passed method every main loop
          addMainLoopListener(checkMusicEnded)
        end
      else
        if currentMusicId == nil then
          currentMusicId = startSound('vispath:assets/music/loop_b.wav', {flags=1, loop=true})
        else
          setSoundProperty(currentMusicId, {flags=1, loop=false})
          --custom method that calls the passed method every main loop
          addMainLoopListener(checkMusicEnded)
        end
      end
    end

    --custom method that calls the passed method when the scene has changed
    addSceneChangedListener(sceneChangedListener)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36

    Newbie

    60 Posts

  • #6, by afrlmeMonday, 02. July 2018, 18:52 2 years ago
    Hmm, you asked for imuse example. That's essentially what I provided, but I didn't use the sync container in the new audio engine because I still not all clued up on how it all works.

    The problem with using the openAL sound engine via Lua script is that all sounds you play through it are linked to the sound channel. There's no way to specify if it should belong to the music channel or speech channel.

    I believe with Simon's new system & the different container types that you should be able to easily sync up tracks - on a end start b, etc, or cross-fade between 2 tracks while keeping the current playback time the same on both tracks.

    Imperator

    6842 Posts

  • #7, by trepnMonday, 02. July 2018, 23:25 2 years ago
    I've had a talk with the music composer and I think I still need to use channels. My solution can create weird transistion when the player moves over 3 screens during one loop each having their own loop. Then loop 1 will be stitched to loop 3 wich might not work. So It would be better to have them in seperate channels and get them sync and fade over at each scene change. 
    Is the new system documented yet? Or can I use the current methods of sound position and start the new loop on that position?

    Newbie

    60 Posts

  • #8, by afrlmeTuesday, 03. July 2018, 01:02 2 years ago
    I've had a talk with the music composer and I think I still need to use channels. My solution can create weird transistion when the player moves over 3 screens during one loop each having their own loop. Then loop 1 will be stitched to loop 3 wich might not work. So It would be better to have them in seperate channels and get them sync and fade over at each scene change. 
    Is the new system documented yet? Or can I use the current methods of sound position and start the new loop on that position?
    No it's not documented yet. Simon gave me a couple of sentence brief run through the other week on the sync container, but I can't remember what he said off the top of my head. I think you have to create the sounds inside of the new sound engine section of the editor, then link them to a sync container & then use a switch container to determine which sound/song should be playing. I think you use a value to switch which song is playing - you create the values in the sound section bit. That's about all I know so far. Maybe have a play around & see if you can figure it out?

    Imperator

    6842 Posts

  • #9, by blablo_101Wednesday, 10. October 2018, 17:26 2 years ago
    I used the first example that AFRLme put, and really I love it. 

    I would like to control the volume of the music from the options menu of my game but now they are part of the Sound track.

    Is there any way to connect those sounds to the Music track?


    Newbie

    69 Posts

  • #10, by afrlmeWednesday, 10. October 2018, 17:56 2 years ago
    There is a drop down menu in the play sound action parts that lets you specify which audio bus the sound should play through. If you set it to the music bus then the volume will be dependent on the volume you specified & will also be affected by the global music volume - like regular background music.

    Are you using Lua example or the second example I uploaded? Please check the second dropbox link at the bottom of the thread. I used action parts for it.

    Imperator

    6842 Posts

  • #11, by blablo_101Thursday, 11. October 2018, 19:17 2 years ago
    Checked. 
    It works great .

    Thanks AFRLme!

    Newbie

    69 Posts

Write post