Exemplo n.º 1
0
 void PrepNote(AudioSource audioSource, Keyzone keyzone, int note, float velocity)
 {
     audioSource.pitch = Utils.MidiChangeToRatio(note - keyzone.rootKey);
     audioSource.clip  = keyzone.audioClip;
     audioSource.outputAudioMixerGroup = keyzone.mixer;
     audioSource.volume = Mathf.Lerp(1.0f - velocityTracking, 1.0f, velocity);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Adds an empty keyzone to the Sampler.
        /// </summary>
        /// <returns>The keyzone created.</returns>
        public Keyzone AddKeyzone()
        {
            Keyzone keyzone = new Keyzone();

            keyzones.Add(keyzone);
            return(keyzone);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Removes a keyzone from the Sampler.
        /// </summary>
        /// <returns>The removed keyzones index. -1 if it doesnt exist.</returns>
        /// <param name="keyzone">The keyzone to remove.</param>
        public int RemoveKeyzone(Keyzone keyzone)
        {
            int index = keyzones.IndexOf(keyzone);

            keyzones.Remove(keyzone);
            return(index);
        }
Exemplo n.º 4
0
        List <Keyzone> GetKeyzonesToPlay(List <Keyzone> validKeyzones)
        {
            if (keyzonePlayMode == KeyzonePlayMode.kAll || validKeyzones.Count == 0)
            {
                return(validKeyzones);
            }

            List <Keyzone> toPlay = new List <Keyzone>();

            if (keyzonePlayMode == KeyzonePlayMode.kRoundRobin)
            {
                Keyzone oldest     = validKeyzones[0];
                double  oldestTime = oldest.lastScheduled;
                foreach (Keyzone keyzone in validKeyzones)
                {
                    if (oldestTime > keyzone.lastScheduled)
                    {
                        oldest     = keyzone;
                        oldestTime = keyzone.lastScheduled;
                    }
                }
                toPlay.Add(oldest);
            }
            else if (keyzonePlayMode == KeyzonePlayMode.kRandom)
            {
                int index = UnityEngine.Random.Range(0, validKeyzones.Count);
                toPlay.Add(validKeyzones[index]);
            }

            return(toPlay);
        }
Exemplo n.º 5
0
        void AddKeyzone(Sampler sampler, SerializedProperty keyzones)
        {
            Keyzone keyzone = sampler.AddKeyzone();

            if (sampler.keyzones.Count >= 2)
            {
                Keyzone lastKeyzone = sampler.keyzones[sampler.keyzones.Count - 2];
                int     min         = lastKeyzone.maxKey + 1;
                int     root        = min + lastKeyzone.rootKey - lastKeyzone.minKey;
                int     max         = min + lastKeyzone.maxKey - lastKeyzone.minKey;
                if (max < Utils.kMidiSize && root < Utils.kMidiSize)
                {
                    keyzone.minKey  = min;
                    keyzone.maxKey  = max;
                    keyzone.rootKey = root;
                }
                keyzone.mixer = lastKeyzone.mixer;
            }

            keyzones.arraySize++;
            SerializedProperty newKeyzone = keyzones.GetArrayElementAtIndex(keyzones.arraySize - 1);

            newKeyzone.FindPropertyRelative("audioClip").objectReferenceValue = keyzone.audioClip;
            newKeyzone.FindPropertyRelative("rootKey").intValue       = keyzone.rootKey;
            newKeyzone.FindPropertyRelative("minKey").intValue        = keyzone.minKey;
            newKeyzone.FindPropertyRelative("maxKey").intValue        = keyzone.maxKey;
            newKeyzone.FindPropertyRelative("minVelocity").floatValue = keyzone.minVelocity;
            newKeyzone.FindPropertyRelative("maxVelocity").floatValue = keyzone.maxVelocity;
        }
Exemplo n.º 6
0
        void MouseDown(float key, int row, Sampler sampler)
        {
            currentKeyzone = null;
            if (row < 0 || row >= sampler.keyzones.Count * 2)
            {
                return;
            }

            int  keyzoneIndex = row / 2;
            bool rootRow      = row % 2 == 0;

            if (keyzoneIndex < 0 || keyzoneIndex >= sampler.keyzones.Count)
            {
                return;
            }

            Keyzone keyzone = sampler.keyzones[keyzoneIndex];

            if (rootRow)
            {
                if (keyzone.rootKey <= key + rootHandleKeyRadius && keyzone.rootKey + 1 >= key - rootHandleKeyRadius)
                {
                    Undo.RecordObject(sampler, "Change Keyzone Root");

                    mouseMode      = MouseMode.kDraggingRoot;
                    currentKeyzone = keyzone;
                    pressOffset    = Mathf.FloorToInt(key) - keyzone.rootKey;
                }
            }
            else if (keyzone.minKey <= key && keyzone.maxKey + 1 >= key)
            {
                currentKeyzone = keyzone;

                int pixelsFromStart = (int)((key - keyzone.minKey) * keyWidth);
                int pixelsFromEnd   = (int)((keyzone.maxKey + 1 - key) * keyWidth);
                if (pixelsFromStart <= resizeHandleWidth && pixelsFromStart < pixelsFromEnd)
                {
                    Undo.RecordObject(sampler, "Change Keyzone Start Note");
                    mouseMode = MouseMode.kDraggingRangeStart;
                }
                else if (pixelsFromEnd <= resizeHandleWidth)
                {
                    Undo.RecordObject(sampler, "Change Keyzone End Note");
                    mouseMode = MouseMode.kDraggingRangeEnd;
                }
                else
                {
                    Undo.RecordObject(sampler, "Drag Keyzone Range");
                    mouseMode = MouseMode.kDraggingRange;
                }

                pressOffset = Mathf.FloorToInt(key) - currentKeyzone.minKey;
            }
        }
Exemplo n.º 7
0
 void MouseUp(float key, int row, Sampler sampler)
 {
     currentKeyzone = null;
 }
Exemplo n.º 8
0
        void DrawClips(Sampler sampler, SerializedProperty keyzones)
        {
            int      height = rowHeight / 2;
            GUIStyle style  = new GUIStyle(GUI.skin.button);

            style.padding  = new RectOffset(0, 0, 0, 0);
            style.fontSize = height - 4;
            int y = keyboardHeight;

            Keyzone remove = null;

            foreach (Keyzone keyzone in sampler.keyzones)
            {
                Rect buttonRect = new Rect(0, y, height, height);
                Rect clipRect   = new Rect(buttonRect.xMax, y, keyzoneWidth - velocityWidth - buttonRect.width, height);
                Rect mixerRect  = new Rect(buttonRect.xMax, y + height, keyzoneWidth - velocityWidth - buttonRect.width, height);

                if (GUI.Button(buttonRect, "X", style))
                {
                    remove = keyzone;
                }

                AudioClip clip = EditorGUI.ObjectField(clipRect, keyzone.audioClip, typeof(AudioClip), false)
                                 as AudioClip;
                AudioMixerGroup mixer = EditorGUI.ObjectField(mixerRect, keyzone.mixer, typeof(AudioMixerGroup), false)
                                        as AudioMixerGroup;

                Rect  velocityMinRect = new Rect(clipRect.xMax, y, velocityWidth, height);
                Rect  velocityMaxRect = new Rect(mixerRect.xMax, y + height, velocityWidth, height);
                float minVelocity     = Mathf.Clamp(EditorGUI.FloatField(velocityMinRect, keyzone.minVelocity), 0.0f, 1.0f);
                float maxVelocity     = Mathf.Clamp(EditorGUI.FloatField(velocityMaxRect, keyzone.maxVelocity), 0.0f, 1.0f);

                if (clip != keyzone.audioClip)
                {
                    if (clip == null)
                    {
                        Undo.RecordObject(sampler, "Remove AudioClip from Keyzone");
                    }
                    else
                    {
                        Undo.RecordObject(sampler, "Change AudioClip in Keyzone");
                    }
                    keyzone.audioClip = clip;
                }
                if (mixer != keyzone.mixer)
                {
                    if (mixer == null)
                    {
                        Undo.RecordObject(sampler, "Remove AudioMixerGroup from Keyzone");
                    }
                    else
                    {
                        Undo.RecordObject(sampler, "Change AudioMixerGroup in Keyzone");
                    }
                    keyzone.mixer = mixer;
                }
                if (minVelocity != keyzone.minVelocity)
                {
                    Undo.RecordObject(sampler, "Change Keyzone Minimum Velocity");
                    keyzone.minVelocity = minVelocity;
                }
                if (maxVelocity != keyzone.maxVelocity)
                {
                    Undo.RecordObject(sampler, "Change Keyzone Maximum Velocity");
                    keyzone.maxVelocity = maxVelocity;
                }
                y += rowHeight;
            }

            if (remove != null)
            {
                Undo.RecordObject(sampler, "Delete Keyzone");
                int index = sampler.RemoveKeyzone(remove);
                if (index >= 0)
                {
                    keyzones.DeleteArrayElementAtIndex(index);
                }
            }
        }