comparison data/fileio/WavFileWriter.cpp @ 1348:b3cb0edc25cd 3.0-integration

Update WAV/MP3/BZipFileDevice code to avoid using local 8-bit encoding
author Chris Cannam
date Fri, 06 Jan 2017 16:40:11 +0000
parents 54af1e21705c
children 1bc6f70cb4c7
comparison
equal deleted inserted replaced
1347:281a8c9d4886 1348:b3cb0edc25cd
33 FileWriteMode mode) : 33 FileWriteMode mode) :
34 m_path(path), 34 m_path(path),
35 m_sampleRate(sampleRate), 35 m_sampleRate(sampleRate),
36 m_channels(channels), 36 m_channels(channels),
37 m_temp(0), 37 m_temp(0),
38 m_file(0) 38 m_sndfile(0),
39 m_qfile(0)
39 { 40 {
40 SF_INFO fileInfo; 41 SF_INFO fileInfo;
41 42
42 int fileRate = int(round(m_sampleRate)); 43 int fileRate = int(round(m_sampleRate));
43 if (m_sampleRate != sv_samplerate_t(fileRate)) { 44 if (m_sampleRate != sv_samplerate_t(fileRate)) {
44 cerr << "WavFileWriter: WARNING: Non-integer sample rate " 45 SVCERR << "WavFileWriter: WARNING: Non-integer sample rate "
45 << m_sampleRate << " presented, rounding to " << fileRate 46 << m_sampleRate << " presented, rounding to " << fileRate
46 << endl; 47 << endl;
47 } 48 }
48 fileInfo.samplerate = fileRate; 49 fileInfo.samplerate = fileRate;
49 fileInfo.channels = m_channels; 50 fileInfo.channels = m_channels;
50 fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT; 51 fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
51 52
52 try { 53 try {
53 if (mode == WriteToTemporary) { 54 if (mode == WriteToTemporary) {
54 m_temp = new TempWriteFile(m_path); 55 m_temp = new TempWriteFile(m_path);
55 m_file = sf_open(m_temp->getTemporaryFilename().toLocal8Bit(), 56 m_qfile = new QFile(m_temp->getTemporaryFilename());
56 SFM_WRITE, &fileInfo); 57 } else {
57 if (!m_file) { 58 m_qfile = new QFile(m_path);
58 cerr << "WavFileWriter: Failed to open file (" 59 }
59 << sf_strerror(m_file) << ")" << endl; 60 if (!m_qfile->open(QIODevice::WriteOnly)) {
61 SVCERR << "WavFileWriter: Failed to open file for writing" << endl;
62 m_error = QString("Failed to open audio file '%1' for writing")
63 .arg(m_qfile->fileName());
64 } else {
65 m_sndfile = sf_open_fd(m_qfile->handle(),
66 SFM_WRITE, &fileInfo, false);
67 if (!m_sndfile) {
68 SVCERR << "WavFileWriter: Failed to open file ("
69 << sf_strerror(m_sndfile) << ")" << endl;
60 m_error = QString("Failed to open audio file '%1' for writing") 70 m_error = QString("Failed to open audio file '%1' for writing")
61 .arg(m_temp->getTemporaryFilename()); 71 .arg(m_qfile->fileName());
62 } 72 }
63 } else { 73 }
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) { 74 } catch (FileOperationFailed &f) {
73 m_error = f.what(); 75 m_error = f.what();
74 m_temp = 0; 76 m_temp = 0;
75 m_file = 0; 77 m_qfile = 0;
78 m_sndfile = 0;
76 } 79 }
77 } 80 }
78 81
79 WavFileWriter::~WavFileWriter() 82 WavFileWriter::~WavFileWriter()
80 { 83 {
81 if (m_file) close(); 84 if (m_sndfile) close();
82 } 85 }
83 86
84 bool 87 bool
85 WavFileWriter::isOK() const 88 WavFileWriter::isOK() const
86 { 89 {
114 m_error = QString("Failed to write model to audio file '%1'") 117 m_error = QString("Failed to write model to audio file '%1'")
115 .arg(getWriteFilename()); 118 .arg(getWriteFilename());
116 return false; 119 return false;
117 } 120 }
118 121
119 if (!m_file) { 122 if (!m_sndfile) {
120 m_error = QString("Failed to write model to audio file '%1': File not open") 123 m_error = QString("Failed to write model to audio file '%1': File not open")
121 .arg(getWriteFilename()); 124 .arg(getWriteFilename());
122 return false; 125 return false;
123 } 126 }
124 127
148 for (int i = 0; in_range_for(chanbuf, i); ++i) { 151 for (int i = 0; in_range_for(chanbuf, i); ++i) {
149 interleaved[i * m_channels + c] = chanbuf[i]; 152 interleaved[i * m_channels + c] = chanbuf[i];
150 } 153 }
151 } 154 }
152 155
153 sf_count_t written = sf_writef_float(m_file, interleaved.data(), n); 156 sf_count_t written = sf_writef_float(m_sndfile, interleaved.data(), n);
154 157
155 if (written < n) { 158 if (written < n) {
156 m_error = QString("Only wrote %1 of %2 frames at file frame %3") 159 m_error = QString("Only wrote %1 of %2 frames at file frame %3")
157 .arg(written).arg(n).arg(f); 160 .arg(written).arg(n).arg(f);
158 break; 161 break;
166 } 169 }
167 170
168 bool 171 bool
169 WavFileWriter::writeSamples(const float *const *samples, sv_frame_t count) 172 WavFileWriter::writeSamples(const float *const *samples, sv_frame_t count)
170 { 173 {
171 if (!m_file) { 174 if (!m_sndfile) {
172 m_error = QString("Failed to write model to audio file '%1': File not open") 175 m_error = QString("Failed to write model to audio file '%1': File not open")
173 .arg(getWriteFilename()); 176 .arg(getWriteFilename());
174 return false; 177 return false;
175 } 178 }
176 179
179 for (int c = 0; c < int(m_channels); ++c) { 182 for (int c = 0; c < int(m_channels); ++c) {
180 b[i * m_channels + c] = samples[c][i]; 183 b[i * m_channels + c] = samples[c][i];
181 } 184 }
182 } 185 }
183 186
184 sv_frame_t written = sf_writef_float(m_file, b, count); 187 sv_frame_t written = sf_writef_float(m_sndfile, b, count);
185 188
186 delete[] b; 189 delete[] b;
187 190
188 if (written < count) { 191 if (written < count) {
189 m_error = QString("Only wrote %1 of %2 frames") 192 m_error = QString("Only wrote %1 of %2 frames")
194 } 197 }
195 198
196 bool 199 bool
197 WavFileWriter::close() 200 WavFileWriter::close()
198 { 201 {
199 if (m_file) { 202 if (m_sndfile) {
200 sf_close(m_file); 203 sf_close(m_sndfile);
201 m_file = 0; 204 m_sndfile = 0;
202 } 205 }
206
207 delete m_qfile;
208 m_qfile = 0;
209
203 if (m_temp) { 210 if (m_temp) {
204 m_temp->moveToTarget(); 211 m_temp->moveToTarget();
205 delete m_temp; 212 delete m_temp;
206 m_temp = 0; 213 m_temp = 0;
207 } 214 }
215
208 return true; 216 return true;
209 } 217 }
210 218