Chromium Code Reviews

Side by Side Diff: services/media/audio/platform/generic/mixers/mixer_utils.h

Issue 1424933002: Add an initial revision of an audio server. (Closed) Base URL: https://github.com/domokit/mojo.git@change4
Patch Set: refactor MixerKernel into a class to prepare for the addition of a linear interpolation sampler Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef SERVICES_MEDIA_AUDIO_PLATFORM_GENERIC_MIXERS_MIXER_UTILS_H_
6 #define SERVICES_MEDIA_AUDIO_PLATFORM_GENERIC_MIXERS_MIXER_UTILS_H_
7
8 namespace mojo {
9 namespace media {
10 namespace audio {
11 namespace mixers {
12 namespace utils {
13
14 // mixer_utils.h is a collection of inline templated utility functions meant to
15 // be used by mixer implementations and expanded/optimized at compile time in
16 // order to produce efficient inner mixing loops for all of the different
17 // variations of source/destination sample type/channel counts.
18
19 // Template to read samples and normalize them into signed 32 bit integers.
jeffbrown 2015/11/04 23:43:34 What you're not saying in this comment is that tho
johngro 2015/11/06 02:20:26 because they don't They contain either signed 16
20 template <typename SType, typename Enable = void> class SampleNormalizer;
21
22 template <typename SType>
23 class SampleNormalizer<
24 SType,
25 typename std::enable_if<
26 std::is_same<SType, uint8_t>::value,
27 void>::type> {
28 public:
29 static inline int32_t Read(const SType* src) {
30 register SType tmp = static_cast<const SType*>(src)[0];
jeffbrown 2015/11/04 23:43:34 *src instead of src[0]? I don't see why you need
johngro 2015/11/06 02:20:27 Done. I think that this is a leftover from an ear
31 return (static_cast<int32_t>(tmp) << 8) - 0x8000;
32 }
33 };
34
35 template <typename SType>
36 class SampleNormalizer<
37 SType,
38 typename std::enable_if<
39 std::is_same<SType, int16_t>::value,
40 void>::type> {
41 public:
42 static inline int32_t Read(const SType* src) {
43 return static_cast<int32_t>(static_cast<const SType*>(src)[0]);
jeffbrown 2015/11/04 23:43:34 Why isn't this good enough? (Of course even the c
johngro 2015/11/06 02:20:26 Done. it is, see above. I think this was leftove
44 }
45 };
46
47 // Template to read normalized source samples, and combine channels if required.
jeffbrown 2015/11/04 23:43:34 Doesn't this need a downmix matrix in the general
johngro 2015/11/06 02:20:27 Absolutely. Its on the feature list, I'm not sure
48 template <typename SType,
49 size_t SChCount,
50 size_t DChCount,
51 typename Enable = void>
52 class SrcReader;
jeffbrown 2015/11/04 23:43:34 SrcReader -> SampleReader?
johngro 2015/11/06 02:20:26 its not really a generalized sampler reader, howev
53
54 template <typename SType,
55 size_t SChCount,
56 size_t DChCount>
57 class SrcReader<SType, SChCount, DChCount,
58 typename std::enable_if<
59 (SChCount == DChCount) ||
60 ((SChCount == 1) && (DChCount == 2)),
61 void>::type> {
62 public:
63 static constexpr size_t DstPerSrc = DChCount / SChCount;
64 static inline int32_t Read(const SType* src) {
65 return SampleNormalizer<SType>::Read(src);
66 }
67 };
68
69 template <typename SType,
70 size_t SChCount,
71 size_t DChCount>
72 class SrcReader<SType, SChCount, DChCount,
73 typename std::enable_if<
74 (SChCount == 2) && (DChCount == 1),
75 void>::type> {
76 public:
77 static constexpr size_t DstPerSrc = 1;
78 static inline int32_t Read(const SType* src) {
79 return (SampleNormalizer<SType>::Read(src + 0) +
80 SampleNormalizer<SType>::Read(src + 1)) >> 1;
81 }
82 };
83
84 // Template to produce destination samples from normalized samples.
85 template <typename DType, typename Enable = void> class DstConverter;
jeffbrown 2015/11/04 23:43:34 DstConverter -> SampleWriter?
johngro 2015/11/06 02:20:27 This is not a writer at all. This is a template w
86
87 template <typename DType>
88 class DstConverter<DType,
89 typename std::enable_if<
90 std::is_same<DType, int16_t>::value,
91 void>::type> {
92 public:
93 static inline DType Convert(int32_t sample) {
94 return static_cast<DType>(sample);
95 }
96 };
97
98 template <typename DType>
99 class DstConverter<DType,
100 typename std::enable_if<
101 std::is_same<DType, uint8_t>::value,
102 void>::type> {
103 public:
104 static inline DType Convert(int32_t sample) {
105 return static_cast<DType>((sample >> 8) + 0x80);
106 }
107 };
108
109 // Template to mix destination samples with normalized source samples based on
110 // accumulation policy.
111 template <typename DType,
112 bool DoAccumulate,
113 typename Enable = void>
114 class DstMixer;
115
116 template <typename DType,
117 bool DoAccumulate>
118 class DstMixer<DType, DoAccumulate,
119 typename std::enable_if<
120 DoAccumulate == false,
jeffbrown 2015/11/04 23:43:34 Instead of testing for DoAccumulate == false maybe
johngro 2015/11/06 02:20:26 Bear in mind, the testing here is happening at com
121 void>::type> {
122 public:
123 static inline int32_t Mix(const DType* dst, uint32_t sample) {
124 return DstConverter<DType>::Convert(sample);
125 }
126 };
127
128 template <typename DType,
129 bool DoAccumulate>
130 class DstMixer<DType, DoAccumulate,
131 typename std::enable_if<
132 DoAccumulate == true,
133 void>::type> {
134 public:
135 static inline int32_t Mix(const DType* dst, int32_t sample) {
jeffbrown 2015/11/04 23:43:34 I'll admit to being deeply confused by this functi
johngro 2015/11/06 02:20:27 See above. Clamping on a sample by sample basis i
136 sample += SampleNormalizer<DType>::Read(dst);
137
138 if (sample > std::numeric_limits<int16_t>::max()) {
139 return DstConverter<DType>::Convert(std::numeric_limits<int16_t>::max());
140 } else if (sample < std::numeric_limits<int16_t>::min()) {
141 return DstConverter<DType>::Convert(std::numeric_limits<int16_t>::min());
142 } else {
143 return DstConverter<DType>::Convert(sample);
144 }
145 }
146 };
147
148
149 } // namespace utils
150 } // namespace mixers
151 } // namespace audio
152 } // namespace media
153 } // namespace mojo
154
155 #endif // SERVICES_MEDIA_AUDIO_PLATFORM_GENERIC_MIXERS_MIXER_UTILS_H_
OLDNEW

Powered by Google App Engine