Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Ambient audio

Discussion in 'Audio & Video' started by Khena_B, Dec 16, 2016.

  1. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    Hey,

    I'm working on a 2D platformer and each rooms are new scenes, to prevent the ambient sounds from getting cut off during scene transitions i created a persistent level that is always active, and i've placed the ambient audio sources there.

    My issue is that having the whole game's ambient sounds loaded in the memory sounds like a bad idea, and even if i only activated the audiosources when in range, the inactive audiosources would still have their audioclip loaded into the memory.

    Do you guys have any suggestions?
     
  2. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    I have a feeling nobody understands my issue so i made a small animation

     
    SuperNeon likes this.
  3. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,021
    You could not have them in the scene, and start loading them as you approach with Resources.LoadAsync (like a scene earlier?).

    (also, I know Unity wants everyone to use asset bundles these days, but until they make a decent workflow for them, I'll keep ignoring them... :p )
     
  4. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    Are you suggesting that i pre-load the other scenes that are connected to the current scenes so that i can hear the audio sources that are in the next room?

    That sounds like it could work but i'd have to figure out how to do that, right now when i exit a scene i load the next scene additively but i don't activate it, then i fade the camera out to black, when the next scene is done loading i unload the current scene then activate the next scene and fade back in, it happens really quickly but i always thought it would be better to pre-load the next scene in advance to always have an instant scene transition
     
  5. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,021
    You could also do nothing. Are you having issues with memory usage, because if you're not, you could get away with your current setup.
     
  6. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    Well right now i'm not having issues because i'm still working on the core mechanics and i only have a few rooms, but when i get hundreds of rooms it could be an issue, but again maybe not, it all depends on how many ambient sounds i use and the size of these sounds, still i'd prefer to not have the sounds from the end-game's areas loaded up when at the beginning of the game, it doesn't sound right
     
  7. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,302
    How many ambient audio clips do you have in a room, potentially playing at the same time?
     
  8. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    Right now i have an audio manager with pooling that handles the SFX and the music. I have my pooling size set to 32 maximum audio sources.

    For the ambient sounds it depends on how i set this up, there is going to be the general ambience of the area for example a cave background sound or a forest background sound, and then some object ambient sounds like a waterfall, sparks coming out of broken wires or a windy zone etc...

    But the thing is, if i transition to an indoor area, i don't want the forest ambient sounds to just stop abruptly when entering an indoor zone, I want it to gradually become inaudible as i move away from the zone, this is why placing 3D audio sources on a persistent level is the first idea that comes to mind.

    Someone suggested that i use Resources.Load to load audio clips when in hearing range and not have them in memory when not in range.
     
  9. hopeful

    hopeful

    Joined:
    Nov 20, 2013
    Posts:
    5,628
    I'm sure there are many solutions to this. Maybe a neighbor-loading strategy using a table? That way, when you're in a scene, it holds the audio for previous and next scenes as well as the current one. And use additive scenes ...?
     
  10. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    I'm not sure i understand your suggestion, are you suggesting that i load the audio in the neighboring rooms as well as the current room somehow?

    I'll have to try some of the suggestions but first i have to figure out how to pre-load all of the neighboring rooms to ensure an instant transition between them as right now it's loading when i get to the exit point and it could take longer than expected if there's a lot of assets in the next room resulting in the player waiting with a black screen. I'm able to load multiple scenes additively and set allowSceneActivationto false, but from there i don't know how to only pick and activate one scene and cancel the others, anyways that's another topic.

    Thanks for the help.
     
  11. hopeful

    hopeful

    Joined:
    Nov 20, 2013
    Posts:
    5,628
    Yes. From my read of the description of how you've organized things, it sounds like you need to play audio from the previous scene and also from the next scene while in the current scene. Or something like that. So if you have the audio physically separated out from each scene - such that you have scene_2 and scene_2_audio, for example - you could add the audio scenes separately, and have loaded the audio for scenes 1-2-3 at the same time, using additive scenes. Or something like that. I'm sure there's many ways of going about it, depending on the details of your game. A neighbor-loading approach would probably require you to keep a table or grid of some sort that maps out in advance which scenes are neighbors to one another. So as the player moves in the game, they would also traverse this audio neighbor map, which could then additively async load and unload the required audio scene neighbors.

    If the player is in scene A and moving to scene B, the audio for scene A and B would already be in memory, so the transition from A to B and back would be smooth. And when the player enters scene B, they'd be loading the audio for C in the background, hopefully before the player moves across to the border with C when it is needed. Something like that.

    You might also want to have a delay timer on the audio scene unload, as you wouldn't want the player to thrash the audio load/unload mechanism by quickly moving back and forth between the same two game scenes. Like, if the player moves from B to C, the game would unload A and start to load the audio for D in the background. But if the player steps back into B, then the audio for D would be told to unload itself and A would have to load. A player who for whatever reason is quickly moving back and forth from C-B-C, etc. - like if they are being chased back and forth - would be thrashing the audio loader. So if you were thinking of having the previous and next audio loaded in addition to the current, there would probably be one more audio scene loaded on certain occasions (before it unloads based on time delay). In other words, if the player is in C, you'd have neighbors B and D loaded, and maybe one more scene (like A) would be on delayed unload. If the player goes back to B before the timer has elapsed, then the countdown for A would be switched off, and D would go onto delayed unload.

    There's all kinds of specific ways of doing this, but it sounds to me like you'd want to implement some variation on this approach.
     
    Last edited: Dec 18, 2016
  12. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    Thanks for the detailed explanation, I'll give every suggestions a try and see what works the best.

    Much appreciated
     
  13. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    Someone has suggested unchecking Preload Audio Data on the ambient clips, and from script i can call AudioClip.LoadAudioData and AudioClip.UnloadAudioData to load and unload the clip from memory, this should work with a trigger and allow me to have all the ambient sounds on a single persistent layer/scene.
     
    hopeful likes this.
  14. Voronoi

    Voronoi

    Joined:
    Jul 2, 2012
    Posts:
    571
    Is there a possibility the player will return to the previous room? If not, you could have the audio source set to don't destroy on load. Have it fade out in the new scene and then destroy itself.
     
  15. Khena_B

    Khena_B

    Joined:
    Aug 21, 2014
    Posts:
    273
    Yes, it's a free roaming game like Metroid.
     
  16. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    It was I will suggest too, also with trigger you can easily cross-fade between two ambiances depending of the position of the listener inside the box.
     
    Khena_B likes this.