Chris@148
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@148
|
2
|
Chris@148
|
3 /*
|
Chris@148
|
4 Sonic Visualiser
|
Chris@148
|
5 An audio file viewer and annotation editor.
|
Chris@148
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@202
|
7 This file copyright 2006 Chris Cannam and QMUL.
|
Chris@148
|
8
|
Chris@148
|
9 This program is free software; you can redistribute it and/or
|
Chris@148
|
10 modify it under the terms of the GNU General Public License as
|
Chris@148
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@148
|
12 License, or (at your option) any later version. See the file
|
Chris@148
|
13 COPYING included with this distribution for more information.
|
Chris@148
|
14 */
|
Chris@148
|
15
|
Chris@148
|
16 #include "WavFileWriter.h"
|
Chris@148
|
17
|
Chris@148
|
18 #include "model/DenseTimeValueModel.h"
|
Chris@148
|
19 #include "base/Selection.h"
|
Chris@674
|
20 #include "base/TempWriteFile.h"
|
Chris@674
|
21 #include "base/Exceptions.h"
|
Chris@148
|
22
|
Chris@148
|
23 #include <QFileInfo>
|
Chris@148
|
24
|
Chris@148
|
25 #include <iostream>
|
Chris@1055
|
26 #include <cmath>
|
Chris@148
|
27
|
Chris@1096
|
28 using namespace std;
|
Chris@1096
|
29
|
Chris@148
|
30 WavFileWriter::WavFileWriter(QString path,
|
Chris@1040
|
31 sv_samplerate_t sampleRate,
|
Chris@929
|
32 int channels,
|
Chris@684
|
33 FileWriteMode mode) :
|
Chris@148
|
34 m_path(path),
|
Chris@148
|
35 m_sampleRate(sampleRate),
|
Chris@174
|
36 m_channels(channels),
|
Chris@674
|
37 m_temp(0),
|
Chris@1348
|
38 m_sndfile(0),
|
Chris@1348
|
39 m_qfile(0)
|
Chris@148
|
40 {
|
Chris@174
|
41 SF_INFO fileInfo;
|
Chris@1040
|
42
|
Chris@1040
|
43 int fileRate = int(round(m_sampleRate));
|
Chris@1040
|
44 if (m_sampleRate != sv_samplerate_t(fileRate)) {
|
Chris@1348
|
45 SVCERR << "WavFileWriter: WARNING: Non-integer sample rate "
|
Chris@1348
|
46 << m_sampleRate << " presented, rounding to " << fileRate
|
Chris@1348
|
47 << endl;
|
Chris@1040
|
48 }
|
Chris@1040
|
49 fileInfo.samplerate = fileRate;
|
Chris@174
|
50 fileInfo.channels = m_channels;
|
Chris@174
|
51 fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
|
Chris@674
|
52
|
Chris@674
|
53 try {
|
Chris@684
|
54 if (mode == WriteToTemporary) {
|
Chris@684
|
55 m_temp = new TempWriteFile(m_path);
|
Chris@1348
|
56 m_qfile = new QFile(m_temp->getTemporaryFilename());
|
Chris@1348
|
57 } else {
|
Chris@1348
|
58 m_qfile = new QFile(m_path);
|
Chris@1348
|
59 }
|
Chris@1348
|
60 if (!m_qfile->open(QIODevice::WriteOnly)) {
|
Chris@1348
|
61 SVCERR << "WavFileWriter: Failed to open file for writing" << endl;
|
Chris@1348
|
62 m_error = QString("Failed to open audio file '%1' for writing")
|
Chris@1348
|
63 .arg(m_qfile->fileName());
|
Chris@1348
|
64 } else {
|
Chris@1348
|
65 m_sndfile = sf_open_fd(m_qfile->handle(),
|
Chris@1348
|
66 SFM_WRITE, &fileInfo, false);
|
Chris@1348
|
67 if (!m_sndfile) {
|
Chris@1348
|
68 SVCERR << "WavFileWriter: Failed to open file ("
|
Chris@1348
|
69 << sf_strerror(m_sndfile) << ")" << endl;
|
Chris@684
|
70 m_error = QString("Failed to open audio file '%1' for writing")
|
Chris@1348
|
71 .arg(m_qfile->fileName());
|
Chris@684
|
72 }
|
Chris@1348
|
73 }
|
Chris@674
|
74 } catch (FileOperationFailed &f) {
|
Chris@674
|
75 m_error = f.what();
|
Chris@674
|
76 m_temp = 0;
|
Chris@1348
|
77 m_qfile = 0;
|
Chris@1348
|
78 m_sndfile = 0;
|
Chris@174
|
79 }
|
Chris@148
|
80 }
|
Chris@148
|
81
|
Chris@148
|
82 WavFileWriter::~WavFileWriter()
|
Chris@148
|
83 {
|
Chris@1348
|
84 if (m_sndfile) close();
|
Chris@148
|
85 }
|
Chris@148
|
86
|
Chris@148
|
87 bool
|
Chris@148
|
88 WavFileWriter::isOK() const
|
Chris@148
|
89 {
|
Chris@148
|
90 return (m_error.isEmpty());
|
Chris@148
|
91 }
|
Chris@148
|
92
|
Chris@148
|
93 QString
|
Chris@148
|
94 WavFileWriter::getError() const
|
Chris@148
|
95 {
|
Chris@148
|
96 return m_error;
|
Chris@148
|
97 }
|
Chris@148
|
98
|
Chris@684
|
99 QString
|
Chris@684
|
100 WavFileWriter::getWriteFilename() const
|
Chris@684
|
101 {
|
Chris@684
|
102 if (m_temp) {
|
Chris@684
|
103 return m_temp->getTemporaryFilename();
|
Chris@684
|
104 } else {
|
Chris@684
|
105 return m_path;
|
Chris@684
|
106 }
|
Chris@684
|
107 }
|
Chris@684
|
108
|
Chris@174
|
109 bool
|
Chris@174
|
110 WavFileWriter::writeModel(DenseTimeValueModel *source,
|
Chris@174
|
111 MultiSelection *selection)
|
Chris@148
|
112 {
|
Chris@174
|
113 if (source->getChannelCount() != m_channels) {
|
Chris@690
|
114 SVDEBUG << "WavFileWriter::writeModel: Wrong number of channels ("
|
Chris@174
|
115 << source->getChannelCount() << " != " << m_channels << ")"
|
Chris@687
|
116 << endl;
|
Chris@174
|
117 m_error = QString("Failed to write model to audio file '%1'")
|
Chris@684
|
118 .arg(getWriteFilename());
|
Chris@174
|
119 return false;
|
Chris@148
|
120 }
|
Chris@148
|
121
|
Chris@1348
|
122 if (!m_sndfile) {
|
Chris@174
|
123 m_error = QString("Failed to write model to audio file '%1': File not open")
|
Chris@684
|
124 .arg(getWriteFilename());
|
Chris@174
|
125 return false;
|
Chris@174
|
126 }
|
Chris@148
|
127
|
Chris@174
|
128 bool ownSelection = false;
|
Chris@174
|
129 if (!selection) {
|
Chris@148
|
130 selection = new MultiSelection;
|
Chris@174
|
131 selection->setSelection(Selection(source->getStartFrame(),
|
Chris@174
|
132 source->getEndFrame()));
|
Chris@174
|
133 ownSelection = true;
|
Chris@148
|
134 }
|
Chris@148
|
135
|
Chris@1038
|
136 sv_frame_t bs = 2048;
|
Chris@148
|
137
|
Chris@148
|
138 for (MultiSelection::SelectionList::iterator i =
|
Chris@148
|
139 selection->getSelections().begin();
|
Chris@148
|
140 i != selection->getSelections().end(); ++i) {
|
Chris@148
|
141
|
Chris@1038
|
142 sv_frame_t f0(i->getStartFrame()), f1(i->getEndFrame());
|
Chris@148
|
143
|
Chris@1038
|
144 for (sv_frame_t f = f0; f < f1; f += bs) {
|
Chris@148
|
145
|
Chris@1096
|
146 sv_frame_t n = min(bs, f1 - f);
|
Chris@1326
|
147 floatvec_t interleaved(n * m_channels, 0.f);
|
Chris@148
|
148
|
Chris@174
|
149 for (int c = 0; c < int(m_channels); ++c) {
|
Chris@1326
|
150 auto chanbuf = source->getData(c, f, n);
|
Chris@1096
|
151 for (int i = 0; in_range_for(chanbuf, i); ++i) {
|
Chris@1096
|
152 interleaved[i * m_channels + c] = chanbuf[i];
|
Chris@148
|
153 }
|
Chris@148
|
154 }
|
Chris@148
|
155
|
Chris@1348
|
156 sf_count_t written = sf_writef_float(m_sndfile, interleaved.data(), n);
|
Chris@148
|
157
|
Chris@148
|
158 if (written < n) {
|
Chris@148
|
159 m_error = QString("Only wrote %1 of %2 frames at file frame %3")
|
Chris@148
|
160 .arg(written).arg(n).arg(f);
|
Chris@148
|
161 break;
|
Chris@148
|
162 }
|
Chris@148
|
163 }
|
Chris@148
|
164 }
|
Chris@148
|
165
|
Chris@174
|
166 if (ownSelection) delete selection;
|
Chris@174
|
167
|
Chris@174
|
168 return isOK();
|
Chris@174
|
169 }
|
Chris@174
|
170
|
Chris@174
|
171 bool
|
Chris@1325
|
172 WavFileWriter::writeSamples(const float *const *samples, sv_frame_t count)
|
Chris@174
|
173 {
|
Chris@1348
|
174 if (!m_sndfile) {
|
Chris@174
|
175 m_error = QString("Failed to write model to audio file '%1': File not open")
|
Chris@684
|
176 .arg(getWriteFilename());
|
Chris@174
|
177 return false;
|
Chris@174
|
178 }
|
Chris@174
|
179
|
Chris@174
|
180 float *b = new float[count * m_channels];
|
Chris@1038
|
181 for (sv_frame_t i = 0; i < count; ++i) {
|
Chris@929
|
182 for (int c = 0; c < int(m_channels); ++c) {
|
Chris@174
|
183 b[i * m_channels + c] = samples[c][i];
|
Chris@174
|
184 }
|
Chris@174
|
185 }
|
Chris@174
|
186
|
Chris@1348
|
187 sv_frame_t written = sf_writef_float(m_sndfile, b, count);
|
Chris@174
|
188
|
Chris@174
|
189 delete[] b;
|
Chris@174
|
190
|
Chris@1038
|
191 if (written < count) {
|
Chris@174
|
192 m_error = QString("Only wrote %1 of %2 frames")
|
Chris@174
|
193 .arg(written).arg(count);
|
Chris@174
|
194 }
|
Chris@174
|
195
|
Chris@174
|
196 return isOK();
|
Chris@174
|
197 }
|
Chris@174
|
198
|
Chris@174
|
199 bool
|
Chris@174
|
200 WavFileWriter::close()
|
Chris@174
|
201 {
|
Chris@1348
|
202 if (m_sndfile) {
|
Chris@1348
|
203 sf_close(m_sndfile);
|
Chris@1348
|
204 m_sndfile = 0;
|
Chris@174
|
205 }
|
Chris@1348
|
206
|
Chris@1348
|
207 delete m_qfile;
|
Chris@1348
|
208 m_qfile = 0;
|
Chris@1348
|
209
|
Chris@684
|
210 if (m_temp) {
|
Chris@684
|
211 m_temp->moveToTarget();
|
Chris@684
|
212 delete m_temp;
|
Chris@684
|
213 m_temp = 0;
|
Chris@684
|
214 }
|
Chris@1348
|
215
|
Chris@174
|
216 return true;
|
Chris@148
|
217 }
|
Chris@148
|
218
|