private AudioHandle GetSource() { if (_availableSourceInfoIndices.Count > 0) { int index = _availableSourceInfoIndices.Count - 1; // Get a source. AudioSource source = GetSourceFromPool(index); // Get a source info index. int infoIndex = _availableSourceInfoIndices [index]; _availableSourceInfoIndices.RemoveAt(index); _activeSourceInfoIndices.Add(infoIndex); // Set up the source info. AudioSourceInfo sourceInfo = _sourceInfos [infoIndex]; Uzu.Dbg.Assert(sourceInfo.Source == null); sourceInfo.Source = source; Uzu.Dbg.Assert(sourceInfo.Source != null); int handleId = CreateHandleId(); sourceInfo.HandleId = handleId; return(new AudioHandle(handleId, infoIndex)); } Debug.LogWarning("No AudioSources available."); return(new AudioHandle()); }
// Use this for initialization void Start() { _gameStarted = false; LoadMusicScore($@"D:\work\NNSongs"); _debug_samplecount = 0; _debug_sourcesused = 0; _debug_audioSourceMaxOutCount = 0; _audioSources = new List <AudioSourceInfo>(); _currentNotesManager = new CurrentNotesManager(this, NotePrefab, LongNoteBodyPrefab, DoriruNoteBodyPrefab, SlideNotePrefab); _lastFrameKeyStatus = new Dictionary <int, bool>(); for (int i = 1; i < 29; i++) { _lastFrameKeyStatus.Add(i, false); } _currentFrameKeyStatus = new Dictionary <int, bool>(); QueuedSubNotes = new List <MusicScoreSubNote>(); //begin create audiosources GameObject asourcepre = Instantiate(AudioSourcePrefab); _audioSourceBg = asourcepre.GetComponent <AudioSource>(); for (int i = 0; i < MaxSources - 1; i++) { GameObject a = Instantiate(AudioSourcePrefab); var info = new AudioSourceInfo() { AudioSource = a.GetComponent <AudioSource>(), InUse = false }; _audioSources.Add(info); } }
public void SetAudioState(AudioSourceInfo audio, AudioState state) { if (null == audio) { return; } switch (state) { case AudioState.Play: audio.Play(); break; case AudioState.Stop: audio.Stop(); break; case AudioState.Pause: audio.Pause(); break; case AudioState.UnPause: audio.UnPause(); break; default: break; } }
public static int PlaySound(AudioSourceInfo info) { if (!isInit && info == null || info.Clip == null) { return(NULL_ID); } AudioSourceData asd; if (!ASDFree.TryGet(out asd)) { //CheckElements need return(NULL_ID); } InitAudioSourceData(asd, info); asd.pool = ASDFree; asd.IsPool = true; //asd.IsMusic = info.IsMusic; //ActiveIDs.Add(maxID); //countIDs++; //Debug.LogError("GetInstanceID=" + asd.AS.GetInstanceID()); ActiveElements.Add(asd); //if (asd.GO.name == "RUS_M1_D1_S1") Debug.Log("YES"); asd.AS.Play(0); if (CheckPause(asd.UnscaleTime)) { asd.AS.Pause(); } //if (!asd.UnscaleTime && (paused || deepPaused)) asd.AS.Pause(); return(asd.ID); }
public void StopAll() { for (int i = 0; i < _activeSourceInfoIndices.Count; i++) { AudioSourceInfo sourceInfo = _sourceInfos [_activeSourceInfoIndices [i]]; sourceInfo.Source.Stop(); } }
private void Start() { asi_BGM = new AudioSourceInfo(go_Music); asi_UI = new AudioSourceInfo(go_UI); asi_BGM.musicEnd = delegate { GameManager.instance.GameEnd(); }; }
public void SetPitch(AudioHandle handle, float pitch) { AudioSourceInfo sourceInfo = GetSourceInfo(handle); if (sourceInfo != null) { sourceInfo.Source.pitch = pitch; } }
public string GetClipId(AudioHandle handle) { AudioSourceInfo sourceInfo = GetSourceInfo(handle); if (sourceInfo != null) { return(sourceInfo.ClipId); } return(string.Empty); }
public float GetVolume(AudioHandle handle) { AudioSourceInfo sourceInfo = GetSourceInfo(handle); if (sourceInfo != null) { return(sourceInfo.Source.volume); } return(0.0f); }
public bool IsPlaying(AudioHandle handle) { AudioSourceInfo sourceInfo = GetSourceInfo(handle); if (sourceInfo != null) { return(sourceInfo.Source.isPlaying); } return(false); }
public float GetPitch(AudioHandle handle) { AudioSourceInfo sourceInfo = GetSourceInfo(handle); if (sourceInfo != null) { return(sourceInfo.Source.pitch); } return(1.0f); }
static AudioController() { DefaultAudioData = new AudioData() { Blend3DSound = 1f, MaxDist = DefaultMaxDistance, MinDist = DefaultMinDistance, Pitch = 1f, Volume = 1f }; DefaultAudioSourceInfo = new AudioSourceInfo(); }
public void SetAudioTrack(AudioSourceInfo audio, AudioClip clip, AudioState state) { if (null == clip) { return; } audio.IsPause = false; audio.TheAudio.clip = clip; if (!m_trackAudio.ContainsKey(audio.TheTrackName)) { m_trackAudio.Add(audio.TheTrackName, new List <AudioSourceInfo>()); } if (!m_pathAudio.ContainsKey(audio.ThePath)) { m_pathAudio.Add(audio.ThePath, new List <AudioSourceInfo>()); } if (!m_groupAudio.ContainsKey(audio.TheGroupName)) { m_groupAudio.Add(audio.TheGroupName, new List <AudioSourceInfo>()); } if (audio.IsTrackMutex) { foreach (var v in m_trackAudio[audio.TheTrackName]) { v.Stop(); } } if (audio.IsGroupMutex) { foreach (var v in m_groupAudio[audio.TheGroupName]) { v.Stop(); } } if (audio.IsPathMutex) { foreach (var v in m_pathAudio[audio.ThePath]) { v.Stop(); } } m_trackAudio[audio.TheTrackName].Add(audio); m_groupAudio[audio.TheGroupName].Add(audio); m_pathAudio[audio.ThePath].Add(audio); UnityEngine.Audio.AudioMixerGroup[] tracks = m_audioMixer.FindMatchingGroups(audio.TheTrackName); if (tracks.Length > 0) { audio.TheAudio.outputAudioMixerGroup = tracks[0]; } SetAudioState(audio, state); }
private void SetSourceVolume(AudioSourceInfo sourceInfo, float volume) { if (_isMuted) { sourceInfo.Source.volume = 0.0f; } else { sourceInfo.Source.volume = volume; } sourceInfo.TargetVolume = volume; }
// Use this for initialization void Awake() { masterVolume = Mathf.Clamp(PlayerPrefs.GetFloat("MAV", 1), 0, 1); musicVolume = Mathf.Clamp(PlayerPrefs.GetFloat("MV", 0.5f), 0, 1); sfxVolume = Mathf.Clamp(PlayerPrefs.GetFloat("SFXV", 1), 0, 1); dVolume = Mathf.Clamp(PlayerPrefs.GetFloat("DV", 1), 0, 1); lastMav = masterVolume; lastMuv = musicVolume; lastsfV = sfxVolume; lastDV = dVolume; //Create Children GameObject musicChild = new GameObject("Music"); musicChild.transform.parent = transform; GameObject sfxChild = new GameObject("SFX"); sfxChild.transform.parent = transform; GameObject dChild = new GameObject("Dialogue"); dChild.transform.parent = transform; mSource = musicChild.AddComponent <AudioSource>(); mSourceInfo = musicChild.AddComponent <AudioSourceInfo>(); mSourceInfo.idealVolume = musicVolume; mSourceInfo.audioType = AudioSourceInfo.AudioType.Music; sfxSource = sfxChild.AddComponent <AudioSource>(); sfxSourceInfo = sfxChild.AddComponent <AudioSourceInfo>(); sfxSourceInfo.idealVolume = sfxVolume; sfxSourceInfo.audioType = AudioSourceInfo.AudioType.SFX; dSource = dChild.AddComponent <AudioSource>(); dSourceInfo = dChild.AddComponent <AudioSourceInfo>(); dSourceInfo.idealVolume = dVolume; dSourceInfo.audioType = AudioSourceInfo.AudioType.Dialogue; mSource.loop = true; DontDestroyOnLoad(this.gameObject); #if UNITY_EDITOR_WIN MasterVolume = masterVolume; MusicVolume = musicVolume; SfxVolume = sfxVolume; DVolume = dVolume; #endif }
/// <summary> /// 初期設定 /// </summary> protected override void Awake() { base.Awake(); //オーディオソース SE + BGM 作成 for (int i = 0; i < SE_SOURCE_NUM + 1; i++) { AudioSourceInfo audioInfo = new AudioSourceInfo(); GameObject soundObj = new GameObject("AudioObj" + i.ToString()); audioInfo.parentObj = null; audioInfo.seSource = soundObj.AddComponent <AudioSource>(); audioInfo.seSource.playOnAwake = false; audioInfo.sourceObj = soundObj; soundObj.transform.SetParent(this.gameObject.transform); if (i == 0) { audioInfo.seSource.spatialBlend = 0.0f; audioInfo.seSource.loop = true; audioInfo.seSource.volume = BGM_VOLUME_DEFULT; _bgmSource = audioInfo.seSource; } else { audioInfo.seSource.minDistance = 0.5f; audioInfo.seSource.spatialBlend = 1.0f; audioInfo.seSource.loop = false; audioInfo.seSource.volume = BGM_VOLUME_DEFULT; audioSourceInfoList.Add(audioInfo); } } //リソースフォルダから全SE&BGMのファイルを読み込みセット _bgmDic = new Dictionary <string, AudioClip>(); _seDic = new Dictionary <string, AudioClip>(); object[] bgmList = Resources.LoadAll(BGM_PATH); object[] seList = Resources.LoadAll(SE_PATH); foreach (AudioClip bgm in bgmList) { _bgmDic[bgm.name] = bgm; } foreach (AudioClip se in seList) { _seDic[se.name] = se; } }
private AudioSourceInfo GetSourceInfo(AudioHandle handle) { if (handle.Id != AudioHandle.INVALID_ID) { AudioSourceInfo sourceInfo = _sourceInfos [handle.Index]; // Verify handle integrity. if (sourceInfo.HandleId == handle.Id) { return(sourceInfo); } } return(null); }
protected override void Awake() { DontDestroyOnLoad(gameObject); base.Awake(); for (int i = 0; i < SE_SOURCE_NUM + 1; i++) { AudioSourceInfo audioInfo = new AudioSourceInfo(); GameObject soundObj = new GameObject("AudioObj" + i.ToString()); audioInfo.ParentObj = null; audioInfo.SESource = soundObj.AddComponent <AudioSource>(); audioInfo.SESource.playOnAwake = false; audioInfo.SourceObj = soundObj; soundObj.transform.SetParent(this.gameObject.transform); if (i == 0) { audioInfo.SESource.spatialBlend = 0.0f; audioInfo.SESource.loop = true; audioInfo.SESource.volume = SEVolume; BGMSource = audioInfo.SESource; } else { audioInfo.SESource.minDistance = 1.0f; audioInfo.SESource.spatialBlend = 1.0f; audioInfo.SESource.loop = false; audioInfo.SESource.volume = SEVolume; AudioSourceInfoList.Add(audioInfo); } } BGMDictionary = new Dictionary <string, AudioClip>(); SEDictionary = new Dictionary <string, AudioClip>(); object[] BGMList = Resources.LoadAll(BGM_PATH); object[] SEList = Resources.LoadAll(SE_PATH); foreach (AudioClip bgm in BGMList) { BGMDictionary[bgm.name] = bgm; } foreach (AudioClip se in SEList) { SEDictionary[se.name] = se; } }
public void Stop(AudioHandle handle, float fadeOutTime) { AudioSourceInfo sourceInfo = GetSourceInfo(handle); if (sourceInfo != null) { if (fadeOutTime > 0.0f && !_isMuted) { sourceInfo.VolumeFluctuationSpeed = -sourceInfo.Source.volume / fadeOutTime; sourceInfo.TargetVolume = 0.0f; } else { sourceInfo.Source.Stop(); } } }
public void SetVolume(AudioHandle handle, float volume, float duration) { AudioSourceInfo sourceInfo = GetSourceInfo(handle); if (sourceInfo != null) { if (duration > 0.0f && !_isMuted) { float deltaVolume = volume - sourceInfo.Source.volume; sourceInfo.VolumeFluctuationSpeed = deltaVolume / duration; sourceInfo.TargetVolume = volume; } else { SetSourceVolume(sourceInfo, volume); } } }
private void TryPlayInSources(Sound s, List <AudioSourceInfo> sourceIs) { foreach (AudioSourceInfo sourceInfo in sourceIs) { if (sourceInfo.source.isPlaying == false) { PlayInSource(s, sourceInfo); return; } } //if we made it to this point in code, it means that every audioSource in the //provided audioSource list was already playing something if (allowNewSources) { bool looping = false; AudioSourceInfo newSourceI = null; if (ReferenceEquals(sourceIs, loopAudioSourcesI)) { looping = true; newSourceI = new AudioSourceInfo(playLoopHolder.AddComponent <AudioSource>(), looping); } else if (ReferenceEquals(sourceIs, onceAudioSourcesI)) { newSourceI = new AudioSourceInfo(playOnceHolder.AddComponent <AudioSource>(), looping); } else { Debug.Log("during the setup of a new AudioSourceInfo, the sourceInfo-list wasn't recognized"); return; } sourceIs.Add(newSourceI); PlayInSource(s, newSourceI); } else { Debug.Log("tried to play sound '" + name + "', but all relevant audioSources were already playing something and new ones can't be added"); } }
private void PlayInSource(Sound s, AudioSourceInfo sourceInfo) { AudioSource source = sourceInfo.source; source.Stop(); source.clip = null; float p = s.pitch + Random.Range(-s.pitchModulation, s.pitchModulation); source.pitch = p; source.volume = s.volume; source.maxDistance = s.maxDistance; //DEBUG: NOT PLAYING source.clip = s.clip; source.Play(); //reset the playback info sourceInfo.samplesPosLastF = 0; sourceInfo.sound = s; }
private AudioSourceInfo GetIdleAudio(bool is3d) { AudioSourceInfo audio = null; for (int i = m_idleAudio.Count - 1; i >= 0; i--) { if (m_idleAudio[i].Is3d == is3d) { audio = m_idleAudio[i]; m_idleAudio.RemoveAt(i); break; } } if (null == audio) { audio = new AudioSourceInfo(); if (is3d) { audio.TheSelf = new GameObject("3dsound").transform; audio.TheAudio = audio.TheSelf.gameObject.AddComponent <AudioSource>(); audio.TheAudio.spatialBlend = 1; audio.TheAudio.rolloffMode = AudioRolloffMode.Linear; audio.TheSelf.parent = m_root; } else { audio.TheSelf = m_root; audio.TheAudio = audio.TheSelf.gameObject.AddComponent <AudioSource>(); audio.TheAudio.spatialBlend = 0; } audio.Is3d = is3d; } audio.IsFade = false; audio.InsId = ++m_audioInsIndex; audio.TheAudio.playOnAwake = false; return(audio); }
protected override void Awake() { base.Awake(); DontDestroyOnLoad(this.gameObject); //設定ファイル読み込み ReadSettingFile(); //AudioSourceの追加 bgmSource = gameObject.AddComponent <AudioSource>(); bgmSource.loop = true; bgmSource.priority = 0; bgmSource.volume = bgmVolume; seSources = new AudioSourceInfo[SE_SOURCE_NUM]; for (int i = 0; i < SE_SOURCE_NUM; i++) { AudioSource seSource = gameObject.AddComponent <AudioSource>(); seSource.volume = seVolume; seSource.priority = 255; seSources[i] = new AudioSourceInfo(seSource); } //ResourcesフォルダからBGM,SEを取得。辞書に登録 bgmDic = new Dictionary <string, AudioClip>(); seDic = new Dictionary <string, AudioClip>(); object[] bgmList = Resources.LoadAll("Audio/BGM"); object[] seList = Resources.LoadAll("Audio/SE"); foreach (AudioClip bgm in bgmList) { bgmDic.Add(bgm.name, bgm); Debug.Log("Add BGM " + bgm.name); } foreach (AudioClip se in seList) { seDic.Add(se.name, se); Debug.Log("Add SE " + se.name); } }
private IEnumerator LerpTrackVolume(AudioSourceInfo audioSourceInfo, float volume, float time) { volume *= 0.6f; float timeElapsed = 0f; float startVolume = audioSourceInfo.AudioSource.volume; float volumeDiff = volume - startVolume; do { yield return(null); timeElapsed += Time.deltaTime; audioSourceInfo.AudioSource.volume = startVolume + (volumeDiff / (time / timeElapsed)); } while (timeElapsed < time); audioSourceInfo.AudioSource.volume = volume; audioSourceInfo.Coroutine = null; }
public AudioHandle Play(string clipId, PlayOptions options) { AudioClip clip = GetClip(clipId); if (clip != null) { AudioHandle handle = GetSource(); AudioSourceInfo sourceInfo = GetSourceInfo(handle); if (sourceInfo != null) { AudioSource source = sourceInfo.Source; source.clip = clip; source.loop = options.Loop; source.transform.localPosition = options.Position; sourceInfo.ClipId = clipId; sourceInfo.TargetVolume = options.Volume; if (options.FadeInTime > 0.0f && !_isMuted) { sourceInfo.VolumeFluctuationSpeed = sourceInfo.TargetVolume / options.FadeInTime; source.volume = 0.0f; } else { SetSourceVolume(sourceInfo, sourceInfo.TargetVolume); } source.Play(); handle._handle_audio_source = source; } return(handle); } return(new AudioHandle()); }
public override void Trigger(TriggerType trigger) { if (gameObject == null) { return; } if (trigger == TriggerType.PersonTalk || trigger == TriggerType.PersonStartTalking) { // Play a random text for a conversation string[] randomTextForConversation = new string[] { "zin1", "zin2", "zin3", "zin4", "zin5", "zin6", "zin7", "zin8", "zin9", "zin10", "goed", "redelijk", "slecht", "hoeishet", "metjou", "straks" }; AudioFragment tekst = AudioManager.GetFragment(randomTextForConversation[PRNG.GetNumber(0, randomTextForConversation.Length - 1)]); current = sourceManager.PlaySound(tekst, voice); signalOtherToStartTalking = true; } else if (trigger == TriggerType.PersonSaysBye) { // Play a random text to end the conversation string[] randomTextForConversation = new string[] { "totziens", "doei", "ciya" }; AudioFragment tekst = AudioManager.GetFragment(randomTextForConversation[PRNG.GetNumber(0, randomTextForConversation.Length - 1)]); current = sourceManager.PlaySound(tekst, voice); } }
static void InitAudioSourceData(AudioSourceData asd, AudioSourceInfo info) { asd.TF.position = info.Pos; asd.AS.clip = info.Clip; asd.AS.volume = info.SettingsSource.Volume; asd.AS.spatialBlend = info.SettingsSource.Blend3DSound; asd.AS.loop = (info.SettingsSource.Type & AS_Loop) != 0; asd.AS.pitch = info.SettingsSource.Pitch; asd.AS.minDistance = info.SettingsSource.MinDist; asd.AS.maxDistance = info.SettingsSource.MaxDist; asd.AS.ignoreListenerVolume = false;//пока такой костыль для звука(не музыки) asd.AS.ignoreListenerPause = (info.SettingsSource.Type & AS_Unscale) != 0; float lt = info.SettingsSource.LifeTime; asd.UnLimit = lt < 0f && ((info.SettingsSource.Type & AS_Loop) != 0); asd.CurLifeTime = asd.LifeTime = lt > 0f ? lt : info.Clip.length; asd.UnscaleTime = (info.SettingsSource.Type & AS_Unscale) != 0; asd.ID = (maxID + 1) == NULL_ID ? 0 : (++maxID); #if UNITY_EDITOR asd.GO.name = info.Clip.name; #endif }
private void Update() { // Don't update if system is paused. if (_isPaused) { return; } // Fade in / out processing. { for (int i = 0; i < _activeSourceInfoIndices.Count; i++) { AudioSourceInfo sourceInfo = _sourceInfos [_activeSourceInfoIndices [i]]; // No change in volume - skip. if (Mathf.Approximately(sourceInfo.VolumeFluctuationSpeed, 0.0f)) { continue; } AudioSource source = sourceInfo.Source; float deltaVolume = sourceInfo.VolumeFluctuationSpeed * Time.deltaTime; float newVolume = source.volume + deltaVolume; // Increasing volume (fade in). if (sourceInfo.VolumeFluctuationSpeed > 0.0f) { if (newVolume >= sourceInfo.TargetVolume) { source.volume = sourceInfo.TargetVolume; sourceInfo.VolumeFluctuationSpeed = 0.0f; } else { source.volume = newVolume; } } // Decreasing volume (fade out). else { if (newVolume <= sourceInfo.TargetVolume) { source.volume = sourceInfo.TargetVolume; sourceInfo.VolumeFluctuationSpeed = 0.0f; if (Mathf.Approximately(source.volume, 0.0f)) { source.Stop(); } } else { source.volume = newVolume; } } } } // Clean up finished sounds. { for (int i = 0; i < _activeSourceInfoIndices.Count; /*i++*/) { int sourceInfoIndex = _activeSourceInfoIndices [i]; AudioSourceInfo sourceInfo = _sourceInfos [sourceInfoIndex]; AudioSource source = sourceInfo.Source; if (!source.isPlaying) { // Reset state. sourceInfo.Reset(); // Remove from active pool. { // Move last element to avoid array copy. int lastIndex = _activeSourceInfoIndices.Count - 1; _activeSourceInfoIndices [i] = _activeSourceInfoIndices [lastIndex]; _activeSourceInfoIndices.RemoveAt(lastIndex); } // Add back to available pools. _availableSourceInfoIndices.Add(sourceInfoIndex); ReturnSourceToPool(source); Uzu.Dbg.Assert(_availableSourceInfoIndices.Count == _availableSources.Count); continue; } i++; } } // Mute processing. { // Was there a change? if (_isMuted != _wasMuted) { if (_isMuted) { // Mute all active sources. for (int i = 0; i < _activeSourceInfoIndices.Count; i++) { AudioSourceInfo sourceInfo = _sourceInfos [_activeSourceInfoIndices [i]]; sourceInfo.Source.volume = 0.0f; } } else { // Unmute all active sources. for (int i = 0; i < _activeSourceInfoIndices.Count; i++) { AudioSourceInfo sourceInfo = _sourceInfos [_activeSourceInfoIndices [i]]; sourceInfo.Source.volume = sourceInfo.TargetVolume; } } } _wasMuted = _isMuted; } }
protected void OnHeard(AudioSourceInfo info) { _sightRenderer.sprite = null; }