private void SetSettingsFromSample(AudioSample sample) { if (AudioSource == null) throw new Exception("AudioSourceContainer: No AudioSource set"); Layer = sample.Layer; AudioLayerSettings layerS = layerSettings; VolumeModifier = sample.Settings.Volume; AudioSource.mute = layerS.Mute; AudioSource.loop = sample.Settings.Loop; AudioSource.priority = sample.Settings.Priority; AudioSource.pitch = sample.Settings.Pitch; AudioSource.bypassEffects = sample.Settings.BypassEffects; AudioSource.bypassListenerEffects = sample.Settings.BypassListenerEffects; AudioSource.bypassReverbZones = sample.Settings.BypassReverbZones; AudioSource.dopplerLevel = sample.Settings.DopplerLevel; AudioSource.panLevel = sample.Settings.PanLevel; AudioSource.spread = sample.Settings.Spread; AudioSource.maxDistance = sample.Settings.MaxDistance; AudioSource.pan = sample.Settings.Pan2D; AudioSource.clip = sample.Clip; }
/// <summary> /// Returns the volume for the audio layer /// </summary> /// <param name="layer"></param> /// <returns></returns> public static float GetVolume(AudioLayer layer) { if (!Volumes.ContainsKey(layer)) { return(1); } return(Volumes[layer]); }
public static void Mute(AudioLayer layer) { AudioLayerSettings settings = AudioLayerManager.GetAudioLayerSettings(layer); List <AudioSourceContainer> containers = Instance.FindSources(layer); foreach (AudioSourceContainer cont in containers) { cont.AudioSource.mute = settings.Mute; } }
/// <summary> /// Sets the volume for the audio layer /// </summary> /// <param name="layer">layer in question</param> /// <param name="volume">volume</param> /// <returns></returns> public static void SetVolume(AudioLayer layer, float volume) { if (!Volumes.ContainsKey(layer)) Volumes.Add(layer, volume); else Volumes[layer] = volume; if (OnVolumeChanged != null) OnVolumeChanged(layer, volume); }
public void GetLayer_Undefined_SpawnNew() { Assert.AreEqual(0, AudioManager.Instance.RuntimeLayers.Count); AudioLayer layer = AudioManager.Instance.GetLayer(); Assert.IsNotNull(layer); Assert.AreEqual(1, AudioManager.Instance.RuntimeLayers.Count); }
public static void UpdateVolume(AudioLayer layer) { AudioLayerSettings settings = AudioLayerManager.GetAudioLayerSettings(layer); List <AudioSourceContainer> containers = Instance.FindSources(layer); foreach (AudioSourceContainer cont in containers) { cont.UpdateVolume(); } }
public void GetLayer_Undefined_GetExistingPlaying_Fail() { new SingleAudio("Warning").Play(); Assert.AreEqual(1, AudioManager.Instance.RuntimeLayers.Count); AudioLayer anotherLayer = AudioManager.Instance.GetLayer(); Assert.IsNotNull(anotherLayer); Assert.AreEqual(2, AudioManager.Instance.RuntimeLayers.Count); }
private IEnumerator FadeIn(AudioLayer layer) { float min = layer.CurrentVolume(); do { min = min + 0.1f; layer.SetVolume(min); yield return(new WaitForSeconds(fadeTime / 10)); } while (min <= 1); yield break; }
public void Unmute(string name) { AudioLayer layer = Array.Find(audioLayers, l => l.name == name); if (layer == null) { Debug.LogWarning("Unable to find: " + name + " audio layer!"); return; } StartCoroutine(FadeIn(layer)); }
public void Mute(int layerIndex, bool mute) { if (layerIndex >= _audioLayers.Count) { return; } AudioLayer layer = _audioLayers[layerIndex]; if (layer != null) { layer.Muted = mute; } }
public static AudioSourceContainer Play(AudioClip clip, AudioLayer layer = AudioLayer.None, AudioSample.AudioSettings settings = null) { AudioSample sample = new AudioSample(); sample.Clip = clip; sample.Layer = layer; sample.Settings = settings; // There should only be one audioListener (Usually main camera) Transform audioListenerTransform = GameObject.FindObjectOfType <AudioListener>().transform; return(Play(sample, audioListenerTransform)); }
public void AudioDataTest() { var input = new AudioLayer { Data = new byte[] { 42, 10, 11, 12 } }; var serializer = new AudioLayerHandler(); var output = (AudioLayer)serializer.Reserialize(input); Assert.That(output.Id, Is.EqualTo(input.Id)); Assert.That(output.BaseVolume, Is.EqualTo(input.BaseVolume)); Assert.That(output.Data, Is.EquivalentTo(input.Data)); }
public void GetLayer_Undefined_GetExisting() { AudioLayer layer = AudioManager.Instance.GetLayer(); Assert.IsNotNull(layer); Assert.AreEqual(1, AudioManager.Instance.RuntimeLayers.Count); AudioLayer anotherLayer = AudioManager.Instance.GetLayer(); Assert.IsNotNull(anotherLayer); Assert.AreEqual(2, AudioManager.Instance.RuntimeLayers.Count); }
public void Stop(int layerIndex) { if (layerIndex >= _audioLayers.Count) { return; } AudioLayer layer = _audioLayers[layerIndex]; if (layer != null) { layer.Looping = false; layer.Time = layer.Duration; } }
public void NewLength(int newLength) { AudioLayer[] newAudioLayers = new AudioLayer[newLength]; // Create a new array for (int i = 0; i < newLength; i++) // For each of the new length { if (i >= audioLayers.Length) // If we are beyond the length of the original { break; // break the loop } newAudioLayers [i] = audioLayers [i]; // Copy the old data } audioLayers = new AudioLayer[newLength]; // Resize the array audioLayers = newAudioLayers; // Copy the data }
public override void RemoveLayer(string layerName) { AudioLayer layer = GetLayer(layerName); if (layer == null) { return; } layer.Stop(); layer.Dispose(); lock (_layers) { _layers.Remove(layer); } }
/// <summary> /// Sets the volume for the audio layer /// </summary> /// <param name="layer">layer in question</param> /// <param name="volume">volume</param> /// <returns></returns> public static void SetVolume(AudioLayer layer, float volume) { if (!Volumes.ContainsKey(layer)) { Volumes.Add(layer, volume); } else { Volumes[layer] = volume; } if (OnVolumeChanged != null) { OnVolumeChanged(layer, volume); } }
/// <summary> /// 添加音效 /// </summary> /// <param name="layer"></param> /// <param name="count"></param> public void AddAudioLayer(eAudioLayer audio_layer, int audio_count = 1) { //------------------判断当前是否已有层次,----------------------------- if (m_AudioLayer.ContainsKey(audio_layer)) { m_AudioLayer[audio_layer].Clear(); m_AudioLayer.Remove(audio_layer); } //---------------------添加新层次--------------------------------- AudioLayer newLayer = new AudioLayer(this); newLayer.Create(audio_count); m_AudioLayer.Add(audio_layer, newLayer); }
/// <summary> /// Plays the Audio /// </summary> /// <param name="c">clip</param> /// <param name="v">volume</param> /// <param name="p">pitch</param> /// <param name="l">Audio ayer</param> /// <returns>A playing audio source</returns> public static AudioSource Play(AudioClip c, float v, float p, AudioLayer l) { var s = GetNext(); // return null if (s == null) { return(null); } s.clip = c; s.pitch = p; s.loop = false; s.volume = v * AudioManager.GetVolume(l); s.Play(); return(s); }
public override void Load() { base.Load(); _pad1 = (PongPaddle)IdToObject["PaddleOne"]; _pad2 = (PongPaddle)IdToObject["PaddleTwo"]; _myPaddle = _pad1.Owner == NetworkHandle ? _pad1 : _pad2; _font = Engine.AssetLoader.Get <FontAsset>("calibrib.ttf"); Engine.Renderer.Camera = new TrueScaleCamera((Engine.Configuration.RenderSize / 2.0f).ToVec3()); _soundtrack = Engine.AssetLoader.Get <AudioAsset>("soundtrack.wav"); if (_soundtrack != null) { Engine.Audio.CreateLayer("BGM", 0.3f); AudioLayer audioLayer = Engine.Audio.GetLayer("BGM"); audioLayer.AddToQueue(_soundtrack); audioLayer.LoopingCurrent = true; } }
public LayeredAudioSource(AudioSource source, int layers) { if (source != null && layers > 0) { _audioSource = source; for (int i = 0; i < layers; i++) { AudioLayer layer = new AudioLayer(); layer.Collection = null; layer.Bank = 0; layer.Looping = true; layer.Time = 0f; layer.Duration = 0f; layer.Muted = false; _audioLayers.Add(layer); } } }
public static void Stop(AudioLayer layer) { Stop(Instance.FindSources(layer)); }
public WaveformVisualization(AudioLayer layer) { Layer = layer; }
//processes the recorded audio and saves it using the SaveNow function public void SaveNow(AudioClip clipToSave, string userFolder, string songFolder, string filename) { Debug.Log(Application.persistentDataPath); int newLength = 1; AudioLayer myobj1 = new AudioLayer(); audioLayers = new AudioLayer[newLength]; // Create a new array audioLayers[0] = myobj1; myobj1.clip = new AudioClip[1]; // 1 export int totalExports = 1; //used to load files from persistentDataPath AudioClip clip1 = clipToSave; myobj1.clip[0] = clip1; for (int n = 0; n < audioLayers.Length; n++) { totalExports *= audioLayers[n].clip.Length; // Multiply by the number of clips in each layer } if (totalExports > 0) { float progressPercent = 0.0f; int clipCount = 0; string[] combinations; // Start an array of all combinations combinations = new string[totalExports]; // Set the number of entries to the number of exports // Reset the onClip value for each layer for (int r = 0; r < audioLayers.Length; r++) { audioLayers[r].onClip = 0; } for (int l = 0; l < audioLayers.Length; l++) { // For each layer... int exportsLeft = 1; // Start at 1... for (int i = l; i < audioLayers.Length; i++) { // For each layer left in the list (don't compute those we've already done) exportsLeft *= audioLayers[i].clip.Length; // Find out how many exports are left if it were just those layers } int entriesPerValue = exportsLeft / audioLayers[l].clip.Length; // Compute how many entires per value, if the total entries were exportsLeft int entryCount = 0; // Set entryCount to 0 for (int e = 0; e < combinations.Length; e++) { // For all combinations if (l != 0) // If this isn't the first layer { combinations[e] = combinations[e] + ","; // Append a "," to the String } combinations[e] = combinations[e] + audioLayers[l].onClip; // Append the "onClip" value to the string entryCount++; // increase entryCount if (entryCount >= entriesPerValue) { // if we've done all the entires for that "onClip" value... audioLayers[l].onClip++; // increase onClip by 1 entryCount = 0; // Reset entryCount if (audioLayers[l].onClip >= audioLayers[l].clip.Length) // if we've also run out of clips for this layer { audioLayers[l].onClip = 0; // Reset onClip count } } } } int number = 0; // for the file name // For each combination, save a .wav file with those clip numbers. foreach (var combination in combinations) { clipCount++; progressPercent = clipCount / (float)totalExports; string[] clipsAsString = combination.Split(","[0]); SaveClip(userFolder, songFolder, filename, number, clipsAsString, audioLayers); number++; } } else { Debug.Log("Nothing To Export! (or maybe a layer is missing clips?)"); } }
//combines the 5 audio files into 1 file and saves it via SaveClip function public void CombineFiles(string userFolder, String songFolder) { //identifies persistentDataPath on device Debug.Log(Application.persistentDataPath); int newLength = 5; AudioLayer myobj1 = new AudioLayer(); AudioLayer myobj2 = new AudioLayer(); AudioLayer myobj3 = new AudioLayer(); AudioLayer myobj4 = new AudioLayer(); AudioLayer myobj5 = new AudioLayer(); audioLayers = new AudioLayer[newLength]; // Create a new array audioLayers[0] = myobj1; audioLayers[1] = myobj2; audioLayers[2] = myobj3; audioLayers[3] = myobj4; audioLayers[4] = myobj5; myobj1.clip = new AudioClip[1]; myobj2.clip = new AudioClip[1]; myobj3.clip = new AudioClip[1]; myobj4.clip = new AudioClip[1]; myobj5.clip = new AudioClip[1]; // 1 export int totalExports = 1; string folderName1 = "guitar"; string filename1 = userFolder + "_" + songFolder + "_guitar.wav"; string folderName2 = "bass"; string filename2 = userFolder + "_" + songFolder + "_bass.wav"; string folderName3 = "piano"; string filename3 = userFolder + "_" + songFolder + "_piano.wav"; string folderName4 = "drums"; string filename4 = userFolder + "_" + songFolder + "_drums.wav"; string folderName5 = "voice"; string filename5 = userFolder + "_" + songFolder + "_voice.wav"; string filename = ""; //used to load files from persistentDataPath AudioClip clip1 = ToAudioClip(Application.persistentDataPath + "/" + userFolder + "/" + songFolder + "/" + folderName1 + "/" + filename1); AudioClip clip2 = ToAudioClip(Application.persistentDataPath + "/" + userFolder + "/" + songFolder + "/" + folderName2 + "/" + filename2); AudioClip clip3 = ToAudioClip(Application.persistentDataPath + "/" + userFolder + "/" + songFolder + "/" + folderName3 + "/" + filename3); AudioClip clip4 = ToAudioClip(Application.persistentDataPath + "/" + userFolder + "/" + songFolder + "/" + folderName4 + "/" + filename4); AudioClip clip5 = ToAudioClip(Application.persistentDataPath + "/" + userFolder + "/" + songFolder + "/" + folderName5 + "/" + filename5); //used to load files from Asset folder in Unity (kept if needed for future testing) //AudioClip clip1 = (AudioClip)Resources.Load(filename1); myobj1.clip[0] = clip1; myobj2.clip[0] = clip2; myobj3.clip[0] = clip3; myobj4.clip[0] = clip4; myobj5.clip[0] = clip5; for (int n = 0; n < audioLayers.Length; n++) { totalExports *= audioLayers[n].clip.Length; // Multiply by the number of clips in each layer } if (totalExports > 0) { float progressPercent = 0.0f; int clipCount = 0; string[] combinations; // Start an array of all combinations combinations = new string[totalExports]; // Set the number of entries to the number of exports // Reset the onClip value for each layer for (int r = 0; r < audioLayers.Length; r++) { audioLayers[r].onClip = 0; } for (int l = 0; l < audioLayers.Length; l++) { // For each layer... int exportsLeft = 1; // Start at 1... for (int i = l; i < audioLayers.Length; i++) { // For each layer left in the list (don't compute those we've already done) exportsLeft *= audioLayers[i].clip.Length; // Find out how many exports are left if it were just those layers } int entriesPerValue = exportsLeft / audioLayers[l].clip.Length; // Compute how many entires per value, if the total entries were exportsLeft int entryCount = 0; // Set entryCount to 0 for (int e = 0; e < combinations.Length; e++) { // For all combinations if (l != 0) // If this isn't the first layer { combinations[e] = combinations[e] + ","; // Append a "," to the String } combinations[e] = combinations[e] + audioLayers[l].onClip; // Append the "onClip" value to the string entryCount++; // increase entryCount if (entryCount >= entriesPerValue) { // if we've done all the entires for that "onClip" value... audioLayers[l].onClip++; // increase onClip by 1 entryCount = 0; // Reset entryCount if (audioLayers[l].onClip >= audioLayers[l].clip.Length) // if we've also run out of clips for this layer { audioLayers[l].onClip = 0; // Reset onClip count } } } } int number = 0; // for the file name // For each combination, save a .wav file with those clip numbers. foreach (var combination in combinations) { clipCount++; progressPercent = clipCount / (float)totalExports; string[] clipsAsString = combination.Split(","[0]); SaveClip(userFolder, songFolder, filename, number, clipsAsString, audioLayers); number++; } } else { Debug.Log("Nothing To Export! (or maybe a layer is missing clips?)"); } }
public AudioLayerSettings(AudioLayer layer) { this.layer = layer; }
void AudioManager_OnVolumeChanged(AudioLayer arg1, float arg2) { if (arg1 == Layer) UpdateVolume(); }
public WaveformCache(AudioLayer layer) { Layer = layer; }
public void Update() { int newActiveLayer = -1; bool refreshAudioSource = false; for (int i = _audioLayers.Count - 1; i >= 0; i--) { AudioLayer layer = _audioLayers[i]; if (layer.Collection == null) { continue; } layer.Time += Time.deltaTime; if (layer.Time >= layer.Duration) { if (layer.Looping || layer.Clip == null) { AudioClip clip = layer.Collection[layer.Bank]; if (clip == layer.Clip) { layer.Time = layer.Time % layer.Clip.length; } else { layer.Time = 0f; } layer.Duration = clip.length; layer.Clip = clip; if (newActiveLayer < i) { newActiveLayer = i; refreshAudioSource = true; } } else { layer.Clip = null; layer.Collection = null; layer.Duration = 0f; layer.Bank = 0; layer.Looping = false; layer.Time = 0f; } } else { if (newActiveLayer < i) { newActiveLayer = i; } } } // If we found a new active layer (or none) if (newActiveLayer != _activeLayer || refreshAudioSource) { // Previous layers expired and no new layer, stop audioSource if (newActiveLayer == -1) { _audioSource.Stop(); _audioSource.clip = null; } else { AudioLayer layer = _audioLayers[newActiveLayer]; _audioSource.clip = layer.Clip; _audioSource.volume = layer.Muted ? 0f : layer.Collection.volume; _audioSource.spatialBlend = layer.Collection.spatialBlend; _audioSource.time = layer.Time; _audioSource.loop = layer.Looping; _audioSource.outputAudioMixerGroup = AudioManager.instance.GetAudioGroupFromATrackName(layer.Collection.audioGroup); _audioSource.Play(); } } // Remember the currently active layer for the next update _activeLayer = newActiveLayer; if (_activeLayer != -1 && _audioSource != null) { AudioLayer audioLayer = _audioLayers[_activeLayer]; if (audioLayer.Muted) { _audioSource.volume = 0f; } else { _audioSource.volume = audioLayer.Collection.volume; } } }
/// <summary> /// Returns the volume for the audio layer /// </summary> /// <param name="layer"></param> /// <returns></returns> public static float GetVolume(AudioLayer layer) { if (!Volumes.ContainsKey(layer)) return 1; return Volumes[layer]; }
protected override void RenderContent(RenderComposer composer) { if (_explorer != null && _explorer.Open) { ImGui.Text("Waiting for file..."); return; } void ExecuteOnFile(Action <AudioTrack> func) { _explorer = new FileExplorer <AudioAsset>(asset => { var modifyModal = new AudioTrackModifyModal(asset, func); Parent.AddWindow(modifyModal); }, true); Parent.AddWindow(_explorer); } float masterVol = Engine.Configuration.MasterVolume; if (ImGui.DragFloat("Global Volume", ref masterVol, 0.01f, 0f, 1f)) { Engine.Configuration.MasterVolume = masterVol; foreach (KeyValuePair <AudioLayer, WaveformVisualization> cache in _waveFormCache) { cache.Value.Recreate(); } } bool mono = Engine.Configuration.ForceMono; if (ImGui.Checkbox("Force Mono", ref mono)) { Engine.Configuration.ForceMono = mono; } // Push waveforms down. composer.PushModelMatrix(Matrix4x4.CreateTranslation(new Vector3(0, 50, 0))); // Render ImGui section of layers. string[] layers = Engine.Audio.GetLayers(); for (var i = 0; i < layers.Length; i++) { AudioLayer layer = Engine.Audio.GetLayer(layers[i]); _waveFormCache.TryGetValue(layer, out WaveformVisualization cache); ImGui.PushID(i); ImGui.Text($"Layer: {layers[i]} [{layer.Status}] {(layer.CurrentTrack != null ? $" {MathF.Truncate(layer.Playback * 100f) / 100f:0}/{layer.CurrentTrack.File.Duration:0.0}s" : "")}"); float volume = layer.Volume; if (ImGui.DragFloat("Volume", ref volume, 0.01f, 0f, 1f)) { cache?.Recreate(); layer.Volume = volume; } if (ImGui.Button("Add To Queue")) { ExecuteOnFile(layer.AddToQueue); } ImGui.SameLine(); if (ImGui.Button("Add To Play Next")) { ExecuteOnFile(layer.PlayNext); } ImGui.SameLine(); if (ImGui.Button("Quick Play")) { ExecuteOnFile(layer.QuickPlay); } if (layer.Status == PlaybackStatus.Paused) { if (ImGui.Button("Resume")) { layer.Resume(); } } else { if (ImGui.Button("Pause")) { layer.Pause(); } } ImGui.SameLine(); ImGui.SameLine(); if (ImGui.Button("Stop")) { layer.Stop(); } ImGui.SameLine(); if (ImGui.Button("Toggle Loop")) { layer.LoopingCurrent = !layer.LoopingCurrent; } ImGui.SameLine(); ImGui.Text($"Looping: {layer.LoopingCurrent}"); string[] items = layer.Playlist.Select(x => x.Name).ToArray(); if (ImGui.TreeNode($"Playlist, Currently Playing: {(items.Length > 0 ? items[0] : "None")}")) { var r = 0; ImGui.ListBox("", ref r, items, items.Length); ImGui.TreePop(); } ImGui.Text($"Missed: {layer.MetricBackendMissedFrames}\nAhead: {layer.MetricDataStoredInBlocks}ms"); ImGui.PopID(); ImGui.NewLine(); composer.PushModelMatrix(Matrix4x4.CreateTranslation(new Vector3(10, i * (_waveFormHeight + 10), 0))); cache?.Render(composer); composer.PopModelMatrix(); } composer.PopModelMatrix(); ImGui.InputText("", ref _newLayerName, 50); ImGui.SameLine(); if (ImGui.Button("Create Layer") && !string.IsNullOrEmpty(_newLayerName)) { Engine.Audio.CreateLayer(_newLayerName); _newLayerName = "New Layer" + Engine.Audio.GetLayers().Length; } }
protected override void RenderContent(RenderComposer composer) { if (_explorer != null && _explorer.Open) { ImGui.Text("Waiting for file..."); return; } void ExecuteOnFile(Action <AudioTrack> func) { _explorer = new FileExplorer <AudioAsset>(asset => { var modifyModal = new AudioTrackModifyModal(asset, func); Parent.AddWindow(modifyModal); }); Parent.AddWindow(_explorer); } float masterVol = Engine.Configuration.MasterVolume; if (ImGui.DragFloat("Global Volume", ref masterVol, 0.01f, 0f, 1f)) { Engine.Configuration.MasterVolume = masterVol; foreach (KeyValuePair <AudioLayer, WaveformCache> cache in _waveFormCache) { cache.Value.Recreate(); } } // Push waveforms down. composer.PushModelMatrix(Matrix4x4.CreateTranslation(new Vector3(0, _waveFormHeight, 0))); // Render ImGui section of layers. string[] layers = Engine.Host.Audio.GetLayers(); for (var i = 0; i < layers.Length; i++) { AudioLayer layer = Engine.Host.Audio.GetLayer(layers[i]); _waveFormCache.TryGetValue(layer, out WaveformCache cache); ImGui.Text($"Layer {layers[i]}"); ImGui.PushID(i); ImGui.Text($"Status: {layer.Status}" + (layer.CurrentTrack != null ? $" {MathF.Truncate(layer.CurrentTrack.Playback * 100f) / 100f:0}/{layer.CurrentTrack.File.Duration}" : "")); float volume = layer.Volume; if (ImGui.DragFloat("Volume", ref volume, 0.01f, 0f, 1f)) { cache?.Recreate(); layer.Volume = volume; } if (ImGui.Button("Add To Queue")) { ExecuteOnFile(layer.AddToQueue); } ImGui.SameLine(); if (ImGui.Button("Play Next")) { ExecuteOnFile(layer.PlayNext); } ImGui.SameLine(); if (ImGui.Button("Quick Play")) { ExecuteOnFile(layer.QuickPlay); } if (ImGui.Button("Resume")) { layer.Resume(); } ImGui.SameLine(); if (ImGui.Button("Pause")) { layer.Pause(); } ImGui.SameLine(); if (ImGui.Button("Stop")) { layer.Stop(); } if (ImGui.Button("Loop Current")) { layer.LoopingCurrent = !layer.LoopingCurrent; } ImGui.SameLine(); ImGui.Text(layer.LoopingCurrent.ToString()); var r = 0; string[] items = layer.Playlist.Select(x => x.Name).ToArray(); ImGui.ListBox("Playlist", ref r, items, items.Length); ImGui.PopID(); ImGui.NewLine(); composer.PushModelMatrix(Matrix4x4.CreateTranslation(new Vector3(0, i * _waveFormHeight, 0))); cache?.Render(composer); composer.PopModelMatrix(); } composer.PopModelMatrix(); if (ImGui.Button("Create Layer") && !string.IsNullOrEmpty(_newLayerName)) { Engine.Host.Audio.CreateLayer(_newLayerName); _newLayerName = ""; } ImGui.InputText("", ref _newLayerName, 50); }
/// <summary> /// Updates the time of all layered clips and makes sure that the audio source is playing the clip on the highest layer. /// </summary> public void Update() { // Used to record the highest layer with a clip assigned and still playing int newActiveLayer = -1; bool refreshAudioSource = false; // Update the stack each frame by iterating the layers (Working backwards) for (int i = audioLayers.Count - 1; i >= 0; i--) { // Layer being processed AudioLayer layer = audioLayers[i]; // Ignore unassigned layers if (layer.collection == null) { continue; } // Update the internal playhead of the layer layer.time += Time.deltaTime; // If it has exceeded its duration then we need to take action if (layer.time > layer.duration) { // If its a looping sound OR the first time we have set up this layer // we need to assign a new clip from the pool assigned to this layer if (layer.looping || layer.clip == null) { // Fetch a new clip from the pool AudioClip clip = layer.collection[layer.bank]; // Calculate the play position based on the time of the layer and store duration if (clip == layer.clip) { layer.time %= layer.clip.length; } else { layer.time = 0.0f; } layer.duration = clip.length; layer.clip = clip; // This is a layer that has focus so we need to chose and play // a new clip from the pool if (newActiveLayer < i) { // This is the active layer index newActiveLayer = i; // We need to issue a play command to the audio source refreshAudioSource = true; } } else { // The clip assigned to this layer has finished playing and is not set to loop // so clear the later and reset its status ready for reuse in the future layer.clip = null; layer.collection = null; layer.duration = 0.0f; layer.bank = 0; layer.looping = false; layer.time = 0.0f; } } // Else this layer is playing else { // If this is the highest layer then record that its the clip currently playing if (newActiveLayer < i) { newActiveLayer = i; } } } // If we found a new active layer (or none) if (newActiveLayer != activeLayer || refreshAudioSource) { // Previous layer expired and no new layer so stop audio source - there are no active layers if (newActiveLayer == -1) { audioSource.Stop(); audioSource.clip = null; } // We found an active layer but its different than the previous update so its time to switch // the audio source to play the clip on the new layer else { // Get the layer AudioLayer layer = audioLayers[newActiveLayer]; audioSource.clip = layer.clip; audioSource.volume = layer.muted ? 0.0f : layer.collection.Volume; audioSource.spatialBlend = layer.collection.SpatialBlend; audioSource.time = layer.time; audioSource.loop = false; audioSource.outputAudioMixerGroup = AudioManager.Instance.GetAudioGroupFromTrackName(layer.collection.AudioGroup); audioSource.Play(); } } // Remember the currently active layer for the next update check activeLayer = newActiveLayer; if (activeLayer != -1 && audioSource) { AudioLayer audioLayer = audioLayers[activeLayer]; if (audioLayer.muted) { audioSource.volume = 0.0f; } else { audioSource.volume = audioLayer.collection.Volume; } } }
/// <summary> /// Plays the Audio /// </summary> /// <param name="c">clip</param> /// <param name="v">volume</param> /// <param name="p">pitch</param> /// <param name="l">Audio ayer</param> /// <returns>A playing audio source</returns> public static AudioSource Play(AudioClip c, float v, float p, AudioLayer l) { var s = GetNext(); // return null if (s == null) return null; s.clip = c; s.pitch = p; s.loop = false; s.volume = v * AudioManager.GetVolume(l); s.Play(); return s; }
public static AudioLayerSettings GetAudioLayerSettings(AudioLayer layer) { return(Instance.audioLayerSettings.Where(a => a.Layer == layer).First()); }
private List <AudioSourceContainer> FindSources(AudioLayer layer) { return(AudioSources.Where(a => a.Layer == layer).ToList()); }