annotate data/fileio/WavFileReader.cpp @ 1125:2c43f9904068 3.0-integration

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