File: midimsg.m

package info (click to toggle)
octave-audio 2.0.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,632 kB
  • sloc: sh: 2,869; cpp: 886; python: 438; makefile: 228; xml: 21
file content (2032 lines) | stat: -rw-r--r-- 68,495 bytes parent folder | download
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
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
## Copyright (C) 2019-2021 John Donoghue <john.donoghue@ieee.org>
## 
## 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 3 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, see
## <https://www.gnu.org/licenses/>.

classdef midimsg
  ## -*- texinfo -*- 
  ## @deftypefn {} {@var{msg} =} midimsg (0)
  ## @deftypefnx {} {@var{msg} =} midimsg (@var{type} ....)
  ## @deftypefnx {} {@var{msg} =} midimsg ("note", @var{channel}, @var{note}, @var{velocity}, @var{duration}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("noteon", @var{channel}, @var{note}, @var{velocity}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("noteoff", @var{channel}, @var{note}, @var{velocity}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("programchange", @var{channel}, @var{prog}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("controlchange", @var{channel}, @var{ccnum}, @var{ccval}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("polykeypressure", @var{channel}, @var{note}, @var{keypressure}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("channelpressure", @var{channel}, @var{chanpressure}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("localcontrol", @var{channel}, @var{localcontrol}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("pitchbend", @var{channel}, @var{pitchchange}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("polyon", @var{channel}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("monoon", @var{channel}, @var{monochannels}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("omnion", @var{channel}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("omnioff", @var{channel}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("allsoundoff", @var{channel}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("allnotesoff", @var{channel}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("resetallcontrollers", @var{channel}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("start", @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("stop", @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("continue", @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("systemreset", @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("activesensing", @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("timingclock", @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("systemexclusive", @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("systemexclusive", @var{bytes}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("eox", @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("data", @var{bytes}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("songselect", @var{song}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("songpositionpointer", @var{songposition}, @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("tunerequest", @var{timestamp})
  ## @deftypefnx {} {@var{msg} =} midimsg ("miditimecodequarterframe", @var{timeseq}, @var{timevalue}, @var{timestamp})
  ## Create a midimsg object
  ##
  ## If the input parameter is 0, create an empty midi message object
  ## Otherwise the first variable is the type of message to create, followed by the additional
  ## parameters for the message.
  ##
  ## For each message type, the timestamp value is optional.
  ##
  ## @subsubheading Inputs
  ## @var{type} - string message type or a midimsgtype.@*
  ## @var{timestamp} - optional seconds time stamp for the event@*
  ## @var{channel} - the channel to use for the message (1..16)@*
  ## @var{note} - the value of the note to play/stop@*
  ## @var{velocity} - the velocity value for a note on/off, with 0 stopping a note from sounding.@*
  ## @var{duration} - seconds between starting and stopping a note when created a 'note' message.@*
  ## @var{prog} - program number when doing a program change message.@*
  ## @var{ccnum} - control change control number.@*
  ## @var{ccval} - control change control value.@*
  ## @var{keypressure} - key pressure value when creating a key pressure message.@*
  ## @var{chanpressure} - channel pressure value when creating a channelpressure message.@*
  ## @var{pitchchange} - pitch change value when creating a pitch bend message.@*
  ## @var{localcontrol} - boolean value when creating a localcontrol message.@*
  ## @var{monochannels} - channels specified for a mono on message.@*
  ## @var{bytes} - array of data in range of 0 to 127 specified as part of a data message or
  ## system exclusive message.@*
  ## @var{song} - song selection number for a song selection message.@*
  ## @var{songposition} - song position value for a song position message.@*
  ## @var{timeseq} - timecode sequence number for a miditimecodequarterframe message.@*
  ## @var{timevalue} - timecode value number for a miditimecodequarterframe message.@*
  ##
  ## @subsubheading Outputs
  ## @var{msg} - a midimsg object containing the midi data of the message
  ##
  ## @subsubheading Properties
  ## @var{timestamp} - timestamp of the message, or an array or timestamps if the the message is a
  ## compound message.@*
  ## @var{msgbytes} - the raw message bytes that make up the MIDI message.@*
  ## @var{nummsgbytes} - the number of message bytes that make up the MIDI message.@*
  ## @var{type} - string or midimsgtype that represents the message type.@*
  ## @var{channel} - the channel number for message.@*
  ## @var{note} - the note value for message (Only valid for noteon/off and polykeypressure).@*
  ## @var{velocity} - the velocity value for message (Only valid for noteon/off).@*
  ## @var{keypressure} - the keypressure value for message (Only valid for polykeypressure).@*
  ## @var{channelpressure} - the chanpressure value for message (Only valid for channelpressure).@*
  ## @var{localcontrol} - the localcontrol value for message (Only valid for localcontrol messages).@*
  ## @var{monochannels} - channels specified for a mono on message.@*
  ## @var{program} - program number specified for a program change message.@*
  ## @var{ccnumber} - control change number specified for a control change message.@*
  ## @var{ccvalue} - control change value specified for a control change message.@*
  ## @var{song} - song number for a song selection message.@*
  ## @var{songposition} - song position value for a song position message.@*
  ## @var{pitchchange} - pitch change value for a pitch bend message.@*
  ## @var{timecodesequence} - timecode sequence number for a miditimecodequarterframe message.@*
  ## @var{timecodevalue} - timecode value number for a miditimecodequarterframe message.@*
  ##
  ## @subsubheading Examples
  ## Create a note on/off pair with a duration of 1.5 seconds
  ## @example
  ## msg = midimsg('note', 1, 60, 100, 1.5)
  ## @end example
  ##
  ## Create a separate note on/off pair with a time between them of 1.5 seconds
  ## @example
  ## msg = [midimsg('noteon', 1, 60, 100, 0), midimsg('noteoff', 1, 60, 0, 1.5)]
  ## @end example
  ##
  ## Create a system reset message
  ## @example
  ## msg = midimsg('systemreset')
  ## @end example
  ##
  ## @seealso{midifileread, midisend, midireceive, midimsgtype}
  ## @end deftypefn

  properties (SetAccess = private, GetAccess = public)
   data = {};
   timestamp = {};
  endproperties
 
  properties (SetAccess = private, GetAccess = public)
    type; 
  endproperties

  methods
    
    function this = midimsg (typev, varargin)
      this.data = {};
      this.timestamp = {};

      if nargin < 1 || (! ischar (typev) && !isscalar (typev) && ! (class(typev) == "midimsgtype"))
        error ("Expected midi type or size");
      endif

      if strcmp (class(typev), "midimsgtype")
        typev = char(typev);
      elseif isscalar (typev)
        if typev == 0
          return; 
        else
          error ("Non zero size not supported yet");
        endif
      endif

      typev = lower (typev);
      switch typev
        case "noteon"
          # channel,note,velocity,timestamp
          if nargin < 4
            error ('noteon expects at least channel,note,velocity')
          endif
          # TODO: change channel to be 1 indexed
          chan = this.check_channel(varargin{1})-1;
          note = this.check_value127("note", varargin{2});
          vel = this.check_value127("velocity", varargin{3});
          timestamp = 0;
          if nargin > 4
            timestamp = this.check_timestamp(varargin{4});
          endif
          this.data{end+1} = uint8 ([bitor(0x90, chan), note, vel]);
          this.timestamp{end+1} = timestamp;
        case "noteoff"
          # channel,note,velocity,timestamp
          if nargin < 4
            error ('noteoff expects at least channel,note,velocity')
          endif
          chan = this.check_channel(varargin{1})-1;
          note = this.check_value127("note", varargin{2});
          vel = this.check_value127("velocity", varargin{3});
          timestamp = 0;
          if nargin > 4
            timestamp = this.check_timestamp(varargin{4});
          endif
          this.data{end+1} = uint8 ([bitor(0x80, chan), note, vel]);
          this.timestamp{end+1} = timestamp;
        case "note"
          # channel,note,velocity,duration, timestamp
          if nargin < 5
            error ('note expects at least channel,note,velocity,duration')
          endif
          chan = this.check_channel(varargin{1})-1;
          note = this.check_value127("note", varargin{2});
          vel = this.check_value127("velocity", varargin{3});
          dur = varargin{4};
          if !isscalar(dur) || !isnumeric(dur) || dur < 0
            error ('note expects at least duration to be a number >= 0')
          endif
          timestamp = 0;
          if nargin > 5
            timestamp = this.check_timestamp(varargin{5});
          endif
          this.data{end+1} = uint8 ([bitor(0x90, chan), note, vel]);
          this.timestamp{end+1} = timestamp;

          timestamp = timestamp + dur;
          this.data{end+1} = uint8 ([bitor(0x90, chan), note, 0]);
          this.timestamp{end+1} = timestamp;
        case "programchange"
          # channel,prog, timestamp
          if nargin < 3
            error ('programchange expects at least channel,program')
          endif
          chan = this.check_channel(varargin{1})-1;
          prog = this.check_value127("program", varargin{2});
          timestamp = 0;
          if nargin > 3
            timestamp = this.check_timestamp(varargin{3});
          endif
          this.data{end+1} = uint8([bitor(0xc0, chan), prog]);
          this.timestamp{end+1} = timestamp;

        case "controlchange"
          # channel,ccnum, ccval,timestamp
          if nargin < 4
            error ('controlchange expects at least channel,ccnum,ccval')
          endif
          chan = this.check_channel(varargin{1})-1;
          ccnum = this.check_value119("ccnum", varargin{2});
          ccval = this.check_value127("ccval", varargin{3});
          timestamp = 0;
          if nargin > 4
            timestamp = this.check_timestamp(varargin{4});
          endif
          this.data{end+1} = uint8([bitor(0xb0, chan), ccnum, ccval]);
          this.timestamp{end+1} = timestamp;

        case "polykeypressure"
          # channel, note, pressure, timestamp
          if nargin < 4
            error ('polykeypressure expects at least channel,note,keypressure')
          endif
          chan = this.check_channel(varargin{1})-1;
          note = this.check_value127("note", varargin{2});
          pres = this.check_value127("pressure", varargin{3});
          timestamp = 0;
          if nargin > 4
            timestamp = this.check_timestamp(varargin{4});
          endif
          this.data{end+1} = uint8([bitor(0xa0, chan), note pres]);
          this.timestamp{end+1} = timestamp;

        case "channelpressure"
          # channel, pressure, timestamp
          if nargin < 3
            error ('channelpressure expects at least channel,keypressure')
          endif
          chan = this.check_channel(varargin{1})-1;
          pres = this.check_value127("pressure", varargin{2});
          timestamp = 0;
          if nargin > 3
            timestamp = this.check_timestamp(varargin{3});
          endif
          this.data{end+1} = uint8([bitor(0xd0, chan), pres]);
          this.timestamp{end+1} = timestamp;

        case "localcontrol"
          # channel, localcontrol, timestamp
          if nargin < 3
            error ('localcontrol expects at least channel,localcontrol')
          endif
          chan = this.check_channel(varargin{1})-1;
          local = this.check_value1("localcontrol", varargin{2});
          timestamp = 0;
          if nargin > 3
            timestamp = this.check_timestamp(varargin{3});
          endif
          this.data{end+1} = uint8([bitor(0xb0, chan), 122, local]);
          this.timestamp{end+1} = timestamp;

        case "pitchbend"
          # channel, pitchchange, timestamp
          if nargin < 3
            error ('pitchbend expects at least channel,pitchchange')
          endif
          chan = this.check_channel(varargin{1})-1;
          # pitch is 0 .. 16383 where 8120 is no change
          # pitch = uint16(varargin{2} + 0x2000);
          pitch = uint16(this.check_value16383("pitchchange", varargin{2}));
          pitchlo = bitand(pitch, uint16(0x7F));
          pitchhi = bitand(bitshift(pitch, -7), uint16(0x7f));
          timestamp = 0;
          if nargin > 3
            timestamp = this.check_timestamp(varargin{3});
          endif
          this.data{end+1} = uint8([bitor(0xe0, chan), pitchlo pitchhi]);
          this.timestamp{end+1} = timestamp;

        case "polyon"
          # channel, timestamp
          if nargin < 2
            error ('polyon expects at least channel')
          endif
          chan = this.check_channel(varargin{1})-1;
          timestamp = 0;
          if nargin > 2
            timestamp = this.check_timestamp(varargin{2});
          endif
          this.data{end+1} = uint8([bitor(0xb0, chan), 127]);
          this.timestamp{end+1} = timestamp;

        case "monoon"
          # channel,  monochan, timestamp
          if nargin < 3
            error ('monoon expects at least channel and monochannels')
          endif
          chan = this.check_channel(varargin{1})-1;
          mono = this.check_value16("monochannels", varargin{2});
          timestamp = 0;
          if nargin > 3
            timestamp = this.check_timestamp(varargin{3});
          endif
          this.data{end+1} = uint8([bitor(0xb0, chan), 126, mono]);
          this.timestamp{end+1} = timestamp;

        case "omnion"
          # channel, timestamp
          if nargin < 2
            error ('omnion expects at least channel')
          endif
          chan = this.check_channel(varargin{1})-1;
          timestamp = 0;
          if nargin > 2
            timestamp = this.check_timestamp(varargin{2});
          endif
          this.data{end+1} = uint8([bitor(0xb0, chan), 125]);
          this.timestamp{end+1} = timestamp;

        case "omnioff"
          # channel, timestamp
          if nargin < 2
            error ('omnioff expects at least channel')
          endif
          chan = this.check_channel(varargin{1})-1;
          timestamp = 0;
          if nargin > 2
            timestamp = this.check_timestamp(varargin{2});
          endif
          this.data{end+1} = uint8([bitor(0xb0, chan), 124]);
          this.timestamp{end+1} = timestamp;

        case "allsoundoff"
          # channel, timestamp
          if nargin < 2
            error ('allsoundoff expects at least channel')
          endif
          chan = this.check_channel(varargin{1})-1;
          timestamp = 0;
          if nargin > 2
            timestamp = this.check_timestamp(varargin{2});
          endif
          this.data{end+1} = uint8([bitor(0xb0, chan), 120]);
          this.timestamp{end+1} = timestamp;

        case "allnotesoff"
          # channel, timestamp
          if nargin < 2
            error ('allnotesoff expects at least channel')
          endif
          chan = this.check_channel(varargin{1})-1;
          timestamp = 0;
          if nargin > 2
            timestamp = this.check_timestamp(varargin{2});
          endif
          this.data{end+1} = uint8([bitor(0xb0, chan), 123]);
          this.timestamp{end+1} = timestamp;

        case "resetallcontrollers"
          # channel, timestamp
          if nargin < 2
            error ('resetallcontrollers expects at least channel')
          endif
          chan = this.check_channel(varargin{1})-1;
          timestamp = 0;
          if nargin > 2
            timestamp = this.check_timestamp(varargin{2});
          endif
          this.data{end+1} = uint8([bitor(0xb0, chan), 121]);
          this.timestamp{end+1} = timestamp;

        case "songselect"
          if nargin < 2
            error ('songselect expects at least song number')
          endif
          timestamp = 0;
          song = this.check_value127("songnumber", varargin{1});
          if nargin > 2
            timestamp = this.check_timestamp(varargin{2});
          endif
          this.data{end+1} = uint8([0xF3 song]);
          this.timestamp{end+1} = timestamp;

        case "songpositionpointer"
          if nargin < 2
            error ('song expects at least song position')
          endif
          timestamp = 0;
          songpos = uint16(this.check_value16383("songposiition", varargin{1}));
          songlo = bitand(songpos, uint16(0x7F));
          songhi = bitand(bitshift(songpos, -7), uint16(0x7f));
          if nargin > 2
            timestamp = this.check_timestamp(varargin{2});
          endif
          this.data{end+1} = uint8([0xF2 songlo songhi]);
          this.timestamp{end+1} = timestamp;

        case "miditimecodequarterframe"
          # timeseq, timeval, timestamp
          if nargin < 3
            error ('miditimecodequarterframe expects at least timeseq and value')
          endif
          seq = this.check_value7("timeseq", varargin{1});
          val = this.check_value15("timeval", varargin{2});
          seq = bitand(uint8(seq), 3);
          val = bitand(uint8(val), 7);
          data = bitshift(seq, 3) + val;
          timestamp = 0;
          if nargin > 3
            timestamp = this.check_timestamp(varargin{3});
          endif
          this.data{end+1} = uint8([0xF1 data]);
          this.timestamp{end+1} = timestamp;

        case "start"
          timestamp = 0;
          if nargin > 1
            timestamp = this.check_timestamp(varargin{1});
          endif
          this.data{end+1} = uint8([0xFA]);
          this.timestamp{end+1} = timestamp;

        case "stop"
          timestamp = 0;
          if nargin > 1
            timestamp = this.check_timestamp(varargin{1});
          endif
          this.data{end+1} = uint8([0xFC]);
          this.timestamp{end+1} = timestamp;

        case "continue"
          timestamp = 0;
          if nargin > 1
            timestamp = this.check_timestamp(varargin{1});
          endif
          this.data{end+1} = uint8([0xFB]);
          this.timestamp{end+1} = timestamp;

        case "systemreset"
          timestamp = 0;
          if nargin > 1
            timestamp = this.check_timestamp(varargin{1});
          endif
          this.data{end+1} = uint8([0xFF]);
          this.timestamp{end+1} = timestamp;

        case "activesensing"
          timestamp = 0;
          if nargin > 1
            timestamp = this.check_timestamp(varargin{1});
          endif
          this.data{end+1} = uint8([0xFE]);
          this.timestamp{end+1} = timestamp;

        case "timingclock"
          timestamp = 0;
          if nargin > 1
            timestamp = this.check_timestamp(varargin{1});
          endif
          this.data{end+1} = uint8([0xF8]);
          this.timestamp{end+1} = timestamp;

        case "tunerequest"
          timestamp = 0;
          if nargin > 1
            timestamp = this.check_timestamp(varargin{1});
          endif
          this.data{end+1} = uint8([0xF6]);
          this.timestamp{end+1} = timestamp;

        case "data"
          # data, timestamp
          if nargin < 2
            error ('data type expects at least data')
          endif
 
          timestamp = 0;
          if nargin > 2
            timestamp = this.check_timestamp(varargin{2});
          endif
          this.data{end+1} = uint8(varargin{1});
          this.timestamp{end+1} = timestamp;

        case "eox"
          timestamp = 0;
          if nargin > 1
            timestamp = this.check_timestamp(varargin{1});
          endif
          this.data{end+1} = uint8([0xF7]);
          this.timestamp{end+1} = timestamp;

        case "systemexclusive"
          # timestamp
          # or data + timestamp
          if nargin == 1
            timestamp = 0;
            data = [];
          elseif nargin == 2
            if !isscalar(varargin{1}) && isvector(varargin{1})
              data = uint8(varargin{1});
              timestamp = 0;
            else
              data = [];
              timestamp = this.check_timestamp(varargin{1});
            endif
          elseif nargin == 3
            data = uint8(varargin{1});
            timestamp = this.check_timestamp(varargin{2});
          else
            error ("systemexclusive expects optional data and timestamp only");
          endif

          this.data{end+1} = uint8([0xF0]);
          this.timestamp{end+1} = timestamp;

          # want to build a full SOX data EOX
          if !isempty(data)
            this.data{end+1} = data;
            this.timestamp{end+1} = timestamp;

            this.data{end+1} = uint8([0xF7]);
            this.timestamp{end+1} = timestamp;
          endif

        case "metaevent"
          if nargin < 3
            error ('metaevent expects at least metatype and data')
          endif
          timestamp = 0;
          if nargin > 3
            timestamp = this.check_timestamp(varargin{3});
          endif
          # TODO: could be a integer or char type for metatype
          event  = this.check_value127("metatype", varargin{1});
          data  = uint8(varargin{2});  # TODO: check validity of the data <= 127
          datasize = midimsg.makevariable(length(data));
          this.data{end+1} = uint8([0xFF event datasize data]);
          this.timestamp{end+1} = timestamp;

        otherwise
          error ("Unknown midi type '%s", typev);
      endswitch
       
    endfunction

    function a = horzcat (a, varargin)
      
      if !isa(a, 'midimsg')
        error ("Cannot concatenate non midimsg elements");
      endif

      for i = 1:nargin-1
        b = varargin{i};
        if !isa(b, 'midimsg')
          error ("Cannot concatenate non midimsg elements");
        endif
        for idx=1:length (b.data)
          a.data{end+1} = b.data{idx};
          a.timestamp{end+1} = b.timestamp{idx};
        endfor
      endfor
    endfunction

    function e = isempty (this)
      e = isempty(this.data);
    endfunction

    function e = length (this)
      e = length(this.data);
    endfunction

    function this = subsasgn (this, s, rhs)
      if isempty(s)
        error ("midimsg.subsref missing index");
      endif

      switch (s(1).type)
        case "."
          if length(this.timestamp) > 1
            error ("Can not set %s on mutiple messages yet", s(1).subs);
          endif
          switch tolower(s(1).subs)
            case "timestamp"
              this.timestamp{1} = this.check_timestamp(rhs);

            case "channel"
              chan = this.check_channel(rhs);
              data = this.data{1};
              data(1) = bitor(bitand(data(1), 0xF0), (chan-1));
              this.data{1} = data;

            case "note"
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if !(cmd == 0x80 || cmd == 0x90 || cmd == 0xA0)
                error ("note property only valid for noteon/off and polykeypressure");
              endif
              data(2) = this.check_value127("note", rhs);
              this.data{1} = data;

            case "velocity"
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if !(cmd == 0x80 || cmd == 0x90)
                error ("velocity property only valid for noteon/off");
              endif
              data(3) = this.check_value127("velocity", rhs);
              this.data{1} = data;

            case "channelpressure"
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if !(cmd == 0xD0)
                error ("channel property only valid for channelpressure messages");
              endif
              data(2) = this.check_value127("channelpressure", rhs);
              this.data{1} = data;

            case "keypressure"
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if !(cmd == 0xA0)
                error ("keypressure property only valid for polykeypressure messages");
              endif
              data(3) = this.check_value127("keypressure", rhs);
              this.data{1} = data;

            case "ccnumber"
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if cmd != 0xB0 || data(2) > 119
                error ("ccnumber property only valid for controlchange messages");
              endif
              data(2) = this.check_value119("ccnumber", rhs);
              this.data{1} = data;

            case "ccvalue"
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if cmd != 0xB0 || data(2) > 119
                error ("ccnumber property only valid for controlchange messages");
              endif
              data(3) = this.check_value127("ccvalue", rhs);
              this.data{1} = data;

            case "song"
              data = this.data{1};
              cmd = data(1);
              if cmd != 0xF3
                error ("song property only valid for song select messages");
              endif
              data(2) = this.check_value127("song", rhs);
              this.data{1} = data;

            case "songposition"
              data = this.data{1};
              cmd = data(1);
              if cmd != 0xF2
                error ("songposition property only valid for songpositionpointer messages");
              endif

              songpos = uint16(this.check_value16383("songposition", rhs));
              songlo = bitand(songpos, uint16(0x7F));
              songhi = bitand(bitshift(songpos, -7), uint16(0x7f));

              data(2) = songlo;
              data(3) = songhi;

              this.data{1} = data;

            case "pitchchange"
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if cmd != 0xE0
                error ("pitchchange property only valid for pitchbend messages");
              endif

              pitchchange = uint16(this.check_value16383("pitchchange", rhs));
              pitchlo = bitand(pitchchange, uint16(0x7F));
              pitchhi = bitand(bitshift(pitchchange, -7), uint16(0x7f));

              data(2) = pitchlo;
              data(3) = pitchhi;

              this.data{1} = data;

            case "timecodesequence"
              data = this.data{1};
              cmd = data(1);
              if cmd != 0xF1
                error ("timecodesequence property only valid for miditimecodequaterframe messages");
              endif
  
              seq = bitshift(data(2), -3);
              val = bitand(data(2), 7);

              seq = this.check_value7("timecodesequence", rhs);

              data(2) = bitshift(seq, 3) + val;

              this.data{1} = data;

            case "timecodevalue"
              data = this.data{1};
              cmd = data(1);
              if cmd != 0xF1
                error ("value property only valid for miditimecodequaterframe messages");
              endif
  
              seq = bitshift(data(2), -3);
              val = bitand(data(2), 7);

              val = this.check_value15("timecodevalue", rhs);
              val = bitand(uint8(val), 7);

              data(2) = bitshift(seq, 3) + val;

              this.data{1} = data;

            otherwise
              error("unimplemented midimsg.subsasgn property '%s'", s(1).subs);
          endswitch

        case "()"
          idx = s(1).subs;
          if (numel (idx) != 1)
            error ("@midimsg/subsasgn: needs exactly one index");
          endif
          if numel (s) == 1
            # assign a value to here - so verify is a midimsg
            if !isa(rhs, "midimsg")
              error ("midimsg.subsasgn rhs of indexed value must be a midimsg");
            endif
            val = rhs;
          else
            # extract out midimsg value and do assign on it
            val = midimsg.createMessage(this.data{idx{1}}, this.timestamp{idx{1}});
            val = subsasgn (val, s(2:end), rhs);
          endif
          # store the modded data back in our object
          this.data{idx{1}} = val.data{1};
          this.timestamp{idx{1}} = val.timestamp{1};
 
        otherwise
          error("unimplemented midimsg.subsasgn type");
      endswitch
    endfunction

    function val = subsref (this, s)
      if isempty(s)
        error ("midimsg.subsref missing index");
      endif

      switch (s(1).type)
        case "()"
          idx = s(1).subs;
          if (numel (idx) != 1)
            error ("@midimsg/subsref: need exactly one index");
          endif
          off=[idx{1}];
          val = midimsg(0);
          for ix=off
            a = midimsg.createMessage(this.data{ix}, this.timestamp{ix});
            val = [val a];
          endfor
        case "."
          switch tolower(s(1).subs)
          case "timestamp"
            if length(this.timestamp) == 1
              val = this.timestamp{1};
            else
              val = cell2mat(this.timestamp);
            endif
          case "msgbytes"
            if length(this.data) == 1
              val = this.data{1};
            else
              val = this.data;
            endif
          case "nummsgbytes"
            if length(this.data) > 0
              val = length(this.data{1});

              if length(this.data) > 1
                for idx = 2:length(this.data)
                  val = [val length(this.data{idx})];
                endfor
              endif
            else
              val = 0;
            endif
          case "type"
            if length(this.data) > 0
              data = this.data{1};
              val = this.type_enum(this.data{1}); 
              if length(this.data) > 1
                # we cant override cellstr yes, so just use strings for multiples
                val = {char(val)}; 
                for idx = 2:length(this.data)
                  val{end+1} = this.type_str(this.data{idx});
                endfor
              endif
            else
              val = midimsgtype.Undefined;
            endif
          case "channel"
            if length(this.data) > 0
              data = this.data{1};
              val = bitand(data(1), 0x0F) + 1;
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  val = [val (bitand(data(1), 0x0F)+1)];
                endfor
              endif
              val = double(val);
            endif
          case "note"
            if length(this.data) > 0
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if cmd == 0x80 || cmd == 0x90 || cmd == 0xA0
                val = data(2);
              else
                error ("note property only valid for noteon/off and polykeypressure");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = bitand(data(1), 0xF0);
                  if cmd == 0x80 || cmd == 0x90 || cmd == 0xA0
                    val = [val data(2)];
                  else
                    error ("note property only valid for noteon/off and polykeypressure");
                  endif
                endfor
              endif
              val = double(val);
            endif
          case "velocity"
            if length(this.data) > 0
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if cmd == 0x80 || cmd == 0x90
                val = data(3);
              else
                error ("velocity property only valid for noteon/off");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = bitand(data(1), 0xF0);
                  if cmd == 0x80 || cmd == 0x90
                    val = [val data(3)];
                  else
                    error ("velocity property only valid for noteon/off");
                  endif
                endfor
              endif
              val = double(val);
            endif
          case "keypressure"
            if length(this.data) > 0
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if cmd == 0xA0
                val = data(3);
              else
                error ("keypressure property only valid for polykeypressure");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = bitand(data(1), 0xF0);
                  if cmd == 0xA0
                    val = [val data(3)];
                  else
                    error ("keypressure property only valid for polykeypressure");
                  endif
                endfor
              endif
              val = double(val);
            endif

          case "channelpressure"
            if length(this.data) > 0
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if cmd == 0xD0
                val = data(2);
              else
                error ("channelpressure property only valid for channelpressure");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = bitand(data(1), 0xF0);
                  if cmd == 0xD0
                    val = [val data(2)];
                  else
                    error ("keypressure property only valid for channelpressure");
                  endif
                endfor
              endif
              val = double(val);
            endif
 
          case "localcontrol"
            if length(this.data) > 0
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if cmd == 0xB0 && data(2) == 122
                val = data(3);
              else
                error ("localcontrol property only valid for localcontrol messages");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = bitand(data(1), 0xF0);
                  if cmd == 0xB0 && data(2) == 122
                    val = [val data(3)];
                  else
                    error ("localcontrol property only valid for localcontrol messages");
                  endif
                endfor
              endif
              val = double(val);
            endif
          case "monochannels"
            if length(this.data) > 0
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if cmd == 0xB0 && data(2) == 126
                val = data(3);
              else
                error ("monochannels property only valid for monoon messages");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = bitand(data(1), 0xF0);
                  if cmd == 0xB0 && data(2) == 126
                    val = [val data(3)];
                  else
                    error ("monochannels property only valid for monoon messages");
                  endif
                endfor
              endif
              val = double(val);
            endif
          case "program"
            if length(this.data) > 0
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if cmd == 0xC0
                val = data(2);
              else
                error ("program property only valid for programchange messages");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = bitand(data(1), 0xF0);
                  if cmd == 0xC0
                    val = [val data(2)];
                  else
                    error ("program property only valid for programchange messages");
                  endif
                endfor
              endif
              val = double(val);
            endif
          case "ccnumber"
            if length(this.data) > 0
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if cmd == 0xB0 && data(2) <= 119
                val = data(2);
              else
                error ("ccnumber property only valid for controlchange messages");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = bitand(data(1), 0xF0);
                  if cmd == 0xB0 && data(2) <= 119
                    val = [val data(2)];
                  else
                    error ("ccnumber property only valid for controlchange messages");
                  endif
                endfor
              endif
              val = double(val);
            endif
          case "ccvalue"
            if length(this.data) > 0
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if cmd == 0xB0 && data(2) <= 119
                val = data(3);
              else
                error ("ccvalue property only valid for controlchange messages");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = bitand(data(1), 0xF0);
                  if cmd == 0xB0 && data(2) <= 119
                    val = [val data(3)];
                  else
                    error ("ccvalue property only valid for controlchange messages");
                  endif
                endfor
              endif
              val = double(val);
            endif

          case "song"
            if length(this.data) > 0
              data = this.data{1};
              cmd = data(1);
              if cmd == 0xF3
                val = data(2);
              else
                error ("song property only valid for songselect messages");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = data(1);
                  if cmd == 0xF3
                    val = [val data(2)];
                  else
                    error ("song property only valid for songselect messages");
                  endif
                endfor
              endif
              val = double(val);
            endif

          case "songposition"
          
            if length(this.data) > 0
              data = this.data{1};
              cmd = data(1);
              if cmd == 0xF2
                val = bitshift(int16(data(3)), 7) + int16(data(2));
              else
                error ("songpostion property only valid for songpositionpointer messages");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = data(1);
                  if cmd == 0xF2
                    v = bitshift(int16(data(3)), 7) + int16(data(2));
                    val = [val v];
                  else
                    error ("songposition property only valid for songpositionpointer messages");
                  endif
                endfor
              endif
              val = double(val);
            endif

          case "pitchchange"
          
            if length(this.data) > 0
              data = this.data{1};
              cmd = bitand(data(1), 0xF0);
              if cmd == 0xE0
                val = bitshift(int16(data(3)), 7) + int16(data(2));
              else
                error ("pitchchange property only valid for pitchbend messages");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = bitand(data(1), 0xF0);
                  if cmd == 0xF2
                    v = bitshift(int16(data(3)), 7) + int16(data(2));
                    val = [val v];
                  else
                    error ("picthchange property only valid for pitchbend messages");
                  endif
                endfor
              endif
              val = double(val);
            endif

          case "timecodesequence"
          
            if length(this.data) > 0
              data = this.data{1};
              cmd = data(1);
              if cmd == 0xF1
                val = bitshift(data(2), -3);
              else
                error ("timecodesequence property only valid for miditimecodequarterframe messages");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = data(1);
                  if cmd == 0xF1
                    v = bitshift(data(2), -3);
                    val = [val v];
                  else
                    error ("timecodesequence property only valid for miditimecodequaterframe messages");
                  endif
                endfor
              endif
              val = double(val);
            endif

          case "timecodevalue"

            if length(this.data) > 0
              data = this.data{1};
              cmd = data(1);
              if cmd == 0xF1
                val = bitand(data(2), 7);
              else
                error ("timecodevalue property only valid for miditimecodequarterframe messages");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = data(1);
                  if cmd == 0xF1
                    v = bitand(data(2), 7);
                    val = [val v];
                  else
                    error ("timecodevalue property only valid for miditimecodequaterframe messages");
                  endif
                endfor
              endif
              val = double(val);
            endif

          case "metatype"

            if length(this.data) > 0
              data = this.data{1};
              cmd = data(1);
              if cmd == 0xFF && length(data) > 1
                val = data(2);
              else
                error ("metatype property only valid for metaevent messages");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = data(1);
                  if cmd == 0xFF &&  length(data) > 1
                    val = data(2);
                    val = [val v];
                  else
                    error ("metatype property only valid for metaevent messages");
                  endif
                endfor
              endif
              val = double(val);
            endif

          case "metadata"

            if length(this.data) > 0
              data = this.data{1};
              cmd = data(1);
              len = length(data);
              if cmd == 0xFF && len > 1
                if len < 128
                  val = data(4:len);
                elseif len < 128*128
                  val = data(5:len);
                elseif len < 128*128*128
                  val = data(6:len);
                else
                  val = data(7:len);
                endif
              else
                error ("metadata property only valid for metaevent messages");
              endif
              if length(this.data) > 1
                for idx = 2:length(this.data)
                  data = this.data{idx};
                  cmd = data(1);
                  len = length(data);
                  if cmd == 0xFF && len > 1
                    if len < 128
                      v = data(4:len);
                    elseif len < 128*128
                      v = data(5:len);
                    elseif len < 128*128*128
                      v = data(6:len);
                    else
                      v = data(7:len);
                    endif
                    val = [val v];
                  else
                    error ("metadata property only valid for metaevent messages");
                  endif
                endfor
              endif
            endif

          otherwise
            error("unimplemented midimsg.subsref property '%s'", s(1).subs);
          endswitch
        otherwise
          error("unimplemented midimsg.subsref type");
      endswitch

      if (numel (s) > 1)
        val = subsref (val, s(2:end));
      endif
    endfunction

    function msg = sort (this)
      msg = midimsg(0);

      # TODO: store timestamp as matrix not a cell
      # then could just use sort, and no swaps ?
      [~, s_idx] = sort(cell2mat(this.timestamp'));

      s_idx = s_idx';

      for idx = 1:numel(s_idx)
        msg.timestamp{end+1} = this.timestamp{s_idx(idx)};
        msg.data{end+1} = this.data{s_idx(idx)};
      endfor

    endfunction

    function out = disp (this)
      if nargout == 0
        disp(" MIDI message:");
      else
        out = "";
      endif
      for idx=1:length(this.data)
        data =  this.data{idx};
        cmd = data(1);
        types = this.type_str(data); 

        if bitand(cmd, 0xF0) != 0xF0 && bitand(cmd, 0x80) != 0
          chan = bitand(cmd, 0x0F) + 1;
          msgtext = sprintf ("%-10s Channel: %2d", types, chan);
        else
          msgtext = sprintf ("%-10s", types);
        endif

        if strcmp(types, "NoteOn") || strcmp(types, "NoteOff")
          msgtext = [msgtext sprintf(" Note: %3d Velocity: %3d", data(2), data(3))];
        endif
        if strcmp(types, "ProgramChange")
          msgtext = [msgtext sprintf(" Program: %3d", data(2))];
        endif
        if strcmp(types, "ControlChange")
          msgtext = [msgtext sprintf(" CCNumber: %3d CCValue: %3d", data(2), data(3))];
        endif
        if strcmp(types, "PitchBend")
          v = bitshift(int16(data(3)), 7) + int16(data(2));
          msgtext = [msgtext sprintf(" PitchChange: %d", v)];
        endif
        if strcmp(types, "ChannelPressure")
          msgtext = [msgtext sprintf(" ChannelPressure: %3d", data(2))];
        endif
        if strcmp(types, "PolyKeyPressure")
          msgtext = [msgtext sprintf(" Note: %3d KeyPressure: %3d", data(2), data(3))];
        endif
        if strcmp(types, "LocalControl")
          msgtext = [msgtext sprintf(" LocalControl: %3d", data(3))];
        endif
        if strcmp(types, "MonoOn")
          msgtext = [msgtext sprintf(" MonoChannels: %3d", data(3))];
        endif
        if strcmp(types, "SongSelect")
          msgtext = [msgtext sprintf(" Song: %3d", data(2))];
        endif
        if strcmp(types, "SongPositionPointer")
          v = bitshift(int16(data(3)), 7) + int16(data(2));
          msgtext = [msgtext sprintf(" SongPosition: %d", v)];
        endif
        if strcmp(types, "MIDITimeCodeQuarterFrame")
          seq = bitshift(data(2), -3);
          val = bitand(data(2), 7);
          msgtext = [msgtext sprintf(" TimeCodeSequence: %d TimeCodeValue: %d", seq, val)];
        endif
        if strcmp(types, "MetaEvent")
          metatype = data(2);
          msgtext = [msgtext sprintf(" MetaType: %d", metatype)];
        endif

        msgtext = [msgtext sprintf(" Timestamp: %f", this.timestamp{idx})];
        msgtext = [msgtext sprintf(" [")];
        msgtext = [msgtext sprintf(" 0x%02X", data)];
        msgtext = [msgtext sprintf(" ]")];
        if nargout == 0
          disp(["   "  msgtext]);
        else
          out = [out msgtext "\n"];
        endif
      endfor
    endfunction
    
    function d = uint8 (this)
      d = this.data;
    endfunction

  endmethods

  methods (Access = private)
    function t = check_timestamp(this, ts)
      if !isscalar (ts) || !isnumeric(ts) || ts < 0
        error ("expected timestamp to be a number >= 0");
      endif
      t = ts;
    endfunction

    function c = check_channel(this, chan)
      if !isscalar (chan) || !isnumeric(chan) || chan < 1 || chan > 16
        error ("expected channel to be a number between 1..16");
      endif
      c = chan;
    endfunction

    function v = check_value127(this, name, value)
      if !isscalar (value) || !isnumeric(value) || value < 0 || value > 127
        error ("expected %s to be a number between 0..127", name);
      endif
      v = value;
    endfunction

    function v = check_value1(this, name, value)
      if !isscalar (value) || !(islogical(value) || isnumeric(value)) || value < 0 || value > 1
        error ("expected %s to be a number between 0..1", name);
      endif
      v = value;
    endfunction

    function v = check_value7(this, name, value)
      if !isscalar (value) || !isnumeric(value) || value < 0 || value > 7
        error ("expected %s to be a number between 0..7", name);
      endif
      v = value;
    endfunction

    function v = check_value15(this, name, value)
      if !isscalar (value) || !isnumeric(value) || value < 0 || value > 7
        error ("expected %s to be a number between 0..15", name);
      endif
      v = value;
    endfunction

    function v = check_value16(this, name, value)
      if !isscalar (value) || !isnumeric(value) || value < 0 || value > 16
        error ("expected %s to be a number between 0..16", name);
      endif
      v = value;
    endfunction

    function v = check_value119(this, name, value)
      if !isscalar (value) || !isnumeric(value) || value < 0 || value > 119
        error ("expected %s to be a number between 0..119", name);
      endif
      v = value;
    endfunction

    function v = check_value16383(this, name, value)
      if !isscalar (value) || !isnumeric(value) || value < 0 || value > 16383
        error ("expected %s to be a number between 0..16386", name);
      endif
      v = value;
    endfunction

    function v = type_enum (this, data)
      cmd = 0;
      b1 = 0;
      if length(data) > 0
        cmd = data(1);
      endif
      if length(data) > 1
        b1 = data(2);
      endif

      cmdgrp = bitand(cmd, 0xF0);
      switch (cmdgrp)
       case 0x80
         v = midimsgtype.NoteOff;             
       case 0x90
         v = midimsgtype.NoteOn;
       case 0xA0
         v = midimsgtype.PolyKeyPressure;
       case 0xB0
         # depends on next byte for actual msg
         if b1 == 120
           v = midimsgtype.AllSoundOff;
         elseif b1 == 121
           v = midimsgtype.ResetAllControllers;
         elseif b1 == 122
           v = midimsgtype.LocalControl;
         elseif b1 == 123
           v = midimsgtype.AllNotesOff;
         elseif b1 == 124
           v = midimsgtype.OmniOff;
         elseif b1 == 125
           v = midimsgtype.OmniOn;
         elseif b1 == 126
           v = midimsgtype.MonoOn;
         elseif b1 == 127
           v = midimsgtype.PolyOn;
         else
           v = midimsgtype.ControlChange;
         endif
       case 0xC0
         v = midimsgtype.ProgramChange;             
       case 0xD0
         v = midimsgtype.ChannelPressure;             
       case 0xE0
         v = midimsgtype.PitchBend;             
       case 0xF0
         if cmd == 0xF0
           v = midimsgtype.SystemExclusive;
         elseif cmd == 0xF1
           v = midimsgtype.MIDITimeCodeQuarterFrame;
         elseif cmd == 0xF2
           v = midimsgtype.SongPositionPointer;
         elseif cmd == 0xF3
           v = midimsgtype.SongSelect;
         elseif cmd == 0xF4
           v = midimsgtype.Reserved;
         elseif cmd == 0xF5
           v = midimsgtype.Reserved;
         elseif cmd == 0xF6
           v = midimsgtype.TuneRequest;
         elseif cmd == 0xF7
           v = midimsgtype.EOX;
         elseif cmd == 0xF8
           v = midimsgtype.TimingClock;
         elseif cmd == 0xF9
           v = midimsgtype.Reserved;
         elseif cmd == 0xFA
           v = midimsgtype.Start;
         elseif cmd == 0xFB
           v = midimsgtype.Continue;
         elseif cmd == 0xFC
           v = midimsgtype.Stop;
         elseif cmd == 0xFD
           v = midimsgtype.Reserved;
         elseif cmd == 0xFE
           v = midimsgtype.ActiveSensing;
         elseif cmd == 0xFF
           v = midimsgtype.SystemReset;
           if length(data) > 1
             v = midimsgtype.MetaEvent;
           endif
         endif
         # depend  on other bytes ?
       otherwise
         if length(data) < 1
           v = midimsgtype.Undefined;
         else
           v = midimsgtype.Data;
         endif
      endswitch
      
    endfunction

    function v = type_str (this, data)
      v = char(this.type_enum(data));
    endfunction

  endmethods

  methods (Static=true)
    function msg = createMessage (data, ts)
      if nargin < 1 || !isa (data, 'uint8') || !ismatrix (data)
        error ("Expected matrix of uint8 data")
      endif
      if nargin < 2
        ts = 0;
      elif !isnumeric(ts)
        error ("Expected Timestamp as a number")
      endif
      msg = midimsg(0);
      msg.data{end+1} = data;
      msg.timestamp{end+1} = ts;
    endfunction

    function msg = fromStruct (msgstruct)
      # expects 'RawBytes' and 'Timestamp' in a struct
      if !isstruct (msgstruct) || !isfield (msgstruct, "RawBytes") || !isfield (msgstruct, "Timestamp")
        error ("Expected struct with RawBytes and Timestamp field");
      endif

      msg = midimsg.createMessage(uint8(msgstruct.RawBytes), msgstruct.Timestamp);
    endfunction
  endmethods

  methods (Static = true)
    # octave 5 wont allow us to call private funcs from the class
    # so a copy of private/makevariable is here
    function data = makevariable (value)
      t = uint64(value);
      if t < 128
        v = uint8(t);
        data = [v];
      else
        tmp = dec2bin(t);
        while mod(length(tmp), 7) != 0
          tmp = [ "0" tmp ];
        endwhile
        data = [];
        for i=1:7:length(tmp)
          v = tmp(i:i+6);
          v = uint8(bin2dec(v));
          if i < length(tmp) -7
            v = 128 + v;
          endif

          data = [data v];
        endfor
      endif
    endfunction

  endmethods

endclassdef

%!fail midimsg('badtype')

%!fail midimsg

%!test
%! a = midimsg(0);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 0);
%! assert(isempty(a));

%!fail midimsg("note", 0, 60, 127, 2)
%!fail midimsg("note", 17, 60, 127, 2)
%!fail midimsg("note", 1, 128, 127, 2)
%!fail midimsg("note", 1, -1, 127, 2)
%!fail midimsg("note", 1, 60, 128, 2)
%!fail midimsg("note", 1, 60, -1, 2)
%!fail midimsg("note", 1, 60, 127, -1)
%!fail midimsg("note", 1, 60, 127, 2, -1)

%!test
%! a = midimsg("note", 1, 60, 127, 2);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 2);
%! assert(!isempty(a));
%! assert(a.channel, [1 1]);
%! a = midimsg("note", 2, 60, 127, 1.2);
%! t = a.type;
%! assert(length(t) == 2);
%! assert(strcmp(t{1}, "NoteOn"))
%! assert(strcmp(t{2}, "NoteOn"))
%! assert(a.timestamp, [0.0 1.2]);
%! assert(a.channel, [2 2]);

%!test
%! a = midimsg("noteon", 1, 60, 20);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "NoteOn");
%! assert(a.channel, 1);
%! assert(a.msgbytes, uint8([0x90 0x3C 0x14]));
%! assert(!isempty(a));
%! a = midimsg("noteon", 2, 60, 20);
%! assert(a.channel, 2);
%! assert(a.msgbytes, uint8([0x91 0x3C 0x14]));

%!test
%! % using midimsgtype enum
%! a = midimsg(midimsgtype.NoteOn, 1, 60, 20);
%! assert(isa(a, "midimsg"));
%! assert(a.type == "NoteOn");

%!test
%! a = midimsg("noteoff", 1, 60, 20);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "NoteOff");
%! assert(a.nummsgbytes, 3);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0x80 0x3C 0x14]));

%!test
%! a = midimsg("programchange", 1, 60);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "ProgramChange");
%! assert(a.program, 60);
%! assert(a.channel, 1);
%! assert(a.nummsgbytes, 2);
%! assert(a.msgbytes, uint8([0xC0 60]));
%! assert(!isempty(a));

%!test
%! a = midimsg("controlchange", 1, 60, 65);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "ControlChange");
%! assert(a.nummsgbytes, 3);
%! assert(!isempty(a));
%! assert(a.ccnumber, 60)
%! assert(a.ccvalue, 65)
%! a.ccnumber = 0;
%! a.ccvalue = 4;
%! assert(a.ccnumber, 0)
%! assert(a.ccvalue, 4)

%!test
%! a = midimsg("polykeypressure", 1, 60, 65);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "PolyKeyPressure");
%! assert(a.nummsgbytes, 3);
%! assert(a.note, 60);
%! assert(a.keypressure, 65);
%! assert(!isempty(a));
%!
%! a.keypressure = 40;
%! assert(a.keypressure, 40);

%!test
%! a = midimsg("pitchbend", 1, 8192);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "PitchBend");
%! assert(a.nummsgbytes, 3);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0xE0 0x00 0x40]));
%! assert(a.pitchchange, 8192);
%! assert(a.channel, 1);
%! a.pitchchange = 8200;
%! assert(a.pitchchange, 8200);

%!test
%! a = midimsg("channelpressure", 1, 60);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "ChannelPressure");
%! assert(a.nummsgbytes, 2);
%! assert(!isempty(a));
%! assert(a.channelpressure, 60);
%!
%! a.channelpressure = 40;
%! assert(a.channelpressure, 40);

%!test
%! a = midimsg("localcontrol", 1, 1);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "LocalControl");
%! assert(a.nummsgbytes, 3);
%! assert(a.localcontrol, 1);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0xB0 122 0x01]));
%!
%! a = midimsg("localcontrol", 2, 0);
%! assert(a.msgbytes, uint8([0xB1 122 0x00]));
%! assert(a.localcontrol, 0);
%! assert(a.channel, 2);

%!test
%! a = midimsg("polyon", 1);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "PolyOn");
%! assert(a.nummsgbytes, 2);
%! assert(!isempty(a));

%!test
%! a = midimsg("monoon", 1, 0);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "MonoOn");
%! assert(a.monochannels, 0);
%! assert(a.nummsgbytes, 3);
%! assert(!isempty(a));

%!test
%! a = midimsg("omnion", 1);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "OmniOn");
%! assert(a.nummsgbytes, 2);
%! assert(!isempty(a));

%!test
%! a = midimsg("omnioff", 1);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "OmniOff");
%! assert(a.nummsgbytes, 2);
%! assert(!isempty(a));

%!test
%! a = midimsg("allsoundoff", 1);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "AllSoundOff");
%! assert(a.nummsgbytes, 2);
%! assert(!isempty(a));

%!test
%! a = midimsg("allnotesoff", 1);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "AllNotesOff");
%! assert(a.nummsgbytes, 2);
%! assert(!isempty(a));

%!test
%! a = midimsg("resetallcontrollers", 1);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "ResetAllControllers");
%! assert(a.nummsgbytes, 2);
%! assert(!isempty(a));

%!test
%! a = midimsg("systemreset");
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "SystemReset");
%! assert(a.nummsgbytes, 1);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0xFF]));

%!test
%! a = midimsg("start");
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "Start");
%! assert(a.nummsgbytes, 1);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0xFA]));

%!test
%! a = midimsg("stop");
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "Stop");
%! assert(a.nummsgbytes, 1);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0xFC]));

%!test
%! a = midimsg("continue");
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "Continue");
%! assert(a.nummsgbytes, 1);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0xFB]));

%!test
%! a = midimsg("activesensing");
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "ActiveSensing");
%! assert(a.nummsgbytes, 1);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0xFE]));

%!test
%! a = midimsg("timingclock");
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "TimingClock");
%! assert(a.nummsgbytes, 1);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0xF8]));

%!test
%! a = midimsg("eox");
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "EOX");
%! assert(a.nummsgbytes, 1);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0xF7]));

%!test
%! a = midimsg("data", [1 2 3]);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "Data");
%! assert(a.nummsgbytes, 3);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([1 2 3]));

%!test
%! a = midimsg("songselect", 1);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "SongSelect");
%! assert(a.nummsgbytes, 2);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0xF3 1]));
%! assert(a.song, 1);
%! a.song = 2;
%! assert(a.song, 2);
%! assert(a.msgbytes, uint8([0xF3 2]));

%!test
%! a = midimsg("songpositionpointer", 0);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "SongPositionPointer");
%! assert(a.nummsgbytes, 3);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0xF2 0 0]));
%! assert(a.songposition, 0);
%! a.songposition = 1000;
%! assert(a.songposition, 1000);

%!test
%! a = midimsg("tunerequest");
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "TuneRequest");
%! assert(a.nummsgbytes, 1);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0xF6]));

%!test
%! a = midimsg("miditimecodequarterframe", 1, 1);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "MIDITimeCodeQuarterFrame");
%! assert(a.nummsgbytes, 2);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0xF1 9]));
%! assert(a.timecodesequence, 1);
%! assert(a.timecodevalue, 1);
%! a.timecodesequence = 5;
%! assert(a.timecodesequence, 5);
%! assert(a.timecodevalue, 1);
%! a.timecodevalue = 2;
%! assert(a.timecodesequence, 5);
%! assert(a.timecodevalue, 2);

%!test
%! a = midimsg("systemexclusive");
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "SystemExclusive");
%! assert(a.nummsgbytes, 1);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0xF0]));
%!
%! a = midimsg("systemexclusive", 1.0);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "SystemExclusive");
%! assert(a.nummsgbytes, 1);
%! assert(!isempty(a));
%! assert(a.msgbytes, uint8([0xF0]));
%! assert(a.timestamp, 1.0);
%!
%! a = midimsg("systemexclusive", [1 2 3]);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 3);
%! assert(a.type, {"SystemExclusive", "Data", "EOX"});
%! assert(a.nummsgbytes, [1 3 1]);
%! assert(!isempty(a));
%! assert(a(1).msgbytes, uint8([0xF0]));
%! assert(a(1).timestamp, 0.0);
%! assert(a(2).msgbytes, uint8([1 2 3]));
%! assert(a(2).timestamp, 0.0);
%! assert(a(3).msgbytes, uint8([0xF7]));
%! assert(a(3).timestamp, 0.0);
%!
%! a = midimsg("systemexclusive", [1 2 3], 5);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 3);
%! assert(a.type, {"SystemExclusive", "Data", "EOX"});
%! assert(a.nummsgbytes, [1 3 1]);
%! assert(!isempty(a));
%! assert(a(1).msgbytes, uint8([0xF0]));
%! assert(a(1).timestamp, 5.0);
%! assert(a(2).msgbytes, uint8([1 2 3]));
%! assert(a(2).timestamp, 5.0);
%! assert(a(3).msgbytes, uint8([0xF7]));
%! assert(a(3).timestamp, 5.0);

%!test
%! a = midimsg("noteon", 1, 60, 20);
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "NoteOn");
%! assert(a.note, 60);
%! assert(a.velocity, 20);
%! b = midimsg("noteoff", 2, 60, 10, 5.0);
%! assert(isa(b, "midimsg"));
%! assert(length(b) == 1);
%! assert(b.type == "NoteOff");
%! assert(b.note, 60);
%! assert(b.velocity, 10);
%! c = [a b];
%! assert(isa(c, "midimsg"));
%! assert(length(c) == 2);
%! assert(c.nummsgbytes, [3 3]);
%! assert(c.channel, [1 2]);
%! assert(c.note, [60 60]);
%! assert(c.velocity, [20 10]);
%! assert(c(1).type == "NoteOn");
%! assert(c(1).channel, 1);
%! assert(c(1).note, 60);
%! assert(c(1).velocity, 20);
%! assert(c(2).type == "NoteOff");
%! assert(c(2).timestamp, 5.0);
%! assert(c(2).channel, 2);
%! assert(c(2).note, 60);
%! assert(c(2).velocity, 10);

%!test
%! a = midimsg("metaevent", 1, "hello");
%! assert(isa(a, "midimsg"));
%! assert(length(a) == 1);
%! assert(a.type == "MetaEvent");
%! assert(a.metatype, 1);
%! assert(a.metadata, uint8([0x68 0x65 0x6C 0x6C 0x6F]));
%! assert(a.msgbytes, uint8([0xFF 0x01 0x05 0x68 0x65 0x6C 0x6C 0x6F]))

%!test
%! # basic assign operations
%!
%! a = midimsg("noteon", 1, 60, 127, 0);
%! assert(length(a) == 1);
%! assert(a.type == "NoteOn");
%! assert(a.channel, 1);
%! assert(a.note, 60);
%! assert(a.velocity, 127);
%! assert(a.timestamp, 0);
%!
%! a.timestamp = 10;
%! a.channel = 2;
%! a.note = 61;
%! a.velocity = 100;
%! assert(a.timestamp, 10);
%! assert(a.channel, 2);
%! assert(a.note, 61);
%! assert(a.velocity, 100);
%!
%! fail ("a.channel = 0;");
%! fail ("a.note = -1;");
%! fail ("a.velocity = -1;");
%
%! a = midimsg("note", 1, 60, 127, 2);
%! assert(length(a) == 2);
%! assert(a(1).timestamp, 0);
%! assert(a(2).timestamp, 2);
%! assert(a(1).note, 60);
%! assert(a(2).note, 60);
%! assert(a(1).channel, 1);
%! assert(a(2).channel, 1);
%!
%! a(1).timestamp = 10;
%! a(2).timestamp = 20;
%! fail ("a(3).timestamp = 1;");
%! a(1).channel = 11;
%! a(2).channel = 12;
%! a(1).note = 71;
%!
%! assert(a(1).timestamp, 10);
%! assert(a(2).timestamp, 20);
%! assert(a(1).channel, 11);
%! assert(a(2).channel, 12);
%! assert(a(1).note, 71);
%!
%! fail ("a(1) = 1;");
%! a(1) = midimsg("noteon", 1, 80, 100, 50);
%! assert(length(a) == 2);
%! assert(a(1).timestamp, 50);
%! assert(a(1).channel, 1);
%! assert(a(1).note, 80);
%! assert(a(1).velocity, 100);
%!
%! # 2nd index still same as was
%! assert(a(2).timestamp, 20);
%! assert(a(2).channel, 12);
%! assert(a(2).note, 60);

%!test
%! as = struct("RawBytes", [0x90 0x3C 0x14], "Timestamp", 10);
%! a = midimsg.fromStruct(as);
%! assert(length(a) == 1);
%! assert(a.type == "NoteOn");
%! assert(a.channel, 1);
%! assert(a.timestamp, 10);
%! assert(a.msgbytes, uint8([0x90 0x3C 0x14]));

%!test
%! a1 = midimsg("noteon", 1, 80, 100, 5);
%! a2 = midimsg("noteon", 1, 81, 100, 1);
%! a3 = midimsg("noteon", 1, 82, 100, 10);
%! msgs = [a1 a2 a3];
%! ts = msgs.Timestamp;
%! [~, idx] = sort(ts);
%! smsgs = msgs(idx);
%! assert(smsgs(1).Timestamp, 1)
%! assert(smsgs(2).Timestamp, 5)
%! assert(smsgs(3).Timestamp, 10)
%! assert(smsgs(1).Note, 81)
%! assert(smsgs(2).Note, 80)
%! assert(smsgs(3).Note, 82)