annotate data/fileio/WavFileWriter.cpp @ 1508:b837ccdd4946 import-audio-data

Catch failure to create model, e.g. because 0 channels for wave-file model
author Chris Cannam
date Tue, 04 Sep 2018 14:11:10 +0100
parents 48e9f538e6e9
children 954d0cf29ca7
rev   line source
Chris@148 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@148 2
Chris@148 3 /*
Chris@148 4 Sonic Visualiser
Chris@148 5 An audio file viewer and annotation editor.
Chris@148 6 Centre for Digital Music, Queen Mary, University of London.
Chris@202 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@148 8
Chris@148 9 This program is free software; you can redistribute it and/or
Chris@148 10 modify it under the terms of the GNU General Public License as
Chris@148 11 published by the Free Software Foundation; either version 2 of the
Chris@148 12 License, or (at your option) any later version. See the file
Chris@148 13 COPYING included with this distribution for more information.
Chris@148 14 */
Chris@148 15
Chris@148 16 #include "WavFileWriter.h"
Chris@148 17
Chris@148 18 #include "model/DenseTimeValueModel.h"
Chris@148 19 #include "base/Selection.h"
Chris@674 20 #include "base/TempWriteFile.h"
Chris@674 21 #include "base/Exceptions.h"
Chris@1428 22 #include "base/Debug.h"
Chris@148 23
Chris@148 24 #include <QFileInfo>
Chris@148 25
Chris@148 26 #include <iostream>
Chris@1055 27 #include <cmath>
Chris@1428 28 #include <string>
Chris@148 29
Chris@1096 30 using namespace std;
Chris@1096 31
Chris@148 32 WavFileWriter::WavFileWriter(QString path,
Chris@1350 33 sv_samplerate_t sampleRate,
Chris@929 34 int channels,
Chris@684 35 FileWriteMode mode) :
Chris@148 36 m_path(path),
Chris@148 37 m_sampleRate(sampleRate),
Chris@174 38 m_channels(channels),
Chris@674 39 m_temp(0),
Chris@1350 40 m_file(0)
Chris@148 41 {
Chris@174 42 SF_INFO fileInfo;
Chris@1040 43
Chris@1040 44 int fileRate = int(round(m_sampleRate));
Chris@1040 45 if (m_sampleRate != sv_samplerate_t(fileRate)) {
Chris@1428 46 SVCERR << "WavFileWriter: WARNING: Non-integer sample rate "
Chris@1350 47 << m_sampleRate << " presented, rounding to " << fileRate
Chris@1350 48 << endl;
Chris@1040 49 }
Chris@1040 50 fileInfo.samplerate = fileRate;
Chris@174 51 fileInfo.channels = m_channels;
Chris@174 52 fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
Chris@674 53
Chris@674 54 try {
Chris@1350 55 QString writePath = m_path;
Chris@684 56 if (mode == WriteToTemporary) {
Chris@684 57 m_temp = new TempWriteFile(m_path);
Chris@1350 58 writePath = m_temp->getTemporaryFilename();
Chris@1348 59 }
Chris@1350 60 #ifdef Q_OS_WIN
Chris@1350 61 m_file = sf_wchar_open((LPCWSTR)writePath.utf16(), SFM_WRITE, &fileInfo);
Chris@1350 62 #else
Chris@1350 63 m_file = sf_open(writePath.toLocal8Bit(), SFM_WRITE, &fileInfo);
Chris@1350 64 #endif
Chris@1350 65 if (!m_file) {
Chris@1508 66 SVCERR << "WavFileWriter: Failed to create float-WAV file of "
Chris@1508 67 << m_channels << " channels at rate " << fileRate << " ("
Chris@1508 68 << sf_strerror(m_file) << ")" << endl;
Chris@1348 69 m_error = QString("Failed to open audio file '%1' for writing")
Chris@1350 70 .arg(writePath);
Chris@1508 71 if (m_temp) {
Chris@1508 72 delete m_temp;
Chris@1508 73 m_temp = 0;
Chris@1508 74 }
Chris@1348 75 }
Chris@674 76 } catch (FileOperationFailed &f) {
Chris@674 77 m_error = f.what();
Chris@674 78 m_temp = 0;
Chris@1350 79 m_file = 0;
Chris@174 80 }
Chris@148 81 }
Chris@148 82
Chris@148 83 WavFileWriter::~WavFileWriter()
Chris@148 84 {
Chris@1350 85 if (m_file) close();
Chris@148 86 }
Chris@148 87
Chris@148 88 bool
Chris@148 89 WavFileWriter::isOK() const
Chris@148 90 {
Chris@148 91 return (m_error.isEmpty());
Chris@148 92 }
Chris@148 93
Chris@148 94 QString
Chris@148 95 WavFileWriter::getError() const
Chris@148 96 {
Chris@148 97 return m_error;
Chris@148 98 }
Chris@148 99
Chris@684 100 QString
Chris@684 101 WavFileWriter::getWriteFilename() const
Chris@684 102 {
Chris@684 103 if (m_temp) {
Chris@684 104 return m_temp->getTemporaryFilename();
Chris@684 105 } else {
Chris@684 106 return m_path;
Chris@684 107 }
Chris@684 108 }
Chris@684 109
Chris@174 110 bool
Chris@174 111 WavFileWriter::writeModel(DenseTimeValueModel *source,
Chris@174 112 MultiSelection *selection)
Chris@148 113 {
Chris@174 114 if (source->getChannelCount() != m_channels) {
Chris@690 115 SVDEBUG << "WavFileWriter::writeModel: Wrong number of channels ("
Chris@174 116 << source->getChannelCount() << " != " << m_channels << ")"
Chris@687 117 << endl;
Chris@174 118 m_error = QString("Failed to write model to audio file '%1'")
Chris@684 119 .arg(getWriteFilename());
Chris@174 120 return false;
Chris@148 121 }
Chris@148 122
Chris@1350 123 if (!m_file) {
Chris@174 124 m_error = QString("Failed to write model to audio file '%1': File not open")
Chris@684 125 .arg(getWriteFilename());
Chris@1350 126 return false;
Chris@174 127 }
Chris@148 128
Chris@174 129 bool ownSelection = false;
Chris@174 130 if (!selection) {
Chris@1350 131 selection = new MultiSelection;
Chris@1350 132 selection->setSelection(Selection(source->getStartFrame(),
Chris@1350 133 source->getEndFrame()));
Chris@174 134 ownSelection = true;
Chris@148 135 }
Chris@148 136
Chris@1038 137 sv_frame_t bs = 2048;
Chris@148 138
Chris@148 139 for (MultiSelection::SelectionList::iterator i =
Chris@1350 140 selection->getSelections().begin();
Chris@1350 141 i != selection->getSelections().end(); ++i) {
Chris@1429 142
Chris@1350 143 sv_frame_t f0(i->getStartFrame()), f1(i->getEndFrame());
Chris@148 144
Chris@1350 145 for (sv_frame_t f = f0; f < f1; f += bs) {
Chris@1429 146
Chris@1350 147 sv_frame_t n = min(bs, f1 - f);
Chris@1326 148 floatvec_t interleaved(n * m_channels, 0.f);
Chris@148 149
Chris@1350 150 for (int c = 0; c < int(m_channels); ++c) {
Chris@1326 151 auto chanbuf = source->getData(c, f, n);
Chris@1350 152 for (int i = 0; in_range_for(chanbuf, i); ++i) {
Chris@1350 153 interleaved[i * m_channels + c] = chanbuf[i];
Chris@1350 154 }
Chris@1350 155 }
Chris@148 156
Chris@1350 157 sf_count_t written = sf_writef_float(m_file, interleaved.data(), n);
Chris@148 158
Chris@1350 159 if (written < n) {
Chris@1350 160 m_error = QString("Only wrote %1 of %2 frames at file frame %3")
Chris@1350 161 .arg(written).arg(n).arg(f);
Chris@1350 162 break;
Chris@1350 163 }
Chris@1350 164 }
Chris@148 165 }
Chris@148 166
Chris@174 167 if (ownSelection) delete selection;
Chris@174 168
Chris@174 169 return isOK();
Chris@174 170 }
Chris@1429 171
Chris@174 172 bool
Chris@1325 173 WavFileWriter::writeSamples(const float *const *samples, sv_frame_t count)
Chris@174 174 {
Chris@1350 175 if (!m_file) {
Chris@174 176 m_error = QString("Failed to write model to audio file '%1': File not open")
Chris@684 177 .arg(getWriteFilename());
Chris@1350 178 return false;
Chris@174 179 }
Chris@174 180
Chris@174 181 float *b = new float[count * m_channels];
Chris@1038 182 for (sv_frame_t i = 0; i < count; ++i) {
Chris@929 183 for (int c = 0; c < int(m_channels); ++c) {
Chris@174 184 b[i * m_channels + c] = samples[c][i];
Chris@174 185 }
Chris@174 186 }
Chris@174 187
Chris@1350 188 sv_frame_t written = sf_writef_float(m_file, b, count);
Chris@174 189
Chris@174 190 delete[] b;
Chris@174 191
Chris@1038 192 if (written < count) {
Chris@174 193 m_error = QString("Only wrote %1 of %2 frames")
Chris@174 194 .arg(written).arg(count);
Chris@174 195 }
Chris@174 196
Chris@174 197 return isOK();
Chris@174 198 }
Chris@174 199
Chris@174 200 bool
Chris@174 201 WavFileWriter::close()
Chris@174 202 {
Chris@1350 203 if (m_file) {
Chris@1350 204 sf_close(m_file);
Chris@1350 205 m_file = 0;
Chris@174 206 }
Chris@684 207 if (m_temp) {
Chris@684 208 m_temp->moveToTarget();
Chris@684 209 delete m_temp;
Chris@684 210 m_temp = 0;
Chris@684 211 }
Chris@174 212 return true;
Chris@148 213 }
Chris@148 214