summaryrefslogtreecommitdiff
path: root/tests/unit/audio/test_echosim_wav_trailer.cpp
blob: cd03de42d25d42c73f66f7cbcce240613bf54dfc (plain)
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
#include <QtTest>

#include <QDir>
#include <QProcess>
#include <QTemporaryDir>
#include <QtEndian>
#include <QtMultimedia/QAudioFormat>

#include "Audio/BWFFile.hpp"

namespace
{
  bool wait_for_echosim (QProcess& process)
  {
    auto const finished = process.waitForFinished (30000);
    if (!finished || process.exitStatus () != QProcess::NormalExit || process.exitCode () != 0)
      {
        auto const reason = !finished ? process.errorString ()
          : process.exitStatus () != QProcess::NormalExit ? QString {"abnormal termination"}
          : QString {"nonzero exit"};
        if (process.state () != QProcess::NotRunning)
          {
            process.kill ();
            process.waitForFinished (5000);
          }
        qWarning ().noquote () << QString {
          "echosim %1 failed: %2 (exit status %3, exit code %4)\nstdout:\n%5\nstderr:\n%6"}
          .arg (process.arguments ().join (' '), reason)
          .arg (process.exitStatus () == QProcess::NormalExit ? "normal" : "crashed")
          .arg (process.exitCode ())
          .arg (QString::fromLocal8Bit (process.readAllStandardOutput ()),
                QString::fromLocal8Bit (process.readAllStandardError ()));
      }
    return finished;
  }

  QAudioFormat default_format ()
  {
    QAudioFormat format;
    format.setByteOrder (QAudioFormat::LittleEndian);
    format.setChannelCount (1);
    format.setCodec ("audio/pcm");
    format.setSampleRate (12000);
    format.setSampleSize (16);
    format.setSampleType (QAudioFormat::SignedInt);
    return format;
  }
}

class TestEchosimWavTrailer
  : public QObject
{
  Q_OBJECT

private:
  Q_SLOT void preserves_generated_sample_alignment ()
  {
    QTemporaryDir dir;
    QVERIFY (dir.isValid ());

    QProcess process;
    process.setWorkingDirectory (dir.path ());
    process.start (ECHOSIM_EXECUTABLE, {"1500", "0.0", "0.0", "1", "100"});
    QVERIFY (wait_for_echosim (process));
    QCOMPARE (process.exitStatus (), QProcess::NormalExit);
    QCOMPARE (process.exitCode (), 0);

    QDir out {dir.path ()};
    auto const wavs = out.entryList ({"*.wav"}, QDir::Files);
    QCOMPARE (wavs.size (), 1);

    BWFFile file {default_format (), out.filePath (wavs.first ())};
    QVERIFY (file.open (QIODevice::ReadOnly));

    // The first ten samples contain Echo metadata; sample 11 is generated audio.
    qint16 sample;
    QVERIFY (file.seek (10 * sizeof sample));
    QCOMPARE (file.read (reinterpret_cast<char *> (&sample), sizeof sample),
              static_cast<qint64> (sizeof sample));
    sample = qFromLittleEndian (sample);
    QVERIFY (sample >= 23168 && sample <= 23172);

    file.close ();
  }

  Q_SLOT void writes_correct_data_size_and_list_info_trailer ()
  {
    QTemporaryDir dir;
    QVERIFY (dir.isValid ());

    QProcess process;
    process.setWorkingDirectory (dir.path ());
    process.start (ECHOSIM_EXECUTABLE, {"1500", "0.0", "4.0", "1", "-22"});
    QVERIFY (wait_for_echosim (process));
    QCOMPARE (process.exitStatus (), QProcess::NormalExit);
    QCOMPARE (process.exitCode (), 0);

    QDir out {dir.path ()};
    auto const wavs = out.entryList ({"*.wav"}, QDir::Files);
    QCOMPARE (wavs.size (), 1);

    BWFFile file {default_format (), out.filePath (wavs.first ())};
    QVERIFY (file.open (QIODevice::ReadOnly));

    qint64 constexpr sample_count = 36000;
    QCOMPARE (file.size (), 2 * sample_count);

    BWFFile::InfoDictionary::key_type const isft {{'I', 'S', 'F', 'T'}};
    BWFFile::InfoDictionary::key_type const icrd {{'I', 'C', 'R', 'D'}};
    BWFFile::InfoDictionary::key_type const icmt {{'I', 'C', 'M', 'T'}};
    QVERIFY (file.list_info ().contains (isft));
    QVERIFY (file.list_info ().contains (icrd));
    QVERIFY (file.list_info ().contains (icmt));
    QVERIFY (!file.list_info ().value (icmt).isEmpty ());

    file.close ();
  }
};

QTEST_GUILESS_MAIN (TestEchosimWavTrailer)

#include "test_echosim_wav_trailer.moc"