annotate data/model/WritableWaveFileModel.cpp @ 230:2854dc429310

...
author Chris Cannam
date Mon, 12 Feb 2007 13:25:12 +0000
parents 185454896a76
children 96a6dd889c68
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@175 18 #include "base/TempDirectory.h"
Chris@175 19 #include "base/Exceptions.h"
Chris@175 20
Chris@175 21 #include "fileio/WavFileWriter.h"
Chris@175 22 #include "fileio/WavFileReader.h"
Chris@175 23
Chris@175 24 #include <QDir>
Chris@175 25
Chris@175 26 #include <cassert>
Chris@175 27 #include <iostream>
Chris@175 28
Chris@187 29
Chris@175 30 WritableWaveFileModel::WritableWaveFileModel(size_t sampleRate,
Chris@175 31 size_t channels,
Chris@175 32 QString path) :
Chris@175 33 m_model(0),
Chris@175 34 m_writer(0),
Chris@175 35 m_reader(0),
Chris@175 36 m_sampleRate(sampleRate),
Chris@175 37 m_channels(channels),
Chris@188 38 m_frameCount(0),
Chris@188 39 m_completion(0)
Chris@175 40 {
Chris@175 41 if (path.isEmpty()) {
Chris@175 42 try {
Chris@175 43 QDir dir(TempDirectory::getInstance()->getPath());
Chris@175 44 path = dir.filePath(QString("written_%1.wav")
Chris@175 45 .arg((intptr_t)this));
Chris@175 46 } catch (DirectoryCreationFailed f) {
Chris@175 47 std::cerr << "WritableWaveFileModel: Failed to create temporary directory" << std::endl;
Chris@175 48 return;
Chris@175 49 }
Chris@175 50 }
Chris@175 51
Chris@175 52 m_writer = new WavFileWriter(path, sampleRate, channels);
Chris@175 53 if (!m_writer->isOK()) {
Chris@175 54 std::cerr << "WritableWaveFileModel: Error in creating WAV file writer: " << m_writer->getError().toStdString() << std::endl;
Chris@175 55 delete m_writer;
Chris@175 56 m_writer = 0;
Chris@175 57 return;
Chris@175 58 }
Chris@187 59
Chris@187 60 m_reader = new WavFileReader(m_writer->getPath(), true);
Chris@187 61 if (!m_reader->getError().isEmpty()) {
Chris@187 62 std::cerr << "WritableWaveFileModel: Error in creating wave file reader" << std::endl;
Chris@187 63 delete m_reader;
Chris@187 64 m_reader = 0;
Chris@187 65 return;
Chris@187 66 }
Chris@187 67
Chris@187 68 m_model = new WaveFileModel(m_writer->getPath(), m_reader);
Chris@187 69 if (!m_model->isOK()) {
Chris@187 70 std::cerr << "WritableWaveFileModel: Error in creating wave file model" << std::endl;
Chris@187 71 delete m_model;
Chris@187 72 m_model = 0;
Chris@187 73 delete m_reader;
Chris@187 74 m_reader = 0;
Chris@187 75 return;
Chris@187 76 }
Chris@187 77
Chris@187 78 connect(m_model, SLOT(modelChanged()), this, SIGNAL(modelChanged()));
Chris@187 79 connect(m_model, SLOT(modelChanged(size_t, size_t)),
Chris@187 80 this, SIGNAL(modelChanged(size_t, size_t)));
Chris@175 81 }
Chris@175 82
Chris@175 83 WritableWaveFileModel::~WritableWaveFileModel()
Chris@175 84 {
Chris@175 85 delete m_model;
Chris@175 86 delete m_writer;
Chris@175 87 delete m_reader;
Chris@175 88 }
Chris@175 89
Chris@175 90 bool
Chris@175 91 WritableWaveFileModel::addSamples(float **samples, size_t count)
Chris@175 92 {
Chris@175 93 if (!m_writer) return false;
Chris@175 94
Chris@175 95 if (!m_writer->writeSamples(samples, count)) {
Chris@175 96 std::cerr << "ERROR: WritableWaveFileModel::addSamples: writer failed: " << m_writer->getError().toStdString() << std::endl;
Chris@175 97 return false;
Chris@175 98 }
Chris@175 99
Chris@175 100 m_frameCount += count;
Chris@175 101
Chris@187 102 static int updateCounter = 0;
Chris@175 103
Chris@187 104 if (m_reader && m_reader->getChannelCount() == 0) {
Chris@187 105 m_reader->updateFrameCount();
Chris@187 106 } else if (++updateCounter == 100) {
Chris@175 107 if (m_reader) m_reader->updateFrameCount();
Chris@175 108 updateCounter = 0;
Chris@175 109 }
Chris@175 110
Chris@175 111 return true;
Chris@175 112 }
Chris@175 113
Chris@175 114 bool
Chris@175 115 WritableWaveFileModel::isOK() const
Chris@175 116 {
Chris@187 117 bool ok = (m_writer && m_writer->isOK());
Chris@188 118 // std::cerr << "WritableWaveFileModel::isOK(): ok = " << ok << std::endl;
Chris@175 119 return ok;
Chris@175 120 }
Chris@175 121
Chris@175 122 bool
Chris@175 123 WritableWaveFileModel::isReady(int *completion) const
Chris@175 124 {
Chris@188 125 if (completion) *completion = m_completion;
Chris@188 126 return (m_completion == 100);
Chris@188 127 }
Chris@188 128
Chris@188 129 void
Chris@188 130 WritableWaveFileModel::setCompletion(int completion)
Chris@188 131 {
Chris@188 132 m_completion = completion;
Chris@188 133 if (completion == 100) {
Chris@188 134 if (m_reader) m_reader->updateDone();
Chris@188 135 }
Chris@175 136 }
Chris@175 137
Chris@175 138 size_t
Chris@175 139 WritableWaveFileModel::getFrameCount() const
Chris@175 140 {
Chris@188 141 // std::cerr << "WritableWaveFileModel::getFrameCount: count = " << m_frameCount << std::endl;
Chris@175 142 return m_frameCount;
Chris@175 143 }
Chris@175 144
Chris@175 145 Model *
Chris@175 146 WritableWaveFileModel::clone() const
Chris@175 147 {
Chris@175 148 assert(0); //!!!
Chris@188 149 return 0;
Chris@175 150 }
Chris@175 151
Chris@175 152 size_t
Chris@175 153 WritableWaveFileModel::getValues(int channel, size_t start, size_t end,
Chris@175 154 float *buffer) const
Chris@175 155 {
Chris@187 156 if (!m_model || m_model->getChannelCount() == 0) return 0;
Chris@175 157 return m_model->getValues(channel, start, end, buffer);
Chris@175 158 }
Chris@175 159
Chris@175 160 size_t
Chris@175 161 WritableWaveFileModel::getValues(int channel, size_t start, size_t end,
Chris@175 162 double *buffer) const
Chris@175 163 {
Chris@187 164 if (!m_model || m_model->getChannelCount() == 0) return 0;
Chris@175 165 // std::cerr << "WritableWaveFileModel::getValues(" << channel << ", "
Chris@175 166 // << start << ", " << end << "): calling model" << std::endl;
Chris@175 167 return m_model->getValues(channel, start, end, buffer);
Chris@175 168 }
Chris@175 169
Chris@225 170 void
Chris@175 171 WritableWaveFileModel::getRanges(size_t channel, size_t start, size_t end,
Chris@225 172 RangeBlock &ranges,
Chris@175 173 size_t &blockSize) const
Chris@175 174 {
Chris@225 175 ranges.clear();
Chris@225 176 if (!m_model || m_model->getChannelCount() == 0) return;
Chris@225 177 m_model->getRanges(channel, start, end, ranges, blockSize);
Chris@175 178 }
Chris@175 179
Chris@175 180 WritableWaveFileModel::Range
Chris@175 181 WritableWaveFileModel::getRange(size_t channel, size_t start, size_t end) const
Chris@175 182 {
Chris@187 183 if (!m_model || m_model->getChannelCount() == 0) return Range();
Chris@175 184 return m_model->getRange(channel, start, end);
Chris@175 185 }
Chris@175 186
Chris@175 187 void
Chris@175 188 WritableWaveFileModel::toXml(QTextStream &out,
Chris@175 189 QString indent,
Chris@175 190 QString extraAttributes) const
Chris@175 191 {
Chris@188 192 // We don't actually write the data to XML. We just write a brief
Chris@188 193 // description of the model. Any code that uses this class is
Chris@188 194 // going to need to be aware that it will have to make separate
Chris@188 195 // arrangements for the audio file itself.
Chris@187 196
Chris@188 197 Model::toXml
Chris@188 198 (out, indent,
Chris@188 199 QString("type=\"writablewavefile\" file=\"%1\" channels=\"%2\" %3")
Chris@188 200 .arg(m_writer->getPath()).arg(m_model->getChannelCount()).arg(extraAttributes));
Chris@175 201 }
Chris@175 202
Chris@175 203 QString
Chris@175 204 WritableWaveFileModel::toXmlString(QString indent,
Chris@175 205 QString extraAttributes) const
Chris@175 206 {
Chris@188 207 // As above.
Chris@188 208
Chris@188 209 return Model::toXmlString
Chris@188 210 (indent,
Chris@188 211 QString("type=\"writablewavefile\" file=\"%1\" channels=\"%2\" %3")
Chris@188 212 .arg(m_writer->getPath()).arg(m_model->getChannelCount()).arg(extraAttributes));
Chris@175 213 }
Chris@175 214