-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathchannels.cpp
206 lines (179 loc) · 7.21 KB
/
channels.cpp
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
#include "nmos/channels.h"
#include <iomanip>
#include <boost/algorithm/string/join.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include "nmos/json_fields.h"
namespace nmos
{
namespace channel_symbols
{
// Numbered Source Channel (001..127)
const channel_symbol NSC(unsigned int channel_number)
{
utility::ostringstream_t symbol;
symbol << U("NSC") << std::setfill(U('0')) << std::setw(3) << channel_number;
return channel_symbol{ symbol.str() };
}
// Undefined channel (01..64)
const channel_symbol Undefined(unsigned int channel_number)
{
utility::ostringstream_t symbol;
symbol << U("U") << std::setfill(U('0')) << std::setw(2) << channel_number;
return channel_symbol{ symbol.str() };
}
}
web::json::value make_channel(const channel& channel)
{
return web::json::value_of({
{ nmos::fields::label, channel.label },
{ nmos::fields::symbol, channel.symbol.name }
});
}
channel parse_channel(const web::json::value& channel)
{
return{
nmos::fields::label(channel),
channel_symbol{ nmos::fields::symbol(channel) }
};
}
// Audio channel order convention grouping symbols
// See SMPTE ST 2110-30:2017 Table 1 - Channel Order Convention Grouping Symbols
DEFINE_STRING_ENUM(channel_group_symbol)
namespace channel_group_symbols
{
// Mono
const channel_group_symbol M{ U("M") };
// Dual Mono
const channel_group_symbol DM{ U("DM") };
// Standard Stereo
const channel_group_symbol ST{ U("ST") };
// Matrix Stereo
const channel_group_symbol LtRt{ U("LtRt") };
// 5.1 Surround
const channel_group_symbol S51{ U("51") };
// 7.1 Surround
const channel_group_symbol S71{ U("71") };
// 22.2 Surround
const channel_group_symbol S222{ U("222") };
// One SDI audio group
const channel_group_symbol SGRP{ U("SGRP") };
// Undefined group (01..64)
// "Undefined is a special designation which is used to signal groups of channels whose mix is unknown or un-representable,
// or otherwise non-conformant to the other defined groupings and orderings in the convention."
const channel_group_symbol Undefined(unsigned int channel_count)
{
utility::ostringstream_t symbol;
symbol << U("U") << std::setfill(U('0')) << std::setw(2) << channel_count;
return channel_group_symbol{ symbol.str() };
}
#include "cpprest/details/push_undef_u.h"
inline const channel_group_symbol U(unsigned int channel_count)
{
return Undefined(channel_count);
}
#include "cpprest/details/pop_u.h"
}
namespace details
{
// carefully ordered 'largest' to 'smallest'
static const std::vector<std::pair<std::vector<channel_symbol>, channel_group_symbol>> channel_groups{
{ channel_symbols::S71, channel_group_symbols::S71 },
{ channel_symbols::S51, channel_group_symbols::S51 },
{ channel_symbols::LtRt, channel_group_symbols::LtRt },
{ channel_symbols::ST, channel_group_symbols::ST },
{ channel_symbols::DM, channel_group_symbols::DM },
{ channel_symbols::M, channel_group_symbols::M }
};
std::vector<channel_group_symbol> make_channel_group_symbols(const std::vector<channel_symbol>& channels)
{
std::vector<channel_group_symbol> group_symbols;
unsigned int undefined_count = 0;
auto channel = channels.begin();
while (channels.end() != channel)
{
auto group = channel_groups.begin();
while (channel_groups.end() != group)
{
auto ch = channel;
auto group_ch = group->first.begin();
while (channels.end() != ch && group->first.end() != group_ch && *ch == *group_ch)
{
++ch;
++group_ch;
}
if (group->first.end() == group_ch)
{
channel = ch;
break;
}
++group;
}
if (channel_groups.end() != group)
{
if (0 != undefined_count)
{
group_symbols.push_back(nmos::channel_group_symbols::Undefined(undefined_count));
undefined_count = 0;
}
group_symbols.push_back(group->second);
}
else
{
++undefined_count;
++channel;
}
}
if (0 != undefined_count)
{
group_symbols.push_back(nmos::channel_group_symbols::Undefined(undefined_count));
}
return group_symbols;
}
}
// See SMPTE ST 2110-30:2017 Section 6.2.2 Channel Order Convention
utility::string_t make_fmtp_channel_order(const std::vector<channel_symbol>& channels)
{
utility::ostringstream_t channel_order;
channel_order << U("SMPTE2110.(") << boost::algorithm::join(details::make_channel_group_symbols(channels) | boost::adaptors::transformed([](const channel_group_symbol& symbol)
{
return symbol.name;
}), U(",")) << U(")");
return channel_order.str();
}
// See SMPTE ST 2110-30:2017 Section 6.2.2 Channel Order Convention
std::vector<nmos::channel_symbol> parse_fmtp_channel_order(const utility::string_t& channel_order)
{
std::vector<nmos::channel_symbol> channels;
const auto first = channel_order.data();
const auto last = first + channel_order.size();
auto it = first;
// check prefix
static const auto prefix = U("SMPTE2110.(");
auto pit = &prefix[0];
while (it != last && *pit != U('\0') && *it == *pit) ++it, ++pit;
if (*pit != U('\0')) return {};
// parse comma-separated channel group symbols
while (true)
{
const auto git = it;
while (it != last && *it != U(')') && *it != U(',')) ++it;
if (it == last) return {};
const channel_group_symbol symbol(utility::string_t(git, it));
// hm, does not handle 22.2 Surround ('222'), SDI audio group ('SGRP') or Undefined ('U01' to 'U64')
auto group = std::find_if(details::channel_groups.begin(), details::channel_groups.end(),
[&](const std::pair<std::vector<channel_symbol>, channel_group_symbol>& group)
{
return symbol == group.second;
});
if (details::channel_groups.end() == group) return {};
channels.insert(channels.end(), group->first.begin(), group->first.end());
if (*it == U(')')) break;
++it;
}
// check suffix
if (it == last) return {};
++it;
if (it != last) return {};
return channels;
}
}