Twitch SDK (Internal)
converttointegraltypeoperator.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 
13 
14 namespace ttv
15 {
32  template <typename InputSource, typename IntegralType, typename ContextType>
34  {
35  public:
36  ConvertToIntegralTypeOperator(ContextType& context)
37  : mInputSource(context)
38  {
39  }
40 
41  using InputSampleType = typename InputSource::SampleType;
42  static_assert(std::is_floating_point<InputSampleType>::value, "Input sample type must be floating point.");
43  static_assert(std::is_integral<IntegralType>::value, "Output sample type must be integral.");
44  static_assert(std::is_signed<IntegralType>::value, "Output sample type must be signed.");
45 
46  using SampleType = IntegralType;
47  static constexpr size_t SampleRate = InputSource::SampleRate;
48 
49 
50  InputSource& GetInputSource()
51  {
52  return mInputSource;
53  }
54 
55 
57  {
58  return mInputSource.GetSampleRange();
59  }
60 
61 
62  SampleType operator[](size_t index) const
63  {
64  assert(index >= GetSampleRange().startIndex);
65  assert(index < GetSampleRange().startIndex + GetSampleRange().sampleCount);
66 
67  InputSampleType inputSample = mInputSource[index];
68 
69  assert(inputSample <= 1.0);
70  assert(inputSample >= -1.0);
71 
72  InputSampleType ditheredSample = ContextType::Options::Ditherer::DitherFloatValue(inputSample * static_cast<InputSampleType>(BisectRange<SampleType>()));
73 
74  // Make sure to clamp the upper end of integral types, since it can't be represented by an integer.
75  ditheredSample = std::min(ditheredSample, static_cast<InputSampleType>(std::numeric_limits<SampleType>::max()));
76  return static_cast<SampleType>(ditheredSample);
77  }
78 
79  private:
80  InputSource mInputSource;
81  };
82 }
Definition: converttointegraltypeoperator.h:33
IntegralType SampleType
Definition: converttointegraltypeoperator.h:46
InputSource & GetInputSource()
Definition: converttointegraltypeoperator.h:50
typename InputSource::SampleType InputSampleType
Definition: converttointegraltypeoperator.h:41
InputSource mInputSource
Definition: converttointegraltypeoperator.h:80
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
#define assert(expr)
Definition: assertion.h:47
SampleType operator[](size_t index) const
Definition: converttointegraltypeoperator.h:62
Definition: dsputilities.h:60
SampleRange GetSampleRange() const
Definition: converttointegraltypeoperator.h:56
static constexpr size_t SampleRate
Definition: converttointegraltypeoperator.h:47
ConvertToIntegralTypeOperator(ContextType &context)
Definition: converttointegraltypeoperator.h:36