annotate data/fileio/AudioFileSizeEstimator.cpp @ 1346:75ad55315db4 3.0-integration

More work on getting tests (especially file encoding ones) running on Windows. Various problems here to do with interaction with test filenames in Hg repos
author Chris Cannam
date Fri, 06 Jan 2017 15:44:55 +0000
parents c0fece5e7755
children aadfb395e933
rev   line source
Chris@1098 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1098 2
Chris@1098 3 /*
Chris@1098 4 Sonic Visualiser
Chris@1098 5 An audio file viewer and annotation editor.
Chris@1098 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1098 7
Chris@1098 8 This program is free software; you can redistribute it and/or
Chris@1098 9 modify it under the terms of the GNU General Public License as
Chris@1098 10 published by the Free Software Foundation; either version 2 of the
Chris@1098 11 License, or (at your option) any later version. See the file
Chris@1098 12 COPYING included with this distribution for more information.
Chris@1098 13 */
Chris@1098 14
Chris@1098 15 #include "AudioFileSizeEstimator.h"
Chris@1098 16
Chris@1098 17 #include "WavFileReader.h"
Chris@1098 18
Chris@1098 19 #include <QFile>
Chris@1098 20
Chris@1342 21 #include "base/Debug.h"
Chris@1104 22
Chris@1098 23 sv_frame_t
Chris@1098 24 AudioFileSizeEstimator::estimate(FileSource source,
Chris@1341 25 sv_samplerate_t targetRate)
Chris@1098 26 {
Chris@1098 27 sv_frame_t estimate = 0;
Chris@1098 28
Chris@1342 29 SVDEBUG << "AudioFileSizeEstimator: Sample count estimate requested for file \""
Chris@1342 30 << source.getLocalFilename() << "\"" << endl;
Chris@1342 31
Chris@1098 32 // Most of our file readers don't know the sample count until
Chris@1098 33 // after they've finished decoding. This is an exception:
Chris@1098 34
Chris@1098 35 WavFileReader *reader = new WavFileReader(source);
Chris@1098 36 if (reader->isOK() &&
Chris@1341 37 reader->getChannelCount() > 0 &&
Chris@1341 38 reader->getFrameCount() > 0) {
Chris@1341 39 sv_frame_t samples =
Chris@1341 40 reader->getFrameCount() * reader->getChannelCount();
Chris@1341 41 sv_samplerate_t rate = reader->getSampleRate();
Chris@1341 42 if (targetRate != 0.0 && targetRate != rate) {
Chris@1341 43 samples = sv_frame_t(double(samples) * targetRate / rate);
Chris@1341 44 }
Chris@1341 45 delete reader;
Chris@1342 46 SVDEBUG << "AudioFileSizeEstimator: WAV file reader accepts this file, reports "
Chris@1342 47 << samples << " samples" << endl;
Chris@1341 48 estimate = samples;
Chris@1342 49 } else {
Chris@1342 50 SVDEBUG << "AudioFileSizeEstimator: WAV file reader doesn't like this file, "
Chris@1342 51 << "estimating from file size and extension instead" << endl;
Chris@1098 52 }
Chris@1098 53
Chris@1098 54 if (estimate == 0) {
Chris@1098 55
Chris@1341 56 // The remainder just makes an estimate based on the file size
Chris@1341 57 // and extension. We don't even know its sample rate at this
Chris@1341 58 // point, so the following is a wild guess.
Chris@1341 59
Chris@1341 60 double rateRatio = 1.0;
Chris@1341 61 if (targetRate != 0.0) {
Chris@1341 62 rateRatio = targetRate / 44100.0;
Chris@1341 63 }
Chris@1098 64
Chris@1341 65 QString extension = source.getExtension();
Chris@1098 66
Chris@1341 67 source.waitForData();
Chris@1341 68 if (!source.isOK()) return 0;
Chris@1098 69
Chris@1341 70 sv_frame_t sz = 0;
Chris@1342 71
Chris@1341 72 {
Chris@1341 73 QFile f(source.getLocalFilename());
Chris@1341 74 if (f.open(QFile::ReadOnly)) {
Chris@1342 75 SVDEBUG << "AudioFileSizeEstimator: opened file, size is "
Chris@1342 76 << f.size() << endl;
Chris@1341 77 sz = f.size();
Chris@1341 78 f.close();
Chris@1341 79 }
Chris@1341 80 }
Chris@1098 81
Chris@1341 82 if (extension == "ogg" || extension == "oga" ||
Chris@1341 83 extension == "m4a" || extension == "mp3" ||
Chris@1341 84 extension == "wma") {
Chris@1098 85
Chris@1341 86 // Usually a lossy file. Compression ratios can vary
Chris@1341 87 // dramatically, but don't usually exceed about 20x compared
Chris@1341 88 // to 16-bit PCM (e.g. a 128kbps mp3 has 11x ratio over WAV at
Chris@1341 89 // 44.1kHz). We can estimate the number of samples to be file
Chris@1341 90 // size x 20, divided by 2 as we're comparing with 16-bit PCM.
Chris@1098 91
Chris@1341 92 estimate = sv_frame_t(double(sz) * 10 * rateRatio);
Chris@1341 93 }
Chris@1098 94
Chris@1341 95 if (extension == "flac") {
Chris@1341 96
Chris@1341 97 // FLAC usually takes up a bit more than half the space of
Chris@1341 98 // 16-bit PCM. So the number of 16-bit samples is roughly the
Chris@1341 99 // same as the file size in bytes. As above, let's be
Chris@1341 100 // conservative.
Chris@1098 101
Chris@1341 102 estimate = sv_frame_t(double(sz) * 1.2 * rateRatio);
Chris@1341 103 }
Chris@1098 104
Chris@1342 105 SVDEBUG << "AudioFileSizeEstimator: for extension \""
Chris@1342 106 << extension << "\", estimate = " << estimate << " samples" << endl;
Chris@1098 107 }
Chris@1098 108
Chris@1098 109 return estimate;
Chris@1098 110 }
Chris@1098 111