comparison data/fileio/WavFileWriter.cpp @ 1096:4d9816ba0ebe simple-fft-model

Rework audio file reader API to prefer using std containers
author Chris Cannam
date Mon, 15 Jun 2015 12:19:47 +0100
parents 0559f25b99f2
children 3aea4f7617bb
comparison
equal deleted inserted replaced
1095:b66734b5f806 1096:4d9816ba0ebe
22 22
23 #include <QFileInfo> 23 #include <QFileInfo>
24 24
25 #include <iostream> 25 #include <iostream>
26 #include <cmath> 26 #include <cmath>
27
28 using namespace std;
27 29
28 WavFileWriter::WavFileWriter(QString path, 30 WavFileWriter::WavFileWriter(QString path,
29 sv_samplerate_t sampleRate, 31 sv_samplerate_t sampleRate,
30 int channels, 32 int channels,
31 FileWriteMode mode) : 33 FileWriteMode mode) :
127 source->getEndFrame())); 129 source->getEndFrame()));
128 ownSelection = true; 130 ownSelection = true;
129 } 131 }
130 132
131 sv_frame_t bs = 2048; 133 sv_frame_t bs = 2048;
132 float *ub = new float[bs]; // uninterleaved buffer (one channel)
133 float *ib = new float[bs * m_channels]; // interleaved buffer
134 134
135 for (MultiSelection::SelectionList::iterator i = 135 for (MultiSelection::SelectionList::iterator i =
136 selection->getSelections().begin(); 136 selection->getSelections().begin();
137 i != selection->getSelections().end(); ++i) { 137 i != selection->getSelections().end(); ++i) {
138 138
139 sv_frame_t f0(i->getStartFrame()), f1(i->getEndFrame()); 139 sv_frame_t f0(i->getStartFrame()), f1(i->getEndFrame());
140 140
141 for (sv_frame_t f = f0; f < f1; f += bs) { 141 for (sv_frame_t f = f0; f < f1; f += bs) {
142 142
143 sv_frame_t n = std::min(bs, f1 - f); 143 sv_frame_t n = min(bs, f1 - f);
144 vector<float> interleaved(n * m_channels, 0.f);
144 145
145 for (int c = 0; c < int(m_channels); ++c) { 146 for (int c = 0; c < int(m_channels); ++c) {
146 source->getData(c, f, n, ub); 147 vector<float> chanbuf = source->getData(c, f, n);
147 for (int i = 0; i < n; ++i) { 148 for (int i = 0; in_range_for(chanbuf, i); ++i) {
148 ib[i * m_channels + c] = ub[i]; 149 interleaved[i * m_channels + c] = chanbuf[i];
149 } 150 }
150 } 151 }
151 152
152 sf_count_t written = sf_writef_float(m_file, ib, n); 153 sf_count_t written = sf_writef_float(m_file, interleaved.data(), n);
153 154
154 if (written < n) { 155 if (written < n) {
155 m_error = QString("Only wrote %1 of %2 frames at file frame %3") 156 m_error = QString("Only wrote %1 of %2 frames at file frame %3")
156 .arg(written).arg(n).arg(f); 157 .arg(written).arg(n).arg(f);
157 break; 158 break;
158 } 159 }
159 } 160 }
160 } 161 }
161 162
162 delete[] ub;
163 delete[] ib;
164 if (ownSelection) delete selection; 163 if (ownSelection) delete selection;
165 164
166 return isOK(); 165 return isOK();
167 } 166 }
168 167