swift
Loading...
Searching...
No Matches
resourcesoundsampleprovider.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 "resourcesoundsampleprovider.h"
5
6#include <QDebug>
7
9
10using namespace swift::misc;
11
12namespace swift::sound::sample_provider
13{
15 : ISampleProvider(parent), m_resourceSound(resourceSound)
16 {
17 const QString on = QStringLiteral("%1 %2").arg(classNameShort(this), resourceSound.getFileName());
18 this->setObjectName(on);
19 m_tempBuffer.resize(m_tempBufferSize);
20 }
21
23 {
24 if (!m_resourceSound.isLoaded())
25 {
26 return 0;
27 }
28 if (count > m_tempBufferSize)
29 {
30 qDebug() << "Count too large for temp buffer" << count;
31 return 0;
32 }
33 const qint64 availableSamples = m_resourceSound.audioData().size() - m_position;
34 const qint64 samplesToCopy = qMin(availableSamples, count);
35 samples.clear();
36 samples.fill(0, static_cast<int>(samplesToCopy));
37
38 for (int i = 0; i < samplesToCopy; i++)
39 {
40 m_tempBuffer[i] = m_resourceSound.audioData().at(static_cast<int>(m_position) + i);
41 }
42
43 if (!qFuzzyCompare(m_gain, 1.0))
44 {
45 for (int i = 0; i < samplesToCopy; i++)
46 {
47 m_tempBuffer[i] = static_cast<float>(m_gain * m_tempBuffer[i]);
48 }
49 }
50
51 for (int i = 0; i < samplesToCopy; i++)
52 {
53 samples[i] = m_tempBuffer.at(i);
54 }
55
56 m_position += samplesToCopy;
57
58 if (m_position > availableSamples - 1)
59 {
60 if (m_looping)
61 {
62 m_position = 0;
63 }
64 else
65 {
66 m_isFinished = true;
67 }
68 }
69
70 return static_cast<int>(samplesToCopy);
71 }
72} // namespace swift::sound::sample_provider
const QString & getFileName() const
Corresponding file.
int readSamples(QVector< float > &samples, qint64 count)
copydoc ISampleProvider::readSamples
CResourceSoundSampleProvider(const CResourceSound &resourceSound, QObject *parent=nullptr)
Ctor.
ISampleProvider(QObject *parent=nullptr)
Ctor.
Free functions in swift::misc.
QString classNameShort(const QObject *object)
Class name as from QMetaObject::className without namespace.
QList< T >::const_reference at(qsizetype i) const const
void clear()
QList< T > & fill(QList< T >::parameter_type value, qsizetype size)
QObject(QObject *parent)
QObject * parent() const const
void setObjectName(QAnyStringView name)
QString arg(Args &&... args) const const