chunik
New Member
Posts: 17
Posts: 17
|
Post by chunik on Aug 21, 2022 12:29:02 GMT
Hi.
I need to have fine control over ambient sounds. So component doesn't have PlayAndFadeIn, FadeOutAndStop methods. Which is fine, you expect it to work on proximity. But i also want to fade it out for reasons. So i wrote tweens for it:
public void StartPlayingAmbientSound() { if (ambientAudioFadeTween != null && ambientAudioFadeTween.IsPlaying()) { ambientAudioFadeTween.Kill(); }
//ambientSoundComponent.enabled = true; Debug.Log("should play now");
var endValue = 0f; var tweener = DOTween .To(() => ambientSoundComponent.playVolume, x => ambientSoundComponent.playVolume = x, endValue, 1) .SetEase(Ease.Linear).OnUpdate(()=>Debug.Log(ambientSoundComponent.playVolume));; ambientAudioFadeTween = tweener; }
public void StopPlayingAmbientSound() { if (ambientAudioFadeTween != null && ambientAudioFadeTween.IsPlaying()) { ambientAudioFadeTween.Kill(); }
Debug.Log("should play no more"); var endValue = -80f; var tweener = DOTween .To(() => ambientSoundComponent.playVolume, x => ambientSoundComponent.playVolume = x, endValue, 1) .SetEase(Ease.Linear).OnUpdate(()=>Debug.Log(ambientSoundComponent.playVolume)); //tweener.onComplete = TurnOffAmbientComponet; ambientAudioFadeTween = tweener; }
But i have buggy behavior. First i started with commented out functionality that turns on and off component, but it leads to glitches when it doesn't start playing sound again when it should (but can retriger again if i drag sound source around). If i don't turn off component it never stops playing even if volume dial on that thing is showing -90 (although i only tween to -80, and volume dial can show NaN sometimes, but tweer logs correct values).
|
|
|
Post by DarkTonic Dev on Aug 21, 2022 16:32:39 GMT
I believe if you have Tweens on an object that gets disabled it won't complete the Tween. Also you'd need to make sure any Tweens on an object are done when you start another. But I haven't used Tweens for years, you should check with the author of that plugin.
|
|
chunik
New Member
Posts: 17
Posts: 17
|
Post by chunik on Aug 21, 2022 17:37:44 GMT
I believe if you have Tweens on an object that gets disabled it won't complete the Tween. Also you'd need to make sure any Tweens on an object are done when you start another. But I haven't used Tweens for years, you should check with the author of that plugin. So just to make sure i've written test without do tween. And here is a video with demonstration www.youtube.com/watch?v=JWdROMT-oYw, and a script. I'm probably miss understanding something. Again all im trying to do is fade out the sound and fade in the sound on command. Buttons are done using odin. But the same could be made with OnPointerEnter / Exit with ui events.
using System; using System.Collections; using System.Collections.Generic; using System.Timers; using DarkTonic.MasterAudio; using Sirenix.OdinInspector; using UnityEngine; using UnityEngine.EventSystems;
public class TickTest : MonoBehaviour { [SerializeField] private AmbientSound ambienctSoundComponent;
public bool turnedOnNow;
private float totalTime = 1; private float timer; private float startingValue; private float endingValue;
private bool shouldMoveValueAtThisFrame; private void Update() { if (turnedOnNow) { if (!shouldMoveValueAtThisFrame) { return; } timer += Time.deltaTime; float ratio = timer / totalTime; ambienctSoundComponent.playVolume = Mathf.Lerp(startingValue, endingValue, ratio); Debug.Log("turning on the volume " + ambienctSoundComponent.playVolume + " current ratio " + ratio); if (timer >= totalTime) { shouldMoveValueAtThisFrame = false; } } else { if (!shouldMoveValueAtThisFrame) { return; } timer += Time.deltaTime; float ratio = timer / totalTime; ambienctSoundComponent.playVolume = Mathf.Lerp(startingValue, endingValue, ratio); Debug.Log("turning off the volume " + ambienctSoundComponent.playVolume + " current ratio " + ratio); if (timer >= totalTime) { shouldMoveValueAtThisFrame = false; ambienctSoundComponent.enabled = false; } } }
[Button] public void TurnOnNow() { turnedOnNow = true; ambienctSoundComponent.enabled = true; startingValue = ambienctSoundComponent.playVolume; timer = 0; endingValue = 0; totalTime = 2f; shouldMoveValueAtThisFrame = true; }
[Button] public void TurnOffNow() { turnedOnNow = false; startingValue = ambienctSoundComponent.playVolume; endingValue = -80; timer = 0; totalTime = 2f; shouldMoveValueAtThisFrame = true; } }
|
|
|
Post by DarkTonic Dev on Aug 21, 2022 18:55:33 GMT
Before I go testing that, what is the reason you want to fade in and fade out? Unless you are teleporting around the Scene, when you go out of range, the sound would already be nearly inaudible (or possibly fully inaudible). And the same goes for when you enter the farthest point, it will be super quiet. Both cases the fade would be possibly inaudible as well.
Thanks, -Brian
|
|
chunik
New Member
Posts: 17
Posts: 17
|
Post by chunik on Aug 22, 2022 1:33:17 GMT
Before I go testing that, what is the reason you want to fade in and fade out? Unless you are teleporting around the Scene, when you go out of range, the sound would already be nearly inaudible (or possibly fully inaudible). And the same goes for when you enter the farthest point, it will be super quiet. Both cases the fade would be possibly inaudible as well. Thanks, -Brian So this hex shape is playing low volume ambient sound (there can be more then one). www.youtube.com/watch?v=kkIr0uw6wLs (it's actually not in UI, its far away, another camera is looking at it, and transmitting image to render texture in UI). The audio source game object is manually moved in update to be exactly as far away from main camera as it is from the second camera that is looking at hex shape. Now user can have multiple of these windows open. To avoid too much ambience noise i want to mute (fade out) all windows but one on top. Hence the issue.
|
|
|
Post by DarkTonic Dev on Aug 22, 2022 4:05:58 GMT
Have you thought about setting a Fade In and Fade Out time for your Variation so that it's played that way all the time, regardless of using Ambient Sound script or not?
|
|
chunik
New Member
Posts: 17
Posts: 17
|
Post by chunik on Aug 22, 2022 6:27:58 GMT
Have you thought about setting a Fade In and Fade Out time for your Variation so that it's played that way all the time, regardless of using Ambient Sound script or not? No didn't play with custom fading on master audio components (had no need so far), tried it now, and it first it was working strangely it just seemed to reduce overall volume of the click but it stayed constantly low never truly fading out.. But then i realized the click sound is too short and it's looping so in 1s time it can't fade in or out if the sound is like 0.2s long. So i did more tests. Custom fading on general sound group variation settings works, but not in case of ambient sound, meaning it will fade in, but not fade out if turn off ambient sound. However custom fading on ambient sound works (the one on component itself) even if just disable component it still slowly fades the sound out. So i don't need the tween i can use that and i'll just disable component. This is great progress. There is still an issue of ambient sound not doing correct math on volume and "spatial blend" that unity one does, but i can live without it it's not that important to my game. All in all i can also do ambient sounds with just unity native sound sources. I can get it correct clip instead of string, i can fade the volume on it as i want. But i'll have to deal with translating overall players volume settings (to modify unity audio sources volume) for this i was hoping to use master audio since i have no clue how db math should work . Which is pain in the ass .
So the first problem is solved. I hope that i've given enough information to look into second problem, when you have the time ! As to why it doesn't do proper math on the distance and doesn't slowly reduce sound volume as you go far/near audio listener.
Have a great day ! And thx for all the work.
|
|
|
Post by DarkTonic Dev on Aug 22, 2022 14:58:47 GMT
Right, Custom Fading fade out does not trigger with stop. Stop means stop, no fade.
This isn't functionality I plan on adding to the product, so I'm afraid my best advice would be to post the same on the Unity audio forum.
Thanks, -Brian
|
|
chunik
New Member
Posts: 17
Posts: 17
|
Post by chunik on Aug 22, 2022 16:41:16 GMT
Right, Custom Fading fade out does not trigger with stop. Stop means stop, no fade. This isn't functionality I plan on adding to the product, so I'm afraid my best advice would be to post the same on the Unity audio forum. Thanks, -Brian Ok one more not so related question on general volume.
How i did things is i import sounds in a group find the right one delete the rest. And i set the sound volume right on the sound itself (where is voices, pitch etc). And it is loud enough for my current setting of volume on my machine in my headphones. And i kinda always assumed that it would be easy to change volume later.
But i realized that my general volume for master playlist and master mixer is at 0. Also group master volume of each group is 0. So technically if i make options menu, player can't raise the volume over 0. So my question is kinda workflow one.
So what i did today is i set up mixer volume and and playlist volume to -45 which i though was like a middle (and i'm vaguely aware that it's not like that for db..). And i cranked up all the individual sounds to 0. This makes some sounds too low and no way to individually raise the volume since they are already on 0. So i moved to -30 on general volume. But what would be like general workflow, or guideline for this, so i don't guess the values. I try to find one that is like mostly in the middle for plenty of room to up and down for the user.
Thx!
|
|
|
Post by DarkTonic Dev on Aug 22, 2022 17:10:52 GMT
If you're saying that you switched on "dB for volumes", there lies the confusion. 0.5 volume is half, so if you switch off "dB" set things to 0.5 and see what the dB value is.
The volume of a sound in a Sound Group is played as: Variation Volume (times) Group Volume (times) Bus Volume (times) Master Mixer Volume. Then the Unity Mixer Group assigned to the bus can affect the volume of the result.
|
|
chunik
New Member
Posts: 17
Posts: 17
|
Post by chunik on Aug 22, 2022 17:57:36 GMT
If you're saying that you switched on "dB for volumes", there lies the confusion. 0.5 volume is half, so if you switch off "dB" set things to 0.5 and see what the dB value is. The volume of a sound in a Sound Group is played as: Variation Volume (times) Group Volume (times) Bus Volume (times) Master Mixer Volume. Then the Unity Mixer Group assigned to the bus can affect the volume of the result. Oh no. Couldn't find the switch but google foo revealed that it's there when importing prefab. Managed to switch it somehow without loosing any settings. 0 to 1 is much better thx a lot
|
|
|
Post by DarkTonic Dev on Aug 22, 2022 18:48:32 GMT
No problem. 0-1 is the default. You have to switch it to decibels on that Master Audio Manager window if you like it that way.
|
|