-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathsoundproxy_test.cpp
761 lines (679 loc) · 35.2 KB
/
soundproxy_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
#include <QTemporaryFile>
#include <QtDebug>
#include "sources/audiosourcestereoproxy.h"
#include "sources/soundsourceproxy.h"
#include "test/mixxxtest.h"
#include "track/track.h"
#include "track/trackmetadata.h"
#include "util/samplebuffer.h"
namespace {
const QDir kTestDir(QDir::current().absoluteFilePath("src/test/id3-test-data"));
const SINT kBufferSizes[] = {
256,
512,
768,
1024,
1536,
2048,
3072,
4096,
6144,
8192,
12288,
16384,
24576,
32768,
};
const SINT kMaxReadFrameCount = kBufferSizes[sizeof(kBufferSizes) / sizeof(kBufferSizes[0]) - 1];
const CSAMPLE kMaxDecodingError = 0.01f;
} // anonymous namespace
class SoundSourceProxyTest : public MixxxTest {
protected:
static QStringList getFileNameSuffixes() {
QStringList availableFileNameSuffixes;
availableFileNameSuffixes
<< ".aiff"
<< "-alac.caf"
<< ".flac"
// Files encoded with iTunes 12.3.0 caused issues when
// decoding with FFMpeg 3.x, because their start_time
// was not correctly handled. The actual FFmpeg version
// that fixed this bug is unknown.
<< "-itunes-12.3.0-aac.m4a"
<< "-itunes-12.7.0-aac.m4a"
<< "-ffmpeg-aac.m4a"
#if defined(__FFMPEG__) || defined(__COREAUDIO__)
<< "-itunes-12.7.0-alac.m4a"
#endif
<< "-png.mp3"
<< "-vbr.mp3"
<< ".ogg"
<< ".opus"
<< ".wav"
<< ".wma"
<< ".wv";
QStringList supportedFileNameSuffixes;
for (const auto& fileNameSuffix : qAsConst(availableFileNameSuffixes)) {
// We need to check for the whole file name here!
if (SoundSourceProxy::isFileNameSupported(fileNameSuffix)) {
supportedFileNameSuffixes << fileNameSuffix;
} else {
qInfo()
<< "Ignoring unsupported file type"
<< fileNameSuffix;
}
}
return supportedFileNameSuffixes;
}
static QStringList getFilePaths() {
QStringList filePaths;
const QStringList fileNameSuffixes = getFileNameSuffixes();
for (const auto& fileNameSuffix : fileNameSuffixes) {
filePaths.append(kTestDir.absoluteFilePath("cover-test" + fileNameSuffix));
}
return filePaths;
}
static mixxx::AudioSourcePointer openAudioSource(
const QString& filePath,
const mixxx::SoundSourceProviderPointer& pProvider = nullptr) {
auto pTrack = Track::newTemporary(filePath);
SoundSourceProxy proxy(pTrack, pProvider);
// All test files are mono, but we are requesting a stereo signal
// to test the upscaling of channels
mixxx::AudioSource::OpenParams openParams;
const auto channelCount = mixxx::audio::ChannelCount(2);
openParams.setChannelCount(mixxx::audio::ChannelCount(2));
auto pAudioSource = proxy.openAudioSource(openParams);
if (pAudioSource) {
if (pAudioSource->getSignalInfo().getChannelCount() != channelCount) {
// Wrap into proxy object
pAudioSource = mixxx::AudioSourceStereoProxy::create(
pAudioSource,
kMaxReadFrameCount);
}
EXPECT_EQ(pAudioSource->getSignalInfo().getChannelCount(), channelCount);
qInfo()
<< "Opened file" << filePath
<< "using provider" << proxy.getProvider()->getDisplayName();
}
return pAudioSource;
}
static void expectDecodedSamplesEqual(
SINT size,
const CSAMPLE* expected,
const CSAMPLE* actual,
const char* errorMessage) {
for (SINT i = 0; i < size; ++i) {
EXPECT_NEAR(expected[i], actual[i],
kMaxDecodingError) << errorMessage;
}
}
mixxx::IndexRange skipSampleFrames(
mixxx::AudioSourcePointer pAudioSource,
mixxx::IndexRange skipRange) {
mixxx::IndexRange skippedRange;
while (skippedRange.length() < skipRange.length()) {
// Seek in forward direction by decoding and discarding samples
const auto nextRange =
mixxx::IndexRange::forward(
skippedRange.empty() ? skipRange.start() : skippedRange.end(),
math_min(
skipRange.length() - skippedRange.length(),
pAudioSource->getSignalInfo().samples2frames(m_skipSampleBuffer.size())));
EXPECT_FALSE(nextRange.empty());
EXPECT_TRUE(nextRange.isSubrangeOf(skipRange));
const auto readRange = pAudioSource->readSampleFrames(
mixxx::WritableSampleFrames(
nextRange,
mixxx::SampleBuffer::WritableSlice(
m_skipSampleBuffer.data(),
m_skipSampleBuffer.size()))).frameIndexRange();
if (readRange.empty()) {
return skippedRange;
}
EXPECT_TRUE(readRange.start() == nextRange.start());
EXPECT_TRUE(readRange.isSubrangeOf(skipRange));
if (skippedRange.empty()) {
skippedRange = readRange;
} else {
EXPECT_TRUE(skippedRange.end() == nextRange.start());
skippedRange.growBack(nextRange.length());
}
}
return skippedRange;
}
SoundSourceProxyTest()
: m_skipSampleBuffer(kMaxReadFrameCount) {
}
private:
mixxx::SampleBuffer m_skipSampleBuffer;
};
TEST_F(SoundSourceProxyTest, open) {
// This test piggy-backs off of the cover-test files.
const QStringList filePaths = getFilePaths();
for (const auto& filePath : filePaths) {
ASSERT_TRUE(SoundSourceProxy::isFileNameSupported(filePath));
const auto fileUrl = QUrl::fromLocalFile(filePath);
const auto providerRegistrations =
SoundSourceProxy::allProviderRegistrationsForUrl(fileUrl);
for (const auto& providerRegistration : providerRegistrations) {
mixxx::AudioSourcePointer pAudioSource = openAudioSource(
filePath,
providerRegistration.getProvider());
// Obtaining an AudioSource may fail for unsupported file formats,
// even if the corresponding file extension is supported, e.g.
// AAC vs. ALAC in .m4a files
if (!pAudioSource) {
// skip test file
continue;
}
EXPECT_LT(0, pAudioSource->getSignalInfo().getChannelCount());
EXPECT_LT(0, pAudioSource->getSignalInfo().getSampleRate());
EXPECT_FALSE(pAudioSource->frameIndexRange().empty());
}
}
}
TEST_F(SoundSourceProxyTest, openEmptyFile) {
const QStringList fileNameSuffixes = getFileNameSuffixes();
for (const auto& fileNameSuffix : fileNameSuffixes) {
const auto tmpFileName =
mixxxtest::createEmptyTemporaryFile("emptyXXXXXX" + fileNameSuffix);
const mixxxtest::FileRemover tmpFileRemover(tmpFileName);
ASSERT_TRUE(QFile::exists(tmpFileName));
ASSERT_TRUE(!tmpFileName.isEmpty());
ASSERT_TRUE(SoundSourceProxy::isFileNameSupported(tmpFileName));
auto pTrack = Track::newTemporary(tmpFileName);
SoundSourceProxy proxy(pTrack);
auto pAudioSource = proxy.openAudioSource();
EXPECT_TRUE(!pAudioSource);
}
}
TEST_F(SoundSourceProxyTest, readArtist) {
auto pTrack = Track::newTemporary(TrackFile(
kTestDir, "artist.mp3"));
SoundSourceProxy proxy(pTrack);
proxy.updateTrackFromSource();
EXPECT_EQ("Test Artist", pTrack->getArtist());
}
TEST_F(SoundSourceProxyTest, readNoTitle) {
// We need to verify every track has at least a title to not have empty lines in the library
// Test a file with no metadata
auto pTrack1 = Track::newTemporary(TrackFile(
kTestDir, "empty.mp3"));
SoundSourceProxy proxy1(pTrack1);
proxy1.updateTrackFromSource();
EXPECT_EQ("empty", pTrack1->getTitle());
// Test a reload also works
pTrack1->setTitle("");
proxy1.updateTrackFromSource(SoundSourceProxy::ImportTrackMetadataMode::Again);
EXPECT_EQ("empty", pTrack1->getTitle());
// Test a file with other metadata but no title
auto pTrack2 = Track::newTemporary(TrackFile(
kTestDir, "cover-test-png.mp3"));
SoundSourceProxy proxy2(pTrack2);
proxy2.updateTrackFromSource();
EXPECT_EQ("cover-test-png", pTrack2->getTitle());
// Test a reload also works
pTrack2->setTitle("");
proxy2.updateTrackFromSource(SoundSourceProxy::ImportTrackMetadataMode::Again);
EXPECT_EQ("cover-test-png", pTrack2->getTitle());
// Test a file with a title
auto pTrack3 = Track::newTemporary(TrackFile(
kTestDir, "cover-test-jpg.mp3"));
SoundSourceProxy proxy3(pTrack3);
proxy3.updateTrackFromSource();
EXPECT_EQ("test22kMono", pTrack3->getTitle());
}
TEST_F(SoundSourceProxyTest, TOAL_TPE2) {
auto pTrack = Track::newTemporary(TrackFile(
kTestDir, "TOAL_TPE2.mp3"));
SoundSourceProxy proxy(pTrack);
mixxx::TrackMetadata trackMetadata;
EXPECT_EQ(mixxx::MetadataSource::ImportResult::Succeeded, proxy.importTrackMetadata(&trackMetadata));
EXPECT_EQ("TITLE2", trackMetadata.getTrackInfo().getArtist());
EXPECT_EQ("ARTIST", trackMetadata.getAlbumInfo().getTitle());
EXPECT_EQ("TITLE", trackMetadata.getAlbumInfo().getArtist());
// The COMM:iTunPGAP comment should not be read
EXPECT_TRUE(trackMetadata.getTrackInfo().getComment().isNull());
}
TEST_F(SoundSourceProxyTest, seekForwardBackward) {
const SINT kReadFrameCount = 10000;
const QStringList filePaths = getFilePaths();
for (const auto& filePath : filePaths) {
ASSERT_TRUE(SoundSourceProxy::isFileNameSupported(filePath));
qDebug() << "Seek forward/backward test:" << filePath;
const auto fileUrl = QUrl::fromLocalFile(filePath);
const auto providerRegistrations =
SoundSourceProxy::allProviderRegistrationsForUrl(fileUrl);
for (const auto& providerRegistration : providerRegistrations) {
mixxx::AudioSourcePointer pContReadSource = openAudioSource(
filePath,
providerRegistration.getProvider());
// Obtaining an AudioSource may fail for unsupported file formats,
// even if the corresponding file extension is supported, e.g.
// AAC vs. ALAC in .m4a files
if (!pContReadSource) {
// skip test file
continue;
}
mixxx::SampleBuffer contReadData(
pContReadSource->getSignalInfo().frames2samples(kReadFrameCount));
mixxx::SampleBuffer seekReadData(
pContReadSource->getSignalInfo().frames2samples(kReadFrameCount));
SINT contFrameIndex = pContReadSource->frameIndexMin();
while (pContReadSource->frameIndexRange().containsIndex(contFrameIndex)) {
const auto readFrameIndexRange =
mixxx::IndexRange::forward(contFrameIndex, kReadFrameCount);
qDebug() << "Seeking and reading" << readFrameIndexRange;
// Read next chunk of frames for Cont source without seeking
const auto contSampleFrames =
pContReadSource->readSampleFrames(
mixxx::WritableSampleFrames(
readFrameIndexRange,
mixxx::SampleBuffer::WritableSlice(contReadData)));
ASSERT_FALSE(contSampleFrames.frameIndexRange().empty());
ASSERT_TRUE(contSampleFrames.frameIndexRange().isSubrangeOf(readFrameIndexRange));
ASSERT_EQ(contSampleFrames.frameIndexRange().start(), readFrameIndexRange.start());
contFrameIndex += contSampleFrames.frameLength();
const SINT sampleCount =
pContReadSource->getSignalInfo().frames2samples(contSampleFrames.frameLength());
mixxx::AudioSourcePointer pSeekReadSource = openAudioSource(
filePath,
providerRegistration.getProvider());
ASSERT_FALSE(!pSeekReadSource);
ASSERT_EQ(
pContReadSource->getSignalInfo().getChannelCount(),
pSeekReadSource->getSignalInfo().getChannelCount());
ASSERT_EQ(pContReadSource->frameIndexRange(), pSeekReadSource->frameIndexRange());
// Seek source to next chunk and read it
auto seekSampleFrames =
pSeekReadSource->readSampleFrames(
mixxx::WritableSampleFrames(
readFrameIndexRange,
mixxx::SampleBuffer::WritableSlice(seekReadData)));
// Both buffers should be equal
ASSERT_EQ(contSampleFrames.frameIndexRange(), seekSampleFrames.frameIndexRange());
expectDecodedSamplesEqual(
sampleCount,
&contReadData[0],
&seekReadData[0],
"Decoding mismatch after seeking forward");
// Seek backwards to beginning of chunk and read again
seekSampleFrames =
pSeekReadSource->readSampleFrames(
mixxx::WritableSampleFrames(
readFrameIndexRange,
mixxx::SampleBuffer::WritableSlice(seekReadData)));
// Both buffers should again be equal
ASSERT_EQ(contSampleFrames.frameIndexRange(), seekSampleFrames.frameIndexRange());
expectDecodedSamplesEqual(
sampleCount,
&contReadData[0],
&seekReadData[0],
"Decoding mismatch after seeking backward");
}
}
}
}
TEST_F(SoundSourceProxyTest, skipAndRead) {
for (auto kReadFrameCount : kBufferSizes) {
const QStringList filePaths = getFilePaths();
for (const auto& filePath : filePaths) {
ASSERT_TRUE(SoundSourceProxy::isFileNameSupported(filePath));
qDebug() << "Skip and read test:" << filePath;
const auto fileUrl = QUrl::fromLocalFile(filePath);
const auto providerRegistrations =
SoundSourceProxy::allProviderRegistrationsForUrl(fileUrl);
for (const auto& providerRegistration : providerRegistrations) {
mixxx::AudioSourcePointer pContReadSource = openAudioSource(
filePath,
providerRegistration.getProvider());
// Obtaining an AudioSource may fail for unsupported file formats,
// even if the corresponding file extension is supported, e.g.
// AAC vs. ALAC in .m4a files
if (!pContReadSource) {
// skip test file
continue;
}
SINT contFrameIndex = pContReadSource->frameIndexMin();
mixxx::AudioSourcePointer pSkipReadSource = openAudioSource(
filePath,
providerRegistration.getProvider());
ASSERT_FALSE(!pSkipReadSource);
ASSERT_EQ(
pContReadSource->getSignalInfo().getChannelCount(),
pSkipReadSource->getSignalInfo().getChannelCount());
ASSERT_EQ(pContReadSource->frameIndexRange(), pSkipReadSource->frameIndexRange());
SINT skipFrameIndex = pSkipReadSource->frameIndexMin();
mixxx::SampleBuffer contReadData(
pContReadSource->getSignalInfo().frames2samples(kReadFrameCount));
mixxx::SampleBuffer skipReadData(
pSkipReadSource->getSignalInfo().frames2samples(kReadFrameCount));
SINT minFrameIndex = pContReadSource->frameIndexMin();
SINT skipCount = 1;
while (pContReadSource->frameIndexRange().containsIndex(
minFrameIndex += skipCount)) {
skipCount = minFrameIndex / 4 + 1; // for next iteration
qDebug() << "Skipping to:" << minFrameIndex;
const auto readFrameIndexRange =
mixxx::IndexRange::forward(minFrameIndex, kReadFrameCount);
// Read (and discard samples) until reaching the desired frame index
// and read next chunk
ASSERT_LE(contFrameIndex, minFrameIndex);
while (contFrameIndex < minFrameIndex) {
auto skippingFrameIndexRange =
mixxx::IndexRange::forward(
contFrameIndex,
std::min(minFrameIndex - contFrameIndex, kReadFrameCount));
auto const skippedSampleFrames =
pContReadSource->readSampleFrames(
mixxx::WritableSampleFrames(
skippingFrameIndexRange,
mixxx::SampleBuffer::WritableSlice(contReadData)));
ASSERT_FALSE(skippedSampleFrames.frameIndexRange().empty());
ASSERT_EQ(skippedSampleFrames.frameIndexRange().start(), contFrameIndex);
contFrameIndex += skippedSampleFrames.frameLength();
}
ASSERT_EQ(minFrameIndex, contFrameIndex);
const auto contSampleFrames =
pContReadSource->readSampleFrames(
mixxx::WritableSampleFrames(
readFrameIndexRange,
mixxx::SampleBuffer::WritableSlice(contReadData)));
ASSERT_FALSE(contSampleFrames.frameIndexRange().empty());
ASSERT_TRUE(contSampleFrames.frameIndexRange()
.isSubrangeOf(readFrameIndexRange));
ASSERT_EQ(contSampleFrames.frameIndexRange().start(),
readFrameIndexRange.start());
contFrameIndex += contSampleFrames.frameLength();
const SINT sampleCount =
pContReadSource->getSignalInfo().frames2samples(
contSampleFrames.frameLength());
// Skip until reaching the frame index and read next chunk
ASSERT_LE(skipFrameIndex, minFrameIndex);
while (skipFrameIndex < minFrameIndex) {
auto const skippedFrameIndexRange =
skipSampleFrames(pSkipReadSource,
mixxx::IndexRange::between(skipFrameIndex, minFrameIndex));
ASSERT_FALSE(skippedFrameIndexRange.empty());
ASSERT_EQ(skippedFrameIndexRange.start(), skipFrameIndex);
skipFrameIndex += skippedFrameIndexRange.length();
}
ASSERT_EQ(minFrameIndex, skipFrameIndex);
const auto skippedSampleFrames =
pSkipReadSource->readSampleFrames(
mixxx::WritableSampleFrames(
readFrameIndexRange,
mixxx::SampleBuffer::WritableSlice(skipReadData)));
skipFrameIndex += skippedSampleFrames.frameLength();
// Both buffers should be equal
ASSERT_EQ(contSampleFrames.frameIndexRange(),
skippedSampleFrames.frameIndexRange());
expectDecodedSamplesEqual(
sampleCount,
&contReadData[0],
&skipReadData[0],
"Decoding mismatch after skipping");
minFrameIndex = contFrameIndex;
}
}
}
}
}
TEST_F(SoundSourceProxyTest, seekBoundaries) {
const SINT kReadFrameCount = 1000;
const QStringList filePaths = getFilePaths();
for (const auto& filePath : filePaths) {
ASSERT_TRUE(SoundSourceProxy::isFileNameSupported(filePath));
qDebug() << "Seek boundaries test:" << filePath;
const auto fileUrl = QUrl::fromLocalFile(filePath);
const auto providerRegistrations =
SoundSourceProxy::allProviderRegistrationsForUrl(fileUrl);
for (const auto& providerRegistration : providerRegistrations) {
mixxx::AudioSourcePointer pSeekReadSource = openAudioSource(
filePath,
providerRegistration.getProvider());
// Obtaining an AudioSource may fail for unsupported file formats,
// even if the corresponding file extension is supported, e.g.
// AAC vs. ALAC in .m4a files
if (!pSeekReadSource) {
// skip test file
continue;
}
mixxx::SampleBuffer seekReadData(
pSeekReadSource->getSignalInfo().frames2samples(kReadFrameCount));
std::vector<SINT> seekFrameIndices;
// Seek to boundaries (alternating)...
seekFrameIndices.push_back(pSeekReadSource->frameIndexMin());
seekFrameIndices.push_back(pSeekReadSource->frameIndexMax() - 1);
seekFrameIndices.push_back(pSeekReadSource->frameIndexMin() + 1);
seekFrameIndices.push_back(pSeekReadSource->frameIndexMax());
// ...seek to middle of the stream...
seekFrameIndices.push_back(
pSeekReadSource->frameIndexMin() +
pSeekReadSource->frameLength() / 2);
// ...and to the boundaries again in opposite order...
seekFrameIndices.push_back(pSeekReadSource->frameIndexMax());
seekFrameIndices.push_back(pSeekReadSource->frameIndexMin() + 1);
seekFrameIndices.push_back(pSeekReadSource->frameIndexMax() - 1);
seekFrameIndices.push_back(pSeekReadSource->frameIndexMin());
// ...near the end and back to middle of the stream...
seekFrameIndices.push_back(
pSeekReadSource->frameIndexMax() - 4 * kReadFrameCount);
seekFrameIndices.push_back(
pSeekReadSource->frameIndexMin() + pSeekReadSource->frameLength() / 2);
// ...before the middle and then near the end of the stream...
seekFrameIndices.push_back(pSeekReadSource->frameIndexMin() +
pSeekReadSource->frameLength() / 2 - 4 * kReadFrameCount);
seekFrameIndices.push_back(
pSeekReadSource->frameIndexMax() - 4 * kReadFrameCount);
// ...to the moddle of the stream and then skipping kReadFrameCount samples.
seekFrameIndices.push_back(
pSeekReadSource->frameIndexMin() + pSeekReadSource->frameLength() / 2);
seekFrameIndices.push_back(pSeekReadSource->frameIndexMin() +
pSeekReadSource->frameLength() / 2 + 2 * kReadFrameCount);
// Read and verify results
for (SINT seekFrameIndex : seekFrameIndices) {
const auto readFrameIndexRange =
mixxx::IndexRange::forward(seekFrameIndex, kReadFrameCount);
qDebug() << "Reading and verifying" << readFrameIndexRange;
const auto expectedFrameIndexRange = intersect(
readFrameIndexRange,
pSeekReadSource->frameIndexRange());
mixxx::AudioSourcePointer pContReadSource = openAudioSource(
filePath,
providerRegistration.getProvider());
ASSERT_FALSE(!pContReadSource);
ASSERT_EQ(
pSeekReadSource->getSignalInfo().getChannelCount(),
pContReadSource->getSignalInfo().getChannelCount());
ASSERT_EQ(pSeekReadSource->frameIndexRange(), pContReadSource->frameIndexRange());
const auto skipFrameIndexRange =
skipSampleFrames(pContReadSource,
mixxx::IndexRange::between(
pContReadSource->frameIndexMin(),
seekFrameIndex));
ASSERT_TRUE(skipFrameIndexRange.empty() ||
(skipFrameIndexRange.end() == seekFrameIndex));
mixxx::SampleBuffer contReadData(
pContReadSource->getSignalInfo().frames2samples(kReadFrameCount));
const auto contSampleFrames =
pContReadSource->readSampleFrames(
mixxx::WritableSampleFrames(
readFrameIndexRange,
mixxx::SampleBuffer::WritableSlice(contReadData)));
ASSERT_EQ(expectedFrameIndexRange, contSampleFrames.frameIndexRange());
const auto seekSampleFrames =
pSeekReadSource->readSampleFrames(
mixxx::WritableSampleFrames(
readFrameIndexRange,
mixxx::SampleBuffer::WritableSlice(seekReadData)));
ASSERT_EQ(expectedFrameIndexRange, seekSampleFrames.frameIndexRange());
if (seekSampleFrames.frameIndexRange().empty()) {
continue; // nothing to do
}
const SINT sampleCount =
pSeekReadSource->getSignalInfo().frames2samples(
seekSampleFrames.frameLength());
expectDecodedSamplesEqual(
sampleCount,
&contReadData[0],
&seekReadData[0],
"Decoding mismatch after seeking");
}
}
}
}
TEST_F(SoundSourceProxyTest, readBeyondEnd) {
const SINT kReadFrameCount = 1000;
const QStringList filePaths = getFilePaths();
for (const auto& filePath : filePaths) {
ASSERT_TRUE(SoundSourceProxy::isFileNameSupported(filePath));
qDebug() << "read beyond end test:" << filePath;
const auto fileUrl = QUrl::fromLocalFile(filePath);
const auto providerRegistrations =
SoundSourceProxy::allProviderRegistrationsForUrl(fileUrl);
for (const auto& providerRegistration : providerRegistrations) {
mixxx::AudioSourcePointer pAudioSource = openAudioSource(
filePath,
providerRegistration.getProvider());
// Obtaining an AudioSource may fail for unsupported file formats,
// even if the corresponding file extension is supported, e.g.
// AAC vs. ALAC in .m4a files
if (!pAudioSource) {
// skip test file
continue;
}
// Seek to position near the end
const SINT seekIndex = pAudioSource->frameIndexMax() - (kReadFrameCount / 2);
const SINT remainingFrames = pAudioSource->frameIndexMax() - seekIndex;
ASSERT_GT(remainingFrames, 0);
ASSERT_LT(remainingFrames, kReadFrameCount);
mixxx::SampleBuffer readBuffer(
pAudioSource->getSignalInfo().frames2samples(kReadFrameCount));
// Read beyond the end, starting within the valid range
EXPECT_EQ(mixxx::IndexRange::forward(seekIndex, remainingFrames),
pAudioSource
->readSampleFrames(mixxx::WritableSampleFrames(
mixxx::IndexRange::forward(
seekIndex, kReadFrameCount),
mixxx::SampleBuffer::WritableSlice(
readBuffer)))
.frameIndexRange());
// Read beyond the end, starting at the upper boundary of the valid range
EXPECT_EQ(mixxx::IndexRange::forward(pAudioSource->frameIndexMax(), 0),
pAudioSource
->readSampleFrames(mixxx::WritableSampleFrames(
mixxx::IndexRange::forward(
pAudioSource->frameIndexMax(), kReadFrameCount),
mixxx::SampleBuffer::WritableSlice(
readBuffer)))
.frameIndexRange());
// Read beyond the end, starting beyond the upper boundary of the valid range
EXPECT_EQ(mixxx::IndexRange::forward(pAudioSource->frameIndexMax() + 1, 0),
pAudioSource
->readSampleFrames(mixxx::WritableSampleFrames(
mixxx::IndexRange::forward(
pAudioSource->frameIndexMax() + 1, kReadFrameCount),
mixxx::SampleBuffer::WritableSlice(
readBuffer)))
.frameIndexRange());
}
}
}
TEST_F(SoundSourceProxyTest, regressionTestCachingReaderChunkJumpForward) {
// NOTE(uklotzde, 2017-12-10): Potential regression test for an infinite
// seek/read loop in SoundSourceMediaFoundation. Unfortunately this
// test doesn't fail even prior to fixing the reported bug.
// https://github.com/mixxxdj/mixxx/pull/1317#issuecomment-349674161
const QStringList filePaths = getFilePaths();
for (auto kReadFrameCount : kBufferSizes) {
for (const auto& filePath : filePaths) {
ASSERT_TRUE(SoundSourceProxy::isFileNameSupported(filePath));
const auto fileUrl = QUrl::fromLocalFile(filePath);
const auto providerRegistrations =
SoundSourceProxy::allProviderRegistrationsForUrl(fileUrl);
for (const auto& providerRegistration : providerRegistrations) {
mixxx::AudioSourcePointer pAudioSource = openAudioSource(
filePath,
providerRegistration.getProvider());
// Obtaining an AudioSource may fail for unsupported file formats,
// even if the corresponding file extension is supported, e.g.
// AAC vs. ALAC in .m4a files
if (!pAudioSource) {
// skip test file
continue;
}
mixxx::SampleBuffer readBuffer(
pAudioSource->getSignalInfo().frames2samples(kReadFrameCount));
// Read chunk from beginning
auto firstChunkRange = mixxx::IndexRange::forward(
pAudioSource->frameIndexMin(), kReadFrameCount);
EXPECT_EQ(
firstChunkRange,
pAudioSource->readSampleFrames(
mixxx::WritableSampleFrames(
firstChunkRange,
mixxx::SampleBuffer::WritableSlice(readBuffer)))
.frameIndexRange());
// Read chunk from near the end, rounded to chunk boundary
auto secondChunkRange = mixxx::IndexRange::forward(
((pAudioSource->frameIndexMax() - 2 * kReadFrameCount) /
kReadFrameCount) *
kReadFrameCount,
kReadFrameCount);
EXPECT_EQ(
secondChunkRange,
pAudioSource->readSampleFrames(
mixxx::WritableSampleFrames(
secondChunkRange,
mixxx::SampleBuffer::WritableSlice(readBuffer)))
.frameIndexRange());
}
}
}
}
TEST_F(SoundSourceProxyTest, getTypeFromFile) {
// Generate file names for the temporary file
const QString filePathWithoutSuffix =
mixxxtest::generateTemporaryFileName("file_with_no_file_suffix");
const QString filePathWithEmptySuffix =
mixxxtest::generateTemporaryFileName("file_with_empty_suffix.");
const QString filePathWithUnknownSuffix =
mixxxtest::generateTemporaryFileName("file_with.unknown_suffix");
const QString filePathWithWrongSuffix =
mixxxtest::generateTemporaryFileName("file_with_wrong_suffix.wav");
const QString filePathWithUppercaseAndLeadingTrailingWhitespaceSuffix =
mixxxtest::generateTemporaryFileName("file_with_uppercase_suffix. MP3 ");
// Create the temporary files by copying an existing file
const QString validFilePath = kTestDir.absoluteFilePath(QStringLiteral("empty.mp3"));
mixxxtest::copyFile(validFilePath, filePathWithoutSuffix);
mixxxtest::copyFile(validFilePath, filePathWithEmptySuffix);
mixxxtest::copyFile(validFilePath, filePathWithUnknownSuffix);
mixxxtest::copyFile(validFilePath, filePathWithWrongSuffix);
mixxxtest::copyFile(validFilePath, filePathWithUppercaseAndLeadingTrailingWhitespaceSuffix);
ASSERT_STREQ(qPrintable("mp3"), qPrintable(mixxx::SoundSource::getTypeFromFile(validFilePath)));
EXPECT_STREQ(qPrintable("mp3"),
qPrintable(mixxx::SoundSource::getTypeFromFile(
filePathWithoutSuffix)));
EXPECT_STREQ(qPrintable("mp3"),
qPrintable(mixxx::SoundSource::getTypeFromFile(
filePathWithEmptySuffix)));
EXPECT_STREQ(qPrintable("mp3"),
qPrintable(mixxx::SoundSource::getTypeFromFile(
filePathWithUnknownSuffix)));
EXPECT_STREQ(qPrintable("mp3"),
qPrintable(mixxx::SoundSource::getTypeFromFile(
filePathWithWrongSuffix)));
EXPECT_STREQ(qPrintable("mp3"),
qPrintable(mixxx::SoundSource::getTypeFromFile(
filePathWithUppercaseAndLeadingTrailingWhitespaceSuffix)));
}
TEST_F(SoundSourceProxyTest, getTypeFromMissingFile) {
const QFileInfo missingFileWithUppercaseSuffixAndLeadingTrailingWhitespaceSuffix =
kTestDir.absoluteFilePath(QStringLiteral("missing_file. AIFF "));
ASSERT_FALSE(missingFileWithUppercaseSuffixAndLeadingTrailingWhitespaceSuffix.exists());
EXPECT_STREQ(qPrintable("aiff"),
qPrintable(mixxx::SoundSource::getTypeFromFile(
missingFileWithUppercaseSuffixAndLeadingTrailingWhitespaceSuffix)));
}