summaryrefslogtreecommitdiffstats
path: root/src/audio/signalinfo.h
blob: 3f6354e0885df260bac729843a6f3619789814a4 (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
#pragma once

#include "audio/types.h"
#include "util/assert.h"
#include "util/macros.h"
#include "util/optional.h"

namespace mixxx {

namespace audio {

// Properties that characterize an uncompressed PCM audio signal.
class SignalInfo final {
    // Properties
    MIXXX_DECL_PROPERTY(ChannelCount, channelCount, ChannelCount)
    MIXXX_DECL_PROPERTY(SampleRate, sampleRate, SampleRate)
    MIXXX_DECL_PROPERTY(OptionalSampleLayout, sampleLayout, SampleLayout)

  public:
    constexpr SignalInfo() = default;
    constexpr explicit SignalInfo(
            OptionalSampleLayout sampleLayout)
            : m_sampleLayout(sampleLayout) {
    }
    SignalInfo(
            ChannelCount channelCount,
            SampleRate sampleRate,
            OptionalSampleLayout sampleLayout = std::nullopt)
            : m_channelCount(channelCount),
              m_sampleRate(sampleRate),
              m_sampleLayout(sampleLayout) {
    }
    SignalInfo(SignalInfo&&) = default;
    SignalInfo(const SignalInfo&) = default;
    /*non-virtual*/ ~SignalInfo() = default;

    constexpr bool isValid() const {
        return getChannelCount().isValid() &&
                getSampleRate().isValid();
    }

    SignalInfo& operator=(SignalInfo&&) = default;
    SignalInfo& operator=(const SignalInfo&) = default;

    // Conversion: #samples / sample offset -> #frames / frame offset
    // Only works for integer sample offsets on frame boundaries!
    SINT samples2frames(SINT samples) const {
        DEBUG_ASSERT(getChannelCount().isValid());
        DEBUG_ASSERT(0 == (samples % getChannelCount()));
        return samples / getChannelCount();
    }

    // Conversion: #frames / frame offset -> #samples / sample offset
    SINT frames2samples(SINT frames) const {
        DEBUG_ASSERT(getChannelCount().isValid());
        return frames * getChannelCount();
    }

    // Conversion: #frames / frame offset -> second offset
    double frames2secs(SINT frames) const {
        DEBUG_ASSERT(getSampleRate().isValid());
        return static_cast<double>(frames) / getSampleRate();
    }

    // Conversion: second offset -> #frames / frame offset
    double secs2frames(double seconds) const {
        DEBUG_ASSERT(getSampleRate().isValid());
        return seconds * getSampleRate();
    }

    // Conversion: #frames / frame offset -> millisecond offset
    double frames2millis(SINT frames) const {
        return frames2secs(frames) * 1000;
    }

    // Conversion: millisecond offset -> #frames / frame offset
    double millis2frames(double milliseconds) const {
        return secs2frames(milliseconds / 1000);
    }
};

bool operator==(
        const SignalInfo& lhs,
        const SignalInfo& rhs);

inline bool operator!=(
        const SignalInfo& lhs,
        const SignalInfo& rhs) {
    return !(lhs == rhs);
}

QDebug operator<<(QDebug dbg, const SignalInfo& arg);

} // namespace audio

} // namespace mixxx

Q_DECLARE_METATYPE(mixxx::audio::SignalInfo)