Mercurial > hg > svcore
comparison data/fileio/test/AudioFileReaderTest.h @ 1040:a1cd5abcb38b cxx11
Introduce and use a samplerate type
author | Chris Cannam |
---|---|
date | Wed, 04 Mar 2015 12:01:04 +0000 |
parents | 6b931eeba385 |
children | 843f67be0ed9 |
comparison
equal
deleted
inserted
replaced
1039:b14064bd1f97 | 1040:a1cd5abcb38b |
---|---|
61 | 61 |
62 void read() | 62 void read() |
63 { | 63 { |
64 QFETCH(QString, audiofile); | 64 QFETCH(QString, audiofile); |
65 | 65 |
66 int readRate = 48000; | 66 sv_samplerate_t readRate = 48000; |
67 | 67 |
68 AudioFileReader *reader = | 68 AudioFileReader *reader = |
69 AudioFileReaderFactory::createReader | 69 AudioFileReaderFactory::createReader |
70 (audioDir + "/" + audiofile, readRate); | 70 (audioDir + "/" + audiofile, readRate); |
71 | 71 |
72 QStringList fileAndExt = audiofile.split("."); | 72 QStringList fileAndExt = audiofile.split("."); |
73 QStringList bits = fileAndExt[0].split("-"); | 73 QStringList bits = fileAndExt[0].split("-"); |
74 QString extension = fileAndExt[1]; | 74 QString extension = fileAndExt[1]; |
75 int nominalRate = bits[0].toInt(); | 75 sv_samplerate_t nominalRate = bits[0].toInt(); |
76 int nominalChannels = bits[1].toInt(); | 76 int nominalChannels = bits[1].toInt(); |
77 int nominalDepth = 16; | 77 int nominalDepth = 16; |
78 if (bits.length() > 2) nominalDepth = bits[2].toInt(); | 78 if (bits.length() > 2) nominalDepth = bits[2].toInt(); |
79 | 79 |
80 if (!reader) { | 80 if (!reader) { |
84 QSKIP("Unsupported file, skipping", SkipSingle); | 84 QSKIP("Unsupported file, skipping", SkipSingle); |
85 #endif | 85 #endif |
86 } | 86 } |
87 | 87 |
88 QCOMPARE((int)reader->getChannelCount(), nominalChannels); | 88 QCOMPARE((int)reader->getChannelCount(), nominalChannels); |
89 QCOMPARE((int)reader->getNativeRate(), nominalRate); | 89 QCOMPARE(reader->getNativeRate(), nominalRate); |
90 QCOMPARE((int)reader->getSampleRate(), readRate); | 90 QCOMPARE(reader->getSampleRate(), readRate); |
91 | 91 |
92 int channels = reader->getChannelCount(); | 92 int channels = reader->getChannelCount(); |
93 AudioTestData tdata(readRate, channels); | 93 AudioTestData tdata(readRate, channels); |
94 | 94 |
95 float *reference = tdata.getInterleavedData(); | 95 float *reference = tdata.getInterleavedData(); |
96 int refFrames = tdata.getFrameCount(); | 96 sv_frame_t refFrames = tdata.getFrameCount(); |
97 | 97 |
98 vector<float> test; | 98 vector<float> test; |
99 | 99 |
100 // The reader should give us exactly the expected number of | 100 // The reader should give us exactly the expected number of |
101 // frames, except for mp3/aac files. We ask for quite a lot | 101 // frames, except for mp3/aac files. We ask for quite a lot |
102 // more, though, so we can (a) check that we only get the | 102 // more, though, so we can (a) check that we only get the |
103 // expected number back (if this is not mp3/aac) or (b) take | 103 // expected number back (if this is not mp3/aac) or (b) take |
104 // into account silence at beginning and end (if it is). | 104 // into account silence at beginning and end (if it is). |
105 reader->getInterleavedFrames(0, refFrames + 5000, test); | 105 reader->getInterleavedFrames(0, refFrames + 5000, test); |
106 int read = test.size() / channels; | 106 sv_frame_t read = test.size() / channels; |
107 | 107 |
108 if (extension == "mp3" || extension == "aac" || extension == "m4a") { | 108 if (extension == "mp3" || extension == "aac" || extension == "m4a") { |
109 // mp3s and aacs can have silence at start and end | 109 // mp3s and aacs can have silence at start and end |
110 QVERIFY(read >= refFrames); | 110 QVERIFY(read >= refFrames); |
111 } else { | 111 } else { |
114 | 114 |
115 // Our limits are pretty relaxed -- we're not testing decoder | 115 // Our limits are pretty relaxed -- we're not testing decoder |
116 // or resampler quality here, just whether the results are | 116 // or resampler quality here, just whether the results are |
117 // plainly wrong (e.g. at wrong samplerate or with an offset) | 117 // plainly wrong (e.g. at wrong samplerate or with an offset) |
118 | 118 |
119 float limit = 0.01; | 119 double limit = 0.01; |
120 float edgeLimit = limit * 10; // in first or final edgeSize frames | 120 double edgeLimit = limit * 10; // in first or final edgeSize frames |
121 int edgeSize = 100; | 121 int edgeSize = 100; |
122 | 122 |
123 if (nominalDepth < 16) { | 123 if (nominalDepth < 16) { |
124 limit = 0.02; | 124 limit = 0.02; |
125 } | 125 } |
128 limit = 0.2; | 128 limit = 0.2; |
129 edgeLimit = limit * 3; | 129 edgeLimit = limit * 3; |
130 } | 130 } |
131 | 131 |
132 // And we ignore completely the last few frames when upsampling | 132 // And we ignore completely the last few frames when upsampling |
133 int discard = 1 + readRate / nominalRate; | 133 int discard = 1 + int(round(readRate / nominalRate)); |
134 | 134 |
135 int offset = 0; | 135 int offset = 0; |
136 | 136 |
137 if (extension == "aac" || extension == "m4a") { | 137 if (extension == "aac" || extension == "m4a") { |
138 // our m4a file appears to have a fixed offset of 1024 (at | 138 // our m4a file appears to have a fixed offset of 1024 (at |
139 // file sample rate) | 139 // file sample rate) |
140 offset = (1024 / float(nominalRate)) * readRate; | 140 offset = int(round((1024 / nominalRate) * readRate)); |
141 } | 141 } |
142 | 142 |
143 if (extension == "mp3") { | 143 if (extension == "mp3") { |
144 // while mp3s appear to vary | 144 // while mp3s appear to vary |
145 for (int i = 0; i < read; ++i) { | 145 for (int i = 0; i < read; ++i) { |
146 bool any = false; | 146 bool any = false; |
147 float thresh = 0.01; | 147 double thresh = 0.01; |
148 for (int c = 0; c < channels; ++c) { | 148 for (int c = 0; c < channels; ++c) { |
149 if (fabsf(test[i * channels + c]) > thresh) { | 149 if (fabs(test[i * channels + c]) > thresh) { |
150 any = true; | 150 any = true; |
151 break; | 151 break; |
152 } | 152 } |
153 } | 153 } |
154 if (any) { | 154 if (any) { |
178 maxdiff = diff; | 178 maxdiff = diff; |
179 maxAt = i; | 179 maxAt = i; |
180 } | 180 } |
181 } | 181 } |
182 } | 182 } |
183 float meandiff = totdiff / read; | 183 float meandiff = totdiff / float(read); |
184 // cerr << "meandiff on channel " << c << ": " << meandiff << endl; | 184 // cerr << "meandiff on channel " << c << ": " << meandiff << endl; |
185 // cerr << "maxdiff on channel " << c << ": " << maxdiff << " at " << maxAt << endl; | 185 // cerr << "maxdiff on channel " << c << ": " << maxdiff << " at " << maxAt << endl; |
186 if (meandiff >= limit) { | 186 if (meandiff >= limit) { |
187 cerr << "ERROR: for audiofile " << audiofile << ": mean diff = " << meandiff << " for channel " << c << endl; | 187 cerr << "ERROR: for audiofile " << audiofile << ": mean diff = " << meandiff << " for channel " << c << endl; |
188 QVERIFY(meandiff < limit); | 188 QVERIFY(meandiff < limit); |