Beispiel #1
0
    private void SwitchNote(int note1, int note2)
    {
        NoteWithDuration temp = noteHeap[note1];

        noteHeap[note1] = noteHeap[note2];
        noteHeap[note2] = temp;
    }
Beispiel #2
0
 public highlight(NoteWithDuration Note, int Ticks, Transform Trans, HighlightState State, float ZHigh)
 {
     note  = Note;
     ticks = Ticks;
     trans = Trans;
     state = State;
     zHigh = ZHigh;
 }
Beispiel #3
0
 public NoteWithDuration(int ticks, int note, int duration, int trackId)
 {
     this.ticks    = ticks;
     this.note     = note;
     this.duration = duration;
     this.trackId  = trackId;
     next          = null;
 }
Beispiel #4
0
    public void Add(int ticks, int note, int duration, int trackId)
    {
        if (trackID != -1 && trackId != trackID)
        {
            throw new Exception(string.Format("Adding a note with different track ID. Each NotesList can only support notes with same track ID. New note ID {0}, current ID {1}",
                                              trackId, trackID));
        }
        NoteWithDuration newNode = new NoteWithDuration(ticks, note, duration, trackId);

        Add(ref newNode);
    }
Beispiel #5
0
 public void Insert(NoteWithDuration note)
 {
     if (note == null)
     {
         return;
     }
     if (size >= capacity)
     {
         throw new Exception("Can't insert. The size of the heap will exceed capacity if insert is done.");
     }
     noteHeap[size++] = note;
     BubbleUp(size - 1);
 }
Beispiel #6
0
    public NoteWithDuration DeleteMin()
    {
        if (size == 0)
        {
            return(null);
        }

        NoteWithDuration ret = noteHeap[0];

        noteHeap[0] = noteHeap[size - 1];
        size--;
        BubbleDown(0);
        return(ret);
    }
Beispiel #7
0
    public IEnumerable <NoteWithDuration> NoteIterator(int position)
    {
        NoteWithDuration cursor = head;

        while (cursor != null && cursor.AbsoluteTicks < position)
        {
            cursor = cursor.next;
        }

        while (cursor != null)
        {
            yield return(cursor);

            cursor = cursor.next;
        }
        yield return(null);
    }
Beispiel #8
0
 public void Add(ref NoteWithDuration node)
 {
     if (trackID != -1 && node.TrackId != trackID)
     {
         throw new Exception(string.Format("Adding a note with different track ID. Each NotesList can only support notes with same track ID. New note ID {0}, current ID {1}",
                                           node.TrackId, trackID));
     }
     if (head == null)
     {
         head    = node;
         tail    = head;
         trackID = node.TrackId;
         return;
     }
     // enforce ticks in ascending order
     if (tail.AbsoluteTicks > node.AbsoluteTicks)
     {
         throw new Exception("Adding a note with lower ticks than ticks of the note at the end of the list. Violates the tick order of the list.");
     }
     tail.next = node;
     tail      = tail.next;
 }
Beispiel #9
0
    public SequencerWP()
    {
        dispatcher.MetaMessageDispatched += delegate(object sender, MetaMessageEventArgs e)
        {
            if (e.Message.MetaType == MetaType.EndOfTrack)
            {
                tracksPlayingCount--;
                UnityEngine.Debug.LogFormat("end. t:{0}", tracksPlayingCount);
                if (tracksPlayingCount == 0)
                {
                    Stop(true);
                    // playing completed handles initialization if looping is enabled.
                    OnPlayingCompleted(EventArgs.Empty);
                }
            }
            else
            {
                clock.Process(e.Message);
            }
        };

        dispatcher.ChannelMessageDispatched += delegate(object sender, ChannelMessageEventArgs e)
        {
            stopper.Process(e.Message);
        };

        clock.Tick += delegate(object sender, EventArgs e)
        {
            lock (lockObject)
            {
                if (!playing)
                {
                    return;
                }

                if (Position == LoopEnd)
                {
                    Stop(true);
                    // playing completed handles initialization if looping is enabled.
                    OnPlayingCompleted(EventArgs.Empty);
                    return;
                }

                // only go through tick iterator if we are out of start offset
                if (Position >= LoopStart)
                {
                    foreach (IEnumerator <int> enumerator in enumerators)
                    {
                        enumerator.MoveNext();
                    }
                }
            }
        };

        sequence = GlobalReference.sequence;

        if (Sequence == null)
        {
            throw new Exception("Sequence should not be null.");
        }
        // Process each track and produce the queue of notes to played. This queue is the player paced sheet music.
        Dictionary <int, NoteWithDuration> noteToTicksTable = new Dictionary <int, NoteWithDuration>();

        foreach (Track t in Sequence)
        {
            if (t.TrackId == -1)
            {
                throw new Exception("Track ID is 0. Track is not loaded correctly.");
            }

            // not part of user select track to play
            if (t.TrackId != GlobalReference.leftHandTrackIndex && t.TrackId != GlobalReference.rightHandTrackIndex)
            {
                continue;
            }

            noteToTicksTable.Clear();
            notesList.Add(new NotesList());
            int trackIndex = notesList.Count - 1;

            IEnumerator <MidiEvent> enumerator = t.Iterator().GetEnumerator();
            while (enumerator.MoveNext())
            {
                if (enumerator.Current.MidiMessage.MessageType != MidiMessageType.Channel)
                {
                    continue;
                }

                ChannelMessage channelMsg = (ChannelMessage)enumerator.Current.MidiMessage;

                // Store absolute ticks of NoteOn message in hash table
                if (channelMsg.Command == ChannelCommand.NoteOn)
                {
                    if (channelMsg.Data2 == 0) // Note on message with velocity of 0 is equivalent of Note Off Message
                    {
                        NoteWithDuration noteOff;
                        if (noteToTicksTable.TryGetValue(channelMsg.Data1, out noteOff))
                        {
                            //UnityEngine.Debug.LogFormat("OFF, {0}", channelMsg.Data1);
                            noteOff.Duration = enumerator.Current.AbsoluteTicks - noteOff.AbsoluteTicks; // TODO: Test if this works
                            noteToTicksTable.Remove(channelMsg.Data1);
                        }
                    }
                    else
                    {
                        //UnityEngine.Debug.LogFormat("ON, {0} Ticks:{1}", channelMsg.Data1, enumerator.Current.AbsoluteTicks);
                        if (!noteToTicksTable.ContainsKey(channelMsg.Data1))
                        {
                            NoteWithDuration note = new NoteWithDuration(enumerator.Current.AbsoluteTicks, channelMsg.Data1, -1, channelMsg.MidiTrack);
                            notesList[trackIndex].Add(ref note);
                            noteToTicksTable.Add(channelMsg.Data1, note);
                        }
                        else
                        {
                            UnityEngine.Debug.LogFormat("Multiple Note On Msgs. Ignored ID: {0}", channelMsg.Data1);
                        }
                    }
                }
                // When encounter NoteOff message, Find corresponding NoteOn message in hash table
                // and calculate duration. Update the duration of that note.
                else if (channelMsg.Command == ChannelCommand.NoteOff)
                {
                    NoteWithDuration noteOff;
                    if (noteToTicksTable.TryGetValue(channelMsg.Data1, out noteOff))
                    {
                        //UnityEngine.Debug.LogFormat("OFF, {0}", channelMsg.Data1);
                        noteOff.Duration = enumerator.Current.AbsoluteTicks - noteOff.AbsoluteTicks; // TODO: Test if this works
                        noteToTicksTable.Remove(channelMsg.Data1);
                    }
                }
            }
        }

        // Find first tick of NoteOn message among the tracks
        for (int i = 0; i < notesList.Count; i++)
        {
            firstTick = (firstTick == -1 || notesList[i].FirstTick < firstTick) ? notesList[i].FirstTick : firstTick;
        }

        // Find last tick of NoteOn message among the tracks
        for (int i = 0; i < Sequence.Count; i++)
        {
            UnityEngine.Debug.LogFormat("Sequence track length: {0}", Sequence[i].Length);
            lastTick = (lastTick == -1 || Sequence[i].Length > lastTick) ? Sequence[i].Length : lastTick;
        }
    }
Beispiel #10
0
 public void Clear()
 {
     head    = null;
     tail    = null;
     trackID = -1;
 }
Beispiel #11
0
 public NotesList()
 {
     head    = null;
     tail    = null;
     trackID = -1;
 }