OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include "webrtc/modules/audio_processing/test/fake_recording_device.h" | |
12 | |
13 #include "webrtc/base/logging.h" | |
14 | |
15 namespace webrtc { | |
16 | |
17 FakeRecordingDevice::FakeRecordingDevice(LevelToScalingMappingKind mapping_kind) | |
18 : mapping_kind_(mapping_kind) {} | |
19 | |
20 FakeRecordingDevice::~FakeRecordingDevice() = default; | |
21 | |
22 // |level| must be within [0, 255]. | |
23 void FakeRecordingDevice::set_analog_level(int level) { | |
24 RTC_DCHECK_LE(0, level); | |
25 RTC_DCHECK_LE(level, 255); | |
26 level_ = level; | |
27 } | |
28 | |
29 int FakeRecordingDevice::analog_level() const { | |
30 return level_; | |
31 } | |
32 | |
33 FakeRecordingDevice::LevelToScalingMappingKind | |
34 FakeRecordingDevice::mapping_kind() const { | |
35 return mapping_kind_; | |
36 } | |
37 | |
38 void FakeRecordingDevice::ProcessStream( | |
39 std::vector<rtc::ArrayView<const float>> src, | |
40 std::vector<rtc::ArrayView<float>> dest) { | |
41 RTC_DCHECK_EQ(src.size(), dest.size()); | |
42 | |
43 const float scaling_factor = ComputeCompoundScalingFactor(); | |
44 | |
45 for (size_t i = 0; i < src.size(); ++i) { | |
46 RTC_DCHECK_EQ(src[i].size(), dest[i].size()); | |
47 std::transform(src[i].begin(), src[i].end(), dest[i].begin(), | |
48 [scaling_factor](float x) { return scaling_factor * x; }); | |
49 } | |
50 real_device_level_.reset(); | |
51 } | |
52 | |
53 void FakeRecordingDevice::ProcessStream(const AudioFrame* src, | |
54 AudioFrame* dest) { | |
55 RTC_DCHECK_EQ(src->num_channels_, dest->num_channels_); | |
56 RTC_DCHECK_EQ(src->samples_per_channel_, dest->samples_per_channel_); | |
57 const size_t number_of_samples = | |
58 src->samples_per_channel_ * src->num_channels_; | |
59 RTC_DCHECK_LE(number_of_samples, AudioFrame::kMaxDataSizeSamples); | |
60 const float scaling_factor = ComputeCompoundScalingFactor(); | |
61 | |
62 std::transform(src->data_, src->data_ + number_of_samples, dest->data_, | |
63 [scaling_factor](int16_t x) { | |
64 return rtc::saturated_cast<int16_t>(scaling_factor * x); | |
65 }); | |
66 | |
67 real_device_level_.reset(); | |
68 } | |
69 | |
70 void FakeRecordingDevice::NotifyAudioDeviceLevel(int level) { | |
71 real_device_level_.emplace(level); | |
72 } | |
73 | |
74 float FakeRecordingDevice::ComputeCompoundScalingFactor() { | |
75 const float level_scaling_factor = GetScalingFactor(level_); | |
76 const float undo_factor = | |
77 real_device_level_ ? GetScalingFactor(*real_device_level_) : 1.0f; | |
78 | |
79 if (undo_factor == 0) { | |
80 LOG(LS_INFO) << "[fake rec device] Cannot undo analog level of 0."; | |
aleloi
2017/05/04 12:47:13
Maybe this one should be verbose? It's probably no
AleBzk
2017/05/05 12:20:18
Done.
| |
81 return level_scaling_factor; | |
82 } else { | |
83 return level_scaling_factor / undo_factor; | |
84 } | |
85 } | |
86 | |
87 float FakeRecordingDevice::GetScalingFactor(int level) const { | |
88 switch (mapping_kind_) { | |
89 case LevelToScalingMappingKind::kLinear: { | |
90 return static_cast<float>(level) / 255.0f; | |
91 } | |
92 case LevelToScalingMappingKind::kIdentity: { | |
93 return 1.0f; | |
94 } | |
95 default: { RTC_NOTREACHED(); } | |
96 } | |
97 } | |
98 | |
99 } // namespace webrtc | |
OLD | NEW |