@@ -225,11 +225,43 @@ void Application::saveCapture()
225
225
return ;
226
226
227
227
QString filePath = QFileDialog::getSaveFileName (
228
- _window, tr (" Save data" ), QString (), tr (" Data files (*.dat)" ));
228
+ _window, tr (" Save data" ), QString (), tr (" Sound files (*.wav *.flac);; Data files (*.dat)" ));
229
229
if (filePath.isEmpty ())
230
230
return ;
231
231
232
- QFile file (filePath);
232
+ QString fileSuffix = QFileInfo (filePath).suffix ();
233
+ if (fileSuffix.compare (" wav" , Qt::CaseInsensitive) == 0 )
234
+ saveSoundFile (filePath, SF_FORMAT_WAV);
235
+ else if (fileSuffix.compare (" flac" , Qt::CaseInsensitive) == 0 )
236
+ saveSoundFile (filePath, SF_FORMAT_FLAC);
237
+ else
238
+ savePlotData (filePath);
239
+ }
240
+
241
+ void Application::saveSoundFile (const QString& path, int format)
242
+ {
243
+ const float *data = _captureBuffer.get ();
244
+ size_t size = _captureFill;
245
+ double sampleRate = jack_get_sample_rate (_client.get ());
246
+ double scaleFactor = 1.0 / _ui->internalGainVal ->value ();
247
+
248
+ SndfileHandle snd (path.toUtf8 ().data (), SFM_WRITE, format|SF_FORMAT_PCM_16, 1 , sampleRate);
249
+
250
+ for (size_t i = 0 ; i < size; ++i) {
251
+ float sample = scaleFactor * data[i];
252
+ sample = std::max (-1 .0f , std::min (+1 .0f , sample));
253
+ snd.write (&sample, 1 );
254
+ }
255
+
256
+ snd.writeSync ();
257
+
258
+ if (snd.error ())
259
+ QFile::remove (path);
260
+ }
261
+
262
+ void Application::savePlotData (const QString& path)
263
+ {
264
+ QFile file (path);
233
265
file.open (QFile::WriteOnly|QFile::Truncate);
234
266
QTextStream stream (&file);
235
267
0 commit comments