comparison data/fileio/AudioFileSizeEstimator.cpp @ 1341:513e4d67d8df 3.0-integration

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