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@148
|
26
|
Chris@148
|
27 WavFileWriter::WavFileWriter(QString path,
|
Chris@148
|
28 size_t sampleRate,
|
Chris@174
|
29 size_t channels) :
|
Chris@148
|
30 m_path(path),
|
Chris@148
|
31 m_sampleRate(sampleRate),
|
Chris@174
|
32 m_channels(channels),
|
Chris@674
|
33 m_temp(0),
|
Chris@174
|
34 m_file(0)
|
Chris@148
|
35 {
|
Chris@174
|
36 SF_INFO fileInfo;
|
Chris@174
|
37 fileInfo.samplerate = m_sampleRate;
|
Chris@174
|
38 fileInfo.channels = m_channels;
|
Chris@174
|
39 fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
|
Chris@674
|
40
|
Chris@674
|
41 try {
|
Chris@674
|
42 m_temp = new TempWriteFile(m_path);
|
Chris@674
|
43 m_file = sf_open(m_temp->getTemporaryFilename().toLocal8Bit(),
|
Chris@674
|
44 SFM_WRITE, &fileInfo);
|
Chris@674
|
45 if (!m_file) {
|
Chris@674
|
46 std::cerr << "WavFileWriter: Failed to open file ("
|
Chris@674
|
47 << sf_strerror(m_file) << ")" << std::endl;
|
Chris@674
|
48 m_error = QString("Failed to open audio file '%1' for writing")
|
Chris@674
|
49 .arg(m_temp->getTemporaryFilename());
|
Chris@674
|
50 }
|
Chris@674
|
51 } catch (FileOperationFailed &f) {
|
Chris@674
|
52 m_error = f.what();
|
Chris@674
|
53 m_temp = 0;
|
Chris@674
|
54 m_file = 0;
|
Chris@174
|
55 }
|
Chris@148
|
56 }
|
Chris@148
|
57
|
Chris@148
|
58 WavFileWriter::~WavFileWriter()
|
Chris@148
|
59 {
|
Chris@174
|
60 if (m_file) close();
|
Chris@148
|
61 }
|
Chris@148
|
62
|
Chris@148
|
63 bool
|
Chris@148
|
64 WavFileWriter::isOK() const
|
Chris@148
|
65 {
|
Chris@148
|
66 return (m_error.isEmpty());
|
Chris@148
|
67 }
|
Chris@148
|
68
|
Chris@148
|
69 QString
|
Chris@148
|
70 WavFileWriter::getError() const
|
Chris@148
|
71 {
|
Chris@148
|
72 return m_error;
|
Chris@148
|
73 }
|
Chris@148
|
74
|
Chris@174
|
75 bool
|
Chris@174
|
76 WavFileWriter::writeModel(DenseTimeValueModel *source,
|
Chris@174
|
77 MultiSelection *selection)
|
Chris@148
|
78 {
|
Chris@674
|
79 if (!m_temp) {
|
Chris@674
|
80 m_error = QString("Failed to write model to audio file: No file open");
|
Chris@674
|
81 return false;
|
Chris@674
|
82 }
|
Chris@674
|
83
|
Chris@174
|
84 if (source->getChannelCount() != m_channels) {
|
Chris@687
|
85 DEBUG << "WavFileWriter::writeModel: Wrong number of channels ("
|
Chris@174
|
86 << source->getChannelCount() << " != " << m_channels << ")"
|
Chris@687
|
87 << endl;
|
Chris@174
|
88 m_error = QString("Failed to write model to audio file '%1'")
|
Chris@674
|
89 .arg(m_temp->getTemporaryFilename());
|
Chris@174
|
90 return false;
|
Chris@148
|
91 }
|
Chris@148
|
92
|
Chris@174
|
93 if (!m_file) {
|
Chris@174
|
94 m_error = QString("Failed to write model to audio file '%1': File not open")
|
Chris@674
|
95 .arg(m_temp->getTemporaryFilename());
|
Chris@174
|
96 return false;
|
Chris@174
|
97 }
|
Chris@148
|
98
|
Chris@174
|
99 bool ownSelection = false;
|
Chris@174
|
100 if (!selection) {
|
Chris@148
|
101 selection = new MultiSelection;
|
Chris@174
|
102 selection->setSelection(Selection(source->getStartFrame(),
|
Chris@174
|
103 source->getEndFrame()));
|
Chris@174
|
104 ownSelection = true;
|
Chris@148
|
105 }
|
Chris@148
|
106
|
Chris@148
|
107 size_t bs = 2048;
|
Chris@148
|
108 float *ub = new float[bs]; // uninterleaved buffer (one channel)
|
Chris@174
|
109 float *ib = new float[bs * m_channels]; // interleaved buffer
|
Chris@148
|
110
|
Chris@148
|
111 for (MultiSelection::SelectionList::iterator i =
|
Chris@148
|
112 selection->getSelections().begin();
|
Chris@148
|
113 i != selection->getSelections().end(); ++i) {
|
Chris@148
|
114
|
Chris@148
|
115 size_t f0(i->getStartFrame()), f1(i->getEndFrame());
|
Chris@148
|
116
|
Chris@148
|
117 for (size_t f = f0; f < f1; f += bs) {
|
Chris@148
|
118
|
Chris@148
|
119 size_t n = std::min(bs, f1 - f);
|
Chris@148
|
120
|
Chris@174
|
121 for (int c = 0; c < int(m_channels); ++c) {
|
Chris@300
|
122 source->getData(c, f, n, ub);
|
Chris@148
|
123 for (size_t i = 0; i < n; ++i) {
|
Chris@174
|
124 ib[i * m_channels + c] = ub[i];
|
Chris@148
|
125 }
|
Chris@148
|
126 }
|
Chris@148
|
127
|
Chris@174
|
128 sf_count_t written = sf_writef_float(m_file, ib, n);
|
Chris@148
|
129
|
Chris@148
|
130 if (written < n) {
|
Chris@148
|
131 m_error = QString("Only wrote %1 of %2 frames at file frame %3")
|
Chris@148
|
132 .arg(written).arg(n).arg(f);
|
Chris@148
|
133 break;
|
Chris@148
|
134 }
|
Chris@148
|
135 }
|
Chris@148
|
136 }
|
Chris@148
|
137
|
Chris@148
|
138 delete[] ub;
|
Chris@148
|
139 delete[] ib;
|
Chris@174
|
140 if (ownSelection) delete selection;
|
Chris@174
|
141
|
Chris@174
|
142 return isOK();
|
Chris@174
|
143 }
|
Chris@174
|
144
|
Chris@174
|
145 bool
|
Chris@174
|
146 WavFileWriter::writeSamples(float **samples, size_t count)
|
Chris@174
|
147 {
|
Chris@674
|
148 if (!m_temp) {
|
Chris@674
|
149 m_error = QString("Failed to write model to audio file: No file open");
|
Chris@674
|
150 return false;
|
Chris@674
|
151 }
|
Chris@674
|
152
|
Chris@174
|
153 if (!m_file) {
|
Chris@174
|
154 m_error = QString("Failed to write model to audio file '%1': File not open")
|
Chris@674
|
155 .arg(m_temp->getTemporaryFilename());
|
Chris@174
|
156 return false;
|
Chris@174
|
157 }
|
Chris@174
|
158
|
Chris@174
|
159 float *b = new float[count * m_channels];
|
Chris@174
|
160 for (size_t i = 0; i < count; ++i) {
|
Chris@174
|
161 for (size_t c = 0; c < m_channels; ++c) {
|
Chris@174
|
162 b[i * m_channels + c] = samples[c][i];
|
Chris@174
|
163 }
|
Chris@174
|
164 }
|
Chris@174
|
165
|
Chris@174
|
166 sf_count_t written = sf_writef_float(m_file, b, count);
|
Chris@174
|
167
|
Chris@174
|
168 delete[] b;
|
Chris@174
|
169
|
Chris@174
|
170 if (written < count) {
|
Chris@174
|
171 m_error = QString("Only wrote %1 of %2 frames")
|
Chris@174
|
172 .arg(written).arg(count);
|
Chris@174
|
173 }
|
Chris@174
|
174
|
Chris@174
|
175 return isOK();
|
Chris@174
|
176 }
|
Chris@174
|
177
|
Chris@174
|
178 bool
|
Chris@174
|
179 WavFileWriter::close()
|
Chris@174
|
180 {
|
Chris@174
|
181 if (m_file) {
|
Chris@174
|
182 sf_close(m_file);
|
Chris@174
|
183 m_file = 0;
|
Chris@174
|
184 }
|
Chris@674
|
185 m_temp->moveToTarget();
|
Chris@674
|
186 delete m_temp;
|
Chris@674
|
187 m_temp = 0;
|
Chris@174
|
188 return true;
|
Chris@148
|
189 }
|
Chris@148
|
190
|