comparison data/fileio/AudioFileSizeEstimator.cpp @ 1098:329ddaf7415d simple-fft-model

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