Mercurial > hg > svcore
comparison data/fileio/WavFileWriter.cpp @ 1527:710e6250a401 zoom
Merge from default branch
author | Chris Cannam |
---|---|
date | Mon, 17 Sep 2018 13:51:14 +0100 |
parents | 8988b27ebf38 |
children | 70e172e6cc59 |
comparison
equal
deleted
inserted
replaced
1324:d4a28d1479a8 | 1527:710e6250a401 |
---|---|
17 | 17 |
18 #include "model/DenseTimeValueModel.h" | 18 #include "model/DenseTimeValueModel.h" |
19 #include "base/Selection.h" | 19 #include "base/Selection.h" |
20 #include "base/TempWriteFile.h" | 20 #include "base/TempWriteFile.h" |
21 #include "base/Exceptions.h" | 21 #include "base/Exceptions.h" |
22 #include "base/Debug.h" | |
23 | |
24 #include <bqvec/Allocators.h> | |
25 #include <bqvec/VectorOps.h> | |
22 | 26 |
23 #include <QFileInfo> | 27 #include <QFileInfo> |
24 | 28 |
25 #include <iostream> | 29 #include <iostream> |
26 #include <cmath> | 30 #include <cmath> |
31 #include <string> | |
27 | 32 |
28 using namespace std; | 33 using namespace std; |
29 | 34 |
30 WavFileWriter::WavFileWriter(QString path, | 35 WavFileWriter::WavFileWriter(QString path, |
31 sv_samplerate_t sampleRate, | 36 sv_samplerate_t sampleRate, |
32 int channels, | 37 int channels, |
33 FileWriteMode mode) : | 38 FileWriteMode mode) : |
34 m_path(path), | 39 m_path(path), |
35 m_sampleRate(sampleRate), | 40 m_sampleRate(sampleRate), |
36 m_channels(channels), | 41 m_channels(channels), |
39 { | 44 { |
40 SF_INFO fileInfo; | 45 SF_INFO fileInfo; |
41 | 46 |
42 int fileRate = int(round(m_sampleRate)); | 47 int fileRate = int(round(m_sampleRate)); |
43 if (m_sampleRate != sv_samplerate_t(fileRate)) { | 48 if (m_sampleRate != sv_samplerate_t(fileRate)) { |
44 cerr << "WavFileWriter: WARNING: Non-integer sample rate " | 49 SVCERR << "WavFileWriter: WARNING: Non-integer sample rate " |
45 << m_sampleRate << " presented, rounding to " << fileRate | 50 << m_sampleRate << " presented, rounding to " << fileRate |
46 << endl; | 51 << endl; |
47 } | 52 } |
48 fileInfo.samplerate = fileRate; | 53 fileInfo.samplerate = fileRate; |
49 fileInfo.channels = m_channels; | 54 fileInfo.channels = m_channels; |
50 fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT; | 55 fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT; |
51 | 56 |
52 try { | 57 try { |
58 QString writePath = m_path; | |
53 if (mode == WriteToTemporary) { | 59 if (mode == WriteToTemporary) { |
54 m_temp = new TempWriteFile(m_path); | 60 m_temp = new TempWriteFile(m_path); |
55 m_file = sf_open(m_temp->getTemporaryFilename().toLocal8Bit(), | 61 writePath = m_temp->getTemporaryFilename(); |
56 SFM_WRITE, &fileInfo); | 62 } |
57 if (!m_file) { | 63 #ifdef Q_OS_WIN |
58 cerr << "WavFileWriter: Failed to open file (" | 64 m_file = sf_wchar_open((LPCWSTR)writePath.utf16(), SFM_WRITE, &fileInfo); |
59 << sf_strerror(m_file) << ")" << endl; | 65 #else |
60 m_error = QString("Failed to open audio file '%1' for writing") | 66 m_file = sf_open(writePath.toLocal8Bit(), SFM_WRITE, &fileInfo); |
61 .arg(m_temp->getTemporaryFilename()); | 67 #endif |
68 if (!m_file) { | |
69 SVCERR << "WavFileWriter: Failed to create float-WAV file of " | |
70 << m_channels << " channels at rate " << fileRate << " (" | |
71 << sf_strerror(m_file) << ")" << endl; | |
72 m_error = QString("Failed to open audio file '%1' for writing") | |
73 .arg(writePath); | |
74 if (m_temp) { | |
75 delete m_temp; | |
76 m_temp = 0; | |
62 } | 77 } |
63 } else { | 78 } |
64 m_file = sf_open(m_path.toLocal8Bit(), SFM_WRITE, &fileInfo); | |
65 if (!m_file) { | |
66 cerr << "WavFileWriter: Failed to open file (" | |
67 << sf_strerror(m_file) << ")" << endl; | |
68 m_error = QString("Failed to open audio file '%1' for writing") | |
69 .arg(m_path); | |
70 } | |
71 } | |
72 } catch (FileOperationFailed &f) { | 79 } catch (FileOperationFailed &f) { |
73 m_error = f.what(); | 80 m_error = f.what(); |
74 m_temp = 0; | 81 m_temp = 0; |
75 m_file = 0; | 82 m_file = 0; |
76 } | 83 } |
117 } | 124 } |
118 | 125 |
119 if (!m_file) { | 126 if (!m_file) { |
120 m_error = QString("Failed to write model to audio file '%1': File not open") | 127 m_error = QString("Failed to write model to audio file '%1': File not open") |
121 .arg(getWriteFilename()); | 128 .arg(getWriteFilename()); |
122 return false; | 129 return false; |
123 } | 130 } |
124 | 131 |
125 bool ownSelection = false; | 132 bool ownSelection = false; |
126 if (!selection) { | 133 if (!selection) { |
127 selection = new MultiSelection; | 134 selection = new MultiSelection; |
128 selection->setSelection(Selection(source->getStartFrame(), | 135 selection->setSelection(Selection(source->getStartFrame(), |
129 source->getEndFrame())); | 136 source->getEndFrame())); |
130 ownSelection = true; | 137 ownSelection = true; |
131 } | 138 } |
132 | 139 |
133 sv_frame_t bs = 2048; | 140 sv_frame_t bs = 2048; |
134 | 141 |
135 for (MultiSelection::SelectionList::iterator i = | 142 for (MultiSelection::SelectionList::iterator i = |
136 selection->getSelections().begin(); | 143 selection->getSelections().begin(); |
137 i != selection->getSelections().end(); ++i) { | 144 i != selection->getSelections().end(); ++i) { |
138 | 145 |
139 sv_frame_t f0(i->getStartFrame()), f1(i->getEndFrame()); | 146 sv_frame_t f0(i->getStartFrame()), f1(i->getEndFrame()); |
140 | 147 |
141 for (sv_frame_t f = f0; f < f1; f += bs) { | 148 for (sv_frame_t f = f0; f < f1; f += bs) { |
142 | 149 |
143 sv_frame_t n = min(bs, f1 - f); | 150 sv_frame_t n = min(bs, f1 - f); |
144 vector<float> interleaved(n * m_channels, 0.f); | 151 floatvec_t interleaved(n * m_channels, 0.f); |
145 | 152 |
146 for (int c = 0; c < int(m_channels); ++c) { | 153 for (int c = 0; c < int(m_channels); ++c) { |
147 vector<float> chanbuf = source->getData(c, f, n); | 154 auto chanbuf = source->getData(c, f, n); |
148 for (int i = 0; in_range_for(chanbuf, i); ++i) { | 155 for (int i = 0; in_range_for(chanbuf, i); ++i) { |
149 interleaved[i * m_channels + c] = chanbuf[i]; | 156 interleaved[i * m_channels + c] = chanbuf[i]; |
150 } | 157 } |
151 } | 158 } |
152 | 159 |
153 sf_count_t written = sf_writef_float(m_file, interleaved.data(), n); | 160 sf_count_t written = sf_writef_float(m_file, interleaved.data(), n); |
154 | 161 |
155 if (written < n) { | 162 if (written < n) { |
156 m_error = QString("Only wrote %1 of %2 frames at file frame %3") | 163 m_error = QString("Only wrote %1 of %2 frames at file frame %3") |
157 .arg(written).arg(n).arg(f); | 164 .arg(written).arg(n).arg(f); |
158 break; | 165 break; |
159 } | 166 } |
160 } | 167 } |
161 } | 168 } |
162 | 169 |
163 if (ownSelection) delete selection; | 170 if (ownSelection) delete selection; |
164 | 171 |
165 return isOK(); | 172 return isOK(); |
166 } | 173 } |
167 | 174 |
168 bool | 175 bool |
169 WavFileWriter::writeSamples(float **samples, sv_frame_t count) | 176 WavFileWriter::writeSamples(const float *const *samples, sv_frame_t count) |
170 { | 177 { |
171 if (!m_file) { | 178 if (!m_file) { |
172 m_error = QString("Failed to write model to audio file '%1': File not open") | 179 m_error = QString("Failed to write model to audio file '%1': File not open") |
173 .arg(getWriteFilename()); | 180 .arg(getWriteFilename()); |
174 return false; | 181 return false; |
175 } | 182 } |
176 | 183 |
177 float *b = new float[count * m_channels]; | 184 float *b = new float[count * m_channels]; |
178 for (sv_frame_t i = 0; i < count; ++i) { | 185 for (sv_frame_t i = 0; i < count; ++i) { |
179 for (int c = 0; c < int(m_channels); ++c) { | 186 for (int c = 0; c < int(m_channels); ++c) { |
190 .arg(written).arg(count); | 197 .arg(written).arg(count); |
191 } | 198 } |
192 | 199 |
193 return isOK(); | 200 return isOK(); |
194 } | 201 } |
195 | 202 |
203 bool | |
204 WavFileWriter::putInterleavedFrames(const floatvec_t &frames) | |
205 { | |
206 sv_frame_t count = frames.size() / m_channels; | |
207 float **samples = | |
208 breakfastquay::allocate_channels<float>(m_channels, count); | |
209 breakfastquay::v_deinterleave | |
210 (samples, frames.data(), m_channels, int(count)); | |
211 bool result = writeSamples(samples, count); | |
212 breakfastquay::deallocate_channels(samples, m_channels); | |
213 return result; | |
214 } | |
215 | |
196 bool | 216 bool |
197 WavFileWriter::close() | 217 WavFileWriter::close() |
198 { | 218 { |
199 if (m_file) { | 219 if (m_file) { |
200 sf_close(m_file); | 220 sf_close(m_file); |