summaryrefslogtreecommitdiff
path: root/Audio/FixtureSoundOutput.cpp
blob: ce5ca1ca951f86ebd2387e1db68612b68a284e74 (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
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
#include "Audio/FixtureSoundOutput.hpp"

#include "Audio/AudioDevice.hpp"
#include "Audio/BWFFile.hpp"

#include <algorithm>
#include <cstring>
#include <utility>

#include <QAudioFormat>
#include <QDateTime>
#include <QIODevice>
#include <QSysInfo>
#include <QTimer>
#include <QtEndian>

#include "moc_FixtureSoundOutput.cpp"

FixtureSoundOutput::FixtureSoundOutput (QString capturePath, Profile profile,
                                        QObject * parent)
  : SoundOutput {parent}
  , m_capturePath {std::move (capturePath)}
  , m_profile {profile}
  , m_timer {new QTimer {this}}
{
  m_timer->setSingleShot (true);
  connect (m_timer, &QTimer::timeout, this, &FixtureSoundOutput::pullAudio);
}

FixtureSoundOutput::~FixtureSoundOutput ()
{
  finishCapture ();
}

int FixtureSoundOutput::bufferSize () const
{
  return simulatedBufferBytes;
}

void FixtureSoundOutput::setFormat (QAudioDeviceInfo const&, unsigned, int)
{
}

void FixtureSoundOutput::restart (QIODevice * source)
{
  restart (source, -1);
}

void FixtureSoundOutput::restart (QIODevice * source, qint64 periodOffsetMs)
{
  ++m_restartCount;
  finishCapture ();

  if (!source || !source->isReadable ())
    {
      fail (tr ("Synthetic audio output requires a readable source."));
      return;
    }
  if (m_capturePath.isEmpty ())
    {
      fail (tr ("Synthetic audio output requires a capture path."));
      return;
    }
  if (QSysInfo::ByteOrder != QSysInfo::LittleEndian)
    {
      fail (tr ("Synthetic audio output does not yet support big-endian hosts."));
      return;
    }

  m_sourceBytesPerFrame = bytesPerCapturedFrame;
  m_sourceSampleOffset = 0;
  if (auto const audioSource = dynamic_cast<AudioDevice *> (source))
    {
      m_sourceBytesPerFrame = static_cast<int> (audioSource->bytesPerFrame ());
      if (audioSource->channel () == AudioDevice::Right)
        {
          m_sourceSampleOffset = bytesPerCapturedFrame;
        }
    }
  if (m_sourceBytesPerFrame != bytesPerCapturedFrame
      && m_sourceBytesPerFrame != 2 * bytesPerCapturedFrame)
    {
      fail (tr ("Synthetic audio output received an unsupported source frame size."));
      return;
    }

  QAudioFormat format;
  format.setByteOrder (QAudioFormat::LittleEndian);
  format.setChannelCount (1);
  format.setCodec ("audio/pcm");
  format.setSampleRate (sampleRate);
  format.setSampleSize (16);
  format.setSampleType (QAudioFormat::SignedInt);
  m_capture.reset (new BWFFile {format, m_capturePath});
  if (!m_capture->open (BWFFile::WriteOnly))
    {
      auto const detail = m_capture->errorString ();
      m_capture.reset ();
      fail (tr ("Unable to open synthetic audio output capture %1: %2")
            .arg (m_capturePath, detail));
      return;
    }

  m_source = source;
  m_framesPulled = 0;
  m_scheduleOriginFrame = 0;
  m_capturedFrames.store (0);
  m_maxInternalSilentFrames.store (0);
  m_chunkIndex = 0;
  m_silentFrames = 0;
  m_nonSilentAudioSeen = false;
  m_active = true;
  m_elapsed.start ();
  if (Profile::Ft8Period == m_profile)
    {
      constexpr qint64 periodMs = 15000;
      auto const capturedOffsetMs = periodOffsetMs >= 0
        ? periodOffsetMs : QDateTime::currentMSecsSinceEpoch () % periodMs;
      auto const prefixFrames = capturedOffsetMs * sampleRate / 1000;
      if (!appendSilence (prefixFrames)) return;
      m_framesPulled = prefixFrames;
      m_scheduleOriginFrame = prefixFrames;
    }
  Q_EMIT captureStarted (m_capturePath);
  Q_EMIT audioOutputActive ();
  m_timer->start (0);
}

void FixtureSoundOutput::stop ()
{
  if (!m_active)
    {
      return;
    }
  ++m_stopCount;
  finishCapture ();
}

void FixtureSoundOutput::finishCapture ()
{
  if (!m_active)
    {
      return;
    }

  m_active = false;
  m_timer->stop ();
  m_source.clear ();
  if (Profile::Ft8Period == m_profile && m_capture
      && m_capturedFrames.load () < ft8PeriodFrames)
    {
      if (!appendSilence (ft8PeriodFrames - m_capturedFrames.load ())) return;
      m_framesPulled = ft8PeriodFrames;
    }
  if (m_capture)
    {
      m_capture->close ();
      m_capture.reset ();
    }
  auto const frames = m_capturedFrames.load ();
  Q_EMIT audioOutputIdle ();
  Q_EMIT captureStopped (m_capturePath, frames);
}

bool FixtureSoundOutput::appendCapturedPcm (QByteArray const& pcm)
{
  auto const frames = pcm.size () / bytesPerCapturedFrame;
  if (pcm.size () % bytesPerCapturedFrame)
    {
      fail (tr ("Synthetic audio output received a partial captured frame."));
      return false;
    }
  if (Profile::Ft8Period == m_profile
      && m_capturedFrames.load () + frames > ft8PeriodFrames)
    {
      fail (tr ("Synthetic FT8 audio output exceeded its 15-second capture period."));
      return false;
    }
  auto const written = m_capture->write (pcm);
  if (written != pcm.size ())
    {
      fail (tr ("Synthetic audio output wrote %1 of %2 capture bytes: %3")
            .arg (written).arg (pcm.size ()).arg (m_capture->errorString ()));
      return false;
    }
  m_capturedFrames.store (m_capturedFrames.load () + frames);
  return true;
}

bool FixtureSoundOutput::appendSilence (qint64 frames)
{
  while (frames > 0)
    {
      auto const chunkFrames = static_cast<int> (std::min<qint64> (frames, 4096));
      if (!appendCapturedPcm (QByteArray {chunkFrames * bytesPerCapturedFrame, '\0'}))
        {
          return false;
        }
      frames -= chunkFrames;
    }
  return true;
}

void FixtureSoundOutput::fail (QString const& message)
{
  Q_EMIT captureFailed (message);
  Q_EMIT error (message);
  finishCapture ();
}

void FixtureSoundOutput::scheduleNextPull ()
{
  auto const targetMs = (m_framesPulled - m_scheduleOriginFrame) * 1000
    / sampleRate;
  auto const delayMs = std::max<qint64> (0, targetMs - m_elapsed.elapsed ());
  m_timer->start (static_cast<int> (delayMs));
}

void FixtureSoundOutput::pullAudio ()
{
  if (!m_active || !m_source || !m_capture)
    {
      if (m_active)
        {
          fail (tr ("Synthetic audio output source disappeared during capture."));
        }
      return;
    }

  if (Profile::Ft8Period == m_profile
      && m_capturedFrames.load () == ft8PeriodFrames)
    {
      fail (tr ("Synthetic FT8 audio output continued beyond its 15-second capture period."));
      return;
    }

  auto frames = m_chunkFrames.at (m_chunkIndex % m_chunkFrames.size ());
  if (Profile::Ft8Period == m_profile)
    {
      frames = static_cast<int> (std::min<qint64> (
        frames, ft8PeriodFrames - m_capturedFrames.load ()));
    }
  auto const requestedBytes = qint64 (frames) * m_sourceBytesPerFrame;
  auto const sourcePcm = m_source->read (requestedBytes);
  if (Profile::JttyStrict == m_profile && sourcePcm.size () != requestedBytes)
    {
      fail (tr ("Synthetic audio output received %1 of %2 source bytes.")
            .arg (sourcePcm.size ()).arg (requestedBytes));
      return;
    }
  if (sourcePcm.size () < 0 || sourcePcm.size () > requestedBytes
      || sourcePcm.size () % m_sourceBytesPerFrame)
    {
      fail (tr ("Synthetic audio output received an invalid source byte count."));
      return;
    }

  QByteArray capturedPcm (frames * bytesPerCapturedFrame, '\0');
  auto const sourceFrames = sourcePcm.size () / m_sourceBytesPerFrame;
  for (int frame = 0; frame < frames; ++frame)
    {
      qint16 sample {0};
      if (frame < sourceFrames)
        {
          std::memcpy (&sample,
                       sourcePcm.constData () + frame * m_sourceBytesPerFrame
                         + m_sourceSampleOffset,
                       sizeof sample);
        }
      if (!m_nonSilentAudioSeen && sample)
        {
          m_nonSilentAudioSeen = true;
          Q_EMIT nonSilentAudioStarted (m_framesPulled + frame);
        }
      if (sample)
        {
          if (m_nonSilentAudioSeen)
            {
              m_maxInternalSilentFrames.store (
                std::max (m_maxInternalSilentFrames.load (), m_silentFrames));
            }
          m_silentFrames = 0;
        }
      else if (m_nonSilentAudioSeen)
        {
          ++m_silentFrames;
        }
      auto const littleEndianSample = qToLittleEndian (sample);
      std::memcpy (capturedPcm.data () + frame * bytesPerCapturedFrame,
                   &littleEndianSample, sizeof littleEndianSample);
    }

  if (!appendCapturedPcm (capturedPcm)) return;

  m_framesPulled += frames;
  ++m_chunkIndex;
  scheduleNextPull ();
}