Exemple #1
0
    void repaint()
    {
        var clip = selectedAudioClip;

        if (clip == null)
        {
            return;
        }

        offset = AudioUtilBindings.getClipSamplePosition(clip);

        for (var i = 0; i < clip.channels; i++)
        {
            drawChannel(channelRect(i, clip.channels), channel: i);
        }

        selectionRenderer.drawHover(scrollRect, Color.red);
        selectionRenderer.drawSelection(scrollRect, Color.red);

        if (AudioUtilBindings.isClipPlaying(clip))
        {
            var currentSample = AudioUtilBindings.getClipSamplePosition(clip);
            var xpos          = (currentSample / (float)currentAudioData.sampleCount) * scrollRect.width;
            EditorGUI.DrawRect(new Rect(xpos, 0, 2, scrollRect.height), Color.green);
            Repaint();
        }

        if (handleTexture != null)
        {
            volumeGraph.drawMouseHover(channelRect(0, currentAudioData.channels), handleTexture);
        }
    }
Exemple #2
0
    void onSelectionRangeChanged()
    {
        var clip = selectedAudioClip;

        if (clip == null)
        {
            return;
        }

        if (AudioUtilBindings.isClipPlaying(clip) && isPaused)
        {
            AudioUtilBindings.stopAllClips();
        }
    }
Exemple #3
0
    void OnGUI()
    {
        var clip = selectedAudioClip;
        var text = (clip != null ? clip.name : "No audio clip selected");

        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("Audio editor: " + text);

        if (clip != null && selectionRenderer.hasSelection)
        {
            if (GUILayout.Button("Fade in"))
            {
                volumeGraph.applyFadeIn(selectionRenderer.selectionFrom, selectionRenderer.selectionTo); Repaint();
            }
            if (GUILayout.Button("Fade out"))
            {
                volumeGraph.applyFadeOut(selectionRenderer.selectionFrom, selectionRenderer.selectionTo); Repaint();
            }
            ;
            if (GUILayout.Button("Silence"))
            {
                volumeGraph.applySilence(selectionRenderer.selectionFrom, selectionRenderer.selectionTo); Repaint();
            }

            if (selectionRenderer.hasSelectionRange)
            {
                if (GUILayout.Button("Crop"))
                {
                    cropAudio(selectionRenderer.selectionFrom, selectionRenderer.selectionTo); Repaint();
                }
            }
            else
            {
                if (GUILayout.Button("crop from"))
                {
                    cropAudio(selectionRenderer.selectionFrom, to: 1); Repaint();
                }
                if (GUILayout.Button("crop to"))
                {
                    cropAudio(from: 0, selectionRenderer.selectionTo); Repaint();
                }
            }
        }

        if (GUILayout.Button("Export"))
        {
            exportAudio(); Repaint();
        }

        if (clip != null)
        {
            if (AudioUtilBindings.isClipPlaying(clip))
            {
                if (GUILayout.Button("Stop"))
                {
                    AudioUtilBindings.stopAllClips();
                    isPaused = false;
                }

                if (!isPaused)
                {
                    if (GUILayout.Button("Pause"))
                    {
                        isPaused = true;
                        AudioUtilBindings.pauseClip(clip);
                    }
                }
                else
                {
                    if (GUILayout.Button("Play"))
                    {
                        isPaused = false;
                        AudioUtilBindings.resumeClip(clip);
                    }
                }
            }
            else
            {
                if (GUILayout.Button("Play"))
                {
                    isPaused = false;
                    AudioUtilBindings.stopAllClips();
                    AudioUtilBindings.playClip(clip);

                    if (selectionRenderer.hasSelection)
                    {
                        var sampleFrom = Mathf.FloorToInt(selectionRenderer.selectionFrom * currentAudioData.sampleCount);
                        AudioUtilBindings.setClipSamplePosition(clip, sampleFrom);
                    }
                }
            }

            EditorGUILayout.PrefixLabel("Playback volume");
            playbackVolume = EditorGUILayout.Slider(playbackVolume, 0, 1);
        }

        EditorGUILayout.EndHorizontal();

        if (clip == null)
        {
            return;
        }

        if (AudioUtilBindings.isClipPlaying(clip) && selectionRenderer.hasSelectionRange)
        {
            var currentSample = AudioUtilBindings.getClipSamplePosition(clip);
            var sampleFrom    = Mathf.FloorToInt(selectionRenderer.selectionFrom * currentAudioData.sampleCount);
            var sampleTo      = Mathf.FloorToInt(selectionRenderer.selectionTo * currentAudioData.sampleCount);

            if (currentSample >= sampleTo)
            {
                AudioUtilBindings.stopAllClips();
            }
        }

        var mouseInScrollView = contentRect.containsPoint(Event.current.mousePosition);

        scrollPos = GUI.BeginScrollView(contentRect, scrollPos, scrollRect, alwaysShowHorizontal: true, alwaysShowVertical: false);

        if (volumeGraph.handleMouseEvent(Event.current, channelRect(0, currentAudioData.channels)))
        {
            Event.current.Use();
            Repaint();
        }
        else if (selectionRenderer.handleEvent(Event.current, scrollRect))
        {
            Event.current.Use();
            Repaint();
        }
        else
        {
            switch (Event.current.type)
            {
            case EventType.Repaint: repaint(); break;

            case EventType.ScrollWheel:
                if (mouseInScrollView)
                {
                    // var mouseBefore = Mathf.InverseLerp(0, scrollRect.width, Event.current.mousePosition.x);
                    scale = Mathf.Clamp(scale - Event.current.delta.y * 0.1f, min: 1, max: 40);
                    // var mouseAfter = Mathf.InverseLerp(0, scrollRect.width, Event.current.mousePosition.x);
                    // Debug.Log($"Before: {mouseBefore}, after: {mouseAfter}");
                    // scrollPos.x += (mouseAfter - mouseBefore);

                    lastScrollTime = Stopwatch.GetTimestamp();
                    Repaint();
                    Event.current.Use();
                }
                break;

            case EventType.KeyDown:
                if (Event.current.keyCode == KeyCode.Space)
                {
                    if (clip == null)
                    {
                        break;
                    }

                    Event.current.Use();
                    if (AudioUtilBindings.isClipPlaying(clip))
                    {
                        if (!isPaused)
                        {
                            isPaused = true;
                            AudioUtilBindings.pauseClip(clip);
                        }
                        else
                        {
                            isPaused = false;
                            AudioUtilBindings.resumeClip(clip);
                        }
                    }
                    else
                    {
                        isPaused = false;
                        AudioUtilBindings.stopAllClips();
                        AudioUtilBindings.playClip(clip);

                        if (selectionRenderer.hasSelection)
                        {
                            var sampleFrom = Mathf.FloorToInt(selectionRenderer.selectionFrom * currentAudioData.sampleCount);
                            AudioUtilBindings.setClipSamplePosition(clip, sampleFrom);
                        }
                    }
                }
                else if (Event.current.keyCode == KeyCode.LeftArrow)
                {
                    Event.current.Use();
                    selectionRenderer.moveSelection(seconds: -1, clipLength: clip.length, moveTo: (Event.current.modifiers & EventModifiers.Shift) == 0);
                    Repaint();
                }
                else if (Event.current.keyCode == KeyCode.RightArrow)
                {
                    Event.current.Use();
                    selectionRenderer.moveSelection(seconds: 1, clipLength: clip.length, moveTo: (Event.current.modifiers & EventModifiers.Shift) == 0);
                    Repaint();
                }
                break;
            }
        }

        GUI.EndScrollView();
    }