annotate data/fileio/WavFileReader.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 c380e56c95f5
children 330bcc92507d
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 "WavFileReader.h"
Chris@148 17
Chris@1256 18 #include "base/HitCount.h"
Chris@1258 19 #include "base/Profiler.h"
Chris@1256 20
Chris@148 21 #include <iostream>
Chris@148 22
Chris@175 23 #include <QMutexLocker>
Chris@316 24 #include <QFileInfo>
Chris@175 25
Chris@1096 26 using namespace std;
Chris@1096 27
Chris@317 28 WavFileReader::WavFileReader(FileSource source, bool fileUpdating) :
Chris@1348 29 m_sndfile(0),
Chris@316 30 m_source(source),
Chris@316 31 m_path(source.getLocalFilename()),
Chris@1348 32 m_qfile(m_path),
Chris@823 33 m_seekable(false),
Chris@148 34 m_lastStart(0),
Chris@176 35 m_lastCount(0),
Chris@176 36 m_updating(fileUpdating)
Chris@148 37 {
Chris@148 38 m_frameCount = 0;
Chris@148 39 m_channelCount = 0;
Chris@148 40 m_sampleRate = 0;
Chris@148 41
Chris@148 42 m_fileInfo.format = 0;
Chris@148 43 m_fileInfo.frames = 0;
Chris@148 44
Chris@1348 45 if (!m_qfile.open(QIODevice::ReadOnly)) {
Chris@1343 46 SVDEBUG << "WavFileReader::initialize: Failed to open file at \""
Chris@1348 47 << m_path << "\"" << endl;
Chris@1348 48 m_error = QString("Failed to open audio file '%1'").arg(m_path);
Chris@1348 49 return;
Chris@1348 50 }
Chris@1348 51
Chris@1348 52 m_sndfile = sf_open_fd(m_qfile.handle(), SFM_READ, &m_fileInfo, false);
Chris@148 53
Chris@1348 54 if (!m_sndfile || (!fileUpdating && m_fileInfo.channels <= 0)) {
Chris@1348 55 SVDEBUG << "WavFileReader::initialize: Failed to open file at \""
Chris@1348 56 << m_path << "\" (" << sf_strerror(m_sndfile) << ")" << endl;
Chris@1348 57 if (m_sndfile) {
Chris@1343 58 m_error = QString("Couldn't load audio file '%1':\n%2")
Chris@1348 59 .arg(m_path).arg(sf_strerror(m_sndfile));
Chris@1343 60 } else {
Chris@1348 61 m_error = QString("Failed to open audio file '%1'").arg(m_path);
Chris@1343 62 }
Chris@1343 63 return;
Chris@148 64 }
Chris@148 65
Chris@187 66 if (m_fileInfo.channels > 0) {
Chris@823 67
Chris@187 68 m_frameCount = m_fileInfo.frames;
Chris@187 69 m_channelCount = m_fileInfo.channels;
Chris@187 70 m_sampleRate = m_fileInfo.samplerate;
Chris@823 71
Chris@823 72 m_seekable = (m_fileInfo.seekable != 0);
Chris@823 73
Chris@823 74 int type = m_fileInfo.format & SF_FORMAT_TYPEMASK;
Chris@1162 75 int subtype = m_fileInfo.format & SF_FORMAT_SUBMASK;
Chris@1162 76
Chris@823 77 if (type >= SF_FORMAT_FLAC || type >= SF_FORMAT_OGG) {
Chris@1162 78 // Our m_seekable reports whether a file is rapidly
Chris@1162 79 // seekable, so things like Ogg don't qualify. We
Chris@1162 80 // cautiously report every file type of "at least" the
Chris@1162 81 // historical period of Ogg or FLAC as non-seekable.
Chris@823 82 m_seekable = false;
Chris@1162 83 } else if (type == SF_FORMAT_WAV && subtype <= SF_FORMAT_DOUBLE) {
Chris@1162 84 // libsndfile 1.0.26 has a bug (subsequently fixed in the
Chris@1162 85 // repo) that causes all files to be reported as
Chris@1162 86 // non-seekable. We know that certain common file types
Chris@1162 87 // are definitely seekable so, again cautiously, identify
Chris@1162 88 // and mark those (basically only non-adaptive WAVs).
Chris@1162 89 m_seekable = true;
Chris@823 90 }
Chris@187 91 }
Chris@175 92
Chris@1279 93 SVDEBUG << "WavFileReader: Filename " << m_path << ", frame count " << m_frameCount << ", channel count " << m_channelCount << ", sample rate " << m_sampleRate << ", format " << m_fileInfo.format << ", seekable " << m_fileInfo.seekable << " adjusted to " << m_seekable << endl;
Chris@148 94 }
Chris@148 95
Chris@148 96 WavFileReader::~WavFileReader()
Chris@148 97 {
Chris@1348 98 if (m_sndfile) sf_close(m_sndfile);
Chris@148 99 }
Chris@148 100
Chris@148 101 void
Chris@175 102 WavFileReader::updateFrameCount()
Chris@175 103 {
Chris@175 104 QMutexLocker locker(&m_mutex);
Chris@175 105
Chris@1038 106 sv_frame_t prevCount = m_fileInfo.frames;
Chris@175 107
Chris@1348 108 if (m_sndfile) {
Chris@1348 109 sf_close(m_sndfile);
Chris@1348 110 m_sndfile = sf_open_fd(m_qfile.handle(), SFM_READ, &m_fileInfo, false);
Chris@1348 111 if (!m_sndfile || m_fileInfo.channels <= 0) {
Chris@1348 112 SVCERR << "WavFileReader::updateFrameCount: Failed to reopen file at \"" << m_path << "\" ("
Chris@1348 113 << sf_strerror(m_sndfile) << ")" << endl;
Chris@175 114 }
Chris@175 115 }
Chris@175 116
Chris@690 117 // SVDEBUG << "WavFileReader::updateFrameCount: now " << m_fileInfo.frames << endl;
Chris@175 118
Chris@176 119 m_frameCount = m_fileInfo.frames;
Chris@176 120
Chris@187 121 if (m_channelCount == 0) {
Chris@187 122 m_channelCount = m_fileInfo.channels;
Chris@187 123 m_sampleRate = m_fileInfo.samplerate;
Chris@187 124 }
Chris@187 125
Chris@258 126 if (m_frameCount != prevCount) {
Chris@258 127 emit frameCountChanged();
Chris@258 128 }
Chris@176 129 }
Chris@176 130
Chris@176 131 void
Chris@176 132 WavFileReader::updateDone()
Chris@176 133 {
Chris@176 134 updateFrameCount();
Chris@176 135 m_updating = false;
Chris@175 136 }
Chris@175 137
Chris@1326 138 floatvec_t
Chris@1041 139 WavFileReader::getInterleavedFrames(sv_frame_t start, sv_frame_t count) const
Chris@148 140 {
Chris@1256 141 static HitCount lastRead("WavFileReader: last read");
Chris@1256 142
Chris@1096 143 if (count == 0) return {};
Chris@175 144
Chris@175 145 QMutexLocker locker(&m_mutex);
Chris@175 146
Chris@1258 147 Profiler profiler("WavFileReader::getInterleavedFrames");
Chris@1258 148
Chris@1348 149 if (!m_sndfile || !m_channelCount) {
Chris@1096 150 return {};
Chris@175 151 }
Chris@148 152
Chris@1040 153 if (start >= m_fileInfo.frames) {
Chris@690 154 // SVDEBUG << "WavFileReader::getInterleavedFrames: " << start
Chris@687 155 // << " > " << m_fileInfo.frames << endl;
Chris@1343 156 return {};
Chris@148 157 }
Chris@148 158
Chris@1040 159 if (start + count > m_fileInfo.frames) {
Chris@1343 160 count = m_fileInfo.frames - start;
Chris@148 161 }
Chris@148 162
Chris@1096 163 // Because WaveFileModel::getSummaries() is called separately for
Chris@1096 164 // individual channels, it's quite common for us to be called
Chris@1096 165 // repeatedly for the same data. So this is worth cacheing.
Chris@1096 166 if (start == m_lastStart && count == m_lastCount) {
Chris@1256 167 lastRead.hit();
Chris@1096 168 return m_buffer;
Chris@1096 169 }
Chris@1256 170
Chris@1256 171 // We don't actually support partial cache reads, but let's use
Chris@1256 172 // the term partial to refer to any forward seek and consider a
Chris@1256 173 // backward seek to be a miss
Chris@1256 174 if (start >= m_lastStart) {
Chris@1256 175 lastRead.partial();
Chris@1256 176 } else {
Chris@1256 177 lastRead.miss();
Chris@1256 178 }
Chris@1096 179
Chris@1348 180 if (sf_seek(m_sndfile, start, SEEK_SET) < 0) {
Chris@1096 181 return {};
Chris@148 182 }
Chris@148 183
Chris@1326 184 floatvec_t data;
Chris@1096 185 sv_frame_t n = count * m_fileInfo.channels;
Chris@1096 186 data.resize(n);
Chris@1096 187
Chris@1096 188 m_lastStart = start;
Chris@1096 189 m_lastCount = count;
Chris@1096 190
Chris@1096 191 sf_count_t readCount = 0;
Chris@1348 192 if ((readCount = sf_readf_float(m_sndfile, data.data(), count)) < 0) {
Chris@1096 193 return {};
Chris@1096 194 }
Chris@1096 195
Chris@1103 196 m_buffer = data;
Chris@1096 197 return data;
Chris@148 198 }
Chris@148 199
Chris@157 200 void
Chris@1096 201 WavFileReader::getSupportedExtensions(set<QString> &extensions)
Chris@157 202 {
Chris@157 203 int count;
Chris@157 204
Chris@157 205 if (sf_command(0, SFC_GET_FORMAT_MAJOR_COUNT, &count, sizeof(count))) {
Chris@157 206 extensions.insert("wav");
Chris@157 207 extensions.insert("aiff");
Chris@316 208 extensions.insert("aifc");
Chris@157 209 extensions.insert("aif");
Chris@157 210 return;
Chris@157 211 }
Chris@157 212
Chris@157 213 SF_FORMAT_INFO info;
Chris@157 214 for (int i = 0; i < count; ++i) {
Chris@157 215 info.format = i;
Chris@157 216 if (!sf_command(0, SFC_GET_FORMAT_MAJOR, &info, sizeof(info))) {
Chris@783 217 QString ext = QString(info.extension).toLower();
Chris@783 218 extensions.insert(ext);
Chris@783 219 if (ext == "oga") {
Chris@783 220 // libsndfile is awfully proper, it says it only
Chris@783 221 // supports .oga but lots of Ogg audio files in the
Chris@783 222 // wild are .ogg and it will accept that
Chris@783 223 extensions.insert("ogg");
Chris@783 224 }
Chris@157 225 }
Chris@157 226 }
Chris@157 227 }
Chris@316 228
Chris@316 229 bool
Chris@316 230 WavFileReader::supportsExtension(QString extension)
Chris@316 231 {
Chris@1096 232 set<QString> extensions;
Chris@316 233 getSupportedExtensions(extensions);
Chris@316 234 return (extensions.find(extension.toLower()) != extensions.end());
Chris@316 235 }
Chris@316 236
Chris@316 237 bool
Chris@316 238 WavFileReader::supportsContentType(QString type)
Chris@316 239 {
Chris@316 240 return (type == "audio/x-wav" ||
Chris@316 241 type == "audio/x-aiff" ||
Chris@316 242 type == "audio/basic");
Chris@316 243 }
Chris@316 244
Chris@316 245 bool
Chris@317 246 WavFileReader::supports(FileSource &source)
Chris@316 247 {
Chris@316 248 return (supportsExtension(source.getExtension()) ||
Chris@316 249 supportsContentType(source.getContentType()));
Chris@316 250 }
Chris@316 251
Chris@316 252