public void Play(float delay, AudioFileObject file, bool loop = false) { // Make sure no remnants from a previous sound remain StopAllCoroutines(); aSource.Stop(); enabled = true; // Enable updates on the script if (file.playReversed) { aSource.time = aSource.clip.length - 0.01f; } aSource.PlayDelayed(delay); audioFile = file; looping = loop; aSource.bypassEffects = file.bypassEffects; aSource.bypassListenerEffects = file.bypassListenerEffects; aSource.bypassReverbZones = file.bypassReverbZones; switch (file.fadeMode) { case FadeMode.FadeIn: case FadeMode.FadeInAndOut: StartCoroutine(FadeIn(file.fadeMode == FadeMode.FadeInAndOut)); break; case FadeMode.FadeOut: StartCoroutine(FadeOut()); break; } ApplyVolumeChanges(); ApplyEffects(); }
void DesignateSound() { if (sound != "") { if (!AudioManager.instance) { return; } foreach (AudioFileObject a in AudioManager.instance.GetSoundLibrary()) { if (a.safeName == sound) { audioObject = a; return; } } } if (audioObject == null) { List <AudioFileObject> audio = AudioManager.instance.GetSoundLibrary(); if (audio.Count > 0) { audioObject = audio[0]; if (audioObject != null) { sound = audioObject.safeName; } } } }
public static bool OnDoubleClickAssets(int instanceID, int line) { string assetPath = AssetDatabase.GetAssetPath(instanceID); AudioFileObject audioFile = AssetDatabase.LoadAssetAtPath <AudioFileObject>(assetPath); if (audioFile) { Init(); // Force a repaint Selection.activeObject = null; EditorApplication.delayCall += () => Selection.activeObject = audioFile; return(true); } AudioFileMusicObject audioFileMusic = AssetDatabase.LoadAssetAtPath <AudioFileMusicObject>(assetPath); if (audioFileMusic) { Init(); // Force a repaint Selection.activeObject = null; EditorApplication.delayCall += () => Selection.activeObject = audioFileMusic; return(true); } return(false); }
public void Play(float delay) { StopAllCoroutines(); aSource.Stop(); aSource.PlayDelayed(delay); audioFile = null; looping = false; aSource.bypassEffects = false; aSource.bypassListenerEffects = false; aSource.bypassReverbZones = false; ClearEffects(); }
/// <summary> /// Conveniently draws a progress bar /// Referenced from the official Unity documentation /// https://docs.unity3d.com/ScriptReference/Editor.html /// </summary> /// <param name="value"></param> /// <param name="label"></param> /// <returns></returns> public static Rect ProgressBar(float value, AudioClip selectedClip, AudioFileObject selectedSound = null, AudioFileMusicObject selectedMusic = null) { // Get a rect for the progress bar using the same margins as a text field // TODO: Make this dynamic, locks its previous size before showing the guide float maxHeight = (showHowTo) ? playbackPreviewClamped : 4000; float minHeight = (showHowTo) ? playbackPreviewClamped : 64; Rect rect = GUILayoutUtility.GetRect(64, 4000, minHeight, maxHeight); if (cachedTex == null || forceRepaint) { Texture2D waveformTexture; if (selectedSound) { waveformTexture = AudioFileObjectEditor.instance.PaintWaveformSpectrum(helperSource.clip, (int)rect.width, (int)rect.height, new Color(1, 0.5f, 0)); } else if (selectedMusic) { waveformTexture = AudioFileMusicObjectEditor.instance.PaintWaveformSpectrum(selectedClip, (int)rect.width, (int)rect.height, new Color(1, 0.5f, 0)); } else { waveformTexture = PaintWaveformSpectrum(selectedClip, (int)rect.width, (int)rect.height, new Color(1, 0.5f, 0)); } cachedTex = waveformTexture; if (waveformTexture != null) { GUI.DrawTexture(rect, waveformTexture); } forceRepaint = false; } else { GUI.DrawTexture(rect, cachedTex); } float left = CalculateZoomedLeftValue(); float right = CalculateZoomedRightValue(); if (value >= left && value <= right) { Rect progressRect = new Rect(rect); progressRect.width *= Mathf.InverseLerp(left, right, value); progressRect.xMin = progressRect.xMax - 1; GUI.Box(progressRect, "", "SelectionRect"); } EditorGUILayout.Space(); return(rect); }
public void Play(AudioFileMusicObject file) { // Make sure no remnants from a previous sound remain StopAllCoroutines(); if (file.playReversed) { aSource.time = aSource.clip.length - 0.01f; } aSource.Play(); audioFile = file; looping = false; aSource.bypassEffects = file.bypassEffects; aSource.bypassListenerEffects = file.bypassListenerEffects; aSource.bypassReverbZones = file.bypassReverbZones; ApplyEffects(); }
// Start is called before the first frame update protected virtual void Start() { // Applies settings from the Audio File Object if (soundFile == null) { audioObject = AudioManager.instance.GetSoundLibrary()[sound]; spatialSound = audioObject.spatialize; priority = audioObject.priority; pitchShift = audioObject.pitchShift; delay = audioObject.delay; ignoreTimeScale = audioObject.ignoreTimeScale; } sTransform = (spatialSound) ? transform : null; }
public void PlayDebug(AudioFileObject file) { aSource.Stop(); if (file.playReversed) { aSource.time = aSource.clip.length - AudioManager.EPSILON; } else { aSource.timeSamples = 0; } aSource.Play(); aSource.pitch = AudioManager.GetRandomPitch(file); aSource.bypassEffects = file.bypassEffects; aSource.bypassListenerEffects = file.bypassListenerEffects; aSource.bypassReverbZones = file.bypassReverbZones; audioFile = file; ApplyVolumeChanges(); ApplyEffects(); }
private void OnSelectionChange() { if (Selection.activeObject == null) { return; } System.Type activeType = Selection.activeObject.GetType(); selectedClip = null; selectedSound = null; selectedMusic = null; if (activeType.Equals(typeof(AudioClip))) { selectedClip = (AudioClip)Selection.activeObject; CreateAudioHelper(selectedClip); } else if (activeType.Equals(typeof(AudioFileObject))) { selectedSound = ((AudioFileObject)Selection.activeObject); selectedClip = selectedSound.file; CreateAudioHelper(selectedClip); } else if (activeType.Equals(typeof(AudioFileMusicObject))) { selectedMusic = ((AudioFileMusicObject)Selection.activeObject); selectedClip = selectedMusic.file; CreateAudioHelper(selectedClip, true); } else { DoForceRepaint(true); return; } helperSource.clip = selectedClip; DoForceRepaint(true); }
public void PlayDebug(AudioFileObject file, bool dontReset) { if (!dontReset) { aSource.Stop(); } if (file.playReversed) { aSource.time = aSource.clip.length - AudioManager.EPSILON; } else { aSource.timeSamples = (int)Mathf.Clamp((float)aSource.timeSamples, 0, (float)aSource.clip.samples - 1); } aSource.Play(); aSource.pitch = AudioManager.GetRandomPitch(file); aSource.bypassEffects = file.bypassEffects; aSource.bypassListenerEffects = file.bypassListenerEffects; aSource.bypassReverbZones = file.bypassReverbZones; audioFile = file; ApplyVolumeChanges(); ApplyEffects(); }
/// <summary> /// </summary> /// <param name="soundProperty">This is passed by reference, thanks Unity!</param> protected void DrawSoundDropdown(SerializedProperty soundProperty) { if (!AudioManager.instance) { EditorGUILayout.HelpBox("Could not find Audio Manager in the scene! This component needs AudioManager " + "in order to function!", MessageType.Error); } EditorGUILayout.LabelField("Choose a Sound to Play", EditorStyles.boldLabel); GUIContent soundDesc = new GUIContent("Sound", "Sound that will be played"); AudioFileObject audioObject = (AudioFileObject)soundProperty.objectReferenceValue; int selected = 0; if (audioObject != null) { selected = options.IndexOf(audioObject.safeName); } if (selected == -1) { selected = 0; } if (options.Count > 0) { selected = EditorGUILayout.Popup(soundDesc, selected, options.ToArray()); soundProperty.objectReferenceValue = AudioManager.instance.GetSoundLibrary()[selected]; } else { using (new EditorGUI.DisabledScope(true)) { EditorGUILayout.Popup(soundDesc, selected, new string[] { "<None>" }); } } }
public void SetAudioFile(AudioFileObject file) { audioFile = file; }
/// <summary> /// Draws a playback /// </summary> /// <param name="music"></param> public void DrawPlaybackTool(AudioClip selectedClip, AudioFileObject selectedSound = null, AudioFileMusicObject selectedMusic = null) { Rect progressRect = ProgressBar((float)helperSource.timeSamples / (float)helperSource.clip.samples, selectedClip, selectedSound, selectedMusic); EditorGUI.BeginChangeCheck(); scrollbarProgress = GUILayout.HorizontalScrollbar(scrollbarProgress, scrollZoom, 0, MAX_SCROLL_ZOOM); if (EditorGUI.EndChangeCheck()) { DoForceRepaint(true); } EditorGUILayout.BeginHorizontal(); PollMouseEvents(progressRect); if (GUILayout.Button(s_BackIcon, new GUILayoutOption[] { GUILayout.MaxHeight(20) })) { if (selectedMusic) { if (selectedMusic.loopMode == LoopMode.LoopWithLoopPoints && selectedMusic.clampToLoopPoints) { helperSource.timeSamples = Mathf.CeilToInt((selectedMusic.loopStart * selectedClip.frequency)); } } else { helperSource.timeSamples = 0; } helperSource.Stop(); mouseScrubbed = false; clipPaused = false; clipPlaying = false; } // Draw Play Button Color colorbackup = GUI.backgroundColor; GUIContent buttonIcon = (clipPlaying) ? s_PlayIcons[1] : s_PlayIcons[0]; if (clipPlaying) { GUI.backgroundColor = buttonPressedColor; } if (GUILayout.Button(buttonIcon, new GUILayoutOption[] { GUILayout.MaxHeight(20) })) { clipPlaying = !clipPlaying; if (clipPlaying) { // Note: For some reason, reading from helperSource.time returns 0 even if timeSamples is not 0 // However, writing a value to helperSource.time changes timeSamples to the appropriate value just fine if (selectedSound) { AudioFileObjectEditor.instance.StartFading(selectedSound, helperSource.clip); } else if (selectedMusic) { helperHelper.PlayDebug(selectedMusic, mouseScrubbed); AudioFileMusicObjectEditor.firstPlayback = true; AudioFileMusicObjectEditor.freePlay = false; } else if (selectedClip) { helperHelper.PlayDebug(mouseScrubbed); } if (clipPaused) { helperSource.Pause(); } } else { helperSource.Stop(); if (!mouseScrubbed) { helperSource.time = 0; } clipPaused = false; } } // Pause button GUI.backgroundColor = colorbackup; GUIContent theText = (clipPaused) ? s_PauseIcons[1] : s_PauseIcons[0]; if (clipPaused) { GUI.backgroundColor = buttonPressedColor; } if (GUILayout.Button(theText, new GUILayoutOption[] { GUILayout.MaxHeight(20) })) { clipPaused = !clipPaused; if (clipPaused) { helperSource.Pause(); } else { helperSource.UnPause(); } } // Loop button GUI.backgroundColor = colorbackup; buttonIcon = (loopClip) ? s_LoopIcons[1] : s_LoopIcons[0]; if (loopClip) { GUI.backgroundColor = buttonPressedColor; } if (GUILayout.Button(buttonIcon, new GUILayoutOption[] { GUILayout.MaxHeight(20) })) { loopClip = !loopClip; // helperSource.loop = true; } GUI.backgroundColor = colorbackup; if (selectedSound) { using (new EditorGUI.DisabledScope(selectedSound.GetFileCount() < 2)) { if (GUILayout.Button(new GUIContent("Play Random", "Preview settings with a random track from your library. Only usable if this Audio File has \"Use Library\" enabled."))) { selectedClip = AudioFileObjectEditor.instance.DesignateRandomAudioClip(selectedSound); clipPlaying = true; helperSource.Stop(); AudioFileObjectEditor.instance.StartFading(selectedSound, selectedClip); } } } int loopPointInputMode = 0; // Reset loop point input mode if not using loop points so the duration shows up as time by default if (selectedMusic) { if (selectedMusic.loopMode != LoopMode.LoopWithLoopPoints) { loopPointInputMode = 0; } } GUIContent blontent = new GUIContent(); switch ((AudioFileMusicObjectEditor.LoopPointTool)loopPointInputMode) { case AudioFileMusicObjectEditor.LoopPointTool.Slider: case AudioFileMusicObjectEditor.LoopPointTool.TimeInput: blontent = new GUIContent(TimeToString((float)helperSource.timeSamples / helperSource.clip.frequency) + " / " + (TimeToString(helperSource.clip.length)), "The playback time in seconds"); break; case AudioFileMusicObjectEditor.LoopPointTool.TimeSamplesInput: blontent = new GUIContent(helperSource.timeSamples + " / " + helperSource.clip.samples, "The playback time in samples"); break; case AudioFileMusicObjectEditor.LoopPointTool.BPMInput: blontent = new GUIContent(string.Format("{0:0}", helperSource.time / (60f / selectedMusic.bpm)) + " / " + helperSource.clip.length / (60f / selectedMusic.bpm), "The playback time in beats"); break; } GUIStyle rightJustified = new GUIStyle(EditorStyles.label); rightJustified.alignment = TextAnchor.UpperRight; EditorGUILayout.LabelField(blontent, rightJustified); EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); }