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@187
|
29 //!!! This class needs completing.
|
Chris@187
|
30
|
Chris@175
|
31 WritableWaveFileModel::WritableWaveFileModel(size_t sampleRate,
|
Chris@175
|
32 size_t channels,
|
Chris@175
|
33 QString path) :
|
Chris@175
|
34 m_model(0),
|
Chris@175
|
35 m_writer(0),
|
Chris@175
|
36 m_reader(0),
|
Chris@175
|
37 m_sampleRate(sampleRate),
|
Chris@175
|
38 m_channels(channels),
|
Chris@175
|
39 m_frameCount(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 void
|
Chris@175
|
115 WritableWaveFileModel::sync()
|
Chris@175
|
116 {
|
Chris@176
|
117 //!!! use setCompletion instead
|
Chris@176
|
118 if (m_reader) m_reader->updateDone();
|
Chris@175
|
119 }
|
Chris@175
|
120
|
Chris@175
|
121 bool
|
Chris@175
|
122 WritableWaveFileModel::isOK() const
|
Chris@175
|
123 {
|
Chris@187
|
124 bool ok = (m_writer && m_writer->isOK());
|
Chris@175
|
125 std::cerr << "WritableWaveFileModel::isOK(): ok = " << ok << std::endl;
|
Chris@175
|
126 return ok;
|
Chris@175
|
127 }
|
Chris@175
|
128
|
Chris@175
|
129 bool
|
Chris@175
|
130 WritableWaveFileModel::isReady(int *completion) const
|
Chris@175
|
131 {
|
Chris@175
|
132 bool ready = (m_model && m_model->isReady(completion));
|
Chris@180
|
133 std::cerr << "WritableWaveFileModel::isReady(): ready = " << ready << ", completion = " << (completion ? *completion : -1) << std::endl;
|
Chris@175
|
134 return ready;
|
Chris@175
|
135 }
|
Chris@175
|
136
|
Chris@175
|
137 size_t
|
Chris@175
|
138 WritableWaveFileModel::getFrameCount() const
|
Chris@175
|
139 {
|
Chris@175
|
140 std::cerr << "WritableWaveFileModel::getFrameCount: count = " << m_frameCount << std::endl;
|
Chris@175
|
141 return m_frameCount;
|
Chris@175
|
142 }
|
Chris@175
|
143
|
Chris@175
|
144 Model *
|
Chris@175
|
145 WritableWaveFileModel::clone() const
|
Chris@175
|
146 {
|
Chris@175
|
147 assert(0); //!!!
|
Chris@175
|
148 }
|
Chris@175
|
149
|
Chris@175
|
150 size_t
|
Chris@175
|
151 WritableWaveFileModel::getValues(int channel, size_t start, size_t end,
|
Chris@175
|
152 float *buffer) const
|
Chris@175
|
153 {
|
Chris@187
|
154 if (!m_model || m_model->getChannelCount() == 0) return 0;
|
Chris@175
|
155 return m_model->getValues(channel, start, end, buffer);
|
Chris@175
|
156 }
|
Chris@175
|
157
|
Chris@175
|
158 size_t
|
Chris@175
|
159 WritableWaveFileModel::getValues(int channel, size_t start, size_t end,
|
Chris@175
|
160 double *buffer) const
|
Chris@175
|
161 {
|
Chris@187
|
162 if (!m_model || m_model->getChannelCount() == 0) return 0;
|
Chris@175
|
163 // std::cerr << "WritableWaveFileModel::getValues(" << channel << ", "
|
Chris@175
|
164 // << start << ", " << end << "): calling model" << std::endl;
|
Chris@175
|
165 return m_model->getValues(channel, start, end, buffer);
|
Chris@175
|
166 }
|
Chris@175
|
167
|
Chris@175
|
168 WritableWaveFileModel::RangeBlock
|
Chris@175
|
169 WritableWaveFileModel::getRanges(size_t channel, size_t start, size_t end,
|
Chris@175
|
170 size_t &blockSize) const
|
Chris@175
|
171 {
|
Chris@187
|
172 if (!m_model || m_model->getChannelCount() == 0) return RangeBlock();
|
Chris@175
|
173 return m_model->getRanges(channel, start, end, blockSize);
|
Chris@175
|
174 }
|
Chris@175
|
175
|
Chris@175
|
176 WritableWaveFileModel::Range
|
Chris@175
|
177 WritableWaveFileModel::getRange(size_t channel, size_t start, size_t end) const
|
Chris@175
|
178 {
|
Chris@187
|
179 if (!m_model || m_model->getChannelCount() == 0) return Range();
|
Chris@175
|
180 return m_model->getRange(channel, start, end);
|
Chris@175
|
181 }
|
Chris@175
|
182
|
Chris@175
|
183 void
|
Chris@175
|
184 WritableWaveFileModel::toXml(QTextStream &out,
|
Chris@175
|
185 QString indent,
|
Chris@175
|
186 QString extraAttributes) const
|
Chris@175
|
187 {
|
Chris@187
|
188 //!!! need to indicate that some models are not saved but must be
|
Chris@187
|
189 //regenerated -- same goes for those very large dense 3d models
|
Chris@187
|
190
|
Chris@175
|
191 assert(0); //!!!
|
Chris@175
|
192 }
|
Chris@175
|
193
|
Chris@175
|
194 QString
|
Chris@175
|
195 WritableWaveFileModel::toXmlString(QString indent,
|
Chris@175
|
196 QString extraAttributes) const
|
Chris@175
|
197 {
|
Chris@175
|
198 assert(0); //!!!
|
Chris@175
|
199 return "";
|
Chris@175
|
200 }
|
Chris@175
|
201
|