Chris@175
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@175
|
2
|
Chris@175
|
3 /*
|
Chris@175
|
4 Sonic Visualiser
|
Chris@175
|
5 An audio file viewer and annotation editor.
|
Chris@175
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@202
|
7 This file copyright 2006 QMUL.
|
Chris@175
|
8
|
Chris@175
|
9 This program is free software; you can redistribute it and/or
|
Chris@175
|
10 modify it under the terms of the GNU General Public License as
|
Chris@175
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@175
|
12 License, or (at your option) any later version. See the file
|
Chris@175
|
13 COPYING included with this distribution for more information.
|
Chris@175
|
14 */
|
Chris@175
|
15
|
Chris@175
|
16 #include "WritableWaveFileModel.h"
|
Chris@175
|
17
|
Chris@1122
|
18 #include "ReadOnlyWaveFileModel.h"
|
Chris@1122
|
19
|
Chris@175
|
20 #include "base/TempDirectory.h"
|
Chris@175
|
21 #include "base/Exceptions.h"
|
Chris@175
|
22
|
Chris@175
|
23 #include "fileio/WavFileWriter.h"
|
Chris@175
|
24 #include "fileio/WavFileReader.h"
|
Chris@175
|
25
|
Chris@175
|
26 #include <QDir>
|
Chris@314
|
27 #include <QTextStream>
|
Chris@175
|
28
|
Chris@175
|
29 #include <cassert>
|
Chris@175
|
30 #include <iostream>
|
Chris@723
|
31 #include <stdint.h>
|
Chris@175
|
32
|
Chris@1096
|
33 using namespace std;
|
Chris@1096
|
34
|
Chris@258
|
35 //#define DEBUG_WRITABLE_WAVE_FILE_MODEL 1
|
Chris@187
|
36
|
Chris@1040
|
37 WritableWaveFileModel::WritableWaveFileModel(sv_samplerate_t sampleRate,
|
Chris@929
|
38 int channels,
|
Chris@175
|
39 QString path) :
|
Chris@175
|
40 m_model(0),
|
Chris@175
|
41 m_writer(0),
|
Chris@175
|
42 m_reader(0),
|
Chris@175
|
43 m_sampleRate(sampleRate),
|
Chris@175
|
44 m_channels(channels),
|
Chris@188
|
45 m_frameCount(0),
|
Chris@300
|
46 m_startFrame(0),
|
Chris@188
|
47 m_completion(0)
|
Chris@175
|
48 {
|
Chris@175
|
49 if (path.isEmpty()) {
|
Chris@175
|
50 try {
|
Chris@175
|
51 QDir dir(TempDirectory::getInstance()->getPath());
|
Chris@175
|
52 path = dir.filePath(QString("written_%1.wav")
|
Chris@175
|
53 .arg((intptr_t)this));
|
Chris@175
|
54 } catch (DirectoryCreationFailed f) {
|
Chris@843
|
55 cerr << "WritableWaveFileModel: Failed to create temporary directory" << endl;
|
Chris@175
|
56 return;
|
Chris@175
|
57 }
|
Chris@175
|
58 }
|
Chris@175
|
59
|
Chris@684
|
60 // Write directly to the target file, so that we can do
|
Chris@684
|
61 // incremental writes and concurrent reads
|
Chris@684
|
62 m_writer = new WavFileWriter(path, sampleRate, channels,
|
Chris@684
|
63 WavFileWriter::WriteToTarget);
|
Chris@175
|
64 if (!m_writer->isOK()) {
|
Chris@843
|
65 cerr << "WritableWaveFileModel: Error in creating WAV file writer: " << m_writer->getError() << endl;
|
Chris@175
|
66 delete m_writer;
|
Chris@175
|
67 m_writer = 0;
|
Chris@175
|
68 return;
|
Chris@175
|
69 }
|
Chris@187
|
70
|
Chris@317
|
71 FileSource source(m_writer->getPath());
|
Chris@316
|
72
|
Chris@316
|
73 m_reader = new WavFileReader(source, true);
|
Chris@290
|
74 if (!m_reader->getError().isEmpty()) {
|
Chris@843
|
75 cerr << "WritableWaveFileModel: Error in creating wave file reader" << endl;
|
Chris@187
|
76 delete m_reader;
|
Chris@187
|
77 m_reader = 0;
|
Chris@187
|
78 return;
|
Chris@187
|
79 }
|
Chris@187
|
80
|
Chris@1122
|
81 m_model = new ReadOnlyWaveFileModel(source, m_reader);
|
Chris@187
|
82 if (!m_model->isOK()) {
|
Chris@843
|
83 cerr << "WritableWaveFileModel: Error in creating wave file model" << endl;
|
Chris@187
|
84 delete m_model;
|
Chris@187
|
85 m_model = 0;
|
Chris@187
|
86 delete m_reader;
|
Chris@187
|
87 m_reader = 0;
|
Chris@187
|
88 return;
|
Chris@187
|
89 }
|
Chris@300
|
90 m_model->setStartFrame(m_startFrame);
|
Chris@187
|
91
|
Chris@258
|
92 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
|
Chris@1038
|
93 connect(m_model, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
|
Chris@1038
|
94 this, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)));
|
Chris@175
|
95 }
|
Chris@175
|
96
|
Chris@175
|
97 WritableWaveFileModel::~WritableWaveFileModel()
|
Chris@175
|
98 {
|
Chris@175
|
99 delete m_model;
|
Chris@175
|
100 delete m_writer;
|
Chris@175
|
101 delete m_reader;
|
Chris@175
|
102 }
|
Chris@175
|
103
|
Chris@300
|
104 void
|
Chris@1038
|
105 WritableWaveFileModel::setStartFrame(sv_frame_t startFrame)
|
Chris@300
|
106 {
|
Chris@300
|
107 m_startFrame = startFrame;
|
Chris@300
|
108 if (m_model) m_model->setStartFrame(startFrame);
|
Chris@300
|
109 }
|
Chris@300
|
110
|
Chris@175
|
111 bool
|
Chris@1038
|
112 WritableWaveFileModel::addSamples(float **samples, sv_frame_t count)
|
Chris@175
|
113 {
|
Chris@175
|
114 if (!m_writer) return false;
|
Chris@175
|
115
|
Chris@258
|
116 #ifdef DEBUG_WRITABLE_WAVE_FILE_MODEL
|
Chris@690
|
117 // SVDEBUG << "WritableWaveFileModel::addSamples(" << count << ")" << endl;
|
Chris@258
|
118 #endif
|
Chris@258
|
119
|
Chris@175
|
120 if (!m_writer->writeSamples(samples, count)) {
|
Chris@843
|
121 cerr << "ERROR: WritableWaveFileModel::addSamples: writer failed: " << m_writer->getError() << endl;
|
Chris@175
|
122 return false;
|
Chris@175
|
123 }
|
Chris@175
|
124
|
Chris@175
|
125 m_frameCount += count;
|
Chris@175
|
126
|
Chris@187
|
127 static int updateCounter = 0;
|
Chris@175
|
128
|
Chris@187
|
129 if (m_reader && m_reader->getChannelCount() == 0) {
|
Chris@258
|
130 #ifdef DEBUG_WRITABLE_WAVE_FILE_MODEL
|
Chris@690
|
131 SVDEBUG << "WritableWaveFileModel::addSamples(" << count << "): calling updateFrameCount (initial)" << endl;
|
Chris@258
|
132 #endif
|
Chris@187
|
133 m_reader->updateFrameCount();
|
Chris@187
|
134 } else if (++updateCounter == 100) {
|
Chris@258
|
135 #ifdef DEBUG_WRITABLE_WAVE_FILE_MODEL
|
Chris@690
|
136 SVDEBUG << "WritableWaveFileModel::addSamples(" << count << "): calling updateFrameCount (periodic)" << endl;
|
Chris@258
|
137 #endif
|
Chris@175
|
138 if (m_reader) m_reader->updateFrameCount();
|
Chris@175
|
139 updateCounter = 0;
|
Chris@175
|
140 }
|
Chris@175
|
141
|
Chris@175
|
142 return true;
|
Chris@175
|
143 }
|
Chris@175
|
144
|
Chris@175
|
145 bool
|
Chris@175
|
146 WritableWaveFileModel::isOK() const
|
Chris@175
|
147 {
|
Chris@187
|
148 bool ok = (m_writer && m_writer->isOK());
|
Chris@690
|
149 // SVDEBUG << "WritableWaveFileModel::isOK(): ok = " << ok << endl;
|
Chris@175
|
150 return ok;
|
Chris@175
|
151 }
|
Chris@175
|
152
|
Chris@175
|
153 bool
|
Chris@175
|
154 WritableWaveFileModel::isReady(int *completion) const
|
Chris@175
|
155 {
|
Chris@188
|
156 if (completion) *completion = m_completion;
|
Chris@188
|
157 return (m_completion == 100);
|
Chris@188
|
158 }
|
Chris@188
|
159
|
Chris@188
|
160 void
|
Chris@188
|
161 WritableWaveFileModel::setCompletion(int completion)
|
Chris@188
|
162 {
|
Chris@188
|
163 m_completion = completion;
|
Chris@188
|
164 if (completion == 100) {
|
Chris@188
|
165 if (m_reader) m_reader->updateDone();
|
Chris@188
|
166 }
|
Chris@175
|
167 }
|
Chris@175
|
168
|
Chris@1038
|
169 sv_frame_t
|
Chris@175
|
170 WritableWaveFileModel::getFrameCount() const
|
Chris@175
|
171 {
|
Chris@690
|
172 // SVDEBUG << "WritableWaveFileModel::getFrameCount: count = " << m_frameCount << endl;
|
Chris@175
|
173 return m_frameCount;
|
Chris@175
|
174 }
|
Chris@175
|
175
|
Chris@1096
|
176 vector<float>
|
Chris@1096
|
177 WritableWaveFileModel::getData(int channel, sv_frame_t start, sv_frame_t count) const
|
Chris@175
|
178 {
|
Chris@1096
|
179 if (!m_model || m_model->getChannelCount() == 0) return {};
|
Chris@1096
|
180 return m_model->getData(channel, start, count);
|
Chris@175
|
181 }
|
Chris@175
|
182
|
Chris@1096
|
183 vector<vector<float>>
|
Chris@1086
|
184 WritableWaveFileModel::getMultiChannelData(int fromchannel, int tochannel,
|
Chris@1096
|
185 sv_frame_t start, sv_frame_t count) const
|
Chris@175
|
186 {
|
Chris@1096
|
187 if (!m_model || m_model->getChannelCount() == 0) return {};
|
Chris@1096
|
188 return m_model->getMultiChannelData(fromchannel, tochannel, start, count);
|
Chris@363
|
189 }
|
Chris@363
|
190
|
Chris@929
|
191 int
|
Chris@929
|
192 WritableWaveFileModel::getSummaryBlockSize(int desired) const
|
Chris@377
|
193 {
|
Chris@377
|
194 if (!m_model) return desired;
|
Chris@377
|
195 return m_model->getSummaryBlockSize(desired);
|
Chris@377
|
196 }
|
Chris@377
|
197
|
Chris@225
|
198 void
|
Chris@1038
|
199 WritableWaveFileModel::getSummaries(int channel, sv_frame_t start, sv_frame_t count,
|
Chris@300
|
200 RangeBlock &ranges,
|
Chris@929
|
201 int &blockSize) const
|
Chris@175
|
202 {
|
Chris@225
|
203 ranges.clear();
|
Chris@225
|
204 if (!m_model || m_model->getChannelCount() == 0) return;
|
Chris@300
|
205 m_model->getSummaries(channel, start, count, ranges, blockSize);
|
Chris@175
|
206 }
|
Chris@175
|
207
|
Chris@175
|
208 WritableWaveFileModel::Range
|
Chris@1038
|
209 WritableWaveFileModel::getSummary(int channel, sv_frame_t start, sv_frame_t count) const
|
Chris@175
|
210 {
|
Chris@187
|
211 if (!m_model || m_model->getChannelCount() == 0) return Range();
|
Chris@300
|
212 return m_model->getSummary(channel, start, count);
|
Chris@175
|
213 }
|
Chris@175
|
214
|
Chris@175
|
215 void
|
Chris@175
|
216 WritableWaveFileModel::toXml(QTextStream &out,
|
Chris@175
|
217 QString indent,
|
Chris@175
|
218 QString extraAttributes) const
|
Chris@175
|
219 {
|
Chris@1123
|
220 // The assumption here is that the underlying wave file has
|
Chris@1123
|
221 // already been saved somewhere (its location is available through
|
Chris@1123
|
222 // getLocation()) and that the code that uses this class is
|
Chris@1123
|
223 // dealing with the problem of making sure it remains available.
|
Chris@1123
|
224 // We just write this out as if it were a normal wave file.
|
Chris@187
|
225
|
Chris@188
|
226 Model::toXml
|
Chris@188
|
227 (out, indent,
|
Chris@1123
|
228 QString("type=\"wavefile\" file=\"%1\" subtype=\"writable\" %2")
|
Chris@603
|
229 .arg(encodeEntities(m_writer->getPath()))
|
Chris@1123
|
230 .arg(extraAttributes));
|
Chris@175
|
231 }
|
Chris@175
|
232
|