-
Notifications
You must be signed in to change notification settings - Fork 0
/
AIIOM.transport.js
141 lines (126 loc) · 4.04 KB
/
AIIOM.transport.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Implemented:
- Play from loop/stop
- Record
- Loop on/off
- Prev./next cue marker
- Cursor
*/
function initTransport() {
// Creating views
transport = host.createTransport();
transport.getInPosition().markInterested();
transport.playStartPosition().markInterested();
transport.timeSignature().markInterested();
transport.getPosition().markInterested();
// Publishing observers
transport.isPlaying().addValueObserver(function (play) {
if (play) {
host.getMidiOutPort(0).sendMidi(MIDI_CC, BUTTON_03_PLAYSTOP, BUTTON_ON);
} else {
host.getMidiOutPort(0).sendMidi(MIDI_CC, BUTTON_03_PLAYSTOP, BUTTON_OFF);
}
});
transport.isArrangerRecordEnabled().addValueObserver(function (record) {
if (record) {
host.getMidiOutPort(0).sendMidi(MIDI_CC, BUTTON_12_RECORD, BUTTON_ON);
} else {
host.getMidiOutPort(0).sendMidi(MIDI_CC, BUTTON_12_RECORD, BUTTON_OFF);
}
})
transport.isArrangerLoopEnabled().addValueObserver(function (loop) {
if (loop) {
host.getMidiOutPort(0).sendMidi(MIDI_CC, BUTTON_10_LOOP, BUTTON_ON);
} else {
host.getMidiOutPort(0).sendMidi(MIDI_CC, BUTTON_10_LOOP, BUTTON_OFF);
}
})
return onMidiTransport;
}
function onMidiTransport(status, data1, data2) {
// Check if message is MIDI CC
if (isChannelController(status)) {
switch (data1) {
case BUTTON_03_PLAYSTOP:
handleTransportPlayStop(data2);
break;
case BUTTON_12_RECORD:
handleTransportRecord(data2);
break;
case BUTTON_10_LOOP:
handleTransportLoop(data2);
break;
case KNOB_02_CURSOR:
handleTransportCursor(data2);
break;
case BUTTON_04_PRECUE:
handleTransportPrevCue(data2);
break;
case BUTTON_05_NEXTCUE:
handleTransportNextCue(data2);
break;
case ENC_BUTTON_02_STARTSTOP:
handleTransportStartStop(data2);
break;
}
}
}
function handleTransportPlayStop(value) {
isPlaying = transport.isPlaying().get();
if (buttonValueToBoolean(value)) {
if (isPlaying) {
transport.isPlaying().toggle();
} else {
inPosition = transport.getInPosition().get(); // punch in position
transport.playStartPosition().set(inPosition);
transport.play();
}
}
}
function handleTransportRecord(value) {
if (buttonValueToBoolean(value)) {
transport.isArrangerRecordEnabled().toggle();
}
}
function handleTransportLoop(value) {
if (buttonValueToBoolean(value)) {
transport.isArrangerLoopEnabled().toggle();
}
}
function handleTransportPrevCue(value) {
if (buttonValueToBoolean(value)) {
transport.jumpToPreviousCueMarker();
}
}
function handleTransportNextCue(value) {
if (buttonValueToBoolean(value)) {
transport.jumpToNextCueMarker();
}
}
function handleTransportStartStop(value) {
isPlaying = transport.isPlaying().get();
if (buttonValueToBoolean(value)) {
transport.play();
}
}
function handleTransportCursor(value) {
position = transport.getPosition().get();
numberOfBeatsPerBar = transport.timeSignature().getNumerator().get();
if (isIncrementKnob02(value)) {
evenBar = Math.floor(position / numberOfBeatsPerBar);
evenPosition = evenBar * numberOfBeatsPerBar;
newPosition = evenPosition + (numberOfBeatsPerBar * KNOB_02_SPEED);
transport.getPosition().set(newPosition);
} else {
evenBar = Math.ceil(position / numberOfBeatsPerBar);
evenPosition = evenBar * numberOfBeatsPerBar;
newPosition = evenPosition - (numberOfBeatsPerBar * KNOB_02_SPEED);
transport.getPosition().set(newPosition);
}
}
function isIncrementKnob02(value) {
if (value <= 63) {
return false;
}
return true;
}