swift
bufferedwaveprovider.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2019 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #include "bufferedwaveprovider.h"
5 
6 #include <QDebug>
7 
8 #include "sound/audioutilities.h"
9 
10 namespace swift::sound::sample_provider
11 {
12  CBufferedWaveProvider::CBufferedWaveProvider(const QAudioFormat &format, QObject *parent) : ISampleProvider(parent)
13  {
14  const QString on =
15  QStringLiteral("%1 format: '%2'").arg(this->metaObject()->className(), swift::sound::toQString(format));
16  this->setObjectName(on);
17 
18  // Set buffer size to 10 secs
19  m_maxBufferSize = format.bytesForDuration(10 * 1000 * 1000);
20  }
21 
22  void CBufferedWaveProvider::addSamples(const QVector<float> &samples)
23  {
24  int delta = m_audioBuffer.size() + samples.size() - m_maxBufferSize;
25  if (delta > 0) { m_audioBuffer.remove(0, delta); }
26  m_audioBuffer.append(samples);
27  }
28 
29  int CBufferedWaveProvider::readSamples(QVector<float> &samples, qint64 count)
30  {
31  const int len = static_cast<int>(qMin(count, static_cast<qint64>(m_audioBuffer.size())));
32  samples = m_audioBuffer.mid(0, len);
33  // if (len != 0) qDebug() << "Reading" << count << "samples." << m_audioBuffer.size() << "currently in the
34  // buffer.";
35  m_audioBuffer.remove(0, len);
36  return len;
37  }
38 
39  void CBufferedWaveProvider::clearBuffer() { m_audioBuffer.clear(); }
40 } // namespace swift::sound::sample_provider
virtual int readSamples(QVector< float > &samples, qint64 count)
ISampleProvider::readSamples.
CBufferedWaveProvider(const QAudioFormat &format, QObject *parent=nullptr)
Ctor.
void addSamples(const QVector< float > &samples)
Add samples.