dev@1430
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
dev@1430
|
2
|
dev@1430
|
3 /*
|
dev@1430
|
4 Sonic Visualiser
|
dev@1430
|
5 An audio file viewer and annotation editor.
|
dev@1430
|
6 Centre for Digital Music, Queen Mary, University of London.
|
dev@1438
|
7 This file copyright 2017 Queen Mary, University of London.
|
dev@1430
|
8
|
dev@1430
|
9 This program is free software; you can redistribute it and/or
|
dev@1430
|
10 modify it under the terms of the GNU General Public License as
|
dev@1430
|
11 published by the Free Software Foundation; either version 2 of the
|
dev@1430
|
12 License, or (at your option) any later version. See the file
|
dev@1430
|
13 COPYING included with this distribution for more information.
|
dev@1430
|
14 */
|
dev@1430
|
15
|
dev@1438
|
16 #ifndef CSV_STREAM_WRITER_H
|
dev@1438
|
17 #define CSV_STREAM_WRITER_H
|
dev@1430
|
18
|
dev@1434
|
19 #include "base/BaseTypes.h"
|
dev@1434
|
20 #include "base/Selection.h"
|
dev@1434
|
21 #include "base/ProgressReporter.h"
|
dev@1434
|
22 #include "base/DataExportOptions.h"
|
dev@1434
|
23 #include "data/model/Model.h"
|
dev@1434
|
24 #include <QString>
|
dev@1434
|
25 #include <algorithm>
|
dev@1445
|
26 #include <numeric>
|
dev@1441
|
27
|
dev@1438
|
28 namespace CSVStreamWriter
|
dev@1430
|
29 {
|
dev@1430
|
30
|
dev@1445
|
31 template <class OutStream>
|
dev@1445
|
32 bool
|
dev@1445
|
33 writeInChunks(OutStream& oss,
|
dev@1445
|
34 const Model& model,
|
dev@1445
|
35 const MultiSelection& regions,
|
dev@1445
|
36 ProgressReporter* reporter = nullptr,
|
dev@1445
|
37 QString delimiter = ",",
|
dev@1445
|
38 DataExportOptions options = DataExportDefaults,
|
dev@1445
|
39 const sv_frame_t blockSize = 16384)
|
dev@1445
|
40 {
|
dev@1445
|
41 if (blockSize <= 0) return false;
|
dev@1445
|
42
|
dev@1445
|
43 // TODO, some form of checking validity of selections?
|
dev@1445
|
44 const auto nFramesToWrite = std::accumulate(
|
dev@1445
|
45 regions.getSelections().begin(),
|
dev@1445
|
46 regions.getSelections().end(),
|
dev@1445
|
47 0,
|
dev@1445
|
48 [](sv_frame_t acc, const Selection& current) -> sv_frame_t {
|
dev@1445
|
49 return acc + (current.getEndFrame() - current.getStartFrame());
|
dev@1445
|
50 }
|
dev@1445
|
51 );
|
dev@1445
|
52
|
dev@1445
|
53 const auto wasCancelled = [&reporter]() {
|
dev@1445
|
54 return reporter && reporter->wasCancelled();
|
dev@1445
|
55 };
|
dev@1445
|
56
|
dev@1445
|
57 sv_frame_t nFramesWritten = 0;
|
dev@1445
|
58 int previousProgress = 0;
|
dev@1445
|
59
|
dev@1445
|
60 for (const auto& extents : regions.getSelections()) {
|
dev@1445
|
61 const auto startFrame = extents.getStartFrame();
|
dev@1445
|
62 const auto endFrame = extents.getEndFrame();
|
dev@1445
|
63 auto readPtr = startFrame;
|
dev@1445
|
64 while (readPtr < endFrame) {
|
dev@1445
|
65 if (wasCancelled()) return false;
|
dev@1445
|
66
|
dev@1445
|
67 const auto start = readPtr;
|
dev@1445
|
68 const auto end = std::min(start + blockSize, endFrame);
|
dev@1445
|
69
|
dev@1445
|
70 oss << model.toDelimitedDataStringSubsetWithOptions(
|
dev@1445
|
71 delimiter,
|
dev@1445
|
72 options,
|
dev@1445
|
73 start,
|
dev@1445
|
74 end
|
dev@1445
|
75 ) << (end < endFrame ? "\n" : "");
|
dev@1445
|
76 nFramesWritten += end - start;
|
dev@1445
|
77 const auto currentProgress = 100 * nFramesWritten / nFramesToWrite;
|
dev@1445
|
78 const bool hasIncreased = currentProgress > previousProgress;
|
dev@1445
|
79 if (hasIncreased) {
|
dev@1445
|
80 if (reporter) reporter->setProgress(currentProgress);
|
dev@1445
|
81 previousProgress = currentProgress;
|
dev@1445
|
82 }
|
dev@1445
|
83 readPtr = end;
|
dev@1445
|
84 }
|
dev@1445
|
85 }
|
dev@1445
|
86 return !wasCancelled(); // setProgress could process event loop
|
dev@1445
|
87 }
|
dev@1445
|
88
|
dev@1445
|
89 template <class OutStream>
|
dev@1441
|
90 bool
|
dev@1441
|
91 writeInChunks(OutStream& oss,
|
dev@1441
|
92 const Model& model,
|
dev@1441
|
93 const Selection& extents,
|
dev@1441
|
94 ProgressReporter* reporter = nullptr,
|
dev@1441
|
95 QString delimiter = ",",
|
dev@1441
|
96 DataExportOptions options = DataExportDefaults,
|
dev@1445
|
97 const sv_frame_t blockSize = 16384)
|
dev@1434
|
98 {
|
dev@1441
|
99 const auto startFrame = extents.isEmpty() ?
|
dev@1434
|
100 model.getStartFrame() : extents.getStartFrame();
|
dev@1441
|
101 const auto endFrame = extents.isEmpty() ?
|
dev@1434
|
102 model.getEndFrame() : extents.getEndFrame();
|
dev@1441
|
103 const auto hasValidExtents = startFrame >= 0 && endFrame > startFrame;
|
dev@1441
|
104 if (!hasValidExtents) return false;
|
dev@1445
|
105 Selection all {
|
dev@1445
|
106 startFrame,
|
dev@1445
|
107 endFrame
|
dev@1434
|
108 };
|
dev@1445
|
109 MultiSelection regions;
|
dev@1445
|
110 regions.addSelection(all);
|
dev@1445
|
111 return CSVStreamWriter::writeInChunks(
|
dev@1445
|
112 oss,
|
dev@1445
|
113 model,
|
dev@1445
|
114 regions,
|
dev@1445
|
115 reporter,
|
dev@1445
|
116 delimiter,
|
dev@1445
|
117 options,
|
dev@1445
|
118 blockSize
|
dev@1445
|
119 );
|
dev@1434
|
120 }
|
dev@1434
|
121
|
dev@1434
|
122 template <class OutStream>
|
dev@1441
|
123 bool
|
dev@1441
|
124 writeInChunks(OutStream& oss,
|
dev@1441
|
125 const Model& model,
|
dev@1441
|
126 ProgressReporter* reporter = nullptr,
|
dev@1441
|
127 QString delimiter = ",",
|
dev@1441
|
128 DataExportOptions options = DataExportDefaults,
|
dev@1441
|
129 const sv_frame_t blockSize = 16384)
|
dev@1434
|
130 {
|
dev@1434
|
131 const Selection empty;
|
dev@1444
|
132 return CSVStreamWriter::writeInChunks(
|
dev@1434
|
133 oss,
|
dev@1434
|
134 model,
|
dev@1434
|
135 empty,
|
dev@1434
|
136 reporter,
|
dev@1434
|
137 delimiter,
|
dev@1434
|
138 options,
|
dev@1434
|
139 blockSize
|
dev@1434
|
140 );
|
dev@1434
|
141 }
|
dev@1438
|
142 } // namespace CSVStreamWriter
|
dev@1434
|
143 #endif |