annotate data/fileio/WavFileWriter.cpp @ 1047:26cf6d5251ec cxx11

Further dedicated-types fixes
author Chris Cannam
date Tue, 10 Mar 2015 17:02:52 +0000
parents a1cd5abcb38b
children 0559f25b99f2
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@148 22
Chris@148 23 #include <QFileInfo>
Chris@148 24
Chris@148 25 #include <iostream>
Chris@148 26
Chris@148 27 WavFileWriter::WavFileWriter(QString path,
Chris@1040 28 sv_samplerate_t sampleRate,
Chris@929 29 int channels,
Chris@684 30 FileWriteMode mode) :
Chris@148 31 m_path(path),
Chris@148 32 m_sampleRate(sampleRate),
Chris@174 33 m_channels(channels),
Chris@674 34 m_temp(0),
Chris@174 35 m_file(0)
Chris@148 36 {
Chris@174 37 SF_INFO fileInfo;
Chris@1040 38
Chris@1040 39 int fileRate = int(round(m_sampleRate));
Chris@1040 40 if (m_sampleRate != sv_samplerate_t(fileRate)) {
Chris@1040 41 cerr << "WavFileWriter: WARNING: Non-integer sample rate "
Chris@1040 42 << m_sampleRate << " presented, rounding to " << fileRate
Chris@1040 43 << endl;
Chris@1040 44 }
Chris@1040 45 fileInfo.samplerate = fileRate;
Chris@174 46 fileInfo.channels = m_channels;
Chris@174 47 fileInfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
Chris@674 48
Chris@674 49 try {
Chris@684 50 if (mode == WriteToTemporary) {
Chris@684 51 m_temp = new TempWriteFile(m_path);
Chris@684 52 m_file = sf_open(m_temp->getTemporaryFilename().toLocal8Bit(),
Chris@684 53 SFM_WRITE, &fileInfo);
Chris@684 54 if (!m_file) {
Chris@843 55 cerr << "WavFileWriter: Failed to open file ("
Chris@843 56 << sf_strerror(m_file) << ")" << endl;
Chris@684 57 m_error = QString("Failed to open audio file '%1' for writing")
Chris@684 58 .arg(m_temp->getTemporaryFilename());
Chris@684 59 }
Chris@684 60 } else {
Chris@684 61 m_file = sf_open(m_path.toLocal8Bit(), SFM_WRITE, &fileInfo);
Chris@684 62 if (!m_file) {
Chris@843 63 cerr << "WavFileWriter: Failed to open file ("
Chris@843 64 << sf_strerror(m_file) << ")" << endl;
Chris@684 65 m_error = QString("Failed to open audio file '%1' for writing")
Chris@684 66 .arg(m_path);
Chris@684 67 }
Chris@684 68 }
Chris@674 69 } catch (FileOperationFailed &f) {
Chris@674 70 m_error = f.what();
Chris@674 71 m_temp = 0;
Chris@674 72 m_file = 0;
Chris@174 73 }
Chris@148 74 }
Chris@148 75
Chris@148 76 WavFileWriter::~WavFileWriter()
Chris@148 77 {
Chris@174 78 if (m_file) close();
Chris@148 79 }
Chris@148 80
Chris@148 81 bool
Chris@148 82 WavFileWriter::isOK() const
Chris@148 83 {
Chris@148 84 return (m_error.isEmpty());
Chris@148 85 }
Chris@148 86
Chris@148 87 QString
Chris@148 88 WavFileWriter::getError() const
Chris@148 89 {
Chris@148 90 return m_error;
Chris@148 91 }
Chris@148 92
Chris@684 93 QString
Chris@684 94 WavFileWriter::getWriteFilename() const
Chris@684 95 {
Chris@684 96 if (m_temp) {
Chris@684 97 return m_temp->getTemporaryFilename();
Chris@684 98 } else {
Chris@684 99 return m_path;
Chris@684 100 }
Chris@684 101 }
Chris@684 102
Chris@174 103 bool
Chris@174 104 WavFileWriter::writeModel(DenseTimeValueModel *source,
Chris@174 105 MultiSelection *selection)
Chris@148 106 {
Chris@174 107 if (source->getChannelCount() != m_channels) {
Chris@690 108 SVDEBUG << "WavFileWriter::writeModel: Wrong number of channels ("
Chris@174 109 << source->getChannelCount() << " != " << m_channels << ")"
Chris@687 110 << endl;
Chris@174 111 m_error = QString("Failed to write model to audio file '%1'")
Chris@684 112 .arg(getWriteFilename());
Chris@174 113 return false;
Chris@148 114 }
Chris@148 115
Chris@174 116 if (!m_file) {
Chris@174 117 m_error = QString("Failed to write model to audio file '%1': File not open")
Chris@684 118 .arg(getWriteFilename());
Chris@174 119 return false;
Chris@174 120 }
Chris@148 121
Chris@174 122 bool ownSelection = false;
Chris@174 123 if (!selection) {
Chris@148 124 selection = new MultiSelection;
Chris@174 125 selection->setSelection(Selection(source->getStartFrame(),
Chris@174 126 source->getEndFrame()));
Chris@174 127 ownSelection = true;
Chris@148 128 }
Chris@148 129
Chris@1038 130 sv_frame_t bs = 2048;
Chris@148 131 float *ub = new float[bs]; // uninterleaved buffer (one channel)
Chris@174 132 float *ib = new float[bs * m_channels]; // interleaved buffer
Chris@148 133
Chris@148 134 for (MultiSelection::SelectionList::iterator i =
Chris@148 135 selection->getSelections().begin();
Chris@148 136 i != selection->getSelections().end(); ++i) {
Chris@148 137
Chris@1038 138 sv_frame_t f0(i->getStartFrame()), f1(i->getEndFrame());
Chris@148 139
Chris@1038 140 for (sv_frame_t f = f0; f < f1; f += bs) {
Chris@148 141
Chris@1038 142 sv_frame_t n = std::min(bs, f1 - f);
Chris@148 143
Chris@174 144 for (int c = 0; c < int(m_channels); ++c) {
Chris@300 145 source->getData(c, f, n, ub);
Chris@929 146 for (int i = 0; i < n; ++i) {
Chris@174 147 ib[i * m_channels + c] = ub[i];
Chris@148 148 }
Chris@148 149 }
Chris@148 150
Chris@174 151 sf_count_t written = sf_writef_float(m_file, ib, n);
Chris@148 152
Chris@148 153 if (written < n) {
Chris@148 154 m_error = QString("Only wrote %1 of %2 frames at file frame %3")
Chris@148 155 .arg(written).arg(n).arg(f);
Chris@148 156 break;
Chris@148 157 }
Chris@148 158 }
Chris@148 159 }
Chris@148 160
Chris@148 161 delete[] ub;
Chris@148 162 delete[] ib;
Chris@174 163 if (ownSelection) delete selection;
Chris@174 164
Chris@174 165 return isOK();
Chris@174 166 }
Chris@174 167
Chris@174 168 bool
Chris@1038 169 WavFileWriter::writeSamples(float **samples, sv_frame_t count)
Chris@174 170 {
Chris@174 171 if (!m_file) {
Chris@174 172 m_error = QString("Failed to write model to audio file '%1': File not open")
Chris@684 173 .arg(getWriteFilename());
Chris@174 174 return false;
Chris@174 175 }
Chris@174 176
Chris@174 177 float *b = new float[count * m_channels];
Chris@1038 178 for (sv_frame_t i = 0; i < count; ++i) {
Chris@929 179 for (int c = 0; c < int(m_channels); ++c) {
Chris@174 180 b[i * m_channels + c] = samples[c][i];
Chris@174 181 }
Chris@174 182 }
Chris@174 183
Chris@1038 184 sv_frame_t written = sf_writef_float(m_file, b, count);
Chris@174 185
Chris@174 186 delete[] b;
Chris@174 187
Chris@1038 188 if (written < count) {
Chris@174 189 m_error = QString("Only wrote %1 of %2 frames")
Chris@174 190 .arg(written).arg(count);
Chris@174 191 }
Chris@174 192
Chris@174 193 return isOK();
Chris@174 194 }
Chris@174 195
Chris@174 196 bool
Chris@174 197 WavFileWriter::close()
Chris@174 198 {
Chris@174 199 if (m_file) {
Chris@174 200 sf_close(m_file);
Chris@174 201 m_file = 0;
Chris@174 202 }
Chris@684 203 if (m_temp) {
Chris@684 204 m_temp->moveToTarget();
Chris@684 205 delete m_temp;
Chris@684 206 m_temp = 0;
Chris@684 207 }
Chris@174 208 return true;
Chris@148 209 }
Chris@148 210