summaryrefslogtreecommitdiffstats
path: root/src/audio_core/mix_context.cpp
blob: 8e150db03b8fae3ef7e52cffffa500011ed2cb48 (plain) (blame)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include "audio_core/behavior_info.h"
#include "audio_core/common.h"
#include "audio_core/mix_context.h"
#include "audio_core/splitter_context.h"

namespace AudioCore {
MixContext::MixContext() = default;
MixContext::~MixContext() = default;

void MixContext::Initialize(const BehaviorInfo& behavior_info, std::size_t mix_count) {
    info_count = mix_count;
    infos.resize(info_count);
    auto& final_mix = GetInfo(AudioCommon::FINAL_MIX);
    final_mix.GetInParams().mix_id = AudioCommon::FINAL_MIX;
    for (auto& info : infos) {
        sorted_info.push_back(&info);
    }

    // Only initialize our edge matrix and node states if splitters are supported
    if (behavior_info.IsSplitterSupported()) {
        node_states.Initialize(mix_count);
        edge_matrix.Initialize(mix_count);
    }
}

void MixContext::UpdateDistancesFromFinalMix() {
    // Set all distances to be invalid
    for (std::size_t i = 0; i < info_count; i++) {
        GetInfo(i).GetInParams().final_mix_distance = AudioCommon::NO_FINAL_MIX;
    }

    for (std::size_t i = 0; i < info_count; i++) {
        auto& info = GetInfo(i);
        auto& in_params = info.GetInParams();
        // Populate our sorted info
        sorted_info[i] = &info;

        if (!in_params.in_use) {
            continue;
        }

        auto mix_id = in_params.mix_id;
        // Needs to be referenced out of scope
        s32 distance_to_final_mix{AudioCommon::FINAL_MIX};
        for (; distance_to_final_mix < info_count; distance_to_final_mix++) {
            if (mix_id == AudioCommon::FINAL_MIX) {
                // If we're at the final mix, we're done
                break;
            } else if (mix_id == AudioCommon::NO_MIX) {
                // If we have no more mix ids, we're done
                distance_to_final_mix = AudioCommon::NO_FINAL_MIX;
                break;
            } else {
                const auto& dest_mix = GetInfo(mix_id);
                const auto dest_mix_distance = dest_mix.GetInParams().final_mix_distance;

                if (dest_mix_distance == AudioCommon::NO_FINAL_MIX) {
                    // If our current mix isn't pointing to a final mix, follow through
                    mix_id = dest_mix.GetInParams().dest_mix_id;
                } else {
                    // Our current mix + 1 = final distance
                    distance_to_final_mix = dest_mix_distance + 1;
                    break;
                }
            }
        }

        // If we're out of range for our distance, mark it as no final mix
        if (distance_to_final_mix >= info_count) {
            distance_to_final_mix = AudioCommon::NO_FINAL_MIX;
        }

        in_params.final_mix_distance = distance_to_final_mix;
    }
}

void MixContext::CalcMixBufferOffset() {
    s32 offset{};
    for (std::size_t i = 0; i < info_count; i++) {
        auto& info = GetSortedInfo(i);
        auto& in_params = info.GetInParams();
        if (in_params.in_use) {
            // Only update if in use
            in_params.buffer_offset = offset;
            offset += in_params.buffer_count;
        }
    }
}

void MixContext::SortInfo() {
    // Get the distance to the final mix
    UpdateDistancesFromFinalMix();

    // Sort based on the distance to the final mix
    std::sort(sorted_info.begin(), sorted_info.end(),
              [](const ServerMixInfo* lhs, const ServerMixInfo* rhs) {
                  return lhs->GetInParams().final_mix_distance >
                         rhs->GetInParams().final_mix_distance;
              });

    // Calculate the mix buffer offset
    CalcMixBufferOffset();
}

bool MixContext::TsortInfo(SplitterContext& splitter_context) {
    // If we're not using mixes, just calculate the mix buffer offset
    if (!splitter_context.UsingSplitter()) {
        CalcMixBufferOffset();
        return true;
    }
    // Sort our node states
    if (!node_states.Tsort(edge_matrix)) {
        return false;
    }

    // Get our sorted list
    const auto sorted_list = node_states.GetIndexList();
    std::size_t info_id{};
    for (auto itr = sorted_list.rbegin(); itr != sorted_list.rend(); ++itr) {
        // Set our sorted info
        sorted_info[info_id++] = &GetInfo(*itr);
    }

    // Calculate the mix buffer offset
    CalcMixBufferOffset();
    return true;
}

std::size_t MixContext::GetCount() const {
    return info_count;
}

ServerMixInfo& MixContext::GetInfo(std::size_t i) {
    ASSERT(i < info_count);
    return infos.at(i);
}

const ServerMixInfo& MixContext::GetInfo(std::size_t i) const {
    ASSERT(i < info_count);
    return infos.at(i);
}

ServerMixInfo& MixContext::GetSortedInfo(std::size_t i) {
    ASSERT(i < info_count);
    return *sorted_info.at(i);
}

const ServerMixInfo& MixContext::GetSortedInfo(std::size_t i) const {
    ASSERT(i < info_count);
    return *sorted_info.at(i);
}

ServerMixInfo& MixContext::GetFinalMixInfo() {
    return infos.at(AudioCommon::FINAL_MIX);
}

const ServerMixInfo& MixContext::GetFinalMixInfo() const {
    return infos.at(AudioCommon::FINAL_MIX);
}

EdgeMatrix& MixContext::GetEdgeMatrix() {
    return edge_matrix;
}

const EdgeMatrix& MixContext::GetEdgeMatrix() const {
    return edge_matrix;
}

ServerMixInfo::ServerMixInfo() {
    Cleanup();
}
ServerMixInfo::~ServerMixInfo() = default;

const ServerMixInfo::InParams& ServerMixInfo::GetInParams() const {
    return in_params;
}

ServerMixInfo::InParams& ServerMixInfo::GetInParams() {
    return in_params;
}

bool ServerMixInfo::Update(EdgeMatrix& edge_matrix, const MixInfo::InParams& mix_in,
                           BehaviorInfo& behavior_info, SplitterContext& splitter_context) {
    in_params.volume = mix_in.volume;
    in_params.sample_rate = mix_in.sample_rate;
    in_params.buffer_count = mix_in.buffer_count;
    in_params.in_use = mix_in.in_use;
    in_params.mix_id = mix_in.mix_id;
    in_params.node_id = mix_in.node_id;
    for (std::size_t i = 0; i < mix_in.mix_volume.size(); i++) {
        std::copy(mix_in.mix_volume[i].begin(), mix_in.mix_volume[i].end(),
                  in_params.mix_volume[i].begin());
    }

    bool require_sort = false;

    if (behavior_info.IsSplitterSupported()) {
        require_sort = UpdateConnection(edge_matrix, mix_in, splitter_context);
    } else {
        in_params.dest_mix_id = mix_in.dest_mix_id;
        in_params.splitter_id = AudioCommon::NO_SPLITTER;
    }

    // TODO(ogniK): Update effect processing order
    return require_sort;
}

bool ServerMixInfo::HasAnyConnection() const {
    return in_params.splitter_id != AudioCommon::NO_SPLITTER ||
           in_params.mix_id != AudioCommon::NO_MIX;
}

void ServerMixInfo::Cleanup() {
    in_params.volume = 0.0f;
    in_params.sample_rate = 0;
    in_params.buffer_count = 0;
    in_params.in_use = false;
    in_params.mix_id = AudioCommon::NO_MIX;
    in_params.node_id = 0;
    in_params.buffer_offset = 0;
    in_params.dest_mix_id = AudioCommon::NO_MIX;
    in_params.splitter_id = AudioCommon::NO_SPLITTER;
    std::memset(in_params.mix_volume.data(), 0, sizeof(float) * in_params.mix_volume.size());
}

bool ServerMixInfo::UpdateConnection(EdgeMatrix& edge_matrix, const MixInfo::InParams& mix_in,
                                     SplitterContext& splitter_context) {
    // Mixes are identical
    if (in_params.dest_mix_id == mix_in.dest_mix_id &&
        in_params.splitter_id == mix_in.splitter_id &&
        ((in_params.splitter_id == AudioCommon::NO_SPLITTER) ||
         !splitter_context.GetInfo(in_params.splitter_id).HasNewConnection())) {
        return false;
    }
    // Remove current edges for mix id
    edge_matrix.RemoveEdges(in_params.mix_id);
    if (mix_in.dest_mix_id != AudioCommon::NO_MIX) {
        // If we have a valid destination mix id, set our edge matrix
        edge_matrix.Connect(in_params.mix_id, mix_in.dest_mix_id);
    } else if (mix_in.splitter_id != AudioCommon::NO_SPLITTER) {
        // Recurse our splitter linked and set our edges
        auto& splitter_info = splitter_context.GetInfo(mix_in.splitter_id);
        const auto length = splitter_info.GetLength();
        for (s32 i = 0; i < length; i++) {
            const auto* splitter_destination =
                splitter_context.GetDestinationData(mix_in.splitter_id, i);
            if (splitter_destination == nullptr) {
                continue;
            }
            if (splitter_destination->ValidMixId()) {
                edge_matrix.Connect(in_params.mix_id, splitter_destination->GetMixId());
            }
        }
    }
    in_params.dest_mix_id = mix_in.dest_mix_id;
    in_params.splitter_id = mix_in.splitter_id;
    return true;
}

} // namespace AudioCore