comparison 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
comparison
equal deleted inserted replaced
174:f8cf055dbf34 175:b0f4555b625e
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam.
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
14 */
15
16 #include "WritableWaveFileModel.h"
17
18 #include "base/TempDirectory.h"
19 #include "base/Exceptions.h"
20
21 #include "fileio/WavFileWriter.h"
22 #include "fileio/WavFileReader.h"
23
24 #include <QDir>
25
26 #include <cassert>
27 #include <iostream>
28
29 WritableWaveFileModel::WritableWaveFileModel(size_t sampleRate,
30 size_t channels,
31 QString path) :
32 m_model(0),
33 m_writer(0),
34 m_reader(0),
35 m_sampleRate(sampleRate),
36 m_channels(channels),
37 m_frameCount(0)
38 {
39 if (path.isEmpty()) {
40 try {
41 QDir dir(TempDirectory::getInstance()->getPath());
42 path = dir.filePath(QString("written_%1.wav")
43 .arg((intptr_t)this));
44 } catch (DirectoryCreationFailed f) {
45 std::cerr << "WritableWaveFileModel: Failed to create temporary directory" << std::endl;
46 return;
47 }
48 }
49
50 m_writer = new WavFileWriter(path, sampleRate, channels);
51 if (!m_writer->isOK()) {
52 std::cerr << "WritableWaveFileModel: Error in creating WAV file writer: " << m_writer->getError().toStdString() << std::endl;
53 delete m_writer;
54 m_writer = 0;
55 return;
56 }
57 }
58
59 WritableWaveFileModel::~WritableWaveFileModel()
60 {
61 delete m_model;
62 delete m_writer;
63 delete m_reader;
64 }
65
66 bool
67 WritableWaveFileModel::addSamples(float **samples, size_t count)
68 {
69 if (!m_writer) return false;
70
71 if (!m_writer->writeSamples(samples, count)) {
72 std::cerr << "ERROR: WritableWaveFileModel::addSamples: writer failed: " << m_writer->getError().toStdString() << std::endl;
73 return false;
74 }
75
76 m_frameCount += count;
77
78 if (!m_model) {
79
80 m_reader = new WavFileReader(m_writer->getPath());
81 if (!m_reader->getError().isEmpty()) {
82 std::cerr << "WritableWaveFileModel: Error in creating wave file reader" << std::endl;
83 delete m_reader;
84 m_reader = 0;
85 return false;
86 }
87
88 m_model = new WaveFileModel(m_writer->getPath(), m_reader);
89 if (!m_model->isOK()) {
90 std::cerr << "WritableWaveFileModel: Error in creating wave file model" << std::endl;
91 delete m_model;
92 m_model = 0;
93 delete m_reader;
94 m_reader = 0;
95 return false;
96 }
97 }
98
99 static int updateCounter = 0;
100 if (++updateCounter == 100) {
101 if (m_reader) m_reader->updateFrameCount();
102 updateCounter = 0;
103 }
104
105 return true;
106 }
107
108 void
109 WritableWaveFileModel::sync()
110 {
111 if (m_reader) m_reader->updateFrameCount();
112 }
113
114 bool
115 WritableWaveFileModel::isOK() const
116 {
117 bool ok = (m_model && m_model->isOK());
118 std::cerr << "WritableWaveFileModel::isOK(): ok = " << ok << std::endl;
119 return ok;
120 }
121
122 bool
123 WritableWaveFileModel::isReady(int *completion) const
124 {
125 bool ready = (m_model && m_model->isReady(completion));
126 std::cerr << "WritableWaveFileModel::isReady(): ready = " << ready << std::endl;
127 return ready;
128 }
129
130 size_t
131 WritableWaveFileModel::getFrameCount() const
132 {
133 std::cerr << "WritableWaveFileModel::getFrameCount: count = " << m_frameCount << std::endl;
134 return m_frameCount;
135 }
136
137 Model *
138 WritableWaveFileModel::clone() const
139 {
140 assert(0); //!!!
141 }
142
143 size_t
144 WritableWaveFileModel::getValues(int channel, size_t start, size_t end,
145 float *buffer) const
146 {
147 if (!m_model) return 0;
148 return m_model->getValues(channel, start, end, buffer);
149 }
150
151 size_t
152 WritableWaveFileModel::getValues(int channel, size_t start, size_t end,
153 double *buffer) const
154 {
155 if (!m_model) return 0;
156 // std::cerr << "WritableWaveFileModel::getValues(" << channel << ", "
157 // << start << ", " << end << "): calling model" << std::endl;
158 return m_model->getValues(channel, start, end, buffer);
159 }
160
161 WritableWaveFileModel::RangeBlock
162 WritableWaveFileModel::getRanges(size_t channel, size_t start, size_t end,
163 size_t &blockSize) const
164 {
165 if (!m_model) return RangeBlock();
166 return m_model->getRanges(channel, start, end, blockSize);
167 }
168
169 WritableWaveFileModel::Range
170 WritableWaveFileModel::getRange(size_t channel, size_t start, size_t end) const
171 {
172 if (!m_model) return Range();
173 return m_model->getRange(channel, start, end);
174 }
175
176 void
177 WritableWaveFileModel::toXml(QTextStream &out,
178 QString indent,
179 QString extraAttributes) const
180 {
181 assert(0); //!!!
182 }
183
184 QString
185 WritableWaveFileModel::toXmlString(QString indent,
186 QString extraAttributes) const
187 {
188 assert(0); //!!!
189 return "";
190 }
191