-
Notifications
You must be signed in to change notification settings - Fork 15
/
gdigi.c
1587 lines (1366 loc) · 43 KB
/
gdigi.c
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2009 Tomasz Moń <[email protected]>
*
* 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; under version 3 of the License.
*
* 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, see <http://www.gnu.org/licenses>.
*/
#include <stdio.h>
#include <gtk/gtk.h>
#include <gio/gio.h>
#include <getopt.h>
#include <alsa/asoundlib.h>
#include <alloca.h>
#include "gdigi.h"
#include "gdigi_xml.h"
#include "gui.h"
static unsigned char device_id = 0x7F;
static unsigned char family_id = 0x7F;
unsigned char product_id = 0x7F;
static snd_rawmidi_t *output = NULL;
static snd_rawmidi_t *input = NULL;
static char *device_port = NULL;
static GQueue *message_queue = NULL;
static GMutex *message_queue_mutex = NULL;
static GCond *message_queue_cond = NULL;
static guint DebugFlags;
gboolean
debug_flag_is_set (debug_flags_t flags)
{
if (DebugFlags & flags) {
return TRUE;
}
return FALSE;
}
gboolean set_debug_flags (const gchar *option_name, const gchar *value,
gpointer data, GError **error)
{
if (strchr(value, 'd')) {
DebugFlags |= DEBUG_MSG2DEV;
}
if (strchr(value, 'h')) {
DebugFlags |= DEBUG_MSG2HOST;
}
if (strchr(value, 'm')) {
DebugFlags |= DEBUG_MSG2DEV|DEBUG_MSG2HOST|DEBUG_GROUP;
}
if (strchr(value, 's')) {
DebugFlags |= DEBUG_STARTUP;
}
if (strchr(value, 'H')) {
DebugFlags |= DEBUG_HEX;
}
if (strchr(value, 'g')) {
DebugFlags |= DEBUG_GROUP;
}
if (strchr(value, 'x')) {
DebugFlags |= DEBUG_XML;
}
if (strchr(value, 'v')) {
DebugFlags |= DEBUG_VERBOSE;
}
if (strchr(value, 'a')) {
DebugFlags = -1;
}
return TRUE;
}
void
debug_msg (debug_flags_t flags, char *fmt, ...)
{
char buf[1024];
if (flags & DebugFlags) {
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, 1024, fmt, ap);
va_end(ap);
fprintf(stderr, "%s\n", buf);
}
}
/*
* Format a value according to the xml setting.
* Returns an allocated buffer that must be freed by the caller.
*/
GString *
format_value (XmlSettings *xml, guint value)
{
GString *buf = g_string_sized_new(1);
EffectValues *values = NULL;
ValueType vtype;
gchar *suffix = "";
gdouble step = 1.0;
gint offset = 0;
gboolean decimal = FALSE;
values = xml->values;
vtype = values->type;
while ((vtype & VALUE_TYPE_EXTRA) && value_is_extra(values, value)) {
values = values->extra;
vtype = values->type;
}
vtype &= ~VALUE_TYPE_EXTRA;
if (vtype & VALUE_TYPE_OFFSET) {
offset = values->offset;
vtype &= ~VALUE_TYPE_OFFSET;
}
if (vtype & VALUE_TYPE_STEP) {
step = values->step;
vtype &= ~VALUE_TYPE_STEP;
}
if (vtype & VALUE_TYPE_SUFFIX) {
suffix = values->suffix;
vtype &= ~VALUE_TYPE_SUFFIX;
}
if (vtype & VALUE_TYPE_DECIMAL) {
decimal = TRUE;
vtype &= ~VALUE_TYPE_DECIMAL;
}
switch (vtype) {
case VALUE_TYPE_LABEL:
{
char *textp = map_xml_value(xml, values, value);
if (!textp) {
g_warning("%s: Unable to map %s value %d for id %d position %d",
__FUNCTION__, xml->label, value, xml->id, xml->position);
textp = "";
}
g_string_printf(buf, "%s", textp);
break;
}
case VALUE_TYPE_PLAIN:
{
if (decimal) {
double dvalue = ((gint)value + offset) * step;
g_string_printf(buf, "%0.2f%s", dvalue, suffix);
} else {
gint ivalue = ((gint)value + offset) * step;
g_string_printf(buf, "%d%s", ivalue, suffix);
}
break;
}
case VALUE_TYPE_NONE:
g_string_printf(buf, "%s", "");
break;
case VALUE_TYPE_POSID:
g_string_printf(buf, "%d", value);
break;
default:
g_warning("Unhandled value type %d", vtype);
break;
}
return buf;
}
GString *
format_ipv (guint id, guint pos, guint val)
{
GString *buf = g_string_sized_new(1);
GString *vec_buf = g_string_sized_new(1);
XmlSettings *xml = get_xml_settings(id, pos);
GString *val_buf;
if (!xml) {
g_warning("Failed to find xml settings for position %d id %d.",
pos, id);
g_string_printf(buf, "%s", "error");
return buf;
}
val_buf = format_value(xml, val);
g_string_printf(vec_buf, "(%d, %d, %d)", pos, id, val);
g_string_printf(buf, "%-16s %s: %s: %s",
vec_buf->str,
get_position(pos), xml->label, val_buf->str);
g_string_free(vec_buf, TRUE);
g_string_free(val_buf, TRUE);
return buf;
}
/**
* Registers an error quark for gdigi if necessary.
*
* \return error quark used for gdigi errors
**/
static GQuark gdigi_error_quark()
{
static GQuark quark = 0;
if (quark == 0) {
quark = g_quark_from_static_string("gdigi-error");
}
return quark;
}
/**
* \param array data to calculate checksum
* \param length data length
*
* Calculates message checksum.
*
* \return calculated checksum.
**/
static char calculate_checksum(gchar *array, gint length)
{
int x;
int checksum = 0;
for (x = 0; x<length; x++) {
checksum ^= array[x];
}
return checksum;
}
/**
* Opens MIDI device. This function modifies global input and output variables.
*
* \return FALSE on success, TRUE on error.
**/
gboolean open_device()
{
int err;
err = snd_rawmidi_open(&input, &output, device_port, SND_RAWMIDI_SYNC);
if (err) {
fprintf(stderr, "snd_rawmidi_open %s failed: %d\n", device_port, err);
return TRUE;
}
err = snd_rawmidi_nonblock(output, 0);
if (err) {
fprintf(stderr, "snd_rawmidi_nonblock failed: %d\n", err);
return TRUE;
}
snd_rawmidi_read(input, NULL, 0); /* trigger reading */
return FALSE;
}
/**
* \param data data to be sent
* \param length data length
*
* Sends data to device. This function uses global output variable.
**/
void send_data(char *data, int length)
{
snd_rawmidi_write(output, data, length);
}
/**
* \param data data to be packed
* \param len data length
*
* Packs data using method used on all newer DigiTech products.
*
* \return GString containing packed data
**/
GString *pack_data(gchar *data, gint len)
{
GString *packed;
gint i;
gint new_len;
unsigned char status;
gint status_byte;
new_len = len + (len/7);
packed = g_string_sized_new(new_len);
status = 0;
status_byte = 0;
for (i=0; i<len; i++) {
if ((i % 7) == 0) {
packed->str[status_byte] = status;
status = 0;
status_byte = packed->len;
g_string_append_c(packed, '\0');
}
g_string_append_c(packed, (data[i] & 0x7F));
status |= (data[i] & 0x80) >> ((i%7) + 1);
}
packed->str[status_byte] = status;
return packed;
}
static void message_free_func(GString *msg, gpointer user_data)
{
g_string_free(msg, TRUE);
}
/**
* \param msg message to unpack
*
* Unpacks message data. This function modifies given GString.
**/
static void unpack_message(GString *msg)
{
int offset;
int x;
int i;
unsigned char status;
unsigned char *str;
gboolean stop = FALSE;
g_return_if_fail(msg != NULL);
g_return_if_fail(msg->len > 9);
offset = 1;
x = 0;
i = 8;
str = (unsigned char*)msg->str;
do {
offset += 8;
status = str[offset-1];
for (x=0; x<7 && !stop; x++) {
if (offset+x >= msg->len) {
i++;
stop = TRUE;
break;
}
if (str[offset+x] == 0xF7) {
if (x == 0) {
str[i] = status;
i++;
}
str[i] = 0xF7;
i++;
stop = TRUE;
break;
}
str[i] = (((status << (x+1)) & 0x80) | str[x+offset]);
i++;
}
} while (!stop);
g_string_truncate(msg, i);
}
/**
* \param msg SysEx message
*
* Checks message ID.
*
* \return MessageID, or -1 on error.
**/
MessageID get_message_id(GString *msg)
{
/** \todo check if msg is valid SysEx message */
g_return_val_if_fail(msg != NULL, -1);
g_return_val_if_fail(msg->str != NULL, -1);
if (msg->len > 7) {
return (unsigned char)msg->str[7];
}
return -1;
}
#define HEX_WIDTH 26
static gboolean modifier_linkable_list_request_pending = FALSE;
void push_message(GString *msg)
{
MessageID msgid = get_message_id(msg);
if (((unsigned char)msg->str[0] == 0xF0) &&
((unsigned char)msg->str[msg->len-1] == 0xF7)) {
debug_msg(DEBUG_VERBOSE, "Pushing correct message!");
} else {
g_warning("Pushing incorrect message!");
}
int x;
if (debug_flag_is_set(DEBUG_HEX)) {
for (x = 0; x<msg->len; x++) {
if (x && (x % HEX_WIDTH) == 0) {
printf("\n");
}
printf("%02x ", (unsigned char)msg->str[x]);
}
if (x % HEX_WIDTH) {
printf("\n");
}
}
debug_msg(DEBUG_VERBOSE, "Received %s", get_message_name(msgid));
SettingParam *param;
switch (msgid) {
case ACK:
g_string_free(msg, TRUE);
return;
case NACK:
g_warning("Received NACK!");
g_string_free(msg, TRUE);
return;
case RECEIVE_PARAMETER_VALUE:
{
unpack_message(msg);
param = setting_param_new_from_data(&msg->str[8], NULL);
if (debug_flag_is_set(DEBUG_MSG2HOST)) {
GString *ipv = format_ipv(param->id,
param->position,
param->value);
debug_msg(DEBUG_MSG2HOST, "RECEIVE_PARAMETER_VALUE\n%s",
ipv->str);
g_string_free(ipv, TRUE);
}
GDK_THREADS_ENTER();
apply_setting_param_to_gui(param);
GDK_THREADS_LEAVE();
setting_param_free(param);
g_string_free(msg, TRUE);
return;
}
case RECEIVE_DEVICE_NOTIFICATION:
unpack_message(msg);
unsigned char *str = (unsigned char*)msg->str;
switch (str[8]) {
case NOTIFY_PRESET_MOVED:
if (str[11] == PRESETS_EDIT_BUFFER && str[12] == 0) {
GDK_THREADS_ENTER();
g_timeout_add(0, apply_current_preset_to_gui, NULL);
GDK_THREADS_LEAVE();
debug_msg(DEBUG_MSG2HOST,
"RECEIVE_DEVICE_NOTIFICATION: Loaded preset "
"%d from bank %d",
str[10], str[9]);
} else {
debug_msg(DEBUG_MSG2HOST,
"RECEIVE_DEVICE_NOTIFICATION: %d %d moved to "
"%d %d",
str[9], str[10],
str[11], str[12]);
}
break;
case NOTIFY_MODIFIER_GROUP_CHANGED:
{
int i;
if (debug_flag_is_set(DEBUG_HEX)) {
printf("\n");
for (i = 0; i < msg->len; i++) {
printf(" %02x", (unsigned char) str[i]);
}
printf("\n");
}
debug_msg(DEBUG_MSG2HOST,
"NOTIFY_MODIFIER_GROUP_CHANGED: Modifier group "
"id %d changed",
(str[9] << 8) | (str[10]));
if (!modifier_linkable_list_request_pending) {
send_message(REQUEST_MODIFIER_LINKABLE_LIST, "\x00\x01", 2);
modifier_linkable_list_request_pending = TRUE;
}
break;
}
default:
g_warning("Received unhandled device notification 0x%x",
str[11]);
}
g_string_free(msg, TRUE);
return;
case RECEIVE_GLOBAL_PARAMETERS:
unpack_message(msg);
gint tot, n, x;
tot = (unsigned char)msg->str[9];
if (debug_flag_is_set(DEBUG_HEX)) {
for (n = 0; n < msg->len; n++) {
printf("%02x ",(unsigned char) msg->str[n]);
}
printf("\n");
}
n = 0;
x = 10;
do {
param = setting_param_new_from_data(&msg->str[x], &x);
debug_msg(DEBUG_MSG2HOST,
"RECEIVE_GLOBAL_PARAMETERS ID: %5d "
"Position: %2.1d Value: %6.1d: %s",
param->id,
param->position, param->value, "XXX");
GDK_THREADS_ENTER();
apply_setting_param_to_gui(param);
GDK_THREADS_LEAVE();
setting_param_free(param);
} while ( (x < msg->len) && n < tot);
g_string_free(msg, TRUE);
return;
case RECEIVE_MODIFIER_LINKABLE_LIST:
modifier_linkable_list_request_pending = FALSE;
unpack_message(msg);
tot = (unsigned char)msg->str[9];
if (debug_flag_is_set(DEBUG_HEX)) {
for (n = 0; n < msg->len; n++) {
printf("%02x ",(unsigned char) msg->str[n]);
}
printf("\n");
}
update_modifier_linkable_list(msg);
g_string_free(msg, TRUE);
GDK_THREADS_ENTER();
create_modifier_group(EXP_POSITION, EXP_ASSIGN1);
create_modifier_group(LFO1_POSITION, LFO_TYPE);
create_modifier_group(LFO2_POSITION, LFO_TYPE);
GDK_THREADS_LEAVE();
return;
default:
g_mutex_lock(message_queue_mutex);
g_queue_push_tail(message_queue, msg);
g_cond_signal(message_queue_cond);
g_mutex_unlock(message_queue_mutex);
break;
}
}
gpointer read_data_thread(gboolean *stop)
{
/* This is mostly taken straight from alsa-utils-1.0.19 amidi/amidi.c
by Clemens Ladisch <[email protected]> */
int err;
int npfds;
struct pollfd *pfds;
GString *string = NULL;
npfds = snd_rawmidi_poll_descriptors_count(input);
pfds = alloca(npfds * sizeof(struct pollfd));
snd_rawmidi_poll_descriptors(input, pfds, npfds);
do {
unsigned char buf[256];
int i, length;
unsigned short revents;
/* SysEx messages can't contain bytes with 8th bit set.
memset our buffer to 0xFF, so if for some reason we'll
get out of reply bounds, we'll catch it */
memset(buf, '\0', sizeof(buf));
err = poll(pfds, npfds, 200);
if (err < 0 && errno == EINTR)
break;
if (err < 0) {
g_error("poll failed: %s", strerror(errno));
break;
}
if ((err = snd_rawmidi_poll_descriptors_revents(input, pfds, npfds, &revents)) < 0) {
g_error("cannot get poll events: %s", snd_strerror(errno));
break;
}
if (revents & (POLLERR | POLLHUP))
break;
if (!(revents & POLLIN))
continue;
err = snd_rawmidi_read(input, buf, sizeof(buf));
if (err == -EAGAIN)
continue;
if (err < 0) {
g_error("cannot read: %s", snd_strerror(err));
break;
}
length = 0;
for (i = 0; i < err; ++i)
if ((unsigned char)buf[i] != 0xFE) /* ignore active sensing */
buf[length++] = buf[i];
i = 0;
while (i < length) {
int pos;
int bytes;
if (string == NULL) {
while (buf[i] != 0xF0 && i < length)
i++;
}
pos = i;
for (bytes = 0; (bytes<length-i) && (buf[i+bytes] != 0xF7); bytes++);
if (buf[i+bytes] == 0xF7) bytes++;
i += bytes;
if (string == NULL)
string = g_string_new_len((gchar*)&buf[pos], bytes);
else
g_string_append_len(string, (gchar*)&buf[pos], bytes);
if ((unsigned char)string->str[string->len-1] == 0xF7) {
/* push message on stack */
push_message(string);
string = NULL;
}
}
} while (*stop == FALSE);
if (string) {
g_string_free(string, TRUE);
string = NULL;
}
return NULL;
}
/**
* \param procedure procedure ID
* \param data unpacked message data
* \param len data length
*
* Creates SysEx message then sends it. This function uses folowing global variables: device_id, family_id and product_id.
**/
void send_message(gint procedure, gchar *data, gint len)
{
GString *msg = g_string_new_len("\xF0" /* SysEx status byte */
"\x00\x00\x10", /* Manufacturer ID */
4);
g_string_append_printf(msg,
"%c%c%c" /* device, family, product ID */
"%c", /* procedure */
device_id, family_id, product_id,
procedure);
if (len > 0) {
GString *tmp = pack_data(data, len);
g_string_append_len(msg, tmp->str, tmp->len);
g_string_free(tmp, TRUE);
}
g_string_append_printf(msg, "%c\xF7",
calculate_checksum(&msg->str[1], msg->len - 1));
debug_msg(DEBUG_VERBOSE, "Sending %s len %d",
get_message_name(procedure), len);
send_data(msg->str, msg->len);
g_string_free(msg, TRUE);
}
/**
* \param id MessageID of requested message
*
* Reads data from message queue until message with matching id is found.
*
* \return GString containing unpacked message.
**/
GString *get_message_by_id(MessageID id)
{
GString *data = NULL;
guint x, len;
gboolean found = FALSE;
g_mutex_lock(message_queue_mutex);
do {
len = g_queue_get_length(message_queue);
for (x = 0; x<len; x++) {
data = g_queue_peek_nth(message_queue, x);
if (get_message_id(data) == id) {
found = TRUE;
g_queue_pop_nth(message_queue, x);
break;
}
}
if (found == FALSE)
g_cond_wait(message_queue_cond, message_queue_mutex);
} while (found == FALSE);
g_mutex_unlock(message_queue_mutex);
unpack_message(data);
return data;
}
/**
* \param msg message to append value
* \param value value to append
*
* Packs value using scheme used on all newer DigiTech products.
**/
void append_value(GString *msg, guint value)
{
/* check how many bytes long the value is */
guint temp = value;
gint n = 0;
do {
n++;
temp = temp >> 8;
} while (temp);
if (n == 1) {
if (value & 0x80)
n = 2;
else
g_string_append_printf(msg, "%c", value);
}
if (n > 1) {
gint x;
g_string_append_printf(msg, "%c", (n | 0x80));
for (x=0; x<n; x++) {
g_string_append_printf(msg, "%c",
((value >> (8*(n-x-1))) & 0xFF));
}
}
}
/**
* \param str pointer to value to unpack
* \param len return location for how many bytes value is encoded on (length is added to current value)
*
* Unpacks value using scheme used on all newer DigiTech products.
*
* \return unpacked value
**/
guint unpack_value(gchar *str, int *len)
{
guint value;
gint tmp;
value = (unsigned char)str[0];
if (len != NULL)
*len += 1;
if (value > 0x80) {
tmp = value & 0x7F;
value = 0;
gint i;
for (i = 0; i<tmp; i++) {
value |= ((unsigned char)str[1+i] << (8*(tmp-i-1)));
}
if (len != NULL)
*len += tmp;
}
return value;
}
/**
* Allocates memory for SettingParam.
*
* \return SettingParam which must be freed using setting_param_free.
**/
SettingParam *setting_param_new()
{
SettingParam *param = g_slice_new(SettingParam);
param->id = -1;
param->position = -1;
param->value = -1;
return param;
}
/**
* \param str pointer to setting param in message
* \param len return location for how many bytes value is encoded on (length is added to current value)
*
* Creates SettingParam basing on data.
* This function expects str to point on:
* -Parameter ID - 2 bytes
* -Parameter position - 1 byte
* -Parameter value - var
*
* \return newly created SettingParam which must be freed using setting_param_free.
**/
SettingParam *setting_param_new_from_data(gchar *str, gint *len)
{
gint id;
gint position;
guint value;
id = ((unsigned char)str[0] << 8) | (unsigned char)str[1];
position = (unsigned char)str[2];
if (len != NULL)
*len += 3;
value = unpack_value(&str[3], len);
SettingParam *param = g_slice_new(SettingParam);
param->id = id;
param->position = position;
param->value = value;
return param;
}
/**
* \param param SettingParam to be freed
*
* Frees all memory used by SettingParam.
**/
void setting_param_free(SettingParam *param)
{
g_slice_free(SettingParam, param);
}
/**
* Allocates memory for SettingGenetx.
*
* \return SettingGenetx which must be freed using setting_genetx_free.
**/
SettingGenetx *setting_genetx_new()
{
SettingGenetx *genetx = g_slice_new(SettingGenetx);
/* Older patches don't specify GeNetX version */
genetx->version = GENETX_VERSION_1;
genetx->type = GENETX_TYPE_NOT_SET;
genetx->channel = -1;
genetx->name = NULL;
genetx->data = NULL;
return genetx;
}
/**
* \param genetx SettingGenetx to be freed
*
* Frees all memory used by SettingGenetx.
**/
void setting_genetx_free(SettingGenetx *genetx)
{
g_free(genetx->name);
if (genetx->data != NULL) {
g_string_free(genetx->data, TRUE);
}
g_slice_free(SettingGenetx, genetx);
}
/**
* \param version GeNetX version
* \param type GeNetX type
*
* Retrieves SectionID for specified GeNetX version and type.
*
* \return SectionID specified by version and type, or -1 on error.
**/
SectionID get_genetx_section_id(gint version, gint type)
{
if (version == GENETX_VERSION_1) {
if (type == GENETX_TYPE_AMP) {
return SECTION_GENETX_AMP;
} else if (type == GENETX_TYPE_CABINET) {
return SECTION_GENETX_CABINET;
}
} else if (version == GENETX_VERSION_2) {
if (type == GENETX_TYPE_AMP) {
return SECTION_GENETX2_AMP;
} else if (type == GENETX_TYPE_CABINET) {
return SECTION_GENETX2_CABINET;
}
}
g_warning("This version of gdigi don't know what to do with this "
"GeNetX version (%d) and type (%d)", version, type);
return -1;
}
/**
* \param id Parameter ID
* \param position Parameter position
* \param value Parameter value
*
* Forms SysEx message to request parameter then sends it to device.
**/
void get_option(guint id, guint position)
{
GString *msg = g_string_sized_new(9);
debug_msg(DEBUG_MSG2DEV, "REQUEST_PARAMETER_VALUE: id %d position %d",
id, position);
g_string_append_printf(msg, "%c%c%c",
((id & 0xFF00) >> 8), (id & 0xFF),
position);
send_message(REQUEST_PARAMETER_VALUE, msg->str, msg->len);
g_string_free(msg, TRUE);
}
/**
* \param id Parameter ID
* \param position Parameter position
* \param value Parameter value
*
* Forms SysEx message to set parameter then sends it to device.
**/
void set_option(guint id, guint position, guint value)
{
GString *msg = g_string_sized_new(9);
g_string_append_printf(msg, "%c%c%c",
((id & 0xFF00) >> 8), (id & 0xFF),
position);
append_value(msg, value);
if (debug_flag_is_set(DEBUG_MSG2DEV)) {
GString *ipv = format_ipv(id, position, value);
debug_msg(DEBUG_MSG2DEV, "RECEIVE_PARAMETER_VALUE\n%s", ipv->str);
g_string_free(ipv, TRUE);
}
send_message(RECEIVE_PARAMETER_VALUE, msg->str, msg->len);
g_string_free(msg, TRUE);
}
/**
* \param section data section ID
* \param bank section-specific bank number
* \param index index of the desired object within the bank
* \param name object name
* \param data GString containing object data
*
* Forms RECEIVE_OBJECT SysEx message then sends it to device.
**/
void send_object(SectionID section, guint bank, guint index,
gchar *name, GString *data)
{
GString *msg = g_string_new(NULL);
gint len = data->len;
g_string_append_printf(msg,
"%c%c%c%c%s%c%c%c",
section, bank,
((index & 0xFF00) >> 8), (index & 0xFF),
name, 0 /* NULL terminated string */,
((len & 0xFF00) >> 8), (len & 0xFF));
g_string_append_len(msg, data->str, data->len);
send_message(RECEIVE_OBJECT, msg->str, msg->len);
g_string_free(msg, TRUE);
}
/**
* \param params GList containing SettingParam
*
* Forms RECEIVE_PRESET_PARAMETERS SysEx message then sends it to device.
**/
void send_preset_parameters(GList *params)
{
GString *msg = g_string_sized_new(500);
GList *iter = params;
gint len = g_list_length(iter);