annotate data/model/WritableWaveFileModel.cpp @ 175:b0f4555b625e

* Introduce WritableWaveFileModel, and use it as an output model for audio real-time plugin transforms. Updates aren't working correctly yet.
author Chris Cannam
date Tue, 03 Oct 2006 14:17:37 +0000
parents
children 570794f6f6a7
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@175 7 This file copyright 2006 Chris Cannam.
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@175 29 WritableWaveFileModel::WritableWaveFileModel(size_t sampleRate,
Chris@175 30 size_t channels,
Chris@175 31 QString path) :
Chris@175 32 m_model(0),
Chris@175 33 m_writer(0),
Chris@175 34 m_reader(0),
Chris@175 35 m_sampleRate(sampleRate),
Chris@175 36 m_channels(channels),
Chris@175 37 m_frameCount(0)
Chris@175 38 {
Chris@175 39 if (path.isEmpty()) {
Chris@175 40 try {
Chris@175 41 QDir dir(TempDirectory::getInstance()->getPath());
Chris@175 42 path = dir.filePath(QString("written_%1.wav")
Chris@175 43 .arg((intptr_t)this));
Chris@175 44 } catch (DirectoryCreationFailed f) {
Chris@175 45 std::cerr << "WritableWaveFileModel: Failed to create temporary directory" << std::endl;
Chris@175 46 return;
Chris@175 47 }
Chris@175 48 }
Chris@175 49
Chris@175 50 m_writer = new WavFileWriter(path, sampleRate, channels);
Chris@175 51 if (!m_writer->isOK()) {
Chris@175 52 std::cerr << "WritableWaveFileModel: Error in creating WAV file writer: " << m_writer->getError().toStdString() << std::endl;
Chris@175 53 delete m_writer;
Chris@175 54 m_writer = 0;
Chris@175 55 return;
Chris@175 56 }
Chris@175 57 }
Chris@175 58
Chris@175 59 WritableWaveFileModel::~WritableWaveFileModel()
Chris@175 60 {
Chris@175 61 delete m_model;
Chris@175 62 delete m_writer;
Chris@175 63 delete m_reader;
Chris@175 64 }
Chris@175 65
Chris@175 66 bool
Chris@175 67 WritableWaveFileModel::addSamples(float **samples, size_t count)
Chris@175 68 {
Chris@175 69 if (!m_writer) return false;
Chris@175 70
Chris@175 71 if (!m_writer->writeSamples(samples, count)) {
Chris@175 72 std::cerr << "ERROR: WritableWaveFileModel::addSamples: writer failed: " << m_writer->getError().toStdString() << std::endl;
Chris@175 73 return false;
Chris@175 74 }
Chris@175 75
Chris@175 76 m_frameCount += count;
Chris@175 77
Chris@175 78 if (!m_model) {
Chris@175 79
Chris@175 80 m_reader = new WavFileReader(m_writer->getPath());
Chris@175 81 if (!m_reader->getError().isEmpty()) {
Chris@175 82 std::cerr << "WritableWaveFileModel: Error in creating wave file reader" << std::endl;
Chris@175 83 delete m_reader;
Chris@175 84 m_reader = 0;
Chris@175 85 return false;
Chris@175 86 }
Chris@175 87
Chris@175 88 m_model = new WaveFileModel(m_writer->getPath(), m_reader);
Chris@175 89 if (!m_model->isOK()) {
Chris@175 90 std::cerr << "WritableWaveFileModel: Error in creating wave file model" << std::endl;
Chris@175 91 delete m_model;
Chris@175 92 m_model = 0;
Chris@175 93 delete m_reader;
Chris@175 94 m_reader = 0;
Chris@175 95 return false;
Chris@175 96 }
Chris@175 97 }
Chris@175 98
Chris@175 99 static int updateCounter = 0;
Chris@175 100 if (++updateCounter == 100) {
Chris@175 101 if (m_reader) m_reader->updateFrameCount();
Chris@175 102 updateCounter = 0;
Chris@175 103 }
Chris@175 104
Chris@175 105 return true;
Chris@175 106 }
Chris@175 107
Chris@175 108 void
Chris@175 109 WritableWaveFileModel::sync()
Chris@175 110 {
Chris@175 111 if (m_reader) m_reader->updateFrameCount();
Chris@175 112 }
Chris@175 113
Chris@175 114 bool
Chris@175 115 WritableWaveFileModel::isOK() const
Chris@175 116 {
Chris@175 117 bool ok = (m_model && m_model->isOK());
Chris@175 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@175 125 bool ready = (m_model && m_model->isReady(completion));
Chris@175 126 std::cerr << "WritableWaveFileModel::isReady(): ready = " << ready << std::endl;
Chris@175 127 return ready;
Chris@175 128 }
Chris@175 129
Chris@175 130 size_t
Chris@175 131 WritableWaveFileModel::getFrameCount() const
Chris@175 132 {
Chris@175 133 std::cerr << "WritableWaveFileModel::getFrameCount: count = " << m_frameCount << std::endl;
Chris@175 134 return m_frameCount;
Chris@175 135 }
Chris@175 136
Chris@175 137 Model *
Chris@175 138 WritableWaveFileModel::clone() const
Chris@175 139 {
Chris@175 140 assert(0); //!!!
Chris@175 141 }
Chris@175 142
Chris@175 143 size_t
Chris@175 144 WritableWaveFileModel::getValues(int channel, size_t start, size_t end,
Chris@175 145 float *buffer) const
Chris@175 146 {
Chris@175 147 if (!m_model) return 0;
Chris@175 148 return m_model->getValues(channel, start, end, buffer);
Chris@175 149 }
Chris@175 150
Chris@175 151 size_t
Chris@175 152 WritableWaveFileModel::getValues(int channel, size_t start, size_t end,
Chris@175 153 double *buffer) const
Chris@175 154 {
Chris@175 155 if (!m_model) return 0;
Chris@175 156 // std::cerr << "WritableWaveFileModel::getValues(" << channel << ", "
Chris@175 157 // << start << ", " << end << "): calling model" << std::endl;
Chris@175 158 return m_model->getValues(channel, start, end, buffer);
Chris@175 159 }
Chris@175 160
Chris@175 161 WritableWaveFileModel::RangeBlock
Chris@175 162 WritableWaveFileModel::getRanges(size_t channel, size_t start, size_t end,
Chris@175 163 size_t &blockSize) const
Chris@175 164 {
Chris@175 165 if (!m_model) return RangeBlock();
Chris@175 166 return m_model->getRanges(channel, start, end, blockSize);
Chris@175 167 }
Chris@175 168
Chris@175 169 WritableWaveFileModel::Range
Chris@175 170 WritableWaveFileModel::getRange(size_t channel, size_t start, size_t end) const
Chris@175 171 {
Chris@175 172 if (!m_model) return Range();
Chris@175 173 return m_model->getRange(channel, start, end);
Chris@175 174 }
Chris@175 175
Chris@175 176 void
Chris@175 177 WritableWaveFileModel::toXml(QTextStream &out,
Chris@175 178 QString indent,
Chris@175 179 QString extraAttributes) const
Chris@175 180 {
Chris@175 181 assert(0); //!!!
Chris@175 182 }
Chris@175 183
Chris@175 184 QString
Chris@175 185 WritableWaveFileModel::toXmlString(QString indent,
Chris@175 186 QString extraAttributes) const
Chris@175 187 {
Chris@175 188 assert(0); //!!!
Chris@175 189 return "";
Chris@175 190 }
Chris@175 191