~aleteoryx/muditaos

muditaos/module-audio/Audio/transcode/TransformFactory.cpp -rw-r--r-- 3.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
107
108
109
110
111
// 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 "TransformFactory.hpp"

#include <Audio/AudioFormat.hpp>

#include "BasicDecimator.hpp"
#include "BasicInterpolator.hpp"
#include "MonoToStereo.hpp"
#include "NullTransform.hpp"
#include "Transform.hpp"
#include "TransformComposite.hpp"

#include <algorithm>
#include <memory>
#include <stdexcept>
#include <vector>

#include <cassert>

using audio::transcode::NullTransform;
using audio::transcode::Transform;
using audio::transcode::TransformFactory;

auto TransformFactory::makeTransform(AudioFormat sourceFormat, AudioFormat sinkFormat) const
    -> std::unique_ptr<Transform>
{
    auto transforms = std::vector<std::unique_ptr<Transform>>{};

    if (sourceFormat == sinkFormat) {
        return std::make_unique<NullTransform>();
    }

    if (sourceFormat.getBitWidth() != sinkFormat.getBitWidth()) {
        throw std::runtime_error("Bitwidth conversion is not implemented");
    }

    if (sourceFormat.getSampleRate() != sinkFormat.getSampleRate()) {
        transforms.push_back(getSamplerateTransform(sourceFormat, sinkFormat));
    }

    if (sourceFormat.getChannels() != sinkFormat.getChannels()) {
        transforms.push_back(getChannelsTransform(sourceFormat, sinkFormat));
    }

    assert(!transforms.empty());

    if (transforms.size() > 1) {
        auto transformsListForComposite = std::vector<std::shared_ptr<Transform>>{};
        std::move(std::begin(transforms), std::end(transforms), std::back_inserter(transformsListForComposite));
        return std::make_unique<audio::transcode::TransformComposite>(transformsListForComposite);
    }
    else {
        return std::move(transforms[0]);
    }
}

auto TransformFactory::getSamplerateTransform(AudioFormat sourceFormat, AudioFormat sinkFormat) const
    -> std::unique_ptr<Transform>
{
    static constexpr auto supportedSampleRateCoversionRatio = 2U;
    static constexpr auto supportedBitWidth                 = 16U;
    static constexpr auto supportedChannelCount             = 1U;

    auto sourceRate = sourceFormat.getSampleRate();
    auto sinkRate   = sinkFormat.getSampleRate();

    auto greater = std::max(sourceRate, sinkRate);
    auto lesser  = std::min(sourceRate, sinkRate);

    if (greater % lesser != 0) {
        throw std::invalid_argument("Sample rate conversion is not supported");
    }

    auto ratio = greater / lesser;
    if (ratio != supportedSampleRateCoversionRatio) {
        throw std::invalid_argument("Sample rate conversion is not supported (ratio != 2)");
    }

    if (sourceFormat.getBitWidth() != supportedBitWidth) {
        throw std::invalid_argument("Sample rate conversion with bit width other than 16 is not supported");
    }

    if (sourceFormat.getChannels() != supportedChannelCount) {
        throw std::invalid_argument("Sample rate conversion supported with mono only");
    }

    if (sourceRate > sinkRate) {
        return std::make_unique<audio::transcode::BasicDecimator<std::uint16_t,
                                                                 supportedChannelCount,
                                                                 supportedSampleRateCoversionRatio>>();
    }
    else {
        return std::make_unique<audio::transcode::BasicInterpolator<std::uint16_t,
                                                                    supportedChannelCount,
                                                                    supportedSampleRateCoversionRatio>>();
    }
}

auto TransformFactory::getChannelsTransform(AudioFormat sourceFormat, AudioFormat sinkFormat) const
    -> std::unique_ptr<Transform>
{
    if (sourceFormat.getChannels() == 1 && sinkFormat.getChannels() == 2 && sourceFormat.getBitWidth() == 16) {
        auto transform = std::make_unique<audio::transcode::MonoToStereo>();
        return transform;
    }
    else {
        throw std::invalid_argument("Channels conversion is not supported");
    }
}