Twitch SDK (Internal)
operatorchainbuilder.h
Go to the documentation of this file.
1 /********************************************************************************************
2  * Twitch Broadcasting SDK
3  *
4  * This software is supplied under the terms of a license agreement with Twitch Interactive, Inc. and
5  * may not be copied or used except in accordance with the terms of that agreement
6  * Copyright (c) 2012-2017 Twitch Interactive, Inc.
7  *
8  *********************************************************************************************/
9 
10 #pragma once
11 
25 
26 namespace ttv
27 {
36  template <typename ContextType, typename InputBufferFormat, typename OutputBufferFormat, size_t SelectedChannelIndex>
38  {
43  template <typename ScalarType, typename Enable = void>
45  {
46  using Type = ScalarType;
47  };
48 
49  template <typename ScalarType>
50  struct MakeSignedIfIntegralSelector<ScalarType, std::enable_if_t<std::is_integral<ScalarType>::value>>
51  {
52  using Type = std::make_signed_t<ScalarType>;
53  };
54 
55  template <typename ScalarType>
57 
58 
65  {
66  template <typename T = void>
68 
69  static constexpr size_t SampleRate = InputBufferFormat::SampleRate;
70  using SampleType = typename InputBufferFormat::SampleType;
71  };
72 
73  template <typename InputSource>
75  {
76  template <typename T = void>
78 
79  static constexpr size_t SampleRate = InputSource::SampleRate;
81  };
82 
83  template <typename InputSource>
85  {
86  template <typename T = void>
88 
89  static constexpr size_t SampleRate = InputSource::SampleRate;
90  using SampleType = std::make_unsigned_t<typename InputSource::SampleType>;
91  };
92 
93  template <typename InputSource, typename OutputSampleType>
95  {
96  template <typename T = void>
98 
99  static constexpr size_t SampleRate = InputSource::SampleRate;
100  using SampleType = OutputSampleType;
101  };
102 
103  template <typename InputSource, typename OutputSampleType>
105  {
106  template <typename T = void>
107  using Unwrap = std::enable_if_t<std::is_void<T>::value, ConvertToIntegralTypeOperator<typename InputSource::template Unwrap<>, OutputSampleType, ContextType>>;
108 
109  static constexpr size_t SampleRate = InputSource::SampleRate;
110  using SampleType = OutputSampleType;
111  };
112 
113  template <typename InputSource, typename OutputSampleType>
115  {
116  template <typename T = void>
118 
119  static constexpr size_t SampleRate = InputSource::SampleRate;
120  using SampleType = OutputSampleType;
121  };
122 
123  template <typename InputSource, typename OutputSampleType>
125  {
126  template <typename T = void>
128 
129  static constexpr size_t SampleRate = InputSource::SampleRate;
130  using SampleType = OutputSampleType;
131  };
132 
133  template <typename InputSource, typename OutputSampleType>
135  {
136  template <typename T = void>
138 
139  static constexpr size_t SampleRate = InputSource::SampleRate;
140  using SampleType = OutputSampleType;
141  };
142 
143 
144  template <typename InputSource, size_t SampleRateArg>
146  {
147  template <typename T = void>
149 
150  static constexpr size_t SampleRate = SampleRateArg;
151  using SampleType = typename InputSource::SampleType;
152  };
153 
154 
155  template <typename Input>
156  using MakeSigned = std::conditional_t<
157  /* if */ std::is_unsigned<typename Input::SampleType>::value,
159  /* else */ Input
160  >;
161 
162  template <typename Input>
163  using FlipSignedness = std::conditional_t<
164  /* if */ std::is_signed<typename Input::SampleType>::value,
166  /* else */ ConvertToSignedWrapper<Input>
167  >;
168 
169  template <typename Input, typename OutputSampleType>
170  using MatchSignedness = std::conditional_t<
171  /* if */ std::is_signed<typename Input::SampleType>::value == std::is_signed<OutputSampleType>::value,
172  /* then */ Input,
173  /* else */ FlipSignedness<Input>
174  >;
175 
176  template <typename Input, typename OutputSampleType>
177  using ConvertFloatingToSampleType = std::conditional_t<
178  /* if */ std::is_floating_point<OutputSampleType>::value,
181  >;
182 
183  template <typename Input, typename OutputSampleType>
184  using ConvertIntegralDepth = std::conditional_t<
185  /* if */ sizeof(typename Input::SampleType) < sizeof(OutputSampleType),
188  >;
189 
190  template <typename Input, typename OutputSampleType>
191  using MatchIntegralDepth = std::conditional_t<
192  /* if */ sizeof(typename Input::SampleType) == sizeof(OutputSampleType),
193  /* then */ Input,
195  >;
196 
197  template <typename Input, typename OutputSampleType>
198  using ConvertIntegralToSampleType = std::conditional_t<
199  /* if */ std::is_floating_point<OutputSampleType>::value,
202  >;
203 
204  template <typename Input, typename OutputSampleType>
205  using ConvertSampleType = std::conditional_t<
206  /* if */ std::is_floating_point<typename Input::SampleType>::value,
209  >;
210 
211  template <typename Input, typename OutputSampleType>
212  using MatchSampleType = std::conditional_t<
213  /* if */ std::is_same<typename Input::SampleType, OutputSampleType>::value,
214  /* then */ Input,
216  >;
217 
218  // If we are increasing our bit depth, we should upscale before resampling. If we are decreasing our bit depth,
219  // we should resample first, then change our bit depth. This minimizes quantization error.
220  template <typename Input, typename Output>
221  using ConvertSampleRateAndType = std::conditional_t<
222  /* if */ (sizeof(typename Input::SampleType) < sizeof(typename Output::SampleType)),
223  /* then */ MatchSignedness<ResampleWrapper<MatchSampleType<Input, MakeSignedIfIntegral<typename Output::SampleType>>, Output::SampleRate>, typename Output::SampleType>,
224  /* else */ MatchSampleType<ResampleWrapper<MakeSigned<Input>, Output::SampleRate>, typename Output::SampleType>
225  >;
226 
227  template <typename Input, typename Output>
228  using ConvertFormat = std::conditional_t<
229  /* if */ Input::SampleRate == Output::SampleRate,
232  >;
233 
235 
236  using Type = typename WrappedType::template Unwrap<>;
237 
238  static_assert(std::is_same<typename OutputBufferFormat::SampleType, typename Type::SampleType>::value, "Sample type mismatch after building operator chain.");
239  static_assert(OutputBufferFormat::SampleRate == Type::SampleRate, "Sample rate mismatch after building operator chain.");
240  };
241 
242 
243  template <typename ContextType, typename InputBufferFormat, typename OutputBufferFormat, size_t SelectedChannelIndex>
245 
246 
247  template <typename ContextType, typename InputBufferFormat, typename OutputBufferFormat, size_t RemainingChannelCount, typename ...CurrentChannels>
249  {
250  using AppendedOperatorChain = BuildOperatorChain<ContextType, InputBufferFormat, OutputBufferFormat, RemainingChannelCount - 1>;
251  using Type = typename OperatorChainTupleBuilder<ContextType, InputBufferFormat, OutputBufferFormat, RemainingChannelCount - 1, AppendedOperatorChain, CurrentChannels...>::Type;
252  };
253 
254 
255  template <typename ContextType, typename InputBufferFormat, typename OutputBufferFormat, typename ...CurrentChannels>
256  struct OperatorChainTupleBuilder<ContextType, InputBufferFormat, OutputBufferFormat, 0, CurrentChannels...>
257  {
258  using Type = std::tuple<CurrentChannels...>;
259  };
260 
261 
262  template <typename ContextType, typename InputBufferFormat, typename OutputBufferFormat>
264 
265 
266  template <typename TupleType, typename ContextType, typename ...Args>
267  std::enable_if_t<sizeof...(Args) < std::tuple_size<TupleType>::value, TupleType> PopulateTupleArgs(ContextType& context, Args&... args)
268  {
269  return PopulateTupleArgs<TupleType>(context, context, args...);
270  }
271 
272 
273  template <typename TupleType, typename ContextType, typename ...Args>
274  std::enable_if_t<sizeof...(Args) == std::tuple_size<TupleType>::value, TupleType> PopulateTupleArgs(ContextType& context, Args&... args)
275  {
276  return TupleType{args...};
277  }
278 
279  template <typename TupleType, typename ContextType>
280  TupleType MakeOperatorTuple(ContextType& context)
281  {
282  return PopulateTupleArgs<TupleType>(context);
283  }
284 }
OutputSampleType SampleType
Definition: operatorchainbuilder.h:100
Definition: increasebitdepthoperator.h:32
std::conditional_t< std::is_floating_point< OutputSampleType >::value, ChangeFloatingPointDepthWrapper< Input, OutputSampleType >, MatchSignedness< ConvertToIntegralTypeWrapper< Input, MakeSignedIfIntegral< OutputSampleType > >, OutputSampleType > > ConvertFloatingToSampleType
Definition: operatorchainbuilder.h:181
typename MakeSignedIfIntegralSelector< ScalarType >::Type MakeSignedIfIntegral
Definition: operatorchainbuilder.h:56
typename OperatorChainTupleBuilder< ContextType, InputBufferFormat, OutputBufferFormat, InputBufferFormat::ChannelCount >::Type BuildOperatorChainTuple
Definition: operatorchainbuilder.h:263
Definition: converttointegraltypeoperator.h:33
Definition: resampleoperator.h:34
std::make_unsigned_t< typename InputSource::SampleType > SampleType
Definition: operatorchainbuilder.h:90
std::conditional_t< std::is_floating_point< typename Input::SampleType >::value, ConvertFloatingToSampleType< Input, OutputSampleType >, ConvertIntegralToSampleType< Input, OutputSampleType > > ConvertSampleType
Definition: operatorchainbuilder.h:209
OutputSampleType SampleType
Definition: operatorchainbuilder.h:120
typename WrappedType::template Unwrap<> Type
Definition: operatorchainbuilder.h:236
std::conditional_t< std::is_same< typename Input::SampleType, OutputSampleType >::value, Input, ConvertSampleType< Input, OutputSampleType > > MatchSampleType
Definition: operatorchainbuilder.h:216
BuildOperatorChain< ContextType, InputBufferFormat, OutputBufferFormat, RemainingChannelCount - 1 > AppendedOperatorChain
Definition: operatorchainbuilder.h:250
Definition: operatorchainbuilder.h:134
typename OperatorChainBuilder< ContextType, InputBufferFormat, OutputBufferFormat, SelectedChannelIndex >::Type BuildOperatorChain
Definition: operatorchainbuilder.h:244
Definition: operatorchainbuilder.h:44
Definition: cpp11transition.h:22
Definition: operatorchainbuilder.h:37
typename InputSource::SampleType SampleType
Definition: operatorchainbuilder.h:151
std::conditional_t<(sizeof(typename Input::SampleType)< sizeof(typename Output::SampleType)), MatchSignedness< ResampleWrapper< MatchSampleType< Input, MakeSignedIfIntegral< typename Output::SampleType > >, Output::SampleRate >, typename Output::SampleType >, MatchSampleType< ResampleWrapper< MakeSigned< Input >, Output::SampleRate >, typename Output::SampleType > > ConvertSampleRateAndType
Definition: operatorchainbuilder.h:225
Definition: converttosignedoperator.h:31
MakeSignedIfIntegral< typename InputSource::SampleType > SampleType
Definition: operatorchainbuilder.h:80
Definition: pcmbufferaudiosource.h:26
OutputSampleType SampleType
Definition: operatorchainbuilder.h:140
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
Definition: operatorchainbuilder.h:248
std::conditional_t< sizeof(typename Input::SampleType)< sizeof(OutputSampleType), IncreaseBitDepthWrapper< Input, OutputSampleType >, DecreaseBitDepthWrapper< Input, OutputSampleType > > ConvertIntegralDepth
Definition: operatorchainbuilder.h:188
Definition: converttounsignedoperator.h:31
ScalarType Type
Definition: operatorchainbuilder.h:46
typename OperatorChainTupleBuilder< ContextType, InputBufferFormat, OutputBufferFormat, RemainingChannelCount - 1, AppendedOperatorChain, CurrentChannels... >::Type Type
Definition: operatorchainbuilder.h:251
Definition: operatorchainbuilder.h:84
std::conditional_t< std::is_unsigned< typename Input::SampleType >::value, ConvertToSignedWrapper< Input >, Input > MakeSigned
Definition: operatorchainbuilder.h:160
Definition: operatorchainbuilder.h:64
Definition: operatorchainbuilder.h:74
Definition: decreasebitdepthoperator.h:32
Definition: converttofloatingpointoperator.h:32
std::conditional_t< std::is_signed< typename Input::SampleType >::value, ConvertToUnsignedWrapper< Input >, ConvertToSignedWrapper< Input > > FlipSignedness
Definition: operatorchainbuilder.h:167
ConvertFormat< InputSourceWrapper, OutputBufferFormat > WrappedType
Definition: operatorchainbuilder.h:234
Definition: changefloatingpointdepthoperator.h:32
std::conditional_t< Input::SampleRate==Output::SampleRate, MatchSampleType< Input, typename Output::SampleType >, ConvertSampleRateAndType< Input, Output > > ConvertFormat
Definition: operatorchainbuilder.h:232
Definition: operatorchainbuilder.h:124
OutputSampleType SampleType
Definition: operatorchainbuilder.h:130
Definition: operatorchainbuilder.h:145
std::conditional_t< std::is_signed< typename Input::SampleType >::value==std::is_signed< OutputSampleType >::value, Input, FlipSignedness< Input > > MatchSignedness
Definition: operatorchainbuilder.h:174
Definition: operatorchainbuilder.h:114
Definition: operatorchainbuilder.h:104
std::conditional_t< sizeof(typename Input::SampleType)==sizeof(OutputSampleType), Input, ConvertIntegralDepth< Input, OutputSampleType > > MatchIntegralDepth
Definition: operatorchainbuilder.h:195
typename InputBufferFormat::SampleType SampleType
Definition: operatorchainbuilder.h:70
OutputSampleType SampleType
Definition: operatorchainbuilder.h:110
std::conditional_t< std::is_floating_point< OutputSampleType >::value, ConvertToFloatingTypeWrapper< MatchSignedness< Input, OutputSampleType >, OutputSampleType >, MatchIntegralDepth< MatchSignedness< Input, OutputSampleType >, OutputSampleType > > ConvertIntegralToSampleType
Definition: operatorchainbuilder.h:202
std::enable_if_t< std::is_void< T >::value, ConvertToIntegralTypeOperator< typename InputSource::template Unwrap<>, OutputSampleType, ContextType > > Unwrap
Definition: operatorchainbuilder.h:107