annotate data/fileio/WavFileReader.cpp @ 1169:134ce7667256

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