~aleteoryx/muditaos

muditaos/module-audio/Audio/AudioFormat.cpp -rw-r--r-- 2.9 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "AudioFormat.hpp"

#include <integer.hpp>

using audio::AudioFormat;

auto AudioFormat::getSampleRate() const noexcept -> unsigned int
{
    return sampleRate;
}

auto AudioFormat::getBitWidth() const noexcept -> unsigned int
{
    return bitWidth;
}

auto AudioFormat::getChannels() const noexcept -> unsigned int
{
    return channels;
}

auto AudioFormat::getBitrate() const noexcept -> unsigned long int
{
    return sampleRate * bitWidth * channels;
}

auto AudioFormat::toString() const -> std::string
{
    return "AudioFormat{" + std::to_string(sampleRate) + "," + std::to_string(bitWidth) + "," +
           std::to_string(channels) + "}";
}

auto AudioFormat::operator==(const AudioFormat &other) const -> bool
{
    return sampleRate == other.sampleRate && bitWidth == other.bitWidth && channels == other.channels;
}

auto AudioFormat::operator!=(const AudioFormat &other) const -> bool
{
    return !operator==(other);
}

auto AudioFormat::operator>(const AudioFormat &other) const -> bool
{
    return getBitrate() > other.getBitrate();
}

auto AudioFormat::operator<(const AudioFormat &other) const -> bool
{
    return getBitrate() < other.getBitrate();
}

auto AudioFormat::operator<=(const AudioFormat &other) const -> bool
{
    return getBitrate() <= other.getBitrate();
}

auto AudioFormat::operator>=(const AudioFormat &other) const -> bool
{
    return getBitrate() >= other.getBitrate();
}

auto AudioFormat::isValid() const noexcept -> bool
{
    return !(sampleRate == 0 || bitWidth == 0 || channels == 0);
}

auto AudioFormat::makeMatrix(std::set<unsigned int> sampleRates,
                             std::set<unsigned int> bitWidths,
                             std::set<unsigned int> channels) -> std::vector<AudioFormat>
{
    std::vector<AudioFormat> v;

    for (auto sampleRate : sampleRates) {
        for (auto bitWidth : bitWidths) {
            for (auto channelCount : channels) {
                v.push_back(AudioFormat{sampleRate, bitWidth, channelCount});
            }
        }
    }

    return v;
}

auto AudioFormat::isNull() const noexcept -> bool
{
    return operator==(audio::nullFormat);
}

auto AudioFormat::bytesToMicroseconds(unsigned int bytes) const noexcept -> std::chrono::microseconds
{
    auto bytesPerSecond = getBitrate() / utils::integer::BitsInByte;
    auto duration       = std::chrono::duration<double>(static_cast<double>(bytes) / bytesPerSecond);

    return std::chrono::duration_cast<std::chrono::microseconds>(duration);
}

auto AudioFormat::microsecondsToBytes(std::chrono::microseconds duration) const noexcept -> unsigned int
{
    auto bytesPerSecond = getBitrate() / utils::integer::BitsInByte;
    auto seconds        = std::chrono::duration<double>(duration).count();
    return static_cast<unsigned int>(seconds * bytesPerSecond);
}