Mercurial > hg > svcore
comparison data/fileio/AudioFileSizeEstimator.cpp @ 1126:39019ce29178 tony-2.0-integration
Merge through to branch for Tony 2.0
author | Chris Cannam |
---|---|
date | Thu, 20 Aug 2015 14:54:21 +0100 |
parents | 393134235fa0 |
children | 513e4d67d8df |
comparison
equal
deleted
inserted
replaced
1119:e22bfe8ca248 | 1126:39019ce29178 |
---|---|
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 //#define DEBUG_AUDIO_FILE_SIZE_ESTIMATOR 1 | |
22 | |
23 sv_frame_t | |
24 AudioFileSizeEstimator::estimate(FileSource source, | |
25 sv_samplerate_t targetRate) | |
26 { | |
27 sv_frame_t estimate = 0; | |
28 | |
29 // Most of our file readers don't know the sample count until | |
30 // after they've finished decoding. This is an exception: | |
31 | |
32 WavFileReader *reader = new WavFileReader(source); | |
33 if (reader->isOK() && | |
34 reader->getChannelCount() > 0 && | |
35 reader->getFrameCount() > 0) { | |
36 sv_frame_t samples = | |
37 reader->getFrameCount() * reader->getChannelCount(); | |
38 sv_samplerate_t rate = reader->getSampleRate(); | |
39 if (targetRate != 0.0 && targetRate != rate) { | |
40 samples = sv_frame_t(double(samples) * targetRate / rate); | |
41 } | |
42 delete reader; | |
43 estimate = samples; | |
44 } | |
45 | |
46 if (estimate == 0) { | |
47 | |
48 // The remainder just makes an estimate based on the file size | |
49 // and extension. We don't even know its sample rate at this | |
50 // point, so the following is a wild guess. | |
51 | |
52 double rateRatio = 1.0; | |
53 if (targetRate != 0.0) { | |
54 rateRatio = targetRate / 44100.0; | |
55 } | |
56 | |
57 QString extension = source.getExtension(); | |
58 | |
59 source.waitForData(); | |
60 if (!source.isOK()) return 0; | |
61 | |
62 sv_frame_t sz = 0; | |
63 { | |
64 QFile f(source.getLocalFilename()); | |
65 if (f.open(QFile::ReadOnly)) { | |
66 #ifdef DEBUG_AUDIO_FILE_SIZE_ESTIMATOR | |
67 cerr << "opened file, size is " << f.size() << endl; | |
68 #endif | |
69 sz = f.size(); | |
70 f.close(); | |
71 } | |
72 } | |
73 | |
74 if (extension == "ogg" || extension == "oga" || | |
75 extension == "m4a" || extension == "mp3" || | |
76 extension == "wma") { | |
77 | |
78 // Usually a lossy file. Compression ratios can vary | |
79 // dramatically, but don't usually exceed about 20x compared | |
80 // to 16-bit PCM (e.g. a 128kbps mp3 has 11x ratio over WAV at | |
81 // 44.1kHz). We can estimate the number of samples to be file | |
82 // size x 20, divided by 2 as we're comparing with 16-bit PCM. | |
83 | |
84 estimate = sv_frame_t(double(sz) * 10 * rateRatio); | |
85 } | |
86 | |
87 if (extension == "flac") { | |
88 | |
89 // FLAC usually takes up a bit more than half the space of | |
90 // 16-bit PCM. So the number of 16-bit samples is roughly the | |
91 // same as the file size in bytes. As above, let's be | |
92 // conservative. | |
93 | |
94 estimate = sv_frame_t(double(sz) * 1.2 * rateRatio); | |
95 } | |
96 | |
97 #ifdef DEBUG_AUDIO_FILE_SIZE_ESTIMATOR | |
98 cerr << "AudioFileSizeEstimator: for extension " << extension << ", estimate = " << estimate << endl; | |
99 #endif | |
100 } | |
101 | |
102 #ifdef DEBUG_AUDIO_FILE_SIZE_ESTIMATOR | |
103 cerr << "estimate = " << estimate << endl; | |
104 #endif | |
105 | |
106 return estimate; | |
107 } | |
108 |