annotate data/model/WritableWaveFileModel.cpp @ 1734:bffccc8de3c1 by-id

Warn for already-existing id
author Chris Cannam
date Mon, 24 Jun 2019 18:42:28 +0100
parents 601851995f4b
children 52705a328b34
rev   line source
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@1133 35 const int WritableWaveFileModel::PROPORTION_UNKNOWN = -1;
Chris@1133 36
Chris@258 37 //#define DEBUG_WRITABLE_WAVE_FILE_MODEL 1
Chris@187 38
Chris@1520 39 WritableWaveFileModel::WritableWaveFileModel(QString path,
Chris@1520 40 sv_samplerate_t sampleRate,
Chris@1429 41 int channels,
Chris@1520 42 Normalisation norm) :
Chris@1582 43 m_model(nullptr),
Chris@1582 44 m_temporaryWriter(nullptr),
Chris@1582 45 m_targetWriter(nullptr),
Chris@1582 46 m_reader(nullptr),
Chris@1520 47 m_normalisation(norm),
Chris@175 48 m_sampleRate(sampleRate),
Chris@175 49 m_channels(channels),
Chris@188 50 m_frameCount(0),
Chris@300 51 m_startFrame(0),
Chris@1133 52 m_proportion(PROPORTION_UNKNOWN)
Chris@175 53 {
Chris@1520 54 init(path);
Chris@1520 55 }
Chris@1520 56
Chris@1520 57 WritableWaveFileModel::WritableWaveFileModel(sv_samplerate_t sampleRate,
Chris@1520 58 int channels,
Chris@1520 59 Normalisation norm) :
Chris@1582 60 m_model(nullptr),
Chris@1582 61 m_temporaryWriter(nullptr),
Chris@1582 62 m_targetWriter(nullptr),
Chris@1582 63 m_reader(nullptr),
Chris@1520 64 m_normalisation(norm),
Chris@1520 65 m_sampleRate(sampleRate),
Chris@1520 66 m_channels(channels),
Chris@1520 67 m_frameCount(0),
Chris@1520 68 m_startFrame(0),
Chris@1520 69 m_proportion(PROPORTION_UNKNOWN)
Chris@1520 70 {
Chris@1520 71 init();
Chris@1520 72 }
Chris@1520 73
Chris@1520 74 WritableWaveFileModel::WritableWaveFileModel(sv_samplerate_t sampleRate,
Chris@1520 75 int channels) :
Chris@1582 76 m_model(nullptr),
Chris@1582 77 m_temporaryWriter(nullptr),
Chris@1582 78 m_targetWriter(nullptr),
Chris@1582 79 m_reader(nullptr),
Chris@1520 80 m_normalisation(Normalisation::None),
Chris@1520 81 m_sampleRate(sampleRate),
Chris@1520 82 m_channels(channels),
Chris@1520 83 m_frameCount(0),
Chris@1520 84 m_startFrame(0),
Chris@1520 85 m_proportion(PROPORTION_UNKNOWN)
Chris@1520 86 {
Chris@1520 87 init();
Chris@1520 88 }
Chris@1520 89
Chris@1520 90 void
Chris@1520 91 WritableWaveFileModel::init(QString path)
Chris@1520 92 {
Chris@175 93 if (path.isEmpty()) {
Chris@175 94 try {
Chris@1520 95 // Temp dir is exclusive to this run of the application,
Chris@1520 96 // so the filename only needs to be unique within that -
Chris@1520 97 // model ID should be ok
Chris@175 98 QDir dir(TempDirectory::getInstance()->getPath());
Chris@1731 99 path = dir.filePath(QString("written_%1.wav")
Chris@1731 100 .arg(getId().toString()));
Chris@1465 101 } catch (const DirectoryCreationFailed &f) {
Chris@1428 102 SVCERR << "WritableWaveFileModel: Failed to create temporary directory" << endl;
Chris@175 103 return;
Chris@175 104 }
Chris@175 105 }
Chris@175 106
Chris@1520 107 m_targetPath = path;
Chris@1520 108 m_temporaryPath = "";
Chris@1520 109
Chris@1520 110 // We don't delete or null-out writer/reader members after
Chris@1520 111 // failures here - they are all deleted in the dtor, and the
Chris@1520 112 // presence/existence of the model is what's used to determine
Chris@1520 113 // whether to go ahead, not the writer/readers. If the model is
Chris@1520 114 // non-null, then the necessary writer/readers must be OK, as the
Chris@1520 115 // model is the last thing initialised
Chris@1520 116
Chris@1520 117 m_targetWriter = new WavFileWriter(m_targetPath, m_sampleRate, m_channels,
Chris@1520 118 WavFileWriter::WriteToTarget);
Chris@1520 119
Chris@1520 120 if (!m_targetWriter->isOK()) {
Chris@1520 121 SVCERR << "WritableWaveFileModel: Error in creating WAV file writer: " << m_targetWriter->getError() << endl;
Chris@175 122 return;
Chris@175 123 }
Chris@1520 124
Chris@1520 125 if (m_normalisation != Normalisation::None) {
Chris@187 126
Chris@1520 127 // Temp dir is exclusive to this run of the application, so
Chris@1520 128 // the filename only needs to be unique within that
Chris@1520 129 QDir dir(TempDirectory::getInstance()->getPath());
Chris@1731 130 m_temporaryPath = dir.filePath(QString("prenorm_%1.wav")
Chris@1731 131 .arg(getId().toString()));
Chris@316 132
Chris@1520 133 m_temporaryWriter = new WavFileWriter
Chris@1520 134 (m_temporaryPath, m_sampleRate, m_channels,
Chris@1520 135 WavFileWriter::WriteToTarget);
Chris@1520 136
Chris@1520 137 if (!m_temporaryWriter->isOK()) {
Chris@1520 138 SVCERR << "WritableWaveFileModel: Error in creating temporary WAV file writer: " << m_temporaryWriter->getError() << endl;
Chris@1520 139 return;
Chris@1520 140 }
Chris@1520 141 }
Chris@1520 142
Chris@1520 143 FileSource source(m_targetPath);
Chris@1520 144
Chris@1520 145 m_reader = new WavFileReader(source, true);
Chris@290 146 if (!m_reader->getError().isEmpty()) {
Chris@1520 147 SVCERR << "WritableWaveFileModel: Error in creating wave file reader: " << m_reader->getError() << endl;
Chris@187 148 return;
Chris@187 149 }
Chris@187 150
Chris@1122 151 m_model = new ReadOnlyWaveFileModel(source, m_reader);
Chris@187 152 if (!m_model->isOK()) {
Chris@1428 153 SVCERR << "WritableWaveFileModel: Error in creating wave file model" << endl;
Chris@187 154 delete m_model;
Chris@1582 155 m_model = nullptr;
Chris@187 156 return;
Chris@187 157 }
Chris@300 158 m_model->setStartFrame(m_startFrame);
Chris@187 159
Chris@258 160 connect(m_model, SIGNAL(modelChanged()), this, SIGNAL(modelChanged()));
Chris@1038 161 connect(m_model, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)),
Chris@1038 162 this, SIGNAL(modelChangedWithin(sv_frame_t, sv_frame_t)));
Chris@175 163 }
Chris@175 164
Chris@175 165 WritableWaveFileModel::~WritableWaveFileModel()
Chris@175 166 {
Chris@175 167 delete m_model;
Chris@1520 168 delete m_targetWriter;
Chris@1520 169 delete m_temporaryWriter;
Chris@175 170 delete m_reader;
Chris@175 171 }
Chris@175 172
Chris@300 173 void
Chris@1038 174 WritableWaveFileModel::setStartFrame(sv_frame_t startFrame)
Chris@300 175 {
Chris@300 176 m_startFrame = startFrame;
Chris@1520 177 if (m_model) {
Chris@1520 178 m_model->setStartFrame(startFrame);
Chris@1520 179 }
Chris@300 180 }
Chris@300 181
Chris@175 182 bool
Chris@1325 183 WritableWaveFileModel::addSamples(const float *const *samples, sv_frame_t count)
Chris@175 184 {
Chris@1520 185 if (!m_model) return false;
Chris@175 186
Chris@258 187 #ifdef DEBUG_WRITABLE_WAVE_FILE_MODEL
Chris@690 188 // SVDEBUG << "WritableWaveFileModel::addSamples(" << count << ")" << endl;
Chris@258 189 #endif
Chris@258 190
Chris@1520 191 WavFileWriter *writer = m_targetWriter;
Chris@1520 192 if (m_normalisation != Normalisation::None) {
Chris@1520 193 writer = m_temporaryWriter;
Chris@1520 194 }
Chris@1520 195
Chris@1520 196 if (!writer->writeSamples(samples, count)) {
Chris@1520 197 SVCERR << "ERROR: WritableWaveFileModel::addSamples: writer failed: " << writer->getError() << endl;
Chris@175 198 return false;
Chris@175 199 }
Chris@175 200
Chris@175 201 m_frameCount += count;
Chris@175 202
Chris@1520 203 if (m_normalisation == Normalisation::None) {
Chris@1520 204 if (m_reader->getChannelCount() == 0) {
Chris@1520 205 m_reader->updateFrameCount();
Chris@1520 206 }
Chris@175 207 }
Chris@175 208
Chris@175 209 return true;
Chris@175 210 }
Chris@175 211
Chris@1337 212 void
Chris@1337 213 WritableWaveFileModel::updateModel()
Chris@1337 214 {
Chris@1520 215 if (!m_model) return;
Chris@1520 216
Chris@1520 217 m_reader->updateFrameCount();
Chris@1337 218 }
Chris@1337 219
Chris@175 220 bool
Chris@175 221 WritableWaveFileModel::isOK() const
Chris@175 222 {
Chris@1520 223 return (m_model && m_model->isOK());
Chris@175 224 }
Chris@175 225
Chris@188 226 void
Chris@1133 227 WritableWaveFileModel::setWriteProportion(int proportion)
Chris@188 228 {
Chris@1133 229 m_proportion = proportion;
Chris@1133 230 }
Chris@1133 231
Chris@1133 232 int
Chris@1133 233 WritableWaveFileModel::getWriteProportion() const
Chris@1133 234 {
Chris@1133 235 return m_proportion;
Chris@1133 236 }
Chris@1133 237
Chris@1133 238 void
Chris@1133 239 WritableWaveFileModel::writeComplete()
Chris@1133 240 {
Chris@1520 241 if (!m_model) return;
Chris@1520 242
Chris@1520 243 if (m_normalisation == Normalisation::None) {
Chris@1520 244 m_targetWriter->close();
Chris@1520 245 } else {
Chris@1520 246 m_temporaryWriter->close();
Chris@1520 247 normaliseToTarget();
Chris@1520 248 }
Chris@1520 249
Chris@1520 250 m_reader->updateDone();
Chris@1133 251 m_proportion = 100;
Chris@1133 252 emit modelChanged();
Chris@1723 253 emit writeCompleted();
Chris@175 254 }
Chris@175 255
Chris@1520 256 void
Chris@1520 257 WritableWaveFileModel::normaliseToTarget()
Chris@1520 258 {
Chris@1520 259 if (m_temporaryPath == "") {
Chris@1520 260 SVCERR << "WritableWaveFileModel::normaliseToTarget: No temporary path available" << endl;
Chris@1520 261 return;
Chris@1520 262 }
Chris@1520 263
Chris@1520 264 WavFileReader normalisingReader(m_temporaryPath, false,
Chris@1520 265 WavFileReader::Normalisation::Peak);
Chris@1520 266
Chris@1520 267 if (!normalisingReader.getError().isEmpty()) {
Chris@1520 268 SVCERR << "WritableWaveFileModel: Error in creating normalising reader: " << normalisingReader.getError() << endl;
Chris@1520 269 return;
Chris@1520 270 }
Chris@1520 271
Chris@1520 272 sv_frame_t frame = 0;
Chris@1520 273 sv_frame_t block = 65536;
Chris@1520 274 sv_frame_t count = normalisingReader.getFrameCount();
Chris@1520 275
Chris@1520 276 while (frame < count) {
Chris@1520 277 auto frames = normalisingReader.getInterleavedFrames(frame, block);
Chris@1520 278 if (!m_targetWriter->putInterleavedFrames(frames)) {
Chris@1520 279 SVCERR << "ERROR: WritableWaveFileModel::normaliseToTarget: writer failed: " << m_targetWriter->getError() << endl;
Chris@1520 280 return;
Chris@1520 281 }
Chris@1520 282 frame += block;
Chris@1520 283 }
Chris@1520 284
Chris@1520 285 m_targetWriter->close();
Chris@1520 286
Chris@1520 287 delete m_temporaryWriter;
Chris@1582 288 m_temporaryWriter = nullptr;
Chris@1520 289 QFile::remove(m_temporaryPath);
Chris@1520 290 }
Chris@1520 291
Chris@1038 292 sv_frame_t
Chris@175 293 WritableWaveFileModel::getFrameCount() const
Chris@175 294 {
Chris@690 295 // SVDEBUG << "WritableWaveFileModel::getFrameCount: count = " << m_frameCount << endl;
Chris@175 296 return m_frameCount;
Chris@175 297 }
Chris@175 298
Chris@1326 299 floatvec_t
Chris@1096 300 WritableWaveFileModel::getData(int channel, sv_frame_t start, sv_frame_t count) const
Chris@175 301 {
Chris@1096 302 if (!m_model || m_model->getChannelCount() == 0) return {};
Chris@1096 303 return m_model->getData(channel, start, count);
Chris@175 304 }
Chris@175 305
Chris@1326 306 vector<floatvec_t>
Chris@1086 307 WritableWaveFileModel::getMultiChannelData(int fromchannel, int tochannel,
Chris@1096 308 sv_frame_t start, sv_frame_t count) const
Chris@175 309 {
Chris@1096 310 if (!m_model || m_model->getChannelCount() == 0) return {};
Chris@1096 311 return m_model->getMultiChannelData(fromchannel, tochannel, start, count);
Chris@363 312 }
Chris@363 313
Chris@929 314 int
Chris@929 315 WritableWaveFileModel::getSummaryBlockSize(int desired) const
Chris@377 316 {
Chris@377 317 if (!m_model) return desired;
Chris@377 318 return m_model->getSummaryBlockSize(desired);
Chris@377 319 }
Chris@377 320
Chris@225 321 void
Chris@1038 322 WritableWaveFileModel::getSummaries(int channel, sv_frame_t start, sv_frame_t count,
Chris@300 323 RangeBlock &ranges,
Chris@929 324 int &blockSize) const
Chris@175 325 {
Chris@225 326 ranges.clear();
Chris@225 327 if (!m_model || m_model->getChannelCount() == 0) return;
Chris@300 328 m_model->getSummaries(channel, start, count, ranges, blockSize);
Chris@175 329 }
Chris@175 330
Chris@175 331 WritableWaveFileModel::Range
Chris@1038 332 WritableWaveFileModel::getSummary(int channel, sv_frame_t start, sv_frame_t count) const
Chris@175 333 {
Chris@187 334 if (!m_model || m_model->getChannelCount() == 0) return Range();
Chris@300 335 return m_model->getSummary(channel, start, count);
Chris@175 336 }
Chris@175 337
Chris@175 338 void
Chris@175 339 WritableWaveFileModel::toXml(QTextStream &out,
Chris@175 340 QString indent,
Chris@175 341 QString extraAttributes) const
Chris@175 342 {
Chris@1123 343 // The assumption here is that the underlying wave file has
Chris@1123 344 // already been saved somewhere (its location is available through
Chris@1123 345 // getLocation()) and that the code that uses this class is
Chris@1123 346 // dealing with the problem of making sure it remains available.
Chris@1123 347 // We just write this out as if it were a normal wave file.
Chris@187 348
Chris@188 349 Model::toXml
Chris@188 350 (out, indent,
Chris@1123 351 QString("type=\"wavefile\" file=\"%1\" subtype=\"writable\" %2")
Chris@1520 352 .arg(encodeEntities(m_targetPath))
Chris@1123 353 .arg(extraAttributes));
Chris@175 354 }
Chris@175 355