| 1 | // Copyright (C) 2024 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QAUDIOBUFFER_SUPPORT_P_H |
| 5 | #define QAUDIOBUFFER_SUPPORT_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtMultimedia/qaudiobuffer.h> |
| 19 | #include <QtCore/qspan.h> |
| 20 | |
| 21 | QT_BEGIN_NAMESPACE |
| 22 | |
| 23 | namespace QtPrivate { |
| 24 | |
| 25 | enum class Mutability { |
| 26 | Mutable, |
| 27 | Immutable, |
| 28 | }; |
| 29 | |
| 30 | constexpr Mutability QAudioBufferMutable = Mutability::Mutable; |
| 31 | constexpr Mutability QAudioBufferImmutable = Mutability::Immutable; |
| 32 | |
| 33 | // LATER: c++23 has std::views::stride(3) |
| 34 | template <typename SampleType> |
| 35 | struct QAudioBufferChannelView |
| 36 | { |
| 37 | SampleType &operator[](int frame) { return m_buffer[frame * m_numberOfChannels + m_channel]; } |
| 38 | |
| 39 | QSpan<SampleType> m_buffer; |
| 40 | const int m_channel; |
| 41 | const int m_numberOfChannels; |
| 42 | }; |
| 43 | |
| 44 | template <typename SampleType> |
| 45 | void validateBufferFormat(const QAudioBuffer &buffer, int channel) |
| 46 | { |
| 47 | Q_ASSERT(channel < buffer.format().channelCount()); |
| 48 | |
| 49 | if constexpr (std::is_same_v<std::remove_const_t<SampleType>, float>) { |
| 50 | Q_ASSERT(buffer.format().sampleFormat() == QAudioFormat::SampleFormat::Float); |
| 51 | } else if constexpr (std::is_same_v<std::remove_const_t<SampleType>, int32_t>) { |
| 52 | Q_ASSERT(buffer.format().sampleFormat() == QAudioFormat::SampleFormat::Int32); |
| 53 | } else if constexpr (std::is_same_v<std::remove_const_t<SampleType>, int16_t>) { |
| 54 | Q_ASSERT(buffer.format().sampleFormat() == QAudioFormat::SampleFormat::Int16); |
| 55 | } else if constexpr (std::is_same_v<std::remove_const_t<SampleType>, uint8_t>) { |
| 56 | Q_ASSERT(buffer.format().sampleFormat() == QAudioFormat::SampleFormat::UInt8); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | template <typename T, bool Predicate> |
| 61 | using add_const_if_t = std::conditional_t<Predicate, std::add_const_t<T>, T>; |
| 62 | |
| 63 | template <typename SampleType> |
| 64 | auto makeChannelView(add_const_if_t<QAudioBuffer, std::is_const_v<SampleType>> &buffer, int channel) |
| 65 | { |
| 66 | validateBufferFormat<SampleType>(buffer, channel); |
| 67 | return QAudioBufferChannelView<SampleType>{ |
| 68 | QSpan{ buffer.template data<SampleType>(), buffer.sampleCount() }, |
| 69 | channel, |
| 70 | buffer.format().channelCount(), |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | template <typename SampleType> |
| 75 | struct QAudioBufferDeinterleaveAdaptor |
| 76 | { |
| 77 | using BufferType = add_const_if_t<QAudioBuffer, std::is_const_v<SampleType>>; |
| 78 | |
| 79 | QAudioBufferChannelView<SampleType> operator[](int channel) |
| 80 | { |
| 81 | return makeChannelView<SampleType>(m_buffer, channel); |
| 82 | } |
| 83 | |
| 84 | QAudioBufferChannelView<const SampleType> operator[](int channel) const |
| 85 | { |
| 86 | return makeChannelView<const SampleType>(m_buffer, channel); |
| 87 | } |
| 88 | |
| 89 | explicit QAudioBufferDeinterleaveAdaptor(BufferType &buffer) : m_buffer(buffer) { } |
| 90 | |
| 91 | private: |
| 92 | BufferType &m_buffer; |
| 93 | int m_numberOfChannels = m_buffer.format().channelCount(); |
| 94 | }; |
| 95 | |
| 96 | } // namespace QtPrivate |
| 97 | |
| 98 | QT_END_NAMESPACE |
| 99 | |
| 100 | #endif // QAUDIOBUFFER_SUPPORT_P_H |
| 101 | |