File: SVFileReader.cpp

package info (click to toggle)
sonic-visualiser 5.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,744 kB
  • sloc: cpp: 158,888; ansic: 11,920; sh: 1,785; makefile: 517; xml: 64; perl: 31
file content (1732 lines) | stat: -rw-r--r-- 53,506 bytes parent folder | download | duplicates (2)
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
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    This file copyright 2006 Chris Cannam and QMUL.
    
    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.  See the file
    COPYING included with this distribution for more information.
*/

#include "SVFileReader.h"

#include "layer/Layer.h"
#include "view/View.h"
#include "base/PlayParameters.h"
#include "base/PlayParameterRepository.h"
#include "base/Preferences.h"

#include "data/fileio/AudioFileReaderFactory.h"
#include "data/fileio/FileSource.h"

#include "data/fileio/FileFinder.h"

#include "data/model/ReadOnlyWaveFileModel.h"
#include "data/model/EditableDenseThreeDimensionalModel.h"
#include "data/model/SparseOneDimensionalModel.h"
#include "data/model/SparseTimeValueModel.h"
#include "data/model/NoteModel.h"
#include "data/model/RegionModel.h"
#include "data/model/TextModel.h"
#include "data/model/ImageModel.h"
#include "data/model/BoxModel.h"
#include "data/model/AlignmentModel.h"
#include "data/model/AggregateWaveModel.h"

#include "transform/TransformFactory.h"

#include "view/Pane.h"

#include "widgets/ProgressDialog.h"

#include "Document.h"

#include <QString>
#include <QMessageBox>
#include <QFileDialog>

#include <QXmlStreamReader>

#include <iostream>

namespace sv {

SVFileReader::SVFileReader(Document *document,
                           SVFileReaderPaneCallback &callback,
                           QString location) :
    m_document(document),
    m_paneCallback(callback),
    m_location(location),
    m_currentPane(nullptr),
    m_currentDataset(XmlExportable::NO_ID),
    m_currentLayer(nullptr),
    m_pendingDerivedModel(XmlExportable::NO_ID),
    m_currentTransformChannel(0),
    m_currentTransformIsNewStyle(true),
    m_datasetSeparator(" "),
    m_inRow(false),
    m_inLayer(false),
    m_inView(false),
    m_inData(false),
    m_inSelections(false),
    m_rowNumber(0),
    m_ok(false)
{
}

void
SVFileReader::parseXml(QString xmlData)
{
    QXmlStreamReader reader(xmlData);
    parseWith(reader);
}

void
SVFileReader::parseFile(QString filename)
{
    QFile file(filename);
    if (!file.open(QFile::ReadOnly)) {
        m_errorString =
            QString("ERROR: SV-XML: Unable to open file \"%1\" for reading")
            .arg(filename);
        return;
    }
    parseFile(&file);
}

void
SVFileReader::parseFile(QIODevice *file)
{
    QXmlStreamReader reader(file);
    parseWith(reader);
}

void
SVFileReader::parseWith(QXmlStreamReader &reader)
{
    bool ok = true;
    
    while (!reader.atEnd()) {

        auto token = reader.readNext();
        
        switch (token) {
        case QXmlStreamReader::Invalid:
            ok = false;
            break;

        case QXmlStreamReader::StartElement:
            ok = startElement(reader.name().toString(), reader.attributes());
            break;

        case QXmlStreamReader::Characters:
            ok = characters(reader.text().toString());
            break;

        case QXmlStreamReader::EndElement:
            ok = endElement(reader.name().toString());
            break;

        default:
            break;
        }

        if (!ok) break;
    }

    if (reader.hasError()) {
        ok = false;
    }

    if (!ok) {
        if (m_errorString == "") {
            QString detail = "Parse error";
            switch (reader.error()) {
            case QXmlStreamReader::NotWellFormedError:
                detail = "Ill-formed XML";
                break;
            case QXmlStreamReader::PrematureEndOfDocumentError:
                detail = "Premature end of document";
                break;
            case QXmlStreamReader::UnexpectedElementError:
                detail = "Unexpected element";
                break;
            default:
                break;
            }
            m_errorString = QString("ERROR: SV-XML: %1 at line %2, column %3")
                .arg(detail)
                .arg(reader.lineNumber())
                .arg(reader.columnNumber());
        }
    }
    
    m_ok = ok;
}    

bool
SVFileReader::isOK()
{
    return m_ok;
}
        
SVFileReader::~SVFileReader()
{
    if (!m_awaitingDatasets.empty()) {
        SVCERR << "WARNING: SV-XML: File ended with "
                  << m_awaitingDatasets.size() << " unfilled model dataset(s)"
                  << endl;
    }

    std::set<ModelId> unaddedModels;

    for (auto i: m_models) {
        if (m_addedModels.find(i.second) == m_addedModels.end()) {
            unaddedModels.insert(i.second);
        }
    }

    if (!unaddedModels.empty()) {
        SVCERR << "WARNING: SV-XML: File contained "
               << unaddedModels.size() << " unused models"
               << endl;
        for (auto m: unaddedModels) {
            ModelById::release(m);
        }
    }

    if (!m_paths.empty()) {
        SVCERR << "WARNING: SV-XML: File contained "
               << m_paths.size() << " unused paths"
               << endl;
        for (auto p: m_paths) {
            delete p.second;
        }
    }
}

bool
SVFileReader::startElement(const QString &localName,
                           const QXmlStreamAttributes &attributes)
{
    QString name = localName.toLower();

    bool ok = false;

    // Valid element names:
    //
    // sv
    // data
    // dataset
    // display
    // derivation
    // playparameters
    // layer
    // model
    // point
    // row
    // view
    // window
    // plugin
    // transform
    // selections
    // selection
    // measurement

    if (name == "sv") {

        // nothing needed
        ok = true;

    } else if (name == "data") {

        // nothing needed
        m_inData = true;
        ok = true;

    } else if (name == "display") {

        // nothing needed
        ok = true;

    } else if (name == "window") {

        ok = readWindow(attributes);

    } else if (name == "model") {

        ok = readModel(attributes);
    
    } else if (name == "dataset") {
        
        ok = readDatasetStart(attributes);

    } else if (name == "bin") {
        
        ok = addBinToDataset(attributes);
    
    } else if (name == "point") {
        
        ok = addPointToDataset(attributes);

    } else if (name == "row") {

        ok = addRowToDataset(attributes);

    } else if (name == "layer") {

        addUnaddedModels(); // all models must be specified before first layer
        ok = readLayer(attributes);

    } else if (name == "view") {

        m_inView = true;
        ok = readView(attributes);

    } else if (name == "derivation") {

        makeAggregateModels(); // must be done before derivations that use them
        ok = readDerivation(attributes);

    } else if (name == "playparameters") {
        
        ok = readPlayParameters(attributes);

    } else if (name == "plugin") {

        ok = readPlugin(attributes);

    } else if (name == "selections") {

        m_inSelections = true;
        ok = true;

    } else if (name == "selection") {

        ok = readSelection(attributes);

    } else if (name == "measurement") {

        ok = readMeasurement(attributes);

    } else if (name == "transform") {
        
        ok = readTransform(attributes);

    } else if (name == "parameter") {

        ok = readParameter(attributes);

    } else {
        SVCERR << "WARNING: SV-XML: Unexpected element \""
                  << name << "\"" << endl;
    }

    if (!ok) {
        SVCERR << "WARNING: SV-XML: Failed to completely process element \""
                  << name << "\"" << endl;
    }

    return true;
}

bool
SVFileReader::characters(const QString &text)
{
    bool ok = false;

    if (m_inRow) {
        ok = readRowData(text);
        if (!ok) {
            SVCERR << "WARNING: SV-XML: Failed to read row data content for row " << m_rowNumber << endl;
        }
    }

    return true;
}

bool
SVFileReader::endElement(const QString &localName)
{
    QString name = localName.toLower();

    if (name == "dataset") {

        if (m_currentDataset != XmlExportable::NO_ID) {
            
            bool foundInAwaiting = false;

            for (auto i: m_awaitingDatasets) {
                if (i.second == m_currentDataset) {
                    m_awaitingDatasets.erase(i.first);
                    foundInAwaiting = true;
                    break;
                }
            }

            if (!foundInAwaiting) {
                SVCERR << "WARNING: SV-XML: Dataset precedes model, or no model uses dataset" << endl;
            }
        }

        m_currentDataset = XmlExportable::NO_ID;

    } else if (name == "data") {

        addUnaddedModels();
        m_inData = false;

    } else if (name == "derivation") {

        if (m_currentDerivedModel.isNone()) {
            if (m_pendingDerivedModel == XmlExportable::NO_ID) {
                SVCERR << "WARNING: SV-XML: No valid output model id "
                       << "for derivation" << endl;
            } else if (haveModel(m_pendingDerivedModel)) {
                SVCERR << "WARNING: SV-XML: Derivation has existing model "
                       << m_pendingDerivedModel
                       << " as target, not regenerating" << endl;
            } else {
                QString message;
                m_currentDerivedModel = m_models[m_pendingDerivedModel] =
                    m_document->addDerivedModel
                    (m_currentTransform,
                     ModelTransformer::Input(m_currentTransformSource,
                                             m_currentTransformChannel),
                     message);
                if (m_currentDerivedModel.isNone()) {
                    emit modelRegenerationFailed(tr("(derived model in SV-XML)"),
                                                 m_currentTransform.getIdentifier(),
                                                 message);
                } else if (message != "") {
                    emit modelRegenerationWarning(tr("(derived model in SV-XML)"),
                                                  m_currentTransform.getIdentifier(),
                                                  message);
                }                    
            }
        } else {
            m_document->addAlreadyDerivedModel
                (m_currentTransform,
                 ModelTransformer::Input(m_currentTransformSource,
                                         m_currentTransformChannel),
                 m_currentDerivedModel);
        }

        m_addedModels.insert(m_currentDerivedModel);
        m_currentDerivedModel = {};
        m_pendingDerivedModel = XmlExportable::NO_ID;
        m_currentTransformSource = {};
        m_currentTransform = Transform();
        m_currentTransformChannel = -1;

    } else if (name == "row") {
        m_inRow = false;
    } else if (name == "layer") {
        m_inLayer = false;
    } else if (name == "view") {
        m_inView = false;
    } else if (name == "selections") {
        m_inSelections = false;
    } else if (name == "playparameters") {
        m_currentPlayParameters = {};
    }

    return true;
}

#define READ_MANDATORY(TYPE, NAME, CONVERSION)                      \
    TYPE NAME = attributes.value(#NAME).toString().trimmed().CONVERSION(&ok); \
    if (!ok) { \
        SVCERR << "WARNING: SV-XML: Missing or invalid mandatory " #TYPE " attribute \"" #NAME "\"" << endl; \
        return false; \
    }

bool
SVFileReader::readWindow(const QXmlStreamAttributes &)
{
    // The window element contains window dimensions, which we used to
    // read and size the window accordingly. This was a Bad Idea [tm]
    // and we now do nothing instead. See #1769 Loading window
    // dimensions from session file is a really bad idea
    return true;
}

void
SVFileReader::makeAggregateModels()
{
    std::map<ExportId, PendingAggregateRec> stillPending;
    
    for (auto p: m_pendingAggregates) {

        int id = p.first;
        const PendingAggregateRec &rec = p.second;
        bool skip = false;

        AggregateWaveModel::ChannelSpecList specs;
        for (ExportId componentId: rec.components) {
            bool found = false;
            if (m_models.find(componentId) != m_models.end()) {
                ModelId modelId = m_models[componentId];
                auto rs = ModelById::getAs<RangeSummarisableTimeValueModel>
                    (modelId);
                if (rs) {
                    specs.push_back(AggregateWaveModel::ModelChannelSpec
                                    (modelId, -1));
                    found = true;
                } else {
                    SVDEBUG << "SVFileReader::makeAggregateModels: "
                            << "Component model id " << componentId
                            << "in aggregate model id " << id
                            << "does not appear to be convertible to "
                            << "RangeSummarisableTimeValueModel"
                            << endl;
                }
            }
            if (!found) {
                SVDEBUG << "SVFileReader::makeAggregateModels: "
                        << "Unknown component model id "
                        << componentId << " in aggregate model id " << id
                        << ", hoping we won't be needing it just yet"
                        << endl;
                skip = true;
            }                
        }

        if (skip) {
            stillPending[id] = rec;
        } else {
            auto model = std::make_shared<AggregateWaveModel>(specs);
            model->setObjectName(rec.name);
            m_models[id] = ModelById::add(model);

            SVDEBUG << "SVFileReader::makeAggregateModels: created aggregate "
                    << "model id " << id << " with " << specs.size()
                    << " components" << endl;
        }
    }

    m_pendingAggregates = stillPending;
}

void
SVFileReader::addUnaddedModels()
{
    makeAggregateModels();

    for (auto i: m_models) {

        ModelId modelId = i.second;

        if (m_addedModels.find(modelId) != m_addedModels.end()) {
            // already added this one
            continue;
        }

        m_document->addNonDerivedModel(modelId);
        
        // make a note of all models that have been added to the
        // document, so they don't get released by our own destructor
        m_addedModels.insert(modelId);
    }
}

bool
SVFileReader::readModel(const QXmlStreamAttributes &attributes)
{
    bool ok = false;

    READ_MANDATORY(int, id, toInt);

    if (haveModel(id)) {
        SVCERR << "WARNING: SV-XML: Ignoring duplicate model id " << id
                  << endl;
        return false;
    }

    QString name = attributes.value("name").toString();

    SVDEBUG << "SVFileReader::readModel: model name \"" << name << "\"" << endl;

    READ_MANDATORY(double, sampleRate, toDouble);

    QString type = attributes.value("type").trimmed().toString();
    bool isMainModel = (attributes.value("mainModel").trimmed() == QString("true"));

    if (type == "wavefile") {
        
        WaveFileModel *model = nullptr;
        FileFinder *ff = FileFinder::getInstance();
        QString originalPath = attributes.value("file").toString();
        QString path = ff->find(FileFinder::AudioFile,
                                originalPath, m_location);

        SVDEBUG << "Wave file originalPath = " << originalPath << ", path = "
                  << path << endl;

        ProgressDialog dialog(tr("Opening audio file or URL..."), true, 2000);
        FileSource file(path, &dialog);
        file.waitForStatus();

        if (!file.isOK()) {
            SVCERR << "SVFileReader::readModel: Failed to retrieve file \"" << path << "\" for wave file model: " << file.getErrorString() << endl;
        } else if (!file.isAvailable()) {
            SVCERR << "SVFileReader::readModel: Failed to retrieve file \"" << path << "\" for wave file model: Source unavailable" << endl;
        } else {

            file.waitForData();

            sv_samplerate_t rate = sampleRate;

            if (Preferences::getInstance()->getFixedSampleRate() != 0) {
                rate = Preferences::getInstance()->getFixedSampleRate();
            } else if (rate == 0 &&
                       !isMainModel &&
                       Preferences::getInstance()->getResampleOnLoad()) {
                auto mm = ModelById::getAs<WaveFileModel>
                    (m_document->getMainModel());
                if (mm) rate = mm->getSampleRate();
            }

            model = new ReadOnlyWaveFileModel(file, rate);
            if (!model->isOK()) {
                delete model;
                model = nullptr;
            }
        }

        if (!model) {
            m_document->setIncomplete(true);
            return false;
        }

        model->setObjectName(name);

        ModelId modelId = ModelById::add(std::shared_ptr<Model>(model));
        m_models[id] = modelId;
        
        if (isMainModel) {
            m_document->setMainModel(modelId);
            m_addedModels.insert(modelId);
        }
        // Derived models will be added when their derivation
        // is found.

        return true;

    } else if (type == "aggregatewave") {

        QString components = attributes.value("components").toString();
        QStringList componentIdStrings = components.split(",");
        std::vector<int> componentIds;
        for (auto cidStr: componentIdStrings) {
            bool ok = false;
            int cid = cidStr.toInt(&ok);
            if (!ok) {
                SVCERR << "SVFileReader::readModel: Failed to convert component model id from part \"" << cidStr << "\" in \"" << components << "\"" << endl;
            } else {
                componentIds.push_back(cid);
            }
        }
        PendingAggregateRec rec { name, sampleRate, componentIds };
        m_pendingAggregates[id] = rec;

        // The aggregate model will be constructed from its pending
        // record in makeAggregateModels; it can't happen here because
        // the component models might not all have been observed yet
        // (an unfortunate accident of the way the file is written)

        return true;

    } else if (type == "dense") {
        
        READ_MANDATORY(int, dimensions, toInt);
                    
        // Currently the only dense model we support here is the dense
        // 3d model.  Dense time-value models are always file-backed
        // waveform data, at this point, and they come in as wavefile
        // models.
        
        if (dimensions == 3) {
            
            READ_MANDATORY(int, windowSize, toInt);
            READ_MANDATORY(int, yBinCount, toInt);
            
            auto model = std::make_shared<EditableDenseThreeDimensionalModel>
                (sampleRate, windowSize, yBinCount);

            model->setObjectName(name);
            m_models[id] = ModelById::add(model);
            
            float minimum = attributes.value("minimum").trimmed().toFloat(&ok);
            if (ok) model->setMinimumLevel(minimum);
            
            float maximum = attributes.value("maximum").trimmed().toFloat(&ok);
            if (ok) model->setMaximumLevel(maximum);

            int dataset = attributes.value("dataset").trimmed().toInt(&ok);
            if (ok) m_awaitingDatasets[dataset] = id;

            int startFrame = attributes.value("startFrame").trimmed().toInt(&ok);
            if (ok) model->setStartFrame(startFrame);

            return true;

        } else {

            SVCERR << "WARNING: SV-XML: Unexpected dense model dimension ("
                      << dimensions << ")" << endl;
        }
    } else if (type == "sparse") {

        READ_MANDATORY(int, dimensions, toInt);
                  
        if (dimensions == 1) {
            
            READ_MANDATORY(int, resolution, toInt);
            
            if (attributes.value("subtype") == QString("image")) {

                bool notifyOnAdd = (attributes.value("notifyOnAdd") == QString("true"));
                auto model = std::make_shared<ImageModel>
                    (sampleRate, resolution, notifyOnAdd);
                model->setObjectName(name);
                m_models[id] = ModelById::add(model);

            } else {

                auto model = std::make_shared<SparseOneDimensionalModel>
                    (sampleRate, resolution);
                model->setObjectName(name);
                m_models[id] = ModelById::add(model);
            }

            int dataset = attributes.value("dataset").trimmed().toInt(&ok);
            if (ok) m_awaitingDatasets[dataset] = id;

            return true;

        } else if (dimensions == 2 || dimensions == 3) {
            
            READ_MANDATORY(int, resolution, toInt);

            bool haveMinMax = true;
            float minimum = attributes.value("minimum").trimmed().toFloat(&ok);
            if (!ok) haveMinMax = false;
            float maximum = attributes.value("maximum").trimmed().toFloat(&ok);
            if (!ok) haveMinMax = false;

            float valueQuantization =
                attributes.value("valueQuantization").trimmed().toFloat(&ok);

            bool notifyOnAdd = (attributes.value("notifyOnAdd") == QString("true"));

            QString units = attributes.value("units").toString();

            if (dimensions == 2) {
                if (attributes.value("subtype") == QString("text")) {
                    auto model = std::make_shared<TextModel>
                        (sampleRate, resolution, notifyOnAdd);
                    model->setObjectName(name);
                    m_models[id] = ModelById::add(model);
                } else if (attributes.value("subtype") == QString("path")) {
                    // Paths are no longer actually models
                    Path *path = new Path(sampleRate, resolution);
                    m_paths[id] = path;
                } else if (attributes.value("subtype") == QString("box") ||
                           attributes.value("subtype") == QString("timefrequencybox")) {
                    auto model = std::make_shared<BoxModel>
                        (sampleRate, resolution, notifyOnAdd);
                    model->setScaleUnits(units);
                    model->setObjectName(name);
                    m_models[id] = ModelById::add(model);
                } else {
                    std::shared_ptr<SparseTimeValueModel> model;
                    if (haveMinMax) {
                        model = std::make_shared<SparseTimeValueModel>
                            (sampleRate, resolution, minimum, maximum,
                             notifyOnAdd);
                    } else {
                        model = std::make_shared<SparseTimeValueModel>
                            (sampleRate, resolution, notifyOnAdd);
                    }
                    model->setScaleUnits(units);
                    model->setObjectName(name);
                    m_models[id] = ModelById::add(model);
                }
            } else {
                if (attributes.value("subtype") == QString("region")) {
                    std::shared_ptr<RegionModel> model;
                    if (haveMinMax) {
                        model = std::make_shared<RegionModel>
                            (sampleRate, resolution, minimum, maximum,
                             notifyOnAdd);
                    } else {
                        model = std::make_shared<RegionModel>
                            (sampleRate, resolution, notifyOnAdd);
                    }
                    model->setValueQuantization(valueQuantization);
                    model->setScaleUnits(units);
                    model->setObjectName(name);
                    m_models[id] = ModelById::add(model);
                } else if (attributes.value("subtype") == QString("flexinote")) {
                    std::shared_ptr<NoteModel> model;
                    if (haveMinMax) {
                        model = std::make_shared<NoteModel>
                            (sampleRate, resolution, minimum, maximum,
                             notifyOnAdd,
                             NoteModel::FLEXI_NOTE);
                    } else {
                        model = std::make_shared<NoteModel>
                            (sampleRate, resolution, notifyOnAdd,
                             NoteModel::FLEXI_NOTE);
                    }
                    model->setValueQuantization(valueQuantization);
                    model->setScaleUnits(units);
                    model->setObjectName(name);
                    m_models[id] = ModelById::add(model);
                } else {
                    // note models written out by SV 1.3 and earlier
                    // have no subtype, so we can't test that
                    std::shared_ptr<NoteModel> model;
                    if (haveMinMax) {
                        model = std::make_shared<NoteModel>
                            (sampleRate, resolution, minimum, maximum, notifyOnAdd);
                    } else {
                        model = std::make_shared<NoteModel>
                            (sampleRate, resolution, notifyOnAdd);
                    }
                    model->setValueQuantization(valueQuantization);
                    model->setScaleUnits(units);
                    model->setObjectName(name);
                    m_models[id] = ModelById::add(model);
                }
            }

            int dataset = attributes.value("dataset").trimmed().toInt(&ok);
            if (ok) m_awaitingDatasets[dataset] = id;

            return true;

        } else {

            SVCERR << "WARNING: SV-XML: Unexpected sparse model dimension ("
                      << dimensions << ")" << endl;
        }

    } else if (type == "alignment") {

        READ_MANDATORY(int, reference, toInt);
        READ_MANDATORY(int, aligned, toInt);
        READ_MANDATORY(int, path, toInt);

        ModelId refModel, alignedModel;
        Path *pathPtr = nullptr;

        if (m_models.find(reference) != m_models.end()) {
            refModel = m_models[reference];
        } else {
            SVCERR << "WARNING: SV-XML: Unknown reference model id "
                      << reference << " in alignment model id " << id
                      << endl;
        }

        if (m_models.find(aligned) != m_models.end()) {
            alignedModel = m_models[aligned];
        } else {
            SVCERR << "WARNING: SV-XML: Unknown aligned model id "
                      << aligned << " in alignment model id " << id
                      << endl;
        }

        if (m_paths.find(path) != m_paths.end()) {
            pathPtr = m_paths[path];
        } else {
            SVCERR << "WARNING: SV-XML: Unknown path id "
                      << path << " in alignment model id " << id
                      << endl;
        }

        if (!refModel.isNone() && !alignedModel.isNone() && pathPtr) {
            auto model = std::make_shared<AlignmentModel>
                (refModel, alignedModel, ModelId());
            model->setPath(*pathPtr);
            model->setObjectName(name);
            m_models[id] = ModelById::add(model);
            if (auto am = ModelById::get(alignedModel)) {
                am->setAlignment(m_models[id]);
            }
            return true;
        }

        if (pathPtr) {
            delete pathPtr;
            m_paths.erase(path);
        }
        
    } else {

        SVCERR << "WARNING: SV-XML: Unexpected model type \""
               << type << "\" for model id " << id << endl;
    }

    return false;
}

bool
SVFileReader::readView(const QXmlStreamAttributes &attributes)
{
    QString type = attributes.value("type").toString();
    m_currentPane = nullptr;
    
    if (type != "pane") {
        SVCERR << "WARNING: SV-XML: Unexpected view type \""
                  << type << "\"" << endl;
        return false;
    }

    m_currentPane = m_paneCallback.addPane();

    SVDEBUG << "SVFileReader::addPane: pane is " << m_currentPane << endl;

    if (!m_currentPane) {
        SVCERR << "WARNING: SV-XML: Internal error: Failed to add pane!"
                  << endl;
        return false;
    }

    bool ok = false;

    View *view = m_currentPane;

    // The view properties first

    READ_MANDATORY(int, centre, toInt);
    READ_MANDATORY(int, zoom, toInt);
    READ_MANDATORY(int, followPan, toInt);
    READ_MANDATORY(int, followZoom, toInt);
    QString tracking = attributes.value("tracking").toString();

    ZoomLevel zoomLevel;
    int deepZoom = attributes.value("deepZoom").trimmed().toInt(&ok);
    if (ok && zoom == 1 && deepZoom > 1) {
        zoomLevel = { ZoomLevel::PixelsPerFrame, deepZoom };
    } else {
        zoomLevel = { ZoomLevel::FramesPerPixel, zoom };
    }

    // Specify the follow modes before we set the actual values
    view->setFollowGlobalPan(followPan);
    view->setFollowGlobalZoom(followZoom);
    view->setPlaybackFollow(tracking == "scroll" ? PlaybackScrollContinuous :
                            tracking == "page" ? PlaybackScrollPageWithCentre :
                            tracking == "daw" ? PlaybackScrollPage
                            : PlaybackIgnore);

    // Then set these values
    view->setCentreFrame(centre);
    view->setZoomLevel(zoomLevel);

    // And pane properties
    READ_MANDATORY(int, centreLineVisible, toInt);
    m_currentPane->setCentreLineVisible(centreLineVisible);

    int height = attributes.value("height").toInt(&ok);
    if (ok) {
        m_currentPane->resize(m_currentPane->width(), height);
    }

    return true;
}

bool
SVFileReader::readLayer(const QXmlStreamAttributes &attributes)
{
    QString type = attributes.value("type").toString();

    int id;
    bool ok = false;
    id = attributes.value("id").trimmed().toInt(&ok);

    if (!ok) {
        SVCERR << "WARNING: SV-XML: No layer id for layer of type \""
                  << type
                  << "\"" << endl;
        return false;
    }

    Layer *layer = nullptr;
    bool isNewLayer = false;

    // Layers are expected to be defined in layer elements in the data
    // section, and referred to in layer elements in the view
    // sections.  So if we're in the data section, we expect this
    // layer not to exist already; if we're in the view section, we
    // expect it to exist.

    if (m_inData) {

        if (m_layers.find(id) != m_layers.end()) {
            SVCERR << "WARNING: SV-XML: Ignoring duplicate layer id " << id
                      << " in data section" << endl;
            return false;
        }

        layer = m_layers[id] = m_document->createLayer
            (LayerFactory::getInstance()->getLayerTypeForName(type));

        if (layer) {
            m_layers[id] = layer;
            isNewLayer = true;
        }

    } else {

        if (!m_currentPane) {
            SVCERR << "WARNING: SV-XML: No current pane for layer " << id
                      << " in view section" << endl;
            return false;
        }

        if (m_layers.find(id) != m_layers.end()) {
            
            layer = m_layers[id];
        
        } else {
            SVCERR << "WARNING: SV-XML: Layer id " << id 
                      << " in view section has not been defined -- defining it here"
                      << endl;

            layer = m_document->createLayer
                (LayerFactory::getInstance()->getLayerTypeForName(type));

            if (layer) {
                m_layers[id] = layer;
                isNewLayer = true;
            }
        }
    }
            
    if (!layer) {
        SVCERR << "WARNING: SV-XML: Failed to add layer of type \""
                  << type
                  << "\"" << endl;
        return false;
    }

    if (isNewLayer) {

        QString name = attributes.value("name").toString();
        layer->setObjectName(name);

        QString presentationName = attributes.value("presentationName").toString();
        layer->setPresentationName(presentationName);

        int modelId;
        bool modelOk = false;
        modelId = attributes.value("model").trimmed().toInt(&modelOk);

        if (modelOk) {
            if (haveModel(modelId)) {
                m_document->setModel(layer, m_models[modelId]);
            } else {
                SVCERR << "WARNING: SV-XML: Unknown model id " << modelId
                       << " in layer definition" << endl;
                if (!layer->canExistWithoutModel()) {
                    // Don't add a layer with an unknown model id
                    // unless it explicitly supports this state
                    m_document->deleteLayer(layer);
                    m_layers[id] = layer = nullptr;
                    return false;
                }
            }
        }

        if (layer) {
            LayerAttributes layerAttrs;
            for (const auto &attr : attributes) {
                layerAttrs[attr.name().toString()] = attr.value().toString();
            }
            layer->setProperties(layerAttrs);
        }
    }

    if (!m_inData && m_currentPane && layer) {

        QString visible = attributes.value("visible").toString();
        bool dormant = (visible == "false");

        // We need to do this both before and after adding the layer
        // to the view -- we need it to be dormant if appropriate
        // before it's actually added to the view so that any property
        // box gets the right state when it's added, but the add layer
        // command sets dormant to false because it assumes it may be
        // restoring a previously dormant layer, so we need to set it
        // again afterwards too.  Hm
        layer->setLayerDormant(m_currentPane, dormant);

        m_document->addLayerToView(m_currentPane, layer);

        layer->setLayerDormant(m_currentPane, dormant);
    }

    m_currentLayer = layer;
    m_inLayer = (layer != nullptr);

    return true;
}

bool
SVFileReader::readDatasetStart(const QXmlStreamAttributes &attributes)
{
    bool ok = false;

    READ_MANDATORY(int, id, toInt);
    READ_MANDATORY(int, dimensions, toInt);
    
    if (m_awaitingDatasets.find(id) == m_awaitingDatasets.end()) {
        SVCERR << "WARNING: SV-XML: Unwanted dataset " << id << endl;
        return false;
    }
    
    int awaitingId = m_awaitingDatasets[id];

    ModelId modelId;
    Path *path = nullptr;
    
    if (haveModel(awaitingId)) {
        modelId = m_models[awaitingId];
    } else if (m_paths.find(awaitingId) != m_paths.end()) {
        path = m_paths[awaitingId];
    } else {
        SVCERR << "WARNING: SV-XML: Internal error: Unknown model or path "
               << modelId << " awaiting dataset " << id << endl;
        return false;
    }

    bool good = false;

    switch (dimensions) {
    case 1:
        good =
            (ModelById::isa<SparseOneDimensionalModel>(modelId) ||
             ModelById::isa<ImageModel>(modelId));
        break;

    case 2:
        good =
            (ModelById::isa<SparseTimeValueModel>(modelId) ||
             ModelById::isa<TextModel>(modelId) ||
             ModelById::isa<BoxModel>(modelId) ||
             path);
        break;

    case 3:
        if (ModelById::isa<EditableDenseThreeDimensionalModel>(modelId)) {
            good = true;
            m_datasetSeparator = attributes.value("separator").toString();
        } else {
            good =
                (ModelById::isa<NoteModel>(modelId) ||
                 ModelById::isa<RegionModel>(modelId));
        }
        break;
    }

    if (!good) {
        SVCERR << "WARNING: SV-XML: Model id " << modelId << " has wrong number of dimensions or inappropriate type for " << dimensions << "-D dataset " << id << endl;
        m_currentDataset = XmlExportable::NO_ID;
        return false;
    }

    m_currentDataset = awaitingId;
    return true;
}

bool
SVFileReader::addPointToDataset(const QXmlStreamAttributes &attributes)
{
    bool ok = false;

    READ_MANDATORY(int, frame, toInt);

    if (m_paths.find(m_currentDataset) != m_paths.end()) {
        Path *path = m_paths[m_currentDataset];
        int mapframe = attributes.value("mapframe").trimmed().toInt(&ok);
        path->add(PathPoint(frame, mapframe));
        return ok;
    }

    if (!haveModel(m_currentDataset)) {
        SVCERR << "WARNING: SV-XML: Point element found in non-point dataset"
               << endl;
        return false;
    }
        
    ModelId modelId = m_models[m_currentDataset];        

    if (auto sodm = ModelById::getAs<SparseOneDimensionalModel>(modelId)) {
        QString label = attributes.value("label").toString();
        sodm->add(Event(frame, label));
        return true;
    }

    if (auto stvm = ModelById::getAs<SparseTimeValueModel>(modelId)) {
        float value = attributes.value("value").trimmed().toFloat(&ok);
        QString label = attributes.value("label").toString();
        stvm->add(Event(frame, value, label));
        return ok;
    }
        
    if (auto nm = ModelById::getAs<NoteModel>(modelId)) {
        float value = attributes.value("value").trimmed().toFloat(&ok);
        int duration = attributes.value("duration").trimmed().toInt(&ok);
        QString label = attributes.value("label").toString();
        float level = attributes.value("level").trimmed().toFloat(&ok);
        if (!ok) { // level is optional
            level = 1.f;
            ok = true;
        }
        nm->add(Event(frame, value, duration, level, label));
        return ok;
    }

    if (auto rm = ModelById::getAs<RegionModel>(modelId)) {
        float value = attributes.value("value").trimmed().toFloat(&ok);
        int duration = attributes.value("duration").trimmed().toInt(&ok);
        QString label = attributes.value("label").toString();
        rm->add(Event(frame, value, duration, label));
        return ok;
    }

    if (auto tm = ModelById::getAs<TextModel>(modelId)) {
        float height = attributes.value("height").trimmed().toFloat(&ok);
        QString label = attributes.value("label").toString();
        tm->add(Event(frame, height, label));
        return ok;
    }

    if (auto bm = ModelById::getAs<BoxModel>(modelId)) {
        float value = attributes.value("value").trimmed().toFloat(&ok);
        if (!ok) {
            value = attributes.value("frequency").trimmed().toFloat(&ok);
            if (bm->getScaleUnits() == "") {
                bm->setScaleUnits("Hz");
            }
        }
        float extent = attributes.value("extent").trimmed().toFloat(&ok);
        int duration = attributes.value("duration").trimmed().toInt(&ok);
        QString label = attributes.value("label").toString();
        bm->add(Event(frame, value, duration, extent, label));
        return ok;
    }

    if (auto im = ModelById::getAs<ImageModel>(modelId)) {
        QString image = attributes.value("image").toString();
        QString label = attributes.value("label").toString();
        im->add(Event(frame).withURI(image).withLabel(label));
        return ok;
    }

    SVCERR << "WARNING: SV-XML: Point element found in non-point dataset"
           << endl;

    return false;
}

bool
SVFileReader::addBinToDataset(const QXmlStreamAttributes &attributes)
{
    if (!haveModel(m_currentDataset)) {
        SVCERR << "WARNING: SV-XML: Bin definition found in incompatible dataset"
               << endl;
        return false;
    }
        
    ModelId modelId = m_models[m_currentDataset];        

    if (auto dtdm = ModelById::getAs<EditableDenseThreeDimensionalModel>
        (modelId)) {

        bool ok = false;
        int n = attributes.value("number").trimmed().toInt(&ok);
        if (!ok) {
            SVCERR << "WARNING: SV-XML: Missing or invalid bin number"
                      << endl;
            return false;
        }

        QString name = attributes.value("name").toString();
        dtdm->setBinName(n, name);
        return true;
    }

    SVCERR << "WARNING: SV-XML: Bin definition found in incompatible dataset"
           << endl;

    return false;
}


bool
SVFileReader::addRowToDataset(const QXmlStreamAttributes &attributes)
{
    m_inRow = false;

    bool ok = false;
    m_rowNumber = attributes.value("n").trimmed().toInt(&ok);
    if (!ok) {
        SVCERR << "WARNING: SV-XML: Missing or invalid row number"
                  << endl;
        return false;
    }
    
    m_inRow = true;

//    SVCERR << "SV-XML: In row " << m_rowNumber << endl;
    
    return true;
}

bool
SVFileReader::readRowData(const QString &text)
{
    if (!haveModel(m_currentDataset)) {
        SVCERR << "WARNING: SV-XML: Row data found in non-row dataset" << endl;
        return false;
    }
        
    ModelId modelId = m_models[m_currentDataset];        
    bool warned = false;

    if (auto dtdm = ModelById::getAs<EditableDenseThreeDimensionalModel>
        (modelId)) {

        QStringList data = text.split(m_datasetSeparator);

        DenseThreeDimensionalModel::Column values;

        for (QStringList::iterator i = data.begin(); i != data.end(); ++i) {

            if (int(values.size()) == dtdm->getHeight()) {
                if (!warned) {
                    SVCERR << "WARNING: SV-XML: Too many y-bins in 3-D dataset row "
                              << m_rowNumber << endl;
                    warned = true;
                }
            }

            bool ok;
            float value = i->toFloat(&ok);
            if (!ok) {
                SVCERR << "WARNING: SV-XML: Bad floating-point value "
                          << i->toLocal8Bit().data()
                          << " in row data" << endl;
            } else {
                values.push_back(value);
            }
        }

        dtdm->setColumn(m_rowNumber, values);
        return true;
    }

    SVCERR << "WARNING: SV-XML: Row data found in non-row dataset" << endl;
    return false;
}

bool
SVFileReader::readDerivation(const QXmlStreamAttributes &attributes)
{
    int modelExportId = 0;
    bool modelOk = false;
    modelExportId = attributes.value("model").trimmed().toInt(&modelOk);

    if (!modelOk) {
        SVCERR << "WARNING: SV-XML: No model id specified for derivation" << endl;
        return false;
    }

    if (haveModel(modelExportId)) {
        m_currentDerivedModel = m_models[modelExportId];
    } else {
        // we'll regenerate the model when the derivation element ends
        m_currentDerivedModel = {};
    }
    
    m_pendingDerivedModel = modelExportId;
    
    int sourceId = 0;
    bool sourceOk = false;
    sourceId = attributes.value("source").trimmed().toInt(&sourceOk);

    if (sourceOk && haveModel(sourceId)) {
        m_currentTransformSource = m_models[sourceId];
    } else {
        SVDEBUG << "NOTE: SV-XML: Can't find a model with id " << sourceId
                << " for derivation source, falling back to main model" << endl;
        m_currentTransformSource = m_document->getMainModel();
    }

    m_currentTransform = Transform();

    bool ok = false;
    int channel = attributes.value("channel").trimmed().toInt(&ok);
    if (ok) m_currentTransformChannel = channel;
    else m_currentTransformChannel = -1;

    QString type = attributes.value("type").toString();

    if (type == "transform") {
        m_currentTransformIsNewStyle = true;
        return true;
    } else {
        m_currentTransformIsNewStyle = false;
        SVDEBUG << "NOTE: SV-XML: Reading old-style derivation element"
                  << endl;
    }

    QString transformId = attributes.value("transform").toString();

    m_currentTransform.setIdentifier(transformId);

    int stepSize = attributes.value("stepSize").trimmed().toInt(&ok);
    if (ok) m_currentTransform.setStepSize(stepSize);

    int blockSize = attributes.value("blockSize").trimmed().toInt(&ok);
    if (ok) m_currentTransform.setBlockSize(blockSize);

    int windowType = attributes.value("windowType").trimmed().toInt(&ok);
    if (ok) m_currentTransform.setWindowType(WindowType(windowType));

    auto currentTransformSourceModel = ModelById::get(m_currentTransformSource);
    if (!currentTransformSourceModel) return true;

    sv_samplerate_t sampleRate = currentTransformSourceModel->getSampleRate();

    QString startFrameStr = attributes.value("startFrame").toString();
    QString durationStr = attributes.value("duration").toString();

    int startFrame = 0;
    int duration = 0;

    if (startFrameStr != "") {
        startFrame = startFrameStr.trimmed().toInt(&ok);
        if (!ok) startFrame = 0;
    }
    if (durationStr != "") {
        duration = durationStr.trimmed().toInt(&ok);
        if (!ok) duration = 0;
    }

    m_currentTransform.setStartTime
        (RealTime::frame2RealTime(startFrame, sampleRate));

    m_currentTransform.setDuration
        (RealTime::frame2RealTime(duration, sampleRate));

    return true;
}

bool
SVFileReader::readPlayParameters(const QXmlStreamAttributes &attributes)
{
    m_currentPlayParameters = {};

    int modelExportId = 0;
    bool modelOk = false;
    modelExportId = attributes.value("model").trimmed().toInt(&modelOk);

    if (!modelOk) {
        SVCERR << "WARNING: SV-XML: No model id specified for play parameters" << endl;
        return false;
    }

    if (haveModel(modelExportId)) {

        bool ok = false;

        auto parameters = PlayParameterRepository::getInstance()->
            getPlayParameters(m_models[modelExportId].untyped);

        if (!parameters) {
            SVCERR << "WARNING: SV-XML: Play parameters for model "
                      << modelExportId
                      << " not found - has model been added to document?"
                      << endl;
            return false;
        }
        
        bool muted = (attributes.value("mute").trimmed() == QString("true"));
        parameters->setPlayMuted(muted);
        
        float pan = attributes.value("pan").toFloat(&ok);
        if (ok) parameters->setPlayPan(pan);
        
        float gain = attributes.value("gain").toFloat(&ok);
        if (ok) parameters->setPlayGain(gain);
        
        QString clipId = attributes.value("clipId").toString();
        if (clipId != "") parameters->setPlayClipId(clipId);
        
        m_currentPlayParameters = parameters;

//        SVCERR << "Current play parameters for model: " << m_models[modelExportId] << ": " << m_currentPlayParameters << endl;

    } else {

        SVCERR << "WARNING: SV-XML: Unknown model " << modelExportId
               << " for play parameters" << endl;
        return false;
    }

    return true;
}

bool
SVFileReader::readPlugin(const QXmlStreamAttributes &attributes)
{
    if (m_pendingDerivedModel != XmlExportable::NO_ID) {
        return readPluginForTransform(attributes);
    } else if (m_currentPlayParameters) {
        return readPluginForPlayback(attributes);
    } else {
        SVCERR << "WARNING: SV-XML: Plugin found outside derivation or play parameters" << endl;
        return false;
    }
}

bool
SVFileReader::readPluginForTransform(const QXmlStreamAttributes &attributes)
{
    if (m_currentTransformIsNewStyle) {
        // Not needed, we have the transform element instead
        return true;
    }

    QString configurationXml = "<plugin";

    for (int i = 0; i < attributes.length(); ++i) {
        configurationXml += QString(" %1=\"%2\"")
            .arg(attributes[i].name().toString())
            .arg(XmlExportable::encodeEntities(attributes[i].value().toString()));
    }

    configurationXml += "/>";

    TransformFactory::getInstance()->
        setParametersFromPluginConfigurationXml(m_currentTransform,
                                                configurationXml);
    return true;
}

bool
SVFileReader::readPluginForPlayback(const QXmlStreamAttributes &attributes)
{
    // Obsolete but supported for compatibility

    QString ident = attributes.value("identifier").toString();
    if (ident == "sample_player") {
        QString clipId = attributes.value("program").toString();
        if (clipId != "") m_currentPlayParameters->setPlayClipId(clipId);
    }

    return true;
}

bool
SVFileReader::readTransform(const QXmlStreamAttributes &attributes)
{
    if (m_pendingDerivedModel == XmlExportable::NO_ID) {
        SVCERR << "WARNING: SV-XML: Transform found outside derivation" << endl;
        return false;
    }

    m_currentTransform = Transform();

    Transform::Attributes ta;
    for (const auto &attr : attributes) {
        ta[attr.name().toString()] = attr.value().toString();
    }
    m_currentTransform.setFromAttributes(ta);
    return true;
}

bool
SVFileReader::readParameter(const QXmlStreamAttributes &attributes)
{
    if (m_pendingDerivedModel == XmlExportable::NO_ID) {
        SVCERR << "WARNING: SV-XML: Parameter found outside derivation" << endl;
        return false;
    }

    QString name = attributes.value("name").toString();
    if (name == "") {
        SVCERR << "WARNING: SV-XML: Ignoring nameless transform parameter"
                  << endl;
        return false;
    }

    float value = attributes.value("value").trimmed().toFloat();

    m_currentTransform.setParameter(name, value);
    return true;
}

bool
SVFileReader::readSelection(const QXmlStreamAttributes &attributes)
{
    bool ok;

    READ_MANDATORY(int, start, toInt);
    READ_MANDATORY(int, end, toInt);

    m_paneCallback.addSelection(start, end);

    return true;
}

bool
SVFileReader::readMeasurement(const QXmlStreamAttributes &attributes)
{
    SVDEBUG << "SVFileReader::readMeasurement: inLayer "
              << m_inLayer << ", layer " << m_currentLayer << endl;

    if (!m_inLayer) {
        SVCERR << "WARNING: SV-XML: Measurement found outside layer" << endl;
        return false;
    }

    LayerAttributes layerAttrs;
    for (const auto &attr : attributes) {
        layerAttrs[attr.name().toString()] = attr.value().toString();
    }

    m_currentLayer->addMeasurementRect(layerAttrs);
    return true;
}

SVFileReaderPaneCallback::~SVFileReaderPaneCallback()
{
}


class SVFileIdentifier
{
public:
    SVFileIdentifier() :
        m_inSv(false),
        m_inData(false),
        m_type(SVFileReader::UnknownFileType)
    { }
    ~SVFileIdentifier() { }

    void parseFile(QString filename) {
        QFile file(filename);
        if (file.open(QFile::ReadOnly)) {
            QXmlStreamReader reader(&file);
            parseWith(reader);
        }
    }

    SVFileReader::FileType getType() const { return m_type; }

    void parseWith(QXmlStreamReader &reader) {
        while (!reader.atEnd()) {
            switch (reader.readNext()) {
            case QXmlStreamReader::Invalid:
                m_type = SVFileReader::UnknownFileType;
                return;
            case QXmlStreamReader::StartElement:
                if (!startElement(reader.name().toString(),
                                  reader.attributes())) {
                    return;
                }
                break;
            case QXmlStreamReader::EndElement:
                if (!endElement(reader.name().toString())) {
                    return;
                }
                break;
            default:
                break;
            }
        }
    }
        
    bool startElement(const QString &localName,
                      const QXmlStreamAttributes &atts) {

        QString name = localName.toLower();

        // SV session files have an sv element containing a data
        // element containing a model element with mainModel="true".

        // If the sv element is present but the rest does not satisfy,
        // then it's (probably) an SV layer file.

        // Otherwise, it's of unknown type.

        if (name == "sv") {
            m_inSv = true;
            if (m_type == SVFileReader::UnknownFileType) {
                m_type = SVFileReader::SVLayerFile;
            }
            return true;
        } else if (name == "data") {
            if (!m_inSv) return true;
            m_inData = true;
        } else if (name == "model") {
            if (!m_inData) return true;
            if (atts.value("mainModel").trimmed() == QString("true")) {
                if (m_type == SVFileReader::SVLayerFile) {
                    m_type = SVFileReader::SVSessionFile;
                    return false; // done
                }
            }
        }
        return true;
    }

    bool endElement(const QString &localName) {
        
        QString name = localName.toLower();

        if (name == "sv") {
            if (m_inSv) {
                m_inSv = false;
                return false; // done
            }
        } else if (name == "data") {
            if (m_inData) {
                m_inData = false;
                return false; // also done, nothing after the first
                              // data element is of use here
            }
        }
        return true;
    }

private:
    bool m_inSv;
    bool m_inData;
    SVFileReader::FileType m_type;
};


SVFileReader::FileType
SVFileReader::identifyXmlFile(QString path)
{
    SVFileIdentifier identifier;
    identifier.parseFile(path);
    return identifier.getType();
}

    
    
} // end namespace sv