Mercurial > hg > svcore
comparison data/fileio/WavFileWriter.cpp @ 1069:32ab6c48efaa
Merge from branch tonioni
author | Chris Cannam |
---|---|
date | Mon, 20 Apr 2015 09:11:34 +0100 |
parents | 0559f25b99f2 |
children | 4d9816ba0ebe |
comparison
equal
deleted
inserted
replaced
1036:682d64f05e72 | 1069:32ab6c48efaa |
---|---|
21 #include "base/Exceptions.h" | 21 #include "base/Exceptions.h" |
22 | 22 |
23 #include <QFileInfo> | 23 #include <QFileInfo> |
24 | 24 |
25 #include <iostream> | 25 #include <iostream> |
26 #include <cmath> | |
26 | 27 |
27 WavFileWriter::WavFileWriter(QString path, | 28 WavFileWriter::WavFileWriter(QString path, |
28 int sampleRate, | 29 sv_samplerate_t sampleRate, |
29 int channels, | 30 int channels, |
30 FileWriteMode mode) : | 31 FileWriteMode mode) : |
31 m_path(path), | 32 m_path(path), |
32 m_sampleRate(sampleRate), | 33 m_sampleRate(sampleRate), |
33 m_channels(channels), | 34 m_channels(channels), |
34 m_temp(0), | 35 m_temp(0), |
35 m_file(0) | 36 m_file(0) |
36 { | 37 { |
37 SF_INFO fileInfo; | 38 SF_INFO fileInfo; |
38 fileInfo.samplerate = m_sampleRate; | 39 |
40 int fileRate = int(round(m_sampleRate)); | |
41 if (m_sampleRate != sv_samplerate_t(fileRate)) { | |
42 cerr << "WavFileWriter: WARNING: Non-integer sample rate " | |
43 << m_sampleRate << " presented, rounding to " << fileRate | |
44 << endl; | |
45 } | |
46 fileInfo.samplerate = fileRate; | |
39 fileInfo.channels = m_channels; | 47 fileInfo.channels = m_channels; |
40 fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT; | 48 fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT; |
41 | 49 |
42 try { | 50 try { |
43 if (mode == WriteToTemporary) { | 51 if (mode == WriteToTemporary) { |
118 selection->setSelection(Selection(source->getStartFrame(), | 126 selection->setSelection(Selection(source->getStartFrame(), |
119 source->getEndFrame())); | 127 source->getEndFrame())); |
120 ownSelection = true; | 128 ownSelection = true; |
121 } | 129 } |
122 | 130 |
123 int bs = 2048; | 131 sv_frame_t bs = 2048; |
124 float *ub = new float[bs]; // uninterleaved buffer (one channel) | 132 float *ub = new float[bs]; // uninterleaved buffer (one channel) |
125 float *ib = new float[bs * m_channels]; // interleaved buffer | 133 float *ib = new float[bs * m_channels]; // interleaved buffer |
126 | 134 |
127 for (MultiSelection::SelectionList::iterator i = | 135 for (MultiSelection::SelectionList::iterator i = |
128 selection->getSelections().begin(); | 136 selection->getSelections().begin(); |
129 i != selection->getSelections().end(); ++i) { | 137 i != selection->getSelections().end(); ++i) { |
130 | 138 |
131 int f0(i->getStartFrame()), f1(i->getEndFrame()); | 139 sv_frame_t f0(i->getStartFrame()), f1(i->getEndFrame()); |
132 | 140 |
133 for (int f = f0; f < f1; f += bs) { | 141 for (sv_frame_t f = f0; f < f1; f += bs) { |
134 | 142 |
135 int n = std::min(bs, f1 - f); | 143 sv_frame_t n = std::min(bs, f1 - f); |
136 | 144 |
137 for (int c = 0; c < int(m_channels); ++c) { | 145 for (int c = 0; c < int(m_channels); ++c) { |
138 source->getData(c, f, n, ub); | 146 source->getData(c, f, n, ub); |
139 for (int i = 0; i < n; ++i) { | 147 for (int i = 0; i < n; ++i) { |
140 ib[i * m_channels + c] = ub[i]; | 148 ib[i * m_channels + c] = ub[i]; |
157 | 165 |
158 return isOK(); | 166 return isOK(); |
159 } | 167 } |
160 | 168 |
161 bool | 169 bool |
162 WavFileWriter::writeSamples(float **samples, int count) | 170 WavFileWriter::writeSamples(float **samples, sv_frame_t count) |
163 { | 171 { |
164 if (!m_file) { | 172 if (!m_file) { |
165 m_error = QString("Failed to write model to audio file '%1': File not open") | 173 m_error = QString("Failed to write model to audio file '%1': File not open") |
166 .arg(getWriteFilename()); | 174 .arg(getWriteFilename()); |
167 return false; | 175 return false; |
168 } | 176 } |
169 | 177 |
170 float *b = new float[count * m_channels]; | 178 float *b = new float[count * m_channels]; |
171 for (int i = 0; i < int(count); ++i) { | 179 for (sv_frame_t i = 0; i < count; ++i) { |
172 for (int c = 0; c < int(m_channels); ++c) { | 180 for (int c = 0; c < int(m_channels); ++c) { |
173 b[i * m_channels + c] = samples[c][i]; | 181 b[i * m_channels + c] = samples[c][i]; |
174 } | 182 } |
175 } | 183 } |
176 | 184 |
177 sf_count_t written = sf_writef_float(m_file, b, count); | 185 sv_frame_t written = sf_writef_float(m_file, b, count); |
178 | 186 |
179 delete[] b; | 187 delete[] b; |
180 | 188 |
181 if (written < int(count)) { | 189 if (written < count) { |
182 m_error = QString("Only wrote %1 of %2 frames") | 190 m_error = QString("Only wrote %1 of %2 frames") |
183 .arg(written).arg(count); | 191 .arg(written).arg(count); |
184 } | 192 } |
185 | 193 |
186 return isOK(); | 194 return isOK(); |