summaryrefslogtreecommitdiff
path: root/tests/test.cpp
blob: 2358a8919b3948dd7073cd93d201ee452bc7eca8 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
/*
  Copyright (C) 2005 Samuel TRACOL for GRAME

  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.

*/

/** @file jack_test.c
 *
 * @brief This client test the jack API.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#ifndef WIN32
#include <unistd.h>
#endif
#include <string.h>
#include <getopt.h>
#include <math.h>
#include <assert.h>
#include <stdarg.h>
#include <jack/jack.h>
#include <jack/transport.h>


#if defined(WIN32) && !defined(M_PI)
#define M_PI 3.151592653
#endif

#ifdef WIN32
#define jack_sleep(val) Sleep((val))
#else
#define jack_sleep(val) usleep((val) * 1000)
#endif

typedef struct
{
    jack_nframes_t ft;		// running counter frame time
    jack_nframes_t fcs;		// from sycle start...
    jack_nframes_t lft;		// last frame time...
}
FrameTimeCollector;

FILE *file;
FrameTimeCollector* framecollect;
FrameTimeCollector perpetualcollect;
FrameTimeCollector lastperpetualcollect;
int frames_collected = 0;

// ports
jack_port_t *output_port1;
jack_port_t *output_port1b;
jack_port_t *input_port2;
jack_port_t *output_port2;
jack_port_t *input_port1;

// clients
jack_client_t *client1;
jack_client_t *client2;
const char *client_name1;
const char *client_name2;

unsigned long sr;	// sample rate
// for time -t option
int time_to_run = 0;
int time_before_exit = 1;
// standard error count
int t_error = 0;
int reorder = 0;	// graph reorder callback
int RT = 0;			// is real time or not...
int FW = 0;			// freewheel mode
int init_clbk = 0;	// init callback
int i, j, k = 0;
int port_callback_reg = 0;
jack_nframes_t cur_buffer_size, old_buffer_size, cur_pos;
int activated = 0;
int count1, count2 = 0;			// for freewheel
int xrun = 0;
int have_xrun = 0;				// msg to tell the process1 function to write a special thing in the frametime file.
int process1_activated = -1;	// to control processing...
int process2_activated = -1;	// to control processing...
unsigned long int index1 = 0;
unsigned long int index2 = 0;
jack_default_audio_sample_t *signal1;	// signal source d'emission
jack_default_audio_sample_t *signal2;	// tableau de reception
jack_transport_state_t ts;
jack_position_t pos;
jack_position_t request_pos;
int silent_error = 0;	// jack silent mode
int verbose_mode = 0;
int transport_mode = 1;
jack_nframes_t input_ext_latency = 0;	// test latency for PHY devices
jack_nframes_t output_ext_latency = 0;	// test latency for PHY devices

int sync_called = 0;
int starting_state = 1;

int linecount = 0;		// line counter for log file of sampleframe counter --> for graph function.
int linebuf = 0;		// reminders for graph analysis
int linetransport = 0;
int linefw = 0;
int lineports = 0;
int linecl2 = 0;

int client_register = 0;

/**
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

							Callbacks & basics functions

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*/

void usage()
{
    fprintf (stderr, "\n\n"
             "usage: jack_test \n"
             "              [ --time OR -t time_to_run (in seconds) ]\n"
             "              [ --quiet OR -q (quiet mode : without jack server errors) ]\n"
             "              [ --verbose OR -v (verbose mode : no details on tests done. Only main results & errors) ]\n"
             "              [ --transport OR -k (Do not test transport functions.) ]\n"
             "                --realtime OR -R (jack is in rt mode)\n\n\n"
            );
    exit(1);
}

void Log(const char *fmt, ...)
{
    if (verbose_mode) {
        va_list ap;
        va_start(ap, fmt);
        vfprintf(stderr, fmt, ap);
        va_end(ap);
    }
}

void Collect(FrameTimeCollector* TheFrame)
{
    TheFrame->lft = jack_last_frame_time(client1);
    TheFrame->ft = jack_frame_time(client1);
    TheFrame->fcs = jack_frames_since_cycle_start(client1);
}

void Jack_Thread_Init_Callback(void *arg)
{
    Log("Init callback has been successfully called. (msg from callback)\n");
    init_clbk = 1;
}

void Jack_Freewheel_Callback(int starting, void *arg)
{
    Log("Freewhell callback has been successfully called with value %i. (msg from callback)\n", starting);
    FW = starting;
}

void Jack_Client_Registration_Callback(const char* name, int val, void *arg)
{
    Log("Client registration callback name = %s has been successfully called with value %i. (msg from callback)\n", name, val);
	if (val)
		client_register++;
	else
		client_register--;
}

int Jack_Update_Buffer_Size(jack_nframes_t nframes, void *arg)
{
    cur_buffer_size = jack_get_buffer_size(client1);
    Log("Buffer size = %d (msg from callback)\n", cur_buffer_size);
    return 0;
}

int Jack_XRun_Callback(void *arg)
{
    xrun++;
    have_xrun = 1;
    Log("Xrun has been detected ! (msg from callback)\n");
    return 0;
}

int Jack_Graph_Order_Callback(void *arg)
{
    reorder++;
    return 0;
}

int Jack_Sample_Rate_Callback(jack_nframes_t nframes, void *arg)
{
    Log("Sample rate : %i.\n", nframes);
    return 0;
}

void Jack_Error_Callback(const char *msg)
{
    if (silent_error == 0) {
        fprintf(stderr, "error : %s (msg from callback)\n", msg);
    }
}

void jack_shutdown(void *arg)
{
    printf("Jack_test has been kicked out by jackd !\n");
    exit(1);
}

void Jack_Port_Register(jack_port_id_t port, int mode, void *arg)
{
    port_callback_reg++;
}

void Jack_Port_Connect(jack_port_id_t a, jack_port_id_t b, int connect, void* arg)
{
	Log("PortConnect src = %ld dst = %ld  onoff = %ld (msg from callback)\n", a, b, connect);
}

int Jack_Sync_Callback(jack_transport_state_t state, jack_position_t *pos, void *arg)
{
    int res = 0;

    switch (state) {

        case JackTransportStarting:
            sync_called++;
            if (starting_state == 0) {
                Log("sync callback : Releasing status : now ready...\n");
                res = 1;
            } else {
                if (sync_called == 1) {
                    Log("sync callback : Holding status...\n");
                }
                res = 0;
            }
            break;

        case JackTransportStopped:
            Log("sync callback : JackTransportStopped...\n");
            res = 0;
            break;

        default:
            res = 0;
            break;
    }

    return res;
}


/**
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

							processing functions

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
 * Proccess1 is for client1
 * 4 modes, activated with process1_activated
 *
 * -1 : idle mode
 *  0 : write zeros to output 1
 *  1 : write continuously signal1 (sinusoidal test signal) to output1
 *  3 : mode for summation test. While record (done by process2) is not running, write signal1 to both out1 & out1b.
 *      when record begin (index2 > 0), write signal1 in phase opposition to out1 & out2
 *  5 : Frames Time checking mode : write the array containing the three values of frame_time, frames cycles start and
 *      last frame time during 150 cycles.
 */

int process1(jack_nframes_t nframes, void *arg)
{
    if (FW == 0) {
        Collect(&perpetualcollect);
        if (have_xrun) {
            fprintf(file, "%i	%i\n", (perpetualcollect.ft - lastperpetualcollect.ft), (2*cur_buffer_size));
            have_xrun = 0;
        } else {
            fprintf(file, "%i	0\n", (perpetualcollect.ft - lastperpetualcollect.ft));
        }
        linecount++;
        lastperpetualcollect.ft = perpetualcollect.ft;
    }

    jack_default_audio_sample_t *out1;
    jack_default_audio_sample_t *out1b;
    activated++; // counter of callback activation
    if (process1_activated == 1) {
        out1 = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port1, nframes);
        for (jack_nframes_t p = 0; p < nframes; p++) {
            out1[p] = signal1[index1];
            index1++;
            if (index1 == 48000)
                index1 = 0;
        }
    }
    if (process1_activated == 3) {
        out1 = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port1, nframes);
        out1b = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port1b, nframes);
        for (jack_nframes_t p = 0; p < nframes; p++) {
            out1[p] = signal1[index1];
            if (index2 != 0) {
                out1b[p] = ( -1 * signal1[index1]);
            } else {
                out1b[p] = signal1[index1];
            }
            index1++;
            if (index1 == 48000)
                index1 = 0;
        }
    }
    if (process1_activated == 0) {
        out1 = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port1, nframes);
        memset (out1, 0, sizeof (jack_default_audio_sample_t) * nframes); //�crit des z�ros en sortie...
    }
    if (process1_activated == 5) {
        Collect(&framecollect[frames_collected]);
        frames_collected++;
        if (frames_collected > 798) {
            process1_activated = -1;
        }
    }
    return 0;
}

/**
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
 * Proccess2 is for client2
 * 3 modes, activated with process1_activated
 *
 * -1 : idle mode
 *  0 : idle mode
 *  1 : record in2 into signal2.(for first transmit test)
 *  2 : record in2 into signal2 while send signal1 in out2. used dor Tie data test.
 *  3 : record in2 into sigal2 for summation data test.
 * In records modes, at the end of the record (signal2 is full), it stop the test, setting both activation states to -1.
 */

int process2(jack_nframes_t nframes, void *arg)
{
    jack_default_audio_sample_t *out2;
    jack_default_audio_sample_t *in2;

    if (process2_activated == 1) { // Reception du process1 pour comparer les donnees
        in2 = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port2, nframes);
        for (unsigned int p = 0; p < nframes; p++) {
            signal2[index2] = in2[p];
            if (index2 == 95999) {
                process2_activated = 0;
                process1_activated = 0;
                //index2 = 0;
            } else {
                index2++;
            }
        }
    }

    if (process2_activated == 2) { // envoie de signal1 pour test tie mode et le r�cup�re direct + latence de la boucle jack...
        out2 = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port2, nframes);
        in2 = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port2, nframes);

        for (unsigned int p = 0; p < nframes; p++) {
            out2[p] = signal1[index1];
            index1++;
            if (index1 == 48000)
                index1 = 0;
            signal2[index2] = in2[p];
            if (index2 == 95999) {
                process2_activated = -1;
                //index2 = 0;
            } else {
                index2++;
            }
        }
    }

    if (process2_activated == 3) { // envoie de -signal1 pour sommation en oppo de phase par jack
        in2 = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port2, nframes);

        for (unsigned int p = 0; p < nframes;p++) {
            signal2[index2] = in2[p];
            if (index2 == 95999) {
                process2_activated = 0;
                process1_activated = 0;
                //index2 = 0;
            } else {
                index2++;
            }
        }
    }

    return 0;
}

// Alternate thread model
static int _process (jack_nframes_t nframes)
{
	jack_default_audio_sample_t *in, *out;
	in = (jack_default_audio_sample_t *)jack_port_get_buffer (input_port1, nframes);
	out = (jack_default_audio_sample_t *)jack_port_get_buffer (output_port1, nframes);
	memcpy (out, in,
		sizeof (jack_default_audio_sample_t) * nframes);
	return 0;
}

static void* jack_thread(void *arg)
{
	jack_client_t* client = (jack_client_t*) arg;
	jack_nframes_t last_thread_time = jack_frame_time(client);

	while (1) {
		jack_nframes_t frames = jack_cycle_wait (client);
		jack_nframes_t current_thread_time = jack_frame_time(client);
		jack_nframes_t delta_time = current_thread_time - last_thread_time;
		Log("jack_thread : delta_time = %ld\n", delta_time);
		int status = _process(frames);
		last_thread_time = current_thread_time;
		jack_cycle_signal (client, status);
	}

	return 0;
}

// To test callback exiting
int process3(jack_nframes_t nframes, void *arg)
{
	static int process3_call = 0;

	if (process3_call++ > 10) {
		Log("process3 callback : exiting...\n");
		return -1;
	} else {
		Log("calling process3 callback : process3_call = %ld\n", process3_call);
		return 0;
	}
}

int process4(jack_nframes_t nframes, void *arg)
{
	jack_client_t* client = (jack_client_t*) arg;

	static jack_nframes_t last_time = jack_frame_time(client);
	static jack_nframes_t tolerance = (jack_nframes_t)(cur_buffer_size * 0.1f);

	jack_nframes_t cur_time = jack_frame_time(client);
	jack_nframes_t delta_time = cur_time - last_time;

	Log("calling process4 callback : jack_frame_time = %ld delta_time = %ld\n", cur_time, delta_time);
	if (delta_time > 0  && (unsigned int)abs(delta_time - cur_buffer_size) > tolerance) {
		printf("!!! ERROR !!! jack_frame_time seems to return incorrect values cur_buffer_size = %d, delta_time = %d\n", cur_buffer_size, delta_time);
	}

	last_time = cur_time;
	return 0;
}

static void display_transport_state()
{
    jack_transport_state_t ts;
    jack_position_t pos;

    ts = jack_transport_query(client2, &pos);
    switch (ts) {
        case JackTransportStopped:
            Log("Transport is stopped...\n");
            break;
        case JackTransportRolling:
            Log("Transport is rolling...\n");
            break;
        case JackTransportLooping:
            Log("Transport is looping...\n");
            break;
        case JackTransportStarting:
            Log("Transport is starting...\n");
            break;
        case JackTransportNetStarting:
            Log("Transport is starting with network sync...\n");
            break;
    }
}

/**
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
							MAIN FUNCTION
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
*/
int main (int argc, char *argv[])
{
    const char **inports; // array of PHY input/output
    const char **outports; // array of PHY input/outputs
    const char *server_name = NULL;
	const char **connexions1;
    const char **connexions2;
    jack_status_t status;
    char portname[128] = "port";
    char filename[128] = "framefile.ext";
    const char *nullportname = "";
    int option_index;
    int opt;
    int a = 0;			// working number for in/out port (PHY)...
    int test_link = 0;	// for testing the "overconnect" function
    int flag;			// flag for ports...
    int is_mine = 0;	// to test jack_port_is_mine function...
    const char *options = "kRnqvt:";
    float ratio;		// for speed calculation in freewheel mode
    jack_options_t jack_options = JackNullOption;
	struct option long_options[] = {
                                       {"realtime", 0, 0, 'R'},
                                       {"non-realtime", 0, 0, 'n'},
                                       {"time", 0, 0, 't'},
                                       {"quiet", 0, 0, 'q'},
                                       {"verbose", 0, 0, 'v'},
                                       {"transport", 0, 0, 'k'},
                                       {0, 0, 0, 0}
                                   };

    client_name1 = "jack_test";
    time_to_run = 1;
    while ((opt = getopt_long (argc, argv, options, long_options, &option_index)) != EOF) {
        switch (opt) {
            case 'k':
                transport_mode = 0;
                break;
            case 'q':
                silent_error = 1;
                break;
            case 'v':
                verbose_mode = 1;
                printf("Verbose mode is activated...\n");
                break;
            case 't':
                time_to_run = atoi(optarg);
                break;
            case 'R':
                RT = 1;
                break;
            default:
                fprintf (stderr, "unknown option %c\n", opt);
                usage ();
        }
    }

    if (RT) {
        printf("Jack server is said being in realtime mode...\n");
    } else {
        printf("Jack server is said being in non-realtime mode...\n");
    }
    /**
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    						init signal data for test
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    */
    framecollect = (FrameTimeCollector *) malloc(800 * sizeof(FrameTimeCollector));

    signal1 = (jack_default_audio_sample_t *) malloc(48000 * sizeof(jack_default_audio_sample_t));
    signal2 = (jack_default_audio_sample_t *) malloc(96000 * sizeof(jack_default_audio_sample_t));
    signal1[0] = 0;
    int p;
    for (p = 1; p < 48000;p++) {
        signal1[p] = (float)(sin((p * 2 * M_PI * 1000 ) / 48000));
    }
    for (p = 0; p < 95999;p++) {
        signal2[p] = 0.0 ;
    }
    index1 = 0;
    index2 = 0;
    /**
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    						begin test
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    */
    printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    printf("*-*-*-*-*-*-*-*-*-*-*-*-*-* Start jack server stress test  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");

    /**
     * Register a client...
     *
     */
	Log("Register a client using jack_client_open()...\n");
    client1 = jack_client_open(client_name1, jack_options, &status, server_name);
    if (client1 == NULL) {
        fprintf (stderr, "jack_client_open() failed, "
                 "status = 0x%2.0x\n", status);
        if (status & JackServerFailed) {
            fprintf(stderr, "Unable to connect to JACK server\n");
        }
        exit (1);
    }
    if (status & JackServerStarted) {
        fprintf(stderr, "JACK server started\n");
    }

    /**
     * try to register another one with the same name...
     *
     */
    Log("trying to register a new jackd client with name %s using jack_client_new()...\n", client_name1);
    client2 = jack_client_new(client_name1);
    if (client2 == NULL) {
        Log ("valid : a second client with the same name cannot be registered\n");
    } else {
        printf("!!! ERROR !!! Jackd server has accepted multiples client with the same name !\n");
        jack_client_close(client2);
    }

	/**
     * try to register another one with the same name using jack_client_open ==> since JackUseExactName is not used, an new client should be opened...
     *
     */
    Log("trying to register a new jackd client with name %s using jack_client_open()...\n", client_name1);
    client2 = jack_client_open(client_name1, jack_options, &status, server_name);
    if (client2 != NULL) {
        Log ("valid : a second client with the same name can be registered (client automatic renaming)\n");
		jack_client_close(client2);
    } else {
        printf("!!! ERROR !!! Jackd server automatic renaming feature does not work!\n");
    }

    /**
     * testing client name...
     * Verify that the name sended at registration and the one returned by jack server is the same...
     *
     */
    Log("Testing name...");
    client_name2 = jack_get_client_name(client1);
    if (strcmp(client_name1, client_name2) == 0)
        Log(" ok\n");
    else
        printf("\n!!! ERROR !!! name returned different from the one given : %s\n", client_name2);

    /**
     * Test RT mode...
     * verify if the real time mode returned by jack match the optional argument defined when launching jack_test*/
    if (jack_is_realtime(client1) == RT)
        Log("Jackd is in realtime mode (RT = %i).\n", RT);
    else
        printf("!!! ERROR !!! Jackd is in a non-expected realtime mode (RT = %i).\n", RT);

    /**
     * Register all callbacks...
     *
     */
    if (jack_set_thread_init_callback(client1, Jack_Thread_Init_Callback, 0) != 0)
        printf("!!! ERROR !!! while calling jack_set_thread_init_callback()...\n");
    if (jack_set_freewheel_callback(client1, Jack_Freewheel_Callback, 0) != 0 )
        printf("\n!!! ERROR !!! while calling jack_set_freewheel_callback()...\n");

    if (jack_set_process_callback(client1, process1, 0) != 0) {
        printf("Error when calling jack_set_process_callback() !\n");
    }

    jack_on_shutdown(client1, jack_shutdown, 0);

    if (jack_set_buffer_size_callback(client1, Jack_Update_Buffer_Size, 0) != 0) {
        printf("Error when calling buffer_size_callback !\n");
    }

    if (jack_set_graph_order_callback(client1, Jack_Graph_Order_Callback, 0) != 0) {
        printf("Error when calling Jack_Graph_Order_Callback() !\n");
    }

    if (jack_set_xrun_callback(client1, Jack_XRun_Callback, 0 ) != 0) {
        printf("Error when calling jack_set_xrun_callback() !\n");
    }

    if (jack_set_sample_rate_callback(client1, Jack_Sample_Rate_Callback, 0 ) != 0) {
        printf("Error when calling Jack_Sample_Rate_Callback() !\n");
    }

    if (jack_set_port_registration_callback(client1, Jack_Port_Register, 0) != 0) {
        printf("Error when calling jack_set_port_registration_callback() !\n");
    }

    if (jack_set_port_connect_callback(client1, Jack_Port_Connect, 0) != 0) {
        printf("Error when calling jack_set_port_connect_callback() !\n");
    }

	if (jack_set_client_registration_callback(client1, Jack_Client_Registration_Callback, 0) != 0) {
		printf("Error when calling jack_set_client_registration_callback() !\n");
	}

    jack_set_error_function(Jack_Error_Callback);

    /**
     * Create file for clock "frame time" analysis
     *
     */
    cur_buffer_size = jack_get_buffer_size(client1);
    sprintf (filename, "framefile-%i.dat", cur_buffer_size);
    file = fopen(filename, "w");
    if (file == NULL) {
        fprintf(stderr, "Erreur dans l'ouverture du fichier log framefile.dat");
        exit(-1);
    }

    /**
     * Try to register a client with a NULL name/zero length name...
     *
     */
    output_port1 = jack_port_register(client1, nullportname,
                                      JACK_DEFAULT_AUDIO_TYPE,
                                      JackPortIsOutput, 0);
    if (output_port1 == NULL) {
        Log("Can't register a port with a NULL portname... ok.\n");
    } else {
        printf("!!! ERROR !!! Can register a port with a NULL portname !\n");
        jack_port_unregister(client1, output_port1);
    }

    /**
     * Register 1 port in order to stress other functions.
     *
     */
    output_port1 = jack_port_register(client1, portname,
                                      JACK_DEFAULT_AUDIO_TYPE,
                                      JackPortIsOutput, 0);
    if (output_port1 == NULL) {
        printf("!!! ERROR !!! Can't register any port for the client !\n");
        exit(1);
    }

   /**
     * Test port type of the just registered port.
     *
     */
    if (strcmp(jack_port_type(output_port1), JACK_DEFAULT_AUDIO_TYPE) != 0) {
        printf("!!! ERROR !!! jack_port_type returns an incorrect value!\n");
	} else {
		Log("Checking jack_port_type()... ok.\n");
	}

    /**
     * Try to register another port with the same name...
     *
     */
    output_port2 = jack_port_register(client1, portname,
                                      JACK_DEFAULT_AUDIO_TYPE,
                                      JackPortIsOutput, 0);
    if (output_port2 == NULL) {
        Log("Can't register two ports with the same name... ok\n");
    } else {
        if (strcmp (jack_port_name(output_port1), jack_port_name(output_port2)) == 0) {
            printf("!!! ERROR !!! Can register two ports with the same name ! (%px : %s & %px : %s).\n", output_port1, jack_port_name(output_port1), output_port2, jack_port_name(output_port2));
            jack_port_unregister(client1, output_port2);
        } else {
            Log("Can't register two ports with the same name... ok (auto-rename %s into %s).\n", jack_port_name(output_port1), jack_port_name(output_port2));
            jack_port_unregister(client1, output_port2);
        }
    }

    /**
     * Verify that both port_name and port_short_name return correct results...
     *
     */
    sprintf (portname, "%s:%s", jack_get_client_name(client1), jack_port_short_name(output_port1));
    if (strcmp(jack_port_name(output_port1), portname) != 0) {
        printf("!!! ERROR !!! functions jack_port_name and/or jack_short_port_name seems to be invalid !\n");
        printf("client_name = %s\n short_port_name = %s\n port_name = %s\n", jack_get_client_name(client1), jack_port_short_name(output_port1), jack_port_name(output_port1));
    }

    /**
     * Verify the function port_set_name
     *
     */
    if (jack_port_set_name (output_port1, "renamed-port#") == 0 ) {
        if (strcmp(jack_port_name(output_port1), "renamed-port#") == 0) {
            printf("!!! ERROR !!! functions jack_port_set_name seems to be invalid !\n");
            printf("jack_port_name return '%s' whereas 'renamed-port#' was expected...\n", jack_port_name(output_port1));
        } else {
            Log("Checking jack_port_set_name()... ok\n");
            jack_port_set_name (output_port1, "port");
        }
    } else {
        printf("error : port_set_name function can't be tested...\n");
    }

    /**
     * Activate the client
     *
     */
    if (jack_activate(client1) < 0) {
        printf ("Fatal error : cannot activate client1\n");
        exit(1);
    }

    /**
     * Test if init callback initThread have been called.
     *
     */
    if (init_clbk == 0)
        printf("!!! ERROR !!! JackThreadInitCallback was not called !!.\n");

    jack_sleep(10 * 1000); // test see the clock in the graph at the begining...

    /**
     * Stress Freewheel mode...
     * Try to enter freewheel mode. Check the realtime mode de-activation.
     * Check that the number of call of the process callback is greater than in non-freewheel mode.
     * Give an approximated speed ratio (number of process call) between the two modes.
     * Then return in normal mode.
     */
    t_error = 0;
    activated = 0;
    jack_sleep(1 * 1000);
    count1 = activated;
    Log("Testing activation freewheel mode...\n");
    linefw = linecount; // count for better graph reading with gnuplot
    jack_set_freewheel(client1, 1);
    activated = 0;
    jack_sleep(1 * 1000);
    count2 = activated;
    if (jack_is_realtime(client1) == 0) {
        t_error = 0;
    } else {
        printf("\n!!! ERROR !!! RT mode is always activated while freewheel mode is applied !\n");
        t_error = 1;
    }
    if (activated == 0)
        printf("!!! ERROR !!! Freewheel mode doesn't activate audio callback !!\n");

    jack_set_freewheel(client1, 0);
    jack_sleep(7 * 1000);

	if (jack_is_realtime(client1) == 1) {}
    else {
        printf("\n!!! ERROR !!! freewheel mode fail to reactivate RT mode when exiting !\n");
        t_error = 1;
    }
    if (t_error == 0) {
        Log("Freewheel mode appears to work well...\n");
    }
    if (count1 == 0) {
        Log("Audio Callback in 'standard' (non-freewheel) mode seems not to be called...\n");
        Log("Ratio speed would be unavailable...\n");
    } else {
        ratio = (float) ((count2 - count1) / count1);
        Log("Approximative speed ratio of freewheel mode = %f : 1.00\n", ratio);
    }

    /**
     * Stress buffer function...
     * get current buffer size.
     * Try to apply a new buffer size value ( 2 x the precedent buffer size value)
     * Then return in previous buffer size mode.
     *
     */

    float factor = 0.5f;
    old_buffer_size = jack_get_buffer_size(client1);
    Log("Testing BufferSize change & Callback...\n--> Current buffer size : %i.\n", old_buffer_size);
    linebuf = linecount;
    if (jack_set_buffer_size(client1, (jack_nframes_t)(old_buffer_size * factor)) < 0) {
        printf("!!! ERROR !!! jack_set_buffer_size fails !\n");
    }
    jack_sleep(1 * 1000);
    cur_buffer_size = jack_get_buffer_size(client1);
    if ((old_buffer_size * factor) != cur_buffer_size) {
        printf("!!! ERROR !!! Buffer size has not been changed !\n");
        printf("!!! Maybe jack was compiled without the '--enable-resize' flag...\n");
    } else {
        Log("jack_set_buffer_size() command successfully applied...\n");
    }
    jack_sleep(3 * 1000);
    jack_set_buffer_size(client1, old_buffer_size);
    cur_buffer_size = jack_get_buffer_size(client1);

    /**
     * Test the last regestered port to see if port_is_mine function the right value.
     * A second test will be performed later.
     * The result will be printed at the end.
     *
     */
    if (jack_port_is_mine(client1, output_port1)) {
        is_mine = 1;
    } else {
        is_mine = 0;
    }

    /**
     * Check that the ID returned by the port_by_name is right.
     * (it seems there is a problem here in some jack versions).
     *
    */
    if (output_port1 != jack_port_by_name(client1, jack_port_name(output_port1))) {
        printf("!!! ERROR !!! function jack_port_by_name() return bad value !\n");
        printf("!!! jack_port_by_name(jack_port_name(_ID_) ) != _ID_returned_at_port_registering ! (%px != %px)\n", jack_port_by_name(client1, jack_port_name(output_port1)), output_port1);
    } else {
        Log("Checking jack_port_by_name() return value... ok\n");
    }
    if (NULL != jack_port_by_name(client1, jack_port_short_name(output_port1))) {
        printf("!!! ERROR !!! function jack_port_by_name() return a value (%px) while name is incomplete !\n", jack_port_by_name(client1, jack_port_short_name(output_port1)));
    } else {
        Log("Checking jack_port_by_name() with bad argument... ok (returned id 0)\n");
    }

    /**
    * remove the output port previously created
    * no more ports should subsist here for our client.
    *
    */
    if (jack_port_unregister(client1, output_port1) != 0) {
        printf("!!! ERROR !!! while unregistering port %s.\n", jack_port_name(output_port1));
    }

    /**
     * list all in ports
     *
     */
    inports = jack_get_ports(client1, NULL, NULL, 0);

    /**
     * Test the first PHY (physical) connection to see if it's "mine".
     * and report the result in the test that began before.
     * The result is printed later.
     *
     */
    if (jack_port_is_mine(client1, jack_port_by_name(client1, inports[0]))) {
        is_mine = 0;
    }

    /**
     * List all devices' flags and print them...
     *
     */
    Log("\nTry functions jack_get_ports, jack_port_flag & jack_port_by_name to list PHY devices...\n");
    Log("-----------------------------------------------------------\n");
    Log("---------------------------DEVICES-------------------------\n");
    Log("-----------------------------------------------------------\n");
    a = 0;
    while (inports[a] != NULL) {
        flag = jack_port_flags(jack_port_by_name(client1, inports[a]) );
        Log("   * %s (id : %i)\n", inports[a], jack_port_by_name(client1, inports[a]));
        Log("    (");
        if (flag & JackPortIsInput)
            Log("JackPortIsInput ");
        if (flag & JackPortIsOutput)
            Log("JackPortIsOutput ");
        if (flag & JackPortIsPhysical)
            Log("JackPortIsPhysical ");
        if (flag & JackPortCanMonitor)
            Log("JackPortCanMonitor ");
        if (flag & JackPortIsTerminal)
            Log("JackPortIsTerminal ");
        Log(")\n\n");
        a++;
    }
    Log("-----------------------------------------------------------\n\n");

    /**
     * list all PHY in/out ports...
     * This list will be used later many times.
     *
     */
    outports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsOutput);
    inports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsInput);

    if (outports == NULL) {
        printf("!!! WARNING !!! no physical capture ports founded !\n");
    }
    if (inports == NULL) {
        printf("!!! WARNING !!! no physical output ports founded !\n");
    }

    /**
     * Brute test : try to create as many ports as possible.
     * It stops when jack returns an error.
     * Then try to connect each port to physical entry...
     * Check also that graph reorder callback is called.
     *
     */
    Log("Registering as many ports as possible and connect them to physical entries...\n");
    lineports = linecount;
    t_error = 0;

    i = 0;	// number of couple 'input-ouput'
    j = 0;	// number of ports created
    port_callback_reg = 0;	// number of port registration received by the callback
    reorder = 0;			// number of graph reorder callback activation
    test_link = 0 ;			// Test the "overconnect" function only one time
    while (t_error == 0) {
        sprintf (portname, "input_%d", i);
        input_port1 = jack_port_register(client1, portname,
                                         JACK_DEFAULT_AUDIO_TYPE,
                                         JackPortIsInput, 0);
        j++;
        if (input_port1 == NULL) {
            j--;
            t_error = 1;
        } else {
            // Connect created input to PHY output
            a = 0;
            while (outports[a] != NULL) {
                if (jack_connect(client1, outports[a], jack_port_name(input_port1))) {
                    printf ("error : cannot connect input PHY port to client port %s\n", jack_port_name(input_port1));
                } else {
                    // printf ("input PHY port %s connected to client port %s\n", outports[a], jack_port_name(input_port1));
                }
                a++;
            }
            // Try one time to "overconnect" 2 ports (the latest created)...
            if (test_link == 0) {
                if (jack_connect(client1, outports[a - 1], jack_port_name(input_port1)) == EEXIST) {
                    // cannot over-connect input PHY port to client port. ok.
                    test_link = 1;
                }
            }
        }

        sprintf(portname, "output_%d", i);
        output_port1 = jack_port_register(client1, portname,
                                          JACK_DEFAULT_AUDIO_TYPE,
                                          JackPortIsOutput, 0);
        j++;
        if (output_port1 == NULL) {
            t_error = 1;
            j--;
        } else {
            // Connect created input to PHY output
            a = 0;
            while (inports[a] != NULL) {
                if (jack_connect(client1, jack_port_name(output_port1), inports[a])) {
                    printf ("error : cannot connect input PHY port %s to client port %s\n", inports[a], jack_port_name(output_port1));
                } else {
                    // output PHY port %s connected to client port. ok.
                }
                a++;
            }
            // Try one time to "overconnect" 2 ports (the latest created)...
            if (test_link == 0) {
                if (jack_connect(client1, jack_port_name(output_port1), inports[a - 1]) == EEXIST) {
                    // cannot over-connect output PHY port to client port. ok.
                    test_link = 1;
                }
            }
        }
        i++;
    }

    jack_sleep(1 * 1000); // To hope all port registration and reorder callback have been received...
    if (j == port_callback_reg) {
        Log("%i ports have been successfully created, and %i callback reg ports have been received... ok\n", j, port_callback_reg);
    } else {
        printf("!!! ERROR !!! %i ports have been created, and %i callback reg ports have been received !\n", j, k);
    }
    if (reorder == (2 * j)) {
        Log("%i graph reorder callback have been received... ok\n", reorder);
    } else {
        printf("!!! ERROR !!! %i graph reorder callback have been received (maybe non-valid value)...\n", reorder);
    }
    /**
     * print basic test connection functions result ...
     * over-connected means here that we try to connect 2 ports that are already connected.
     *
     */
    if (test_link) {
        Log("Jack links can't be 'over-connected'... ok\n");
    } else {
        printf("!!! ERROR !!! Jack links can be 'over-connected'...\n");
    }
    /**
     * Print the result of the two jack_is_mine test.
     *
     */
    if (is_mine == 1) {
        Log("Checking jack_port_is_mine()... ok\n");
    } else {
        printf("!!! ERROR !!! jack_port_is_mine() function seems to send non-valid datas !\n");
    }
    /**
     * Free the array of the physical input and ouput ports.
     * (as mentionned in the doc of jack_get_ports)
     *
     */
    free(inports);
    free(outports);

    /**
     * Try to "reactivate" the client whereas it's already activated...
     *
     */
    if (jack_activate(client1) < 0) {
        printf("!!! ERROR !!! Cannot activate client1 a second time...\n");
        exit(1);
    } else {
        Log("jackd server accept client.jack_activate() re-activation (while client was already activated).\n");
    }

    /**
     * Deregister all ports previously created.
     *
     */
    Log("Deregistering all ports of the client...\n");
    inports = jack_get_ports(client1, NULL, NULL, 0);
    a = 0;
    while (inports[a] != NULL) {
        flag = jack_port_flags(jack_port_by_name(client1, inports[a]));
        input_port1 = jack_port_by_name(client1, inports[a]);
        if (jack_port_is_mine(client1, input_port1)) {
            if (jack_port_unregister(client1, input_port1) != 0) {
                printf("!!! ERROR !!! while unregistering port %s.\n", jack_port_name(output_port1));
            }
        }
        a++;
    }

    free(inports); // free array of ports (as mentionned in the doc of jack_get_ports)

    /**
     *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
     *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
     				Open a new client (second one) to test some other things...
     *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
     *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
     */

    Log("\n\n----------------------------------------------------------------------\n");
    Log("Starting second new client 'jack_test_#2'...\n");
    /* open a client connection to the JACK server */
    client_name2 = "jack_test_#2";
    linecl2 = linecount; // reminders for graph analysis
    client2 = jack_client_new(client_name2);

    if (client2 == NULL) {
        fprintf(stderr, "jack_client_new() failed for %s.\n"
                "status = 0x%2.0x\n", client_name2, status);
        if (status & JackServerFailed) {
            fprintf(stderr, "Unable to connect client2 to JACK server\n");
        }
        exit(1);
    }

	// Check client registration callback
	jack_sleep(1000);
	if (client_register == 0)
		printf("!!! ERROR !!! Client registration callback not called!\n");

    /**
     * Register callback for this client.
     * Callbacks are the same as the first client for most of them, excepted for process audio callback.
     *
     */
    jack_set_port_registration_callback(client2, Jack_Port_Register, 0);

    jack_set_process_callback(client2, process2, 0);

    jack_on_shutdown(client2, jack_shutdown, 0);

    /**
     * Register one input and one output for each client.
     *
     */
    Log("registering 1 input/output ports for each client...\n");

    output_port1 = jack_port_register(client1, "out1",
                                      JACK_DEFAULT_AUDIO_TYPE,
                                      JackPortIsOutput, 0);
    output_port2 = jack_port_register(client2, "out2",
                                      JACK_DEFAULT_AUDIO_TYPE,
                                      JackPortIsOutput, 0);
    input_port1 = jack_port_register(client1, "in1",
                                     JACK_DEFAULT_AUDIO_TYPE,
                                     JackPortIsInput, 0);
    input_port2 = jack_port_register(client2, "in2",
                                     JACK_DEFAULT_AUDIO_TYPE,
                                     JackPortIsInput, 0);
    if ((output_port1 == NULL) || (output_port2 == NULL) || (input_port1 == NULL) || (input_port2 == NULL)) {
        printf("!!! ERROR !!! Unable to register ports...\n");
    }

    /**
     * Set each process mode to idle and activate client2
     *
     */
    process2_activated = -1;
    process1_activated = -1;
    if (jack_activate(client2) < 0) {
        printf ("Fatal error : cannot activate client2\n");
        exit (1);
    }

    /**
     * Connect the two clients and check that all connections are well-done.
     *
     */
    Log("Testing connections functions between clients...\n");
    if (jack_connect(client1, jack_port_name(output_port1), jack_port_name(input_port2)) != 0) {
        printf("!!! ERROR !!! while client1 intenting to connect ports...\n");
    }
    if (jack_connect(client2, jack_port_name(output_port2), jack_port_name(input_port1)) != 0) {
        printf("!!! ERROR !!! while client2 intenting to connect ports...\n");
    }
    if (jack_connect(client1, jack_port_name(output_port1), jack_port_name(input_port1)) != 0) {
        printf("!!! ERROR !!! while client1 intenting to connect ports...\n");
    }

    /**
     * Test the port_connected function...
     *
     */
    if ((jack_port_connected(output_port1) == jack_port_connected(input_port1)) &&
            (jack_port_connected(output_port2) == jack_port_connected(input_port2)) &&
            (jack_port_connected(output_port2) == 1) &&
            (jack_port_connected(output_port1) == 2)
       ) {
        Log("Checking jack_port_connected()... ok.\n");
    } else {
        printf("!!! ERROR !!! function jack_port_connected() return a bad value !\n");
        printf("jack_port_connected(output_port1) %d\n", jack_port_connected(output_port1));
        printf("jack_port_connected(output_port2) %d\n", jack_port_connected(output_port2));
        printf("jack_port_connected(input_port1) %d\n", jack_port_connected(input_port1));
        printf("jack_port_connected(input_port2) %d\n", jack_port_connected(input_port2));
    }

    /**
     * Test a new time the port_by_name function...(now we are in multi-client mode)
     *
     */
    Log("Testing again jack_port_by_name...\n");
    if (output_port1 != jack_port_by_name(client1, jack_port_name(output_port1))) {
        printf("!!! ERROR !!! function jack_port_by_name() return bad value in a multi-client application!\n");
        printf("!!! jack_port_by_name(jack_port_name(_ID_) ) != _ID_ in multiclient application.\n");
    } else {
        Log("Checking jack_port_by_name() function with a multi-client application... ok\n");
    }

    /**
     * Test the port_connected_to function...
     *
     */
    if ((jack_port_connected_to (output_port1, jack_port_name(input_port2))) &&
            (!(jack_port_connected_to (output_port2, jack_port_name(input_port2))))) {
        Log("checking jack_port_connected_to()... ok\n");
    } else {
        printf("!!! ERROR !!! jack_port_connected_to() return bad value !\n");
    }

    /**
     * Test the port_get_connections & port_get_all_connections functions...
     *
     */
    Log("Testing jack_port_get_connections and jack_port_get_all_connections...\n");
    a = 0;
    t_error = 0;
    connexions1 = jack_port_get_connections (output_port1);
    connexions2 = jack_port_get_all_connections(client1, output_port1);
    if ((connexions1 == NULL) || (connexions2 == NULL)) {
        printf("!!! ERROR !!! port_get_connexions or port_get_all_connexions return a NULL pointer !\n");
    } else {
        while ((connexions1[a] != NULL) && (connexions2[a] != NULL) && (t_error == 0)) {
            t_error = strcmp(connexions1[a], connexions2[a]);
            a++;
        }

        if (t_error == 0) {
            Log("Checking jack_port_get_connections Vs jack_port_get_all_connections... ok\n");
        } else {
            printf("!!! ERROR !!! while checking jack_port_get_connections Vs jack_port_get_all_connections...\n");
        }
    }
    a = 0;
    t_error = 0;
    inports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
    connexions1 = NULL;
    assert(inports != NULL);
    if (inports[0] != NULL) {
        connexions1 = jack_port_get_connections (jack_port_by_name(client1, inports[0]));
        connexions2 = jack_port_get_all_connections(client1, jack_port_by_name(client1, inports[0]));
    }

    free (inports);
    if (connexions1 == NULL) {
        Log("checking jack_port_get_connections() for external client... ok\n");
    } else {
        while ((connexions1[a] != NULL) && (connexions2[a] != NULL) && (t_error == 0)) {
            t_error = strcmp(connexions1[a], connexions2[a]);
            a++;
        }
    }
    if (t_error == 0) {
        Log("Checking jack_port_get_connections() Vs jack_port_get_all_connections() on PHY port... ok\n");
    } else {
        printf("!!! ERROR !!! while checking jack_port_get_connections() Vs jack_port_get_all_connections() on PHY port...\n");
    }

	if (jack_disconnect(client1, jack_port_name(output_port1), jack_port_name(input_port1)) != 0) {
        printf("!!! ERROR !!! while client1 intenting to disconnect ports...\n");
    }
    if (jack_disconnect(client1, jack_port_name(output_port2), jack_port_name(input_port1)) != 0) {
        printf("!!! ERROR !!! while client1 intenting to disconnect ports...\n");
    }
    // No links should subsist now...

    /**
     * Checking data connexion
     * establishing a link between client1.out1 --> client2.in2
     * Send the signal1 test on out1. Record the result into signal2. (see process functions).
    ---------------------------------------------------------------------------*/
    Log("Testing connections datas between clients...\n");
    jack_connect(client2, jack_port_name(output_port1), jack_port_name(input_port2) );
    process2_activated = -1;
    process1_activated = -1;
    Log("process 2 : idle mode...\n");
    Log("Sending datas...");
    index1 = 0;
    index2 = 0;
    process1_activated = 1; // We start emitting first.
    process2_activated = 1; // So record begin at least when we just begin to emitt the signal, else at next call of process with
    // nframe = jack buffersize shifting.

    while (process2_activated == 1) {
        jack_sleep(1 * 1000);
        Log(".");
    }
    index2 = 0;
    Log("\nAnalysing datas...\n"); // search the first occurence of the first element of the reference signal in the recorded signal
    while (signal2[index2] != signal1[1] ) {
        index2++;
        if (index2 == 95999) {
            printf("!!! ERROR !!! Data not found in first connexion data check!\n");
            break;
        }
    }
    index1 = index2;
    Log("Data founded at offset %i.\n", index2);
    // And now we founded were the recorded data are, we can see if the two signals matches...
    while ( (signal2[index2] == signal1[index2 - index1 + 1]) || (index2 == 95999) || ((index2 - index1 + 1) == 47999) ) {
        index2++;
    }
    Log("Checking difference between datas... %i have the same value...\n", index2 - index1);
    if ((index2 - index1) == 48000) {
        Log("Data received are valid...\n");
    } else {
        printf("!!! ERROR !!! data transmission seems not to be valid in first connexion data check!\n");
    }
    if (jack_disconnect(client1, jack_port_name(output_port1), jack_port_name(input_port2) ) != 0)
        // no more connection between ports exist now...
    {
        printf("Error while establishing new connexion (disconnect).\n");
    }

    /**
     * Test TIE MODE
     * (This mode seems to be problematic in standard jack version 0.100. It seems that nobody
     * is used to apply this mode because the tie mode doesn't work at all. A patch seems difficult to produce
     * in this version of jack. Tie mode work well in MP version.)
     * Test some basic thinks (tie with 2 differents client, tie non-owned ports...)
     * Tie client1.in1 and client1.out1 ports, and make some data test to check the validity of the tie.
     *
     */
    Log("Testing tie mode...\n");
    if (jack_port_tie(input_port1, output_port2) != 0) {
        Log("not possible to tie two ports from two differents clients... ok\n");
    } else {
        printf("!!! ERROR !!! port_tie has allowed a connexion between two differents clients !\n");
        jack_port_untie(output_port2);
    }
    Log("Testing connections datas in tie mode...\n");
    int g;
    for (g = 0; g < 96000; g++)
        signal2[g] = 0.0;
    // Create a loop (emit test) client2.out2----client.in1--tie--client1.out1-----client2.in1 (receive test)
    if (jack_port_tie(input_port1, output_port1) != 0) {
        printf("Unable to tie... fatal error : data test will not be performed on tie mode !!\n");
    } else { // begin of tie
        if (jack_connect(client1, jack_port_name(output_port1), jack_port_name(input_port2)) != 0) {
            printf("!!! ERROR !!! while client1 intenting to connect ports...\n");
        }
        if (jack_connect(client1, jack_port_name(output_port2), jack_port_name(input_port1)) != 0) {
            printf("!!! ERROR !!! while client1 intenting to connect ports...\n");
        }

        process1_activated = -1;
        process2_activated = -1;

        //		We can manualy check here that the tie is effective.
        //		ie : playing a wav with a client, connecting ports manualy with qjackctl, and listen...
        // 		printf("manual test\n");
        // 		jack_sleep(50);
        // 		printf("end of manual test\n");

        index1 = 0;
        index2 = 0;
        process1_activated = -1;
        process2_activated = 2;

        Log("Sending datas...");

        while (process2_activated == 2) {
            jack_sleep(1 * 1000);
            Log(".");
        }
        process1_activated = -1;
        process2_activated = -1;
        index2 = 0;
        Log("\nAnalysing datas...\n");
        // We must find at least 2 identical values to ensure we are at the right place in the siusoidal array...
        while (!((signal2[index2] == signal1[1]) && (signal2[index2 + 1] == signal1[2]))) {
            index2++;
            if (index2 == 95999) {
                printf("!!! ERROR !!! Data not found in connexion check of tie mode!\n");
                break;
            }
        }
        index1 = index2;
        Log("Tie mode : Data founded at offset %i.\n", index2);
        while (signal2[index2] == signal1[index2 - index1 + 1]) {
            index2++;
            if ((index2 == 95999) || ((index2 - index1 + 1) == 47999)) {
                break;
            }
        }
        Log("Checking difference between datas... %i have the same value...\n", index2 - index1);
        if ((index2 - index1) > 47995) {
            Log("Data received in tie mode are valid...\n");
        } else {
            // in tie mode, the buffers adress should be the same for the two tied ports.
            printf("!!! ERROR !!! data transmission seems not to be valid !\n");
            printf("Links topology : (emitt) client2.out2 ----> client1.in1--(tie)--client1.out1----->client2.in2 (recive)\n");
            printf("  port_name    : Port_adress \n");
            printf("  output_port1 : %px\n", jack_port_get_buffer(output_port1, cur_buffer_size));
            printf("  input_port2  : %px\n", jack_port_get_buffer(input_port2, cur_buffer_size));
            printf("  output_port2 : %px\n", jack_port_get_buffer(output_port2, cur_buffer_size));
            printf("  input_port1  : %px\n", jack_port_get_buffer(input_port1, cur_buffer_size));
        }

        jack_port_untie(output_port1);
        jack_port_disconnect(client1, output_port2);
        jack_port_disconnect(client1, output_port1);

    } //end of tie


    /**
     * Testing SUMMATION CAPABILITIES OF JACK CONNECTIONS
     *
     * In a short test, we just check a simple summation in jack.
     * A first client(client1) send two signal in phase opposition
     * A second client(client2) record the summation at one of his port
     * So, the result must be zero...
     * See process1 for details about steps of this test
     *
     */
    // fprintf(file, "Sum test\n");
    Log("Checking summation capabilities of patching...\n");
    output_port1b = jack_port_register(client1, "out1b",
                                       JACK_DEFAULT_AUDIO_TYPE,
                                       JackPortIsOutput, 0);
    jack_connect(client2, jack_port_name(output_port1), jack_port_name(input_port2));
    jack_connect(client2, jack_port_name(output_port1b), jack_port_name(input_port2));

    process1_activated = 3;
    process2_activated = -1;
    for (g = 0; g < 96000; g++)
        signal2[g] = 0.0;
    index1 = 0;
    index2 = 0;

    Log("Sending datas...");
    process2_activated = 3;

    while (process2_activated == 3) {
        jack_sleep(1 * 1000);
        Log(".");
    }
    process1_activated = -1;
    process2_activated = -1;
    index2 = 0;
    Log("\nAnalysing datas...\n"); // same idea as above, with first data check...
    while (!((signal2[index2] == 0.0 ) && (signal2[(index2 + 1)] == 0.0 ))) {
        index2++;
        if (index2 == 95999) {
            printf("!!! ERROR !!! Data not found in summation check!\n");
            break;
        }
    }
    index1 = index2;
    Log("Data founded at offset %i.\n", index2);

    while ( signal2[index2] == 0.0 ) {
        index2++;
        if ((index2 > 95998) || ((index2 - index1 + 1) > 47998)) {
            break;
        }
    }
    Log("Checking difference between datas...\n");
    if ((index2 - index1) > 47996) {
        Log("Data mixed received are valid...\nSummation is well done.\n");
    } else {
        printf("!!! ERROR !!! data transmission / summation seems not to be valid !\n");
    }
    jack_port_disconnect(client1, output_port1);
    jack_port_disconnect(client1, output_port1b);
    jack_port_unregister(client1, output_port1b);

    if (jack_port_name(output_port1b) != NULL ) {
        printf("!!! WARNING !!! port_name return something while the port have been unregistered !\n");
        printf("!!! Name of unregistered port : %s !\n", jack_port_name(output_port1b));
    } else {
        Log("Checking jack_port_name() with a non valid port... ok\n");
    }

    if (jack_port_set_name(output_port1b, "new_name") == 0 ) {
        printf("!!! WARNING !!! An unregistered port can be renamed successfully !\n");
    } else {
        Log("Checking renaming of an unregistered port... ok\n");
    }
    inports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
    if (jack_port_set_name(jack_port_by_name(client1, inports[0]), "new_name") == 0 ) {
        printf("!!! WARNING !!! A PHYSICAL port can be renamed successfully !\n");
    } else {
        Log("Checking renaming of an unregistered port... ok\n");
    }
    free (inports);


    /**
     * Checking latency issues
     * here are simple latency check
     * We simply check that the value returned by jack seems ok
     * Latency compensation is a difficult point.
     * Actually, jack is not able to see "thru" client to build a full latency chain.
     * Ardour use theses informations to do internally his compensations.
     *
     * 3 test are done : one with no connections between client, one with a serial connection, and one with parallel connection
     */
    Log("Checking about latency functions...\n");
    t_error = 0;
    jack_recompute_total_latencies(client1);
    if ((jack_port_get_latency (output_port1) != 0) ||
            (jack_port_get_total_latency(client1, output_port1) != 0) ) {
        t_error = 1;
        printf("!!! ERROR !!! default latency of a non-PHY device is not set to zero !\n");
    }

    inports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
    outports = jack_get_ports(client1, NULL, NULL, JackPortIsPhysical | JackPortIsOutput);
    if (inports[0] != NULL) {
        output_ext_latency = jack_port_get_latency (jack_port_by_name(client1, inports[0]));  // from client to out driver (which has "inputs" ports..)
		input_ext_latency = jack_port_get_latency (jack_port_by_name(client1, outports[0]));  // from in driver (which has "output" ports..) to client
        if (output_ext_latency != jack_port_get_total_latency(client1, jack_port_by_name(client1, inports[0]))) {
            t_error = 1;
            printf("!!! ERROR !!! get_latency & get_all_latency for a PHY device (unconnected) didn't return the same value !\n");
        }
        Log("Checking a serial model with 2 clients...\n");

        jack_connect(client1, jack_port_name(output_port1), jack_port_name(input_port2));
        jack_connect(client1, outports[0], jack_port_name(input_port1));
        jack_connect(client2, jack_port_name(output_port2), inports[0]);
        jack_port_set_latency(output_port2, 256);
        jack_recompute_total_latencies(client1);

        if ((jack_port_get_latency (output_port1) != 0) ||
                (jack_port_get_total_latency(client1, output_port1) != 0) ||
                (jack_port_get_latency (jack_port_by_name(client1, inports[0])) != (output_ext_latency)) ||
                (jack_port_get_total_latency(client1, jack_port_by_name(client1, inports[0])) != (output_ext_latency + 256)) ||
                (jack_port_get_total_latency(client1, output_port2) != (output_ext_latency + 256)) ||
                (jack_port_get_total_latency(client1, input_port2) != 0) ||
                (jack_port_get_total_latency(client1, input_port1) != input_ext_latency) ||
                (jack_port_get_latency (jack_port_by_name(client1, outports[0])) != input_ext_latency) ||
                (jack_port_get_total_latency(client1, jack_port_by_name(client1, outports[0])) != input_ext_latency)
           ) {
            printf("!!! WARNING !!! get_latency functions may have a problem : bad value returned !\n");
            printf("!!! get_latency(output_port1) : %i (must be 0)\n", jack_port_get_latency(output_port1));
            printf("!!! get_total_latency(output_port1) : %i (must be 0)\n", jack_port_get_total_latency(client1, output_port1));
            printf("!!! get_latency(PHY[0]) : %i (must be external latency : %i)\n", jack_port_get_latency(jack_port_by_name(client1, inports[0])), output_ext_latency);
            printf("!!! get_total_latency(PHY[0]) : %i (must be %i)\n", jack_port_get_total_latency(client1, jack_port_by_name(client1, inports[0])) , (output_ext_latency + 256));
            printf("!!! get_total_latency(output_port2) : %i (must be %i)\n", jack_port_get_total_latency(client1, output_port2), (output_ext_latency + 256));
            printf("!!! get_total_latency(input_port2) : %i (must be 0)\n", jack_port_get_total_latency(client1, input_port2));
            printf("!!! get_total_latency(input_port1) : %i (must be %i)\n", jack_port_get_total_latency(client1, input_port1), input_ext_latency);
            printf("!!! get_latency(PHY[0]) : %i (must be %i)\n", jack_port_get_latency(jack_port_by_name(client1, outports[0])), input_ext_latency);
            printf("!!! get_total_latency(PHY[0]) : %i (must be %i)\n", jack_port_get_total_latency(client1, jack_port_by_name(client1, outports[0])), input_ext_latency);

        } else {
            Log("get_latency & get_total_latency seems quite ok...\n");
        }

        jack_port_disconnect(client1, output_port1);
	    jack_port_disconnect(client1, output_port2);
		jack_port_disconnect(client1, input_port1);
	    jack_port_disconnect(client1, input_port2);
	    Log("Checking a parallel model with 2 clients...\n");
        jack_connect(client2, outports[0], jack_port_name(input_port1));
        jack_connect(client2, outports[0], jack_port_name(input_port2));
        jack_connect(client2, jack_port_name(output_port1), inports[0]);
        jack_connect(client2, jack_port_name(output_port2), inports[0]);
        jack_port_set_latency(output_port1, 256);
        jack_port_set_latency(output_port2, 512);
        jack_recompute_total_latencies(client1);

        if ((jack_port_get_latency(output_port1) != 256 ) ||
			(jack_port_get_total_latency(client1, output_port1) != (256 + output_ext_latency)) ||
			(jack_port_get_latency(output_port2) != 512) ||
			(jack_port_get_total_latency(client1, output_port2) != (512 + output_ext_latency)) ||
			(jack_port_get_latency(jack_port_by_name(client1, inports[0])) != output_ext_latency) ||
			(jack_port_get_total_latency(client1, jack_port_by_name(client1, inports[0])) != (512 + output_ext_latency))
           ) {
            printf("!!! WARNING !!! get_latency functions may have a problem : bad value returned !\n");
			printf("!!! get_latency(output_port1) : %i (must be 256)\n", jack_port_get_latency(output_port1));
			printf("!!! get_total_latency(output_port1) : %i (must be 256 + output_ext_latency)\n", jack_port_get_total_latency(client1, output_port1));
			printf("!!! get_latency(output_port2) : %i (must 512)\n", jack_port_get_latency(output_port2));
			printf("!!! get_total_latency(output_port2) : %i (must 512 + output_ext_latency)\n", jack_port_get_total_latency(client1, output_port2));
			printf("!!! get_latency(inports[0])) : %i (must output_ext_latency)\n", jack_port_get_latency(jack_port_by_name(client1, inports[0])));
			printf("!!! get_total_latency(inports[0]) : %i (must 512 + output_ext_latency)\n", jack_port_get_total_latency(client1, jack_port_by_name(client1, inports[0])));
        } else {
            Log("get_latency & get_total_latency seems quite ok...\n");
        }
    } else {
        printf("No physical port founded : not able to test latency functions...");
    }

    jack_port_disconnect(client1, input_port1);
    jack_port_disconnect(client1, input_port2);
    jack_port_disconnect(client1, output_port1);
    jack_port_disconnect(client1, output_port2);

	jack_sleep(1000);

    free(inports);
    free(outports);

    /**
     * Checking transport API.
     * Simple transport test.
     * Check a transport start with a "slow" client, simulating a delay around 1 sec before becoming ready.
     *
     */
	Log("-----------------------------------------------------------\n");
    Log("---------------------------TRANSPORT-----------------------\n");
    Log("-----------------------------------------------------------\n");

    lineports = linecount;

    if (transport_mode) {
        int wait_count;
        ts = jack_transport_query(client1, &pos);
        if (ts == JackTransportStopped) {
            Log("Transport is stopped...\n");
        } else {
            jack_transport_stop(client1);
            Log("Transport state : %i\n", ts);
        }
        if (jack_set_sync_callback(client2, Jack_Sync_Callback, 0) != 0)
            printf("error while calling set_sync_callback...\n");

        Log("starting transport...\n");

        starting_state = 1; // Simulate starting state
        jack_transport_start(client1);

        // Wait until sync callback is called
        while (!(sync_called)) {
            jack_sleep(1 * 1000);
        }

        // Wait untill rolling : simulate sync time out
        Log("Simulate a slow-sync client exceeding the time-out\n");
        wait_count = 0;

        do {
            jack_sleep(100); // Wait 100 ms each cycle
            wait_count++;
            if (wait_count == 100) {
                Log("!!! ERROR !!! max time-out exceedeed : sync time-out does not work correctly\n");
                break;
            }
            ts = jack_transport_query(client2, &pos);
            Log("Waiting....pos = %ld\n", pos.frame);
            display_transport_state();

        } while (ts != JackTransportRolling);

        Log("Sync callback have been called %i times.\n", sync_called);
        jack_transport_stop(client1);

        // Wait until stopped
        ts = jack_transport_query(client2, &pos);
        while (ts != JackTransportStopped) {
            jack_sleep(1 * 1000);
            ts = jack_transport_query(client2, &pos);
        }

        // Simulate starting a slow-sync client that rolls after 0.5 sec
        Log("Simulate a slow-sync client that needs 0.5 sec to start\n");
        sync_called = 0;
        wait_count = 0;
        starting_state = 1; // Simulate starting state

        Log("Starting transport...\n");
        jack_transport_start(client1);
        display_transport_state();

        Log("Waiting 0.5 sec...\n");
        jack_sleep(500);
        starting_state = 0; // Simulate end of starting state after 0.5 sec

        // Wait untill rolling
        ts = jack_transport_query(client2, &pos);
        while (ts != JackTransportRolling) {
            jack_sleep(100); // Wait 100 ms each cycle
            wait_count++;
            if (wait_count == 10) {
                Log("!!! ERROR !!! starting a slow-sync client does not work correctly\n");
                break;
            }
            ts = jack_transport_query(client2, &pos);
        }

        if (sync_called == 0)
            Log("!!! ERROR !!! starting a slow-sync client does not work correctly\n");

        Log("Sync callback have been called %i times.\n", sync_called);
        display_transport_state();

        // Test jack_transport_locate while rolling
        Log("Test jack_transport_locate while rolling\n");
        ts = jack_transport_query(client2, &pos);
        Log("Transport current frame = %ld\n", pos.frame);
        jack_nframes_t cur_frame = pos.frame;

        wait_count = 0;
        do {
            display_transport_state();
            jack_sleep(10);  // 10 ms
            // locate at first...
            wait_count++;
            if (wait_count == 1) {
                Log("Do jack_transport_locate\n");
                jack_transport_locate(client1, cur_frame / 2);
            } else if (wait_count == 100) {
                break;
            }
            ts = jack_transport_query(client2, &pos);
            Log("Locating.... frame = %ld\n", pos.frame);
        } while (pos.frame > cur_frame);

        ts = jack_transport_query(client2, &pos);
        Log("Transport current frame = %ld\n", pos.frame);
        if (wait_count == 100) {
            printf("!!! ERROR !!! jack_transport_locate does not work correctly\n");
        }

        // Test jack_transport_reposition while rolling
        Log("Test jack_transport_reposition while rolling\n");
        ts = jack_transport_query(client2, &pos);
        Log("Transport current frame = %ld\n", pos.frame);
        cur_frame = pos.frame;

        wait_count = 0;
        do {
            display_transport_state();
            jack_sleep(10);  // 10 ms
            // locate at first...
            wait_count++;
            if (wait_count == 1) {
                Log("Do jack_transport_reposition\n");
                request_pos.frame = cur_frame / 2;
                jack_transport_reposition(client1, &request_pos);
            } else if (wait_count == 100) {
                break;
            }
            ts = jack_transport_query(client2, &pos);
            Log("Locating.... frame = %ld\n", pos.frame);
        } while (pos.frame > cur_frame);

        ts = jack_transport_query(client2, &pos);
        Log("Transport current frame = %ld\n", pos.frame);
        if (wait_count == 100) {
            printf("!!! ERROR !!! jack_transport_reposition does not work correctly\n");
        }

        // Test jack_transport_reposition while stopped
        jack_transport_stop(client1);
        ts = jack_transport_query(client2, &pos);
        Log("Transport current frame = %ld\n", pos.frame);

        Log("Test jack_transport_reposition while stopped\n");
        wait_count = 0;
        request_pos.frame = 10000;
        jack_transport_reposition(client1, &request_pos);

        do {
            display_transport_state();
            jack_sleep(100); // 100 ms
            if (wait_count++ == 10)
                break;
            ts = jack_transport_query(client2, &pos);
            Log("Locating.... frame = %ld\n", pos.frame);
        } while (pos.frame != 10000);

        ts = jack_transport_query(client2, &pos);
        Log("Transport current frame = %ld\n", pos.frame);
        if (pos.frame != 10000) {
            printf("!!! ERROR !!! jack_transport_reposition does not work correctly\n");
        }

        jack_transport_stop(client1);

        /* Tell the JACK server that we are ready to roll.  Our
         * process() callback will start running now. */

    } else {
        printf("Transport check is disabled...\n");
    }

    time_before_exit = time_to_run;
    while (time_before_exit != 0) {
        jack_sleep (1 * 1000);
        time_before_exit--;
    }

	if (jack_deactivate(client2) != 0) {
        printf("!!! ERROR !!! jack_deactivate does not return 0 for client2 !\n");
    }
    if (jack_deactivate(client1) != 0) {
        printf("!!! ERROR !!! jack_deactivate does not return 0 for client1 !\n");
    }

	/**
     * Checking jack_frame_time.
    */
	Log("Testing jack_frame_time...\n");
	jack_set_process_callback(client1, process4, client1);
	jack_activate(client1);
	jack_sleep(2 * 1000);


	/**
     * Checking alternate thread model
    */
	Log("Testing alternate thread model...\n");
	jack_deactivate(client1);
	jack_set_process_callback(client1, NULL, NULL);  // remove callback
	jack_set_process_thread(client1, jack_thread, client1);
	jack_activate(client1);
	jack_sleep(2 * 1000);

	/**
     * Checking callback exiting : when the return code is != 0, the client is desactivated.
    */
	Log("Testing callback exiting...\n");
	jack_deactivate(client1);
	jack_set_process_thread(client1, NULL, NULL); // remove thread callback
	jack_set_process_callback(client1, process3, 0);
	jack_activate(client1);
	jack_sleep(3 * 1000);

    /**
     *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
     *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
     *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
     						Closing program
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
    *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
	*/

    if (jack_deactivate(client2) != 0) {
        printf("!!! ERROR !!! jack_deactivate does not return 0 for client2 !\n");
    }
    if (jack_deactivate(client1) != 0) {
        printf("!!! ERROR !!! jack_deactivate does not return 0 for client1 !\n");
    }
    if (jack_client_close(client2) != 0) {
        printf("!!! ERROR !!! jack_client_close does not return 0 for client2 !\n");
    }
    if (jack_client_close(client1) != 0) {
        printf("!!! ERROR !!! jack_client_close does not return 0 for client1 !\n");
    }

    if (xrun == 0) {
        Log("No Xrun have been detected during this test... cool !\n");
    } else {
        printf("%i Xrun have been detected during this session (seen callback messages to see where are the problems).\n", xrun);
    }
    free(framecollect);
    free(signal1);
    free(signal2);
    Log("Exiting jack_test...\n");
    fclose(file);
    printf("*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n");
    sprintf (filename, "framegraph-%i.gnu", cur_buffer_size);
    file = fopen(filename, "w");
    if (file == NULL) {
        fprintf(stderr, "Erreur dans l'ouverture du fichier");
        exit( -1);
    }
    fprintf(file, "reset\n");
    fprintf(file, "set terminal png transparent nocrop enhanced\n");
    fprintf(file, "set output 'framegraph-%i-1.png'\n", cur_buffer_size);
    fprintf(file, "set title \"Frame time evolution during jack_test run\"\n");
    fprintf(file, "set yrange [ %i.00000 : %i.0000 ] noreverse nowriteback\n", cur_buffer_size - (cur_buffer_size / 8), cur_buffer_size + (cur_buffer_size / 8));
    fprintf(file, "set xrange [ 0.00000 : %i.0000 ] noreverse nowriteback\n" , linecount - 1);
    fprintf(file, "set ylabel \"Frametime evolution (d(ft)/dt)\"\n");
    fprintf(file, "set xlabel \"FrameTime\"\n");
    fprintf(file, "set label \"| buf.siz:%i | fr.wl:%i | rg.ports:%i | 2nd.client:%i | trsprt:%i |\" at graph 0.01, 0.04\n", linebuf, linefw, lineports, linecl2, linetransport);
    fprintf(file, "plot 'framefile-%i.dat' using 2 with impulses title \"Xruns\",'framefile-%i.dat' using 1 with line title \"Sampletime variation at %i\"\n", cur_buffer_size, cur_buffer_size, cur_buffer_size);

    fprintf(file, "set output 'framegraph-%i-2.png'\n", cur_buffer_size);
    fprintf(file, "set title \"Frame time evolution during jack_test run\"\n");
    fprintf(file, "set yrange [ %i.00000 : %i.0000 ] noreverse nowriteback\n", (int) (cur_buffer_size / 2), (int) (2*cur_buffer_size + (cur_buffer_size / 8)));
    fprintf(file, "set xrange [ 0.00000 : %i.0000 ] noreverse nowriteback\n" , linecount - 1);
    fprintf(file, "set ylabel \"Frametime evolution (d(ft)/dt)\"\n");
    fprintf(file, "set xlabel \"FrameTime\"\n");
    fprintf(file, "set label \"| buf.siz:%i | fr.wl:%i | rg.ports:%i | 2nd.client:%i | trsprt:%i |\" at graph 0.01, 0.04\n", linebuf, linefw, lineports, linecl2, linetransport);
    fprintf(file, "plot 'framefile-%i.dat' using 2 with impulses title \"Xruns\",'framefile-%i.dat' using 1 with line title \"Sampletime variation at %i\"\n", cur_buffer_size, cur_buffer_size, cur_buffer_size);
    fclose(file);
    return 0;
}