summaryrefslogtreecommitdiffstats
path: root/src/sources/audiosourcestereoproxy.cpp
blob: cd2f40233efc48548af3f51c4e0c08af17342c68 (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
#include "sources/audiosourcestereoproxy.h"

#include "util/logger.h"
#include "util/sample.h"

namespace mixxx {

namespace {

const Logger kLogger("AudioSourceStereoProxy");

constexpr audio::ChannelCount kChannelCount = audio::ChannelCount::stereo();

audio::SignalInfo proxySignalInfo(
        const audio::SignalInfo& signalInfo) {
    DEBUG_ASSERT(signalInfo.isValid());
    return audio::SignalInfo(
            kChannelCount,
            signalInfo.getSampleRate());
}

} // anonymous namespace

AudioSourceStereoProxy::AudioSourceStereoProxy(
        AudioSourcePointer pAudioSource,
        SINT maxReadableFrames)
        : AudioSourceProxy(
                std::move(pAudioSource),
                proxySignalInfo(pAudioSource->getSignalInfo())),
          m_tempSampleBuffer(
                  (m_pAudioSource->getSignalInfo().getChannelCount() != kChannelCount) ?
                  m_pAudioSource->getSignalInfo().frames2samples(maxReadableFrames) :
                  0),
          m_tempWritableSlice(m_tempSampleBuffer) {
}

AudioSourceStereoProxy::AudioSourceStereoProxy(
        AudioSourcePointer pAudioSource,
        SampleBuffer::WritableSlice tempWritableSlice)
        : AudioSourceProxy(
                std::move(pAudioSource),
                proxySignalInfo(pAudioSource->getSignalInfo())),
          m_tempWritableSlice(std::move(tempWritableSlice)) {
}

namespace {

inline bool isDisjunct(
        const SampleBuffer::WritableSlice& slice1,
        const SampleBuffer::WritableSlice& slice2) {
    if (slice1.data() == slice2.data()) {
        return false;
    }
    if ((slice1.length() == 0) || (slice2.length() == 0)) {
        return true;
    }
    if (slice1.data() < slice2.data()) {
        return slice1.data(slice1.length()) <= slice2.data();
    } else {
        return slice2.data(slice2.length()) <= slice1.data();
    }
}

} // namespace

ReadableSampleFrames AudioSourceStereoProxy::readSampleFramesClamped(
        const WritableSampleFrames& sampleFrames) {
    if (m_pAudioSource->getSignalInfo().getChannelCount() == kChannelCount) {
        return readSampleFramesClampedOn(*m_pAudioSource, sampleFrames);
    }

    // Check location and capacity of temporary buffer
    VERIFY_OR_DEBUG_ASSERT(isDisjunct(
            m_tempWritableSlice,
            SampleBuffer::WritableSlice(sampleFrames.writableSlice()))) {
        kLogger.warning()
                << "Overlap between output and temporary sample buffer detected";
        return ReadableSampleFrames();
    }
    {
        const SINT numberOfSamplesToRead =
                m_pAudioSource->getSignalInfo().frames2samples(
                        sampleFrames.frameLength());
        VERIFY_OR_DEBUG_ASSERT(m_tempWritableSlice.length() >= numberOfSamplesToRead) {
            kLogger.warning()
                    << "Insufficient temporary sample buffer capacity"
                    << m_tempWritableSlice.length()
                    << "<"
                    << numberOfSamplesToRead
                    << "for reading frames"
                    << sampleFrames.frameIndexRange();
            return ReadableSampleFrames();
        }
    }

    const auto readableSampleFrames =
            readSampleFramesClampedOn(
                    *m_pAudioSource,
                    WritableSampleFrames(
                            sampleFrames.frameIndexRange(),
                            m_tempWritableSlice));
    if (readableSampleFrames.frameIndexRange().empty()) {
        return readableSampleFrames;
    }
    DEBUG_ASSERT(
            readableSampleFrames.frameIndexRange().isSubrangeOf(sampleFrames.frameIndexRange()));
    DEBUG_ASSERT(
            readableSampleFrames.frameIndexRange().start() >=
            sampleFrames.frameIndexRange().start());
    const SINT frameOffset =
            readableSampleFrames.frameIndexRange().start() -
            sampleFrames.frameIndexRange().start();
    SampleBuffer::WritableSlice writableSlice(
            sampleFrames.writableData(getSignalInfo().frames2samples(frameOffset)),
            getSignalInfo().frames2samples(readableSampleFrames.frameLength()));
    if (m_pAudioSource->getSignalInfo().getChannelCount() == 1) {
        SampleUtil::copyMonoToDualMono(
                writableSlice.data(),
                readableSampleFrames.readableData(),
                readableSampleFrames.frameLength());
    } else {
        SampleUtil::copyMultiToStereo(
                writableSlice.data(),
                readableSampleFrames.readableData(),
                readableSampleFrames.frameLength(),
                m_pAudioSource->getSignalInfo().getChannelCount());
    }
    return ReadableSampleFrames(
            readableSampleFrames.frameIndexRange(),
            SampleBuffer::ReadableSlice(
                    writableSlice.data(),
                    writableSlice.length()));
}

} // namespace mixxx