summaryrefslogtreecommitdiff
path: root/libs/surfaces/launch_control_xl/controllers.cc
blob: b8a72272a38714118f6dacce8d3478086cd3f289 (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/*
  Copyright (C) 2016 Paul Davis

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <algorithm>

#include "ardour/debug.h"
#include "ardour/mute_control.h"
#include "ardour/session.h"
#include "ardour/solo_control.h"
#include "ardour/solo_isolate_control.h"

#include "launch_control_xl.h"

using namespace ArdourSurface;
using namespace ARDOUR;
using namespace PBD;
using std::cerr;

void
LaunchControlXL::build_maps ()
{
	/* Knobs */

	boost::shared_ptr<Knob> knob;

	#define MAKE_KNOB(i,cc, index) \
		knob.reset (new Knob ((i), (cc), (index), (*this))); \
		cc_knob_map.insert (std::make_pair (knob->controller_number(), knob)); \
		id_knob_map.insert (std::make_pair (knob->id(), knob))

		for (uint8_t n = 0; n < 8; ++n) {
			MAKE_KNOB (static_cast<KnobID>(n), (n + 13), n);
			MAKE_KNOB (static_cast<KnobID>(n + 8), (n + 29), (n + 8));
			MAKE_KNOB (static_cast<KnobID>(n + 16), (n + 49), (n + 16));
		}

	/* Faders */

	boost::shared_ptr<Fader> fader;

	#define MAKE_FADER(i,cc) \
		fader.reset (new Fader ((i), (cc))); \
		cc_fader_map.insert (std::make_pair (fader->controller_number(), fader)); \
		id_fader_map.insert (std::make_pair (fader->id(), fader))

		for (uint8_t n = 0; n < 8; ++n) {
			MAKE_FADER (static_cast<FaderID>(n), (n + 77) );
		}

	/* Buttons */

	boost::shared_ptr<ControllerButton> controller_button;
	boost::shared_ptr<NoteButton> note_button;


	#define MAKE_TRACK_BUTTON_PRESS(i,nn,index,color,p) \
		note_button.reset (new TrackButton ((i), (nn), (index), (color), (p), (*this))); \
		nn_note_button_map.insert (std::make_pair (note_button->note_number(), note_button)); \
		id_note_button_map.insert (std::make_pair (note_button->id(), note_button))
	#define MAKE_SELECT_BUTTON_PRESS(i,cc,index,p) \
		controller_button.reset (new SelectButton ((i), (cc), (index), (p), (*this))); \
		cc_controller_button_map.insert (std::make_pair (controller_button->controller_number(), controller_button)); \
		id_controller_button_map.insert (std::make_pair (controller_button->id(), controller_button))
	#define MAKE_TRACK_STATE_BUTTON_PRESS(i,nn,index,p) \
		note_button.reset (new TrackStateButton ((i), (nn), (index), (p), (*this))); \
		nn_note_button_map.insert (std::make_pair (note_button->note_number(), note_button)); \
		id_note_button_map.insert (std::make_pair (note_button->id(), note_button))
		#define MAKE_TRACK_STATE_BUTTON_PRESS_RELEASE_LONG(i,nn,index, p,r,l) \
			note_button.reset (new TrackStateButton ((i), (nn), (index), (p), (r), (l), (*this))); \
			nn_note_button_map.insert (std::make_pair (note_button->note_number(), note_button)); \
			id_note_button_map.insert (std::make_pair (note_button->id(), note_button))


	MAKE_TRACK_BUTTON_PRESS(Focus1, 41, 24, YellowLow, &LaunchControlXL::button_track_focus_1);
	MAKE_TRACK_BUTTON_PRESS(Focus2, 42, 25, YellowLow, &LaunchControlXL::button_track_focus_2);
	MAKE_TRACK_BUTTON_PRESS(Focus3, 43, 26, YellowLow, &LaunchControlXL::button_track_focus_3);
	MAKE_TRACK_BUTTON_PRESS(Focus4, 44, 27, YellowLow, &LaunchControlXL::button_track_focus_4);
	MAKE_TRACK_BUTTON_PRESS(Focus5, 57, 28, YellowLow, &LaunchControlXL::button_track_focus_5);
	MAKE_TRACK_BUTTON_PRESS(Focus6, 58, 29, YellowLow, &LaunchControlXL::button_track_focus_6);
	MAKE_TRACK_BUTTON_PRESS(Focus7, 59, 30, YellowLow, &LaunchControlXL::button_track_focus_7);
	MAKE_TRACK_BUTTON_PRESS(Focus8, 60, 31, YellowLow, &LaunchControlXL::button_track_focus_8);
	MAKE_TRACK_BUTTON_PRESS(Control1, 73, 32, AmberLow, &LaunchControlXL::button_track_control_1);
	MAKE_TRACK_BUTTON_PRESS(Control2, 74, 33, AmberLow, &LaunchControlXL::button_track_control_2);
	MAKE_TRACK_BUTTON_PRESS(Control3, 75, 34, AmberLow, &LaunchControlXL::button_track_control_3);
	MAKE_TRACK_BUTTON_PRESS(Control4, 76, 35, AmberLow, &LaunchControlXL::button_track_control_4);
	MAKE_TRACK_BUTTON_PRESS(Control5, 89, 36, AmberLow, &LaunchControlXL::button_track_control_5);
	MAKE_TRACK_BUTTON_PRESS(Control6, 90, 37, AmberLow, &LaunchControlXL::button_track_control_6);
	MAKE_TRACK_BUTTON_PRESS(Control7, 91, 38, AmberLow, &LaunchControlXL::button_track_control_7);
	MAKE_TRACK_BUTTON_PRESS(Control8, 92, 39, AmberLow, &LaunchControlXL::button_track_control_8);

	MAKE_SELECT_BUTTON_PRESS(SelectUp, 104, 44, &LaunchControlXL::button_select_up);
	MAKE_SELECT_BUTTON_PRESS(SelectDown, 105, 45, &LaunchControlXL::button_select_down);
	MAKE_SELECT_BUTTON_PRESS(SelectLeft, 106, 46, &LaunchControlXL::button_select_left);
	MAKE_SELECT_BUTTON_PRESS(SelectRight, 107, 47, &LaunchControlXL::button_select_right);

	MAKE_TRACK_STATE_BUTTON_PRESS_RELEASE_LONG(Device, 105, 40, &LaunchControlXL::relax, &LaunchControlXL::button_device, &LaunchControlXL::button_device_long_press);;
	MAKE_TRACK_STATE_BUTTON_PRESS(Mute, 106, 41, &LaunchControlXL::button_mute);
	MAKE_TRACK_STATE_BUTTON_PRESS(Solo, 107, 42, &LaunchControlXL::button_solo);
	MAKE_TRACK_STATE_BUTTON_PRESS(Record, 108, 43, &LaunchControlXL::button_record);

}

std::string
LaunchControlXL::button_name_by_id (ButtonID id)
{
	switch (id) {
		case Device:
			return "Device";
		case Mute:
			return "Mute";
		case Solo:
			return "Solo";
		case Record:
			return "Record";
		case SelectUp:
			return "Select Up";
		case SelectDown:
			return "Select Down";
		case SelectRight:
			return "Select Right";
		case SelectLeft:
			return "Select Left";
		case Focus1:
			return "Focus 1";
		case Focus2:
			return "Focus 2";
		case Focus3:
			return "Focus 3";
		case Focus4:
			return "Focus 4";
		case Focus5:
			return "Focus 5";
		case Focus6:
			return "Focus 6";
		case Focus7:
			return "Focus 7";
		case Focus8:
			return "Focus 8";
		case Control1:
			return "Control 1";
		case Control2:
			return "Control 2";
		case Control3:
			return "Control 3";
		case Control4:
			return "Control 4";
		case Control5:
			return "Control 5";
		case Control6:
			return "Control 6";
		case Control7:
			return "Control 7";
		case Control8:
			return "Control 8";
	default:
		break;
	}

	return "???";
}

std::string
LaunchControlXL::knob_name_by_id (KnobID id)
{
	switch (id) {
		case SendA1:
			return "SendA 1";
		case SendA2:
			return "SendA 2";
		case SendA3:
			return "SendA 3";
		case SendA4:
			return "SendA 4";
		case SendA5:
			return "SendA 5";
		case SendA6:
			return "SendA 6";
		case SendA7:
			return "SendA 7";
		case SendA8:
			return "SendA 8";
		case SendB1:
			return "SendB 1";
		case SendB2:
			return "SendB 2";
		case SendB3:
			return "SendB 3";
		case SendB4:
			return "SendB 4";
		case SendB5:
			return "SendB 5";
		case SendB6:
			return "SendB 6";
		case SendB7:
			return "SendB 7";
		case SendB8:
			return "SendB 8";
		case Pan1:
			return "Pan 1";
		case Pan2:
			return "Pan 2";
		case Pan3:
			return "Pan 3";
		case Pan4:
			return "Pan 4";
		case Pan5:
			return "Pan 5";
		case Pan6:
			return "Pan 6";
		case Pan7:
			return "Pan 7";
		case Pan8:
			return "Pan 8";
	default:
		break;
	}

	return "???";
}

std::string
LaunchControlXL::fader_name_by_id (FaderID id)
{
	switch (id) {
		case Fader1:
			return "Fader 1";
		case Fader2:
			return "Fader 2";
		case Fader3:
			return "Fader 3";
		case Fader4:
			return "Fader 4";
		case Fader5:
			return "Fader 5";
		case Fader6:
			return "Fader 6";
		case Fader7:
			return "Fader 7";
		case Fader8:
			return "Fader 8";
	default:
		break;
	}

	return "???";
}

boost::shared_ptr<LaunchControlXL::TrackButton>
LaunchControlXL::track_button_by_range(uint8_t n, uint8_t first, uint8_t middle)
{
	NNNoteButtonMap::iterator b;
	if ( n < 4)	{
	 	b = nn_note_button_map.find (first + n);
	} else {
		b = nn_note_button_map.find (middle + n - 4);
	}

	boost::shared_ptr<TrackButton> button = 0;

	if (b != nn_note_button_map.end()) {
		button = boost::dynamic_pointer_cast<TrackButton> (b->second);
	}

	return button;

}

void
LaunchControlXL::update_track_focus_led(uint8_t n)
{
	boost::shared_ptr<TrackButton> b = focus_button_by_column(n);

	if (!b) {
		return;
	}

	if (stripable[n]) {
		if ( stripable[n]->is_selected() ) {
			b->set_color(YellowFull);
		} else {
			b->set_color(AmberLow);
		}
	} else {
		b->set_color(Off);
	}

	write (b->state_msg());
}

void
LaunchControlXL::button_track_focus(uint8_t n)
{
	if (buttons_down.find(Device) != buttons_down.end()) {
		DEBUG_TRACE (DEBUG::LaunchControlXL, "DEVICE BUTTON HOLD\n");
		if (stripable[n]->solo_isolate_control()) {
			bool solo_isolate_active = stripable[n]->solo_isolate_control()->get_value();
			stripable[n]->solo_isolate_control()->set_value (!solo_isolate_active, PBD::Controllable::UseGroup);
		}
		return;
	}

	if (stripable[n]) {
		if ( stripable[n]->is_selected() ) {
			 ControlProtocol::RemoveStripableFromSelection (stripable[n]);
		} else {
			ControlProtocol::AddStripableToSelection (stripable[n]);
		}
	} else {
		return;
	}
}



boost::shared_ptr<AutomationControl>
LaunchControlXL::get_ac_by_state(uint8_t n) {
		boost::shared_ptr<AutomationControl> ac;

		switch(track_mode()) {
			case TrackMute:
				ac = stripable[n]->mute_control();
				break;

			case TrackSolo:
				ac = stripable[n]->solo_control();
				break;

			case TrackRecord:
				ac = stripable[n]->rec_enable_control();
				break;

			default:
			break;
		}
		return ac;
}

boost::shared_ptr<LaunchControlXL::Knob>*
LaunchControlXL::knobs_by_column(uint8_t col, boost::shared_ptr<Knob>* knob_col)
{
	for (uint8_t n = 0; n < 3; ++n) {
		knob_col[n] = id_knob_map.find(static_cast<KnobID>(col+n*8))->second;
	}

	return knob_col;
}

void
LaunchControlXL::update_knob_led(uint8_t n)
{
	LEDColor color;

	uint32_t absolute_strip_num = (n + bank_start) % 8;

	if (stripable[n]) {
		switch (absolute_strip_num) {
			case 0:
			case 4:
				if (stripable[n]->is_selected()) {
					color = RedFull;
				} else {
					color = RedLow;
				}
				break;

			case 1:
			case 5:
				if (stripable[n]->is_selected()) {
					color = YellowFull;
				} else {
					color = YellowLow;
				}
				break;

			case 2:
			case 6:
				if (stripable[n]->is_selected()) {
					color = GreenFull;
				} else {
					color = GreenLow;
				}
				break;

			case 3:
			case 7:
				if (stripable[n]->is_master()) {
					color = RedFull;
				} else {
					if (stripable[n]->is_selected()) {
						color = AmberFull;
					} else {
						color = AmberLow;
					}
				}
		}
	}

	boost::shared_ptr<Knob> knobs_col[3];
	knobs_by_column(n, knobs_col);

	for  (uint8_t s = 0; s < 3; ++s)
	{
		if (stripable[n]) {
			knobs_col[s]->set_color(color);
		} else {
			knobs_col[s]->set_color(Off);
		}
		write (knobs_col[s]->state_msg());
	}
}

void
LaunchControlXL::update_track_control_led(uint8_t n)
{
	boost::shared_ptr<TrackButton> b = control_button_by_column(n);

	if (!b) {
		return;
	}

	if (stripable[n]) {
		boost::shared_ptr<AutomationControl> ac = get_ac_by_state(n);

		switch(track_mode()) {
			case TrackMute:
				if (ac->get_value()) {
					b->set_color(YellowFull);
				} else {
					b->set_color(AmberLow);
				}
				break;
			case TrackSolo:
				if (ac && !(stripable[n]->is_master())) {
					if (ac->get_value()) {
						b->set_color(GreenFull);
					} else {
						b->set_color(GreenLow);
					}
				} else {
					b->set_color(Off);
				}
				break;
			case TrackRecord:
				if (ac) {
					if (ac->get_value()) {
						b->set_color(RedFull);
					} else {
						b->set_color(RedLow);
					}
				} else {
					b->set_color(Off);
				}
				break;

			default:
			break;
		}
	} else {
		b->set_color(Off);
	}

	write (b->state_msg());
}

void
LaunchControlXL::solo_mute_rec_changed(uint32_t n) {
	if (!stripable[n]) {
		return;
	}
	update_track_control_led(n);
}

void
LaunchControlXL::solo_iso_changed(uint32_t n)
{
	if (!stripable[n]) {
		return;
	} else {
		solo_iso_led_bank();
	}
}

void
LaunchControlXL::solo_iso_led_bank ()
{
	int stripable_counter = get_amount_of_tracks();

	if (!(buttons_down.find(Device) != buttons_down.end())) {
		return;
	} else {
		for (int n = 0; n < stripable_counter; ++n) {
			boost::shared_ptr<TrackButton> b = focus_button_by_column(n);
			if (stripable[n] && stripable[n]->solo_isolate_control()) {
				if (stripable[n]->solo_isolate_control()->get_value()) {
					b->set_color(RedFull);
				} else {
					b->set_color(Off);
				}
				write (b->state_msg());
			}
		}
		LaunchControlXL::set_refresh_leds_flag(true);
	}
}

#ifdef MIXBUS
void
LaunchControlXL::master_send_changed(uint32_t n)
{
	if (!stripable[n]) {
		return;
	} else {
		master_send_led_bank();
	}

}

void
LaunchControlXL::master_send_led_bank ()
{
	if (!(buttons_down.find(Device) != buttons_down.end())) {
		return;
	} else {
		int stripable_counter = LaunchControlXL::get_amount_of_tracks();

		for (int n = 0; n < stripable_counter; ++n) {
			boost::shared_ptr<TrackButton> b = control_button_by_column(n);
			if (stripable[n] && stripable[n]->master_send_enable_controllable()) {
				if (stripable[n]->master_send_enable_controllable()->get_value()) {
					b->set_color(GreenFull);
				} else {
					b->set_color(Off);
				}
			}
			write (b->state_msg());
		}
		LaunchControlXL::set_refresh_leds_flag(true);
	}
}
# endif

void
LaunchControlXL::button_track_control(uint8_t n) {
	if (!stripable[n]) {
		return;
	}

	if (buttons_down.find(Device) != buttons_down.end()) {
		DEBUG_TRACE (DEBUG::LaunchControlXL, "DEVICE BUTTON HOLD\n");
#ifdef MIXBUS
		if (stripable[n]->master_send_enable_controllable()) {
			bool master_send_active = stripable[n]->master_send_enable_controllable()->get_value();

			DEBUG_TRACE (DEBUG::LaunchControlXL, "MIXBUS Master Assign\n");
			stripable[n]->master_send_enable_controllable()->set_value (!master_send_active, PBD::Controllable::UseGroup);
		}

#else
		/* something useful for Ardour */
#endif
		return;
	}

	boost::shared_ptr<AutomationControl> ac = get_ac_by_state(n);

	if (ac) {
		session->set_control (ac, !ac->get_value(), PBD::Controllable::UseGroup);
	}
}

void
LaunchControlXL::button_track_mode(TrackMode state)
{
		set_track_mode(state);
		for (uint8_t n = 0; n < 8; ++n) {
			update_track_control_led(n);
		}

		boost::shared_ptr<TrackStateButton> mute = boost::dynamic_pointer_cast<TrackStateButton> (id_note_button_map[Mute]);
		boost::shared_ptr<TrackStateButton> solo = boost::dynamic_pointer_cast<TrackStateButton> (id_note_button_map[Solo]);
		boost::shared_ptr<TrackStateButton> record = boost::dynamic_pointer_cast<TrackStateButton> (id_note_button_map[Record]);

		write(mute->state_msg((state == TrackMute)));
		write(solo->state_msg((state == TrackSolo)));
		write(record->state_msg((state == TrackRecord)));
}

void
LaunchControlXL::button_select_left()
{
	switch_bank (max (0, bank_start - (7 + (fader8master() ? 0 : 1))));
}

void
LaunchControlXL::button_select_right()
{
	switch_bank (max (0, bank_start + 7 + (fader8master() ? 0 : 1)));
}

void
LaunchControlXL::button_select_up()
{

}

void
LaunchControlXL::button_select_down()
{

}

void
LaunchControlXL::button_device()
{

}

void
LaunchControlXL::button_device_long_press()
{
	solo_iso_led_bank();
#ifdef MIXBUS
	master_send_led_bank();
#endif
}

void
LaunchControlXL::button_mute()
{
	if (buttons_down.find(Device) != buttons_down.end()) {
		access_action ("Editor/track-mute-toggle");
	} else {
		button_track_mode(TrackMute);
	}
}

void
LaunchControlXL::button_solo()
{
	if (buttons_down.find(Device) != buttons_down.end()) {
		access_action ("Editor/track-solo-toggle");
	} else {
		button_track_mode(TrackSolo);
	}
}

void
LaunchControlXL::button_record()
{
	if (buttons_down.find(Device) != buttons_down.end()) {
		access_action ("Editor/track-record-enable-toggle");
	} else {
		button_track_mode(TrackRecord);
	}
}

bool
LaunchControlXL::button_long_press_timeout (ButtonID id, boost::shared_ptr<Button> button)
{
	if (buttons_down.find (id) != buttons_down.end()) {
		DEBUG_TRACE (DEBUG::LaunchControlXL, string_compose ("long press timeout for %1, invoking method\n", id));
		(this->*button->long_press_method) ();
	} else {
		DEBUG_TRACE (DEBUG::LaunchControlXL, string_compose ("long press timeout for %1, expired/cancelled\n", id));
		/* release happened and somehow we were not cancelled */
	}

	/* whichever button this was, we've used it ... don't invoke the
	   release action.
	*/
	consumed.insert (id);

	return false; /* don't get called again */
}


void
LaunchControlXL::start_press_timeout (boost::shared_ptr<Button> button, ButtonID id)
{
	Glib::RefPtr<Glib::TimeoutSource> timeout = Glib::TimeoutSource::create (500); // milliseconds
	button->timeout_connection = timeout->connect (sigc::bind (sigc::mem_fun (*this, &LaunchControlXL::button_long_press_timeout), id, button));
	timeout->attach (main_loop()->get_context());
}