annotate data/fileio/WavFileReader.cpp @ 1256:d8d6d01505ed 3.0-integration

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