File size: 3,006 Bytes
b0bfea8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { WorkletSequencerReturnMessageType } from "./sequencer_message.js";

/**
 * Processes a single tick
 * @this {WorkletSequencer}
 */
export function processTick()
{
    if (!this.isActive)
    {
        return;
    }
    let current = this.currentTime;
    while (this.playedTime < current)
    {
        // find the next event
        let trackIndex = this._findFirstEventIndex();
        let event = this.tracks[trackIndex][this.eventIndex[trackIndex]];
        this._processEvent(event, trackIndex);
        
        this.eventIndex[trackIndex]++;
        
        // find the next event
        trackIndex = this._findFirstEventIndex();
        if (this.tracks[trackIndex].length <= this.eventIndex[trackIndex])
        {
            // the song has ended
            if (this.loop)
            {
                this.setTimeTicks(this.midiData.loop.start);
                return;
            }
            this.eventIndex[trackIndex]--;
            this.pause(true);
            if (this.songs.length > 1)
            {
                this.nextSong();
            }
            return;
        }
        let eventNext = this.tracks[trackIndex][this.eventIndex[trackIndex]];
        this.playedTime += this.oneTickToSeconds * (eventNext.ticks - event.ticks);
        
        const canLoop = this.loop && (this.loopCount > 0 || this.loopCount === -1);
        
        // if we reached loop.end
        if ((this.midiData.loop.end <= event.ticks) && canLoop)
        {
            // loop
            if (this.loopCount !== Infinity)
            {
                this.loopCount--;
                this.post(WorkletSequencerReturnMessageType.loopCountChange, this.loopCount);
            }
            this.setTimeTicks(this.midiData.loop.start);
            return;
        }
        // if the song has ended
        else if (current >= this.duration)
        {
            if (canLoop)
            {
                // loop
                if (this.loopCount !== Infinity)
                {
                    this.loopCount--;
                    this.post(WorkletSequencerReturnMessageType.loopCountChange, this.loopCount);
                }
                this.setTimeTicks(this.midiData.loop.start);
                return;
            }
            // stop the playback
            this.eventIndex[trackIndex]--;
            this.pause(true);
            if (this.songs.length > 1)
            {
                this.nextSong();
            }
            return;
        }
    }
}


/**
 * @returns {number} the index of the first to the current played time
 * @this {WorkletSequencer}
 */
export function _findFirstEventIndex()
{
    let index = 0;
    let ticks = Infinity;
    this.tracks.forEach((track, i) =>
    {
        if (this.eventIndex[i] >= track.length)
        {
            return;
        }
        if (track[this.eventIndex[i]].ticks < ticks)
        {
            index = i;
            ticks = track[this.eventIndex[i]].ticks;
        }
    });
    return index;
}